David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

Committer:
dkp14
Date:
Tue Mar 14 23:40:16 2017 +0000
Revision:
20:c3bdb8f73c02
Parent:
18:12598db37e38
Child:
21:dd4bbb617415
tuning PID controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkp14 0:74a5723d604a 1 #include "mbed.h"
dkp14 0:74a5723d604a 2 #include "rtos.h"
dkp14 0:74a5723d604a 3 #include "definitions.h"
dkp14 0:74a5723d604a 4 #include "motorControl.h"
lb3314 15:b6025338e0eb 5 #include "parser.h"
dkp14 20:c3bdb8f73c02 6 #include <cmath>
dkp14 0:74a5723d604a 7
dkp14 20:c3bdb8f73c02 8 #define kp 0.3f
dkp14 20:c3bdb8f73c02 9 #define ki 0.05f
dkp14 20:c3bdb8f73c02 10 #define kd 0.5f
davidanderle 16:d4948633c559 11 #define dt 0.02f //given in ms, used to call the PID c.
dkp14 4:dc705df93090 12
dkp14 0:74a5723d604a 13 volatile uint8_t state = 0;
davidanderle 16:d4948633c559 14 volatile float w3 = 0; //Angular velocity
dkp14 11:043a63c952a0 15 volatile float duty = 0.40;
dkp14 4:dc705df93090 16 volatile int count_i3 = 0;
davidanderle 16:d4948633c559 17 const float angularVelocities[29] = {0, 0, 0, 56.3705020, 153.953598,
davidanderle 16:d4948633c559 18 221.162308, 436.561981, 652.034058, 669.472534, 671.117249, 703.662231,
davidanderle 16:d4948633c559 19 704.767273, 695.868835, 676.609924, 689.303345, 685.318481, 680.420166,
davidanderle 16:d4948633c559 20 681.527283, 683.529175, 700.758423, 742.759216, 737.354797, 733.224365,
davidanderle 16:d4948633c559 21 716.746521, 714.952209, 717.483154, 723.597839, 727.788757, 727.957336};
dkp14 0:74a5723d604a 22
davidanderle 16:d4948633c559 23 const float dutyCycles [29] = { 0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35,
davidanderle 16:d4948633c559 24 0.36, 0.37, 0.38, 0.39, 0.40, 0.41, 0.42, 0.43,
davidanderle 16:d4948633c559 25 0.44, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75,
davidanderle 16:d4948633c559 26 0.80, 0.85, 0.90, 0.95, 1};
davidanderle 16:d4948633c559 27 const float angle = 6.283; //2*pi for 1 revolution
dkp14 0:74a5723d604a 28 Timer dt_I3;
dkp14 0:74a5723d604a 29 Timer motorTimer;
dkp14 4:dc705df93090 30 Ticker controlTicker;
dkp14 4:dc705df93090 31
davidanderle 16:d4948633c559 32 volatile float fi0 = 0; //number of revs done
dkp14 20:c3bdb8f73c02 33 volatile int goalRevs = 200;
dkp14 4:dc705df93090 34 volatile float fi = 2*3.1415*goalRevs;
davidanderle 16:d4948633c559 35 volatile float goalW = 0; //desired angular velocity
dkp14 10:25d8696cb2c6 36 volatile float accError = 0;
dkp14 10:25d8696cb2c6 37 volatile float prevError = 0;
dkp14 4:dc705df93090 38
dkp14 11:043a63c952a0 39 float getDuty(float w){
davidanderle 16:d4948633c559 40 for (int i = 0; i < 28; i++) { //iterate through the angular velocities
dkp14 11:043a63c952a0 41 if (w > angularVelocities[i] && w <= angularVelocities[i+1]) {
dkp14 11:043a63c952a0 42 if (w-angularVelocities[i] < angularVelocities[i+1]-w ) {
dkp14 11:043a63c952a0 43 return dutyCycles[i];
dkp14 11:043a63c952a0 44 }
dkp14 11:043a63c952a0 45 else {
dkp14 11:043a63c952a0 46 return dutyCycles[i+1];
dkp14 11:043a63c952a0 47 }
dkp14 11:043a63c952a0 48 }
dkp14 11:043a63c952a0 49 }
dkp14 11:043a63c952a0 50 return 0;
dkp14 11:043a63c952a0 51 }
dkp14 11:043a63c952a0 52
dkp14 4:dc705df93090 53 void control(){
davidanderle 16:d4948633c559 54 fi0 = 6.283 * count_i3; //fi0 = 2*pi*revs
dkp14 10:25d8696cb2c6 55 float error = fi - fi0;
dkp14 10:25d8696cb2c6 56 accError += error*dt;
dkp14 20:c3bdb8f73c02 57 float dError = (error - prevError)/dt;
dkp14 4:dc705df93090 58 goalW = kp*error + ki*accError + kd*dError;
dkp14 10:25d8696cb2c6 59 prevError = error;
dkp14 20:c3bdb8f73c02 60 duty = getDuty(abs(goalW));
dkp14 20:c3bdb8f73c02 61 if (goalW > 0) lead = -2;
dkp14 20:c3bdb8f73c02 62 else lead = 2;
dkp14 0:74a5723d604a 63 }
dkp14 0:74a5723d604a 64
dkp14 0:74a5723d604a 65 void i3rise(){
dkp14 0:74a5723d604a 66 state = updateState();
davidanderle 2:fe637a5f3387 67 motorOut((state-orState+lead+6)%6, duty);
dkp14 0:74a5723d604a 68
davidanderle 16:d4948633c559 69 w3 = angle/dt_I3.read(); //Calc angular velocity
dkp14 0:74a5723d604a 70
dkp14 0:74a5723d604a 71 dt_I3.reset();
dkp14 4:dc705df93090 72 count_i3++;
dkp14 0:74a5723d604a 73 }
dkp14 0:74a5723d604a 74
dkp14 11:043a63c952a0 75 void i_edge(){
dkp14 0:74a5723d604a 76 state = updateState();
davidanderle 2:fe637a5f3387 77 motorOut((state-orState+lead+6)%6, duty);
dkp14 0:74a5723d604a 78 }
dkp14 0:74a5723d604a 79
dkp14 0:74a5723d604a 80 void CHA_rise(){
dkp14 0:74a5723d604a 81 }
dkp14 0:74a5723d604a 82 void CHA_fall(){
dkp14 0:74a5723d604a 83 }
dkp14 0:74a5723d604a 84 void CHB_rise(){
dkp14 0:74a5723d604a 85 }
dkp14 0:74a5723d604a 86 void CHB_fall(){
dkp14 0:74a5723d604a 87 }
dkp14 0:74a5723d604a 88
dkp14 0:74a5723d604a 89 int main() {
davidanderle 16:d4948633c559 90 motorHome(); //Initialise motor before any interrupt
dkp14 0:74a5723d604a 91
davidanderle 16:d4948633c559 92 dt_I3.start(); //Start the time counters for velocity
dkp14 11:043a63c952a0 93
dkp14 13:deb1e793f125 94 controlTicker.attach(&control, dt);
dkp14 0:74a5723d604a 95
davidanderle 16:d4948633c559 96 I1.rise(&i_edge); //Assign interrupt handlers for LEDs
dkp14 11:043a63c952a0 97 I1.fall(&i_edge);
dkp14 11:043a63c952a0 98 I2.rise(&i_edge);
dkp14 11:043a63c952a0 99 I2.fall(&i_edge);
dkp14 0:74a5723d604a 100 I3.rise(&i3rise);
dkp14 11:043a63c952a0 101 I3.fall(&i_edge);
dkp14 0:74a5723d604a 102 // CHA.rise(&CHA_rise);
dkp14 0:74a5723d604a 103 // CHA.fall(&CHA_fall);
dkp14 0:74a5723d604a 104 // CHB.rise(&CHB_rise);
dkp14 0:74a5723d604a 105 // CHB.fall(&CHB_fall);
dkp14 11:043a63c952a0 106
dkp14 10:25d8696cb2c6 107 state = updateState();
dkp14 11:043a63c952a0 108 motorTimer.start();
davidanderle 16:d4948633c559 109 motorOut((state-orState+lead+6)%6, 1.0f); //Kickstart the motor
dkp14 10:25d8696cb2c6 110
davidanderle 16:d4948633c559 111 while (count_i3 <= goalRevs) {
lb3314 18:12598db37e38 112
dkp14 20:c3bdb8f73c02 113 pc.printf("Speed: %f, goal speed: %f, duty cycle: %f, revs done: %d \n\r",w3, goalW, duty, count_i3);
dkp14 11:043a63c952a0 114 /*
dkp14 11:043a63c952a0 115 if(duty < 0.00f) {
dkp14 4:dc705df93090 116 stopMotor();
dkp14 10:25d8696cb2c6 117 return 0;
dkp14 4:dc705df93090 118 }
davidanderle 16:d4948633c559 119
dkp14 10:25d8696cb2c6 120 if(motorTimer.read() >= 30) {
dkp14 0:74a5723d604a 121 stopMotor();
dkp14 0:74a5723d604a 122 return 0;
dkp14 0:74a5723d604a 123 }
dkp14 4:dc705df93090 124 */
dkp14 0:74a5723d604a 125 }
dkp14 20:c3bdb8f73c02 126 while (abs(w3) > 50){
dkp14 20:c3bdb8f73c02 127 pc.printf("Speed: %f, goal speed: %f, duty cycle: %f, revs done: %d \n\r",w3, goalW, duty, count_i3);
dkp14 20:c3bdb8f73c02 128 }
dkp14 13:deb1e793f125 129 return 0;
dkp14 0:74a5723d604a 130 }