David Pasztor / Mbed 2 deprecated Motor_control

Dependencies:   mbed-rtos mbed

Committer:
dkp14
Date:
Thu Mar 16 21:49:07 2017 +0000
Revision:
35:5857105c9956
Parent:
34:599634375ba0
Parent:
30:e60fd3834ee7
Child:
36:747d80e3aca9
another 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 20:c3bdb8f73c02 6 #include <cmath>
dkp14 0:74a5723d604a 7
davidanderle 16:d4948633c559 8 volatile float w3 = 0; //Angular velocity
dkp14 21:dd4bbb617415 9 volatile float duty = 0.5;
dkp14 4:dc705df93090 10 volatile int count_i3 = 0;
dkp14 0:74a5723d604a 11
dkp14 0:74a5723d604a 12 Timer dt_I3;
dkp14 0:74a5723d604a 13 Timer motorTimer;
dkp14 4:dc705df93090 14 Ticker controlTicker;
dkp14 4:dc705df93090 15
davidanderle 16:d4948633c559 16 volatile float fi0 = 0; //number of revs done
dkp14 11:043a63c952a0 17 volatile int goalRevs = 50;
dkp14 4:dc705df93090 18 volatile float fi = 2*3.1415*goalRevs;
dkp14 10:25d8696cb2c6 19 volatile float accError = 0;
dkp14 10:25d8696cb2c6 20 volatile float prevError = 0;
dkp14 4:dc705df93090 21
davidanderle 31:bfb4629ca327 22 #define kp 0.0016f
davidanderle 31:bfb4629ca327 23 #define ki 0.0f //0.05f
davidanderle 31:bfb4629ca327 24 #define kd 0.00006f //0.5f
davidanderle 31:bfb4629ca327 25 #define dt 0.002f //given in ms, used to call the PID c.
dkp14 11:043a63c952a0 26
dkp14 4:dc705df93090 27 void control(){
davidanderle 16:d4948633c559 28 fi0 = 6.283 * count_i3; //fi0 = 2*pi*revs
dkp14 10:25d8696cb2c6 29 float error = fi - fi0;
dkp14 10:25d8696cb2c6 30 accError += error*dt;
dkp14 20:c3bdb8f73c02 31 float dError = (error - prevError)/dt;
davidanderle 31:bfb4629ca327 32 duty = kp*error + ki*accError + kd*dError;
dkp14 10:25d8696cb2c6 33 prevError = error;
davidanderle 31:bfb4629ca327 34 //if (abs(goalW) > w3) duty = 1 - w3/abs(goalW);
davidanderle 31:bfb4629ca327 35 //else duty = abs(goalW) / w3;
dkp14 25:0ee6b164f234 36 //duty = getDuty(abs(goalW));
davidanderle 31:bfb4629ca327 37 if (duty > 0) lead = -2;
dkp14 20:c3bdb8f73c02 38 else lead = 2;
dkp14 25:0ee6b164f234 39 //if (duty > 0) lead = -2;
dkp14 25:0ee6b164f234 40 //else lead = 2;
dkp14 0:74a5723d604a 41 }
dkp14 0:74a5723d604a 42
dkp14 0:74a5723d604a 43 void i3rise(){
dkp14 0:74a5723d604a 44 state = updateState();
davidanderle 2:fe637a5f3387 45 motorOut((state-orState+lead+6)%6, duty);
dkp14 0:74a5723d604a 46
davidanderle 16:d4948633c559 47 w3 = angle/dt_I3.read(); //Calc angular velocity
dkp14 0:74a5723d604a 48
dkp14 0:74a5723d604a 49 dt_I3.reset();
dkp14 25:0ee6b164f234 50 count_i3++;
dkp14 0:74a5723d604a 51 }
dkp14 0:74a5723d604a 52
dkp14 11:043a63c952a0 53 void i_edge(){
dkp14 0:74a5723d604a 54 state = updateState();
davidanderle 2:fe637a5f3387 55 motorOut((state-orState+lead+6)%6, duty);
dkp14 0:74a5723d604a 56 }
dkp14 0:74a5723d604a 57
dkp14 0:74a5723d604a 58 void CHA_rise(){
dkp14 0:74a5723d604a 59 }
dkp14 0:74a5723d604a 60 void CHA_fall(){
dkp14 0:74a5723d604a 61 }
dkp14 0:74a5723d604a 62 void CHB_rise(){
dkp14 0:74a5723d604a 63 }
dkp14 0:74a5723d604a 64 void CHB_fall(){
dkp14 0:74a5723d604a 65 }
dkp14 0:74a5723d604a 66
dkp14 0:74a5723d604a 67 int main() {
davidanderle 16:d4948633c559 68 motorHome(); //Initialise motor before any interrupt
dkp14 0:74a5723d604a 69
davidanderle 16:d4948633c559 70 dt_I3.start(); //Start the time counters for velocity
dkp14 35:5857105c9956 71
dkp14 13:deb1e793f125 72 controlTicker.attach(&control, dt);
dkp14 0:74a5723d604a 73
davidanderle 16:d4948633c559 74 I1.rise(&i_edge); //Assign interrupt handlers for LEDs
dkp14 11:043a63c952a0 75 I1.fall(&i_edge);
dkp14 11:043a63c952a0 76 I2.rise(&i_edge);
dkp14 11:043a63c952a0 77 I2.fall(&i_edge);
dkp14 0:74a5723d604a 78 I3.rise(&i3rise);
dkp14 11:043a63c952a0 79 I3.fall(&i_edge);
dkp14 21:dd4bbb617415 80
dkp14 0:74a5723d604a 81 // CHA.rise(&CHA_rise);
dkp14 0:74a5723d604a 82 // CHA.fall(&CHA_fall);
dkp14 0:74a5723d604a 83 // CHB.rise(&CHB_rise);
dkp14 0:74a5723d604a 84 // CHB.fall(&CHB_fall);
dkp14 11:043a63c952a0 85
dkp14 10:25d8696cb2c6 86 state = updateState();
dkp14 11:043a63c952a0 87 motorTimer.start();
dkp14 25:0ee6b164f234 88 motorOut((state-orState+lead+6)%6, 0.3f); //Kickstart the motor
davidanderle 30:e60fd3834ee7 89
davidanderle 16:d4948633c559 90 while (count_i3 <= goalRevs) {
dkp14 11:043a63c952a0 91 pc.printf("Speed: %f, duty cycle: %f, revs done: %d \n\r",w3, duty, count_i3);
dkp14 0:74a5723d604a 92 }
dkp14 25:0ee6b164f234 93 while (abs(w3) > 10){
dkp14 34:599634375ba0 94 pc.printf("Speed: %f, duty cycle: %f, revs done: %d \n\r",w3, duty, count_i3);
dkp14 0:74a5723d604a 95 }
davidanderle 16:d4948633c559 96 stopMotor();
davidanderle 16:d4948633c559 97 return 0;
dkp14 0:74a5723d604a 98 }