David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

Committer:
dkp14
Date:
Wed Mar 15 18:14:40 2017 +0000
Revision:
24:487c898c8d71
Parent:
21:dd4bbb617415
Child:
25:0ee6b164f234
slight error in getDuty function fixed

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