David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

Committer:
davidanderle
Date:
Tue Mar 14 22:14:41 2017 +0000
Revision:
16:d4948633c559
Parent:
11:043a63c952a0
Child:
18:12598db37e38
Child:
30:e60fd3834ee7
Velocity table updated

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