David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

Committer:
davidanderle
Date:
Tue Mar 14 22:18:55 2017 +0000
Revision:
30:e60fd3834ee7
Parent:
15:b6025338e0eb
Parent:
16:d4948633c559
Child:
35:5857105c9956
merge

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