No.9 Robotics / Mbed 2 deprecated Robotics_DCMotor

Dependencies:   mbed

Fork of Robotics_Lab_DCMotor by LDSC_Robotics

Committer:
ChangYuHsuan
Date:
Thu Apr 14 08:47:12 2016 +0000
Revision:
4:04702de80697
Parent:
3:178ee1fe1c60
Child:
5:ff3b5f31a9ce
Yu Hsuan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dg0704 3:178ee1fe1c60 1 /*DCMotor*/
YCTung 0:0971f0666990 2 #include "mbed.h"
YCTung 0:0971f0666990 3
YCTung 0:0971f0666990 4 //The number will be compiled as type "double" in default
YCTung 0:0971f0666990 5 //Add a "f" after the number can make it compiled as type "float"
YCTung 0:0971f0666990 6 #define Ts 0.01f //period of timer1 (s)
dg0704 3:178ee1fe1c60 7 #define Kp 0.05f
dg0704 3:178ee1fe1c60 8 #define Ki 0.001f
dg0704 3:178ee1fe1c60 9
dg0704 3:178ee1fe1c60 10 Serial bluetooth(D10,D2); //宣告藍牙腳位
YCTung 0:0971f0666990 11
YCTung 0:0971f0666990 12 PwmOut pwm1(D7);
YCTung 0:0971f0666990 13 PwmOut pwm1n(D11);
YCTung 0:0971f0666990 14 PwmOut pwm2(D8);
YCTung 0:0971f0666990 15 PwmOut pwm2n(A3);
YCTung 0:0971f0666990 16
YCTung 0:0971f0666990 17 DigitalOut led1(A4);
YCTung 0:0971f0666990 18 DigitalOut led2(A5);
YCTung 0:0971f0666990 19
YCTung 0:0971f0666990 20 //Motor1 sensor
YCTung 0:0971f0666990 21 InterruptIn HallA_1(A1);
YCTung 0:0971f0666990 22 InterruptIn HallB_1(A2);
YCTung 0:0971f0666990 23 //Motor2 sensor
YCTung 0:0971f0666990 24 InterruptIn HallA_2(D13);
YCTung 0:0971f0666990 25 InterruptIn HallB_2(D12);
YCTung 0:0971f0666990 26
YCTung 0:0971f0666990 27
YCTung 0:0971f0666990 28 Ticker timer1;
YCTung 0:0971f0666990 29 void timer1_interrupt(void);
YCTung 0:0971f0666990 30 void CN_interrupt(void);
YCTung 0:0971f0666990 31
YCTung 0:0971f0666990 32 void init_TIMER(void);
YCTung 0:0971f0666990 33 void init_PWM(void);
YCTung 0:0971f0666990 34 void init_CN(void);
ChangYuHsuan 4:04702de80697 35 void init_BLUETOOTH(void);
ChangYuHsuan 4:04702de80697 36
ChangYuHsuan 4:04702de80697 37 char speedCmd;
YCTung 0:0971f0666990 38
YCTung 0:0971f0666990 39 int8_t stateA_1=0, stateB_1=0, stateA_2=0, stateB_2=0;
YCTung 0:0971f0666990 40 int8_t state_1 = 0, state_1_old = 0, state_2 = 0, state_2_old = 0;
YCTung 0:0971f0666990 41
YCTung 0:0971f0666990 42 int v1Count = 0;
YCTung 0:0971f0666990 43 int v2Count = 0;
YCTung 0:0971f0666990 44
YCTung 0:0971f0666990 45 float v1 = 0.0, v1_ref = 0.0;
YCTung 0:0971f0666990 46 float v1_err = 0.0, v1_ierr = 0.0, PIout_1 = 0.0, PIout_1_old = 0.0;
YCTung 0:0971f0666990 47 float v2 = 0.0, v2_ref = 0.0;
YCTung 0:0971f0666990 48 float v2_err = 0.0, v2_ierr = 0.0, PIout_2 = 0.0, PIout_2_old = 0.0;
YCTung 0:0971f0666990 49
YCTung 0:0971f0666990 50 int main() {
YCTung 0:0971f0666990 51
YCTung 0:0971f0666990 52 init_TIMER();
YCTung 0:0971f0666990 53 init_PWM();
YCTung 0:0971f0666990 54 init_CN();
YCTung 0:0971f0666990 55
YCTung 0:0971f0666990 56 v1_ref = 0.0;
YCTung 0:0971f0666990 57 v2_ref = 0.0;
YCTung 0:0971f0666990 58
dg0704 3:178ee1fe1c60 59 while(1)
dg0704 3:178ee1fe1c60 60 {
dg0704 3:178ee1fe1c60 61 if(bluetooth.readable())
dg0704 3:178ee1fe1c60 62 {
ChangYuHsuan 4:04702de80697 63 speedCmd = bluetooth.getc();
dg0704 3:178ee1fe1c60 64
ChangYuHsuan 4:04702de80697 65 if (speedCmd == 'a')
dg0704 3:178ee1fe1c60 66 {
dg0704 3:178ee1fe1c60 67 v1_ref = v1_ref + 10;
dg0704 3:178ee1fe1c60 68 v2_ref = v2_ref + 10;
dg0704 3:178ee1fe1c60 69 }
ChangYuHsuan 4:04702de80697 70 else if (speedCmd == 's')
dg0704 3:178ee1fe1c60 71 {
dg0704 3:178ee1fe1c60 72 v1_ref = v1_ref - 10;
dg0704 3:178ee1fe1c60 73 v2_ref = v2_ref - 10;
dg0704 3:178ee1fe1c60 74 }
ChangYuHsuan 4:04702de80697 75 else if (speedCmd == 'q')
dg0704 3:178ee1fe1c60 76 {
dg0704 3:178ee1fe1c60 77 v1_ref = v1_ref + 20;
dg0704 3:178ee1fe1c60 78 v2_ref = v2_ref + 20;
dg0704 3:178ee1fe1c60 79 }
ChangYuHsuan 4:04702de80697 80 else if (speedCmd == 'w')
dg0704 3:178ee1fe1c60 81 {
dg0704 3:178ee1fe1c60 82 v1_ref = v1_ref - 20;
dg0704 3:178ee1fe1c60 83 v2_ref = v2_ref - 20;
dg0704 3:178ee1fe1c60 84 }
dg0704 3:178ee1fe1c60 85
dg0704 3:178ee1fe1c60 86 if (v1_ref < -100)
dg0704 3:178ee1fe1c60 87 v1_ref = -100;
dg0704 3:178ee1fe1c60 88 else if (v1_ref > 100)
ChangYuHsuan 4:04702de80697 89 v1_ref = 100;
dg0704 3:178ee1fe1c60 90
dg0704 3:178ee1fe1c60 91 if (v2_ref < -100)
dg0704 3:178ee1fe1c60 92 v2_ref = -100;
dg0704 3:178ee1fe1c60 93 else if (v2_ref > 100)
dg0704 3:178ee1fe1c60 94 v2_ref = 100;
dg0704 3:178ee1fe1c60 95 }
YCTung 0:0971f0666990 96 }
YCTung 0:0971f0666990 97 }
YCTung 0:0971f0666990 98
YCTung 0:0971f0666990 99 void timer1_interrupt(void)
YCTung 0:0971f0666990 100 {
YCTung 0:0971f0666990 101 //Motor 1
YCTung 1:6747911cdd90 102 v1 = (float)v1Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
YCTung 0:0971f0666990 103 v1Count = 0;
YCTung 0:0971f0666990 104
YCTung 0:0971f0666990 105 ///code for PI control///
dg0704 3:178ee1fe1c60 106 v1_err = v1_ref - v1;
dg0704 3:178ee1fe1c60 107 v1_ierr += v1_err;
dg0704 3:178ee1fe1c60 108 PIout_1 = Kp * v1_err + Ki * Ts * (v1_ierr - v1_err);
YCTung 0:0971f0666990 109
dg0704 3:178ee1fe1c60 110 // saturation
YCTung 0:0971f0666990 111 if(PIout_1 >= 0.5f)PIout_1 = 0.5f;
YCTung 0:0971f0666990 112 else if(PIout_1 <= -0.5f)PIout_1 = -0.5f;
YCTung 0:0971f0666990 113 pwm1.write(PIout_1 + 0.5f);
YCTung 0:0971f0666990 114 TIM1->CCER |= 0x4;
YCTung 0:0971f0666990 115
dg0704 3:178ee1fe1c60 116 PIout_1_old = PIout_1;
YCTung 0:0971f0666990 117
YCTung 0:0971f0666990 118 //Motor 2
YCTung 1:6747911cdd90 119 v2 = (float)v2Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
YCTung 0:0971f0666990 120 v2Count = 0;
YCTung 0:0971f0666990 121
YCTung 0:0971f0666990 122 ///code for PI control///
dg0704 3:178ee1fe1c60 123 v2_err = v2_ref - v2;
dg0704 3:178ee1fe1c60 124 v2_ierr += v2_err;
dg0704 3:178ee1fe1c60 125 PIout_2 = Kp * v2_err + Ki * Ts * (v2_ierr - v2_err);
YCTung 0:0971f0666990 126
dg0704 3:178ee1fe1c60 127 //saturation
YCTung 0:0971f0666990 128 if(PIout_2 >= 0.5f)PIout_2 = 0.5f;
YCTung 0:0971f0666990 129 else if(PIout_2 <= -0.5f)PIout_2 = -0.5f;
YCTung 0:0971f0666990 130 pwm2.write(PIout_2 + 0.5f);
YCTung 0:0971f0666990 131 TIM1->CCER |= 0x40;
dg0704 3:178ee1fe1c60 132
dg0704 3:178ee1fe1c60 133 PIout_2_old = PIout_2;
YCTung 0:0971f0666990 134 }
YCTung 0:0971f0666990 135
YCTung 0:0971f0666990 136 void CN_interrupt(void)
YCTung 0:0971f0666990 137 {
YCTung 0:0971f0666990 138 //Motor 1
YCTung 0:0971f0666990 139 stateA_1 = HallA_1.read();
YCTung 0:0971f0666990 140 stateB_1 = HallB_1.read();
YCTung 0:0971f0666990 141
YCTung 0:0971f0666990 142 ///code for state determination///
dg0704 3:178ee1fe1c60 143 if (stateA_1 == 0)
dg0704 3:178ee1fe1c60 144 {
dg0704 3:178ee1fe1c60 145 if (stateB_1 == 0)
dg0704 3:178ee1fe1c60 146 state_1 = 1;
dg0704 3:178ee1fe1c60 147 else
ChangYuHsuan 4:04702de80697 148 state_1 = 2;
dg0704 3:178ee1fe1c60 149 }
dg0704 3:178ee1fe1c60 150 else
dg0704 3:178ee1fe1c60 151 {
dg0704 3:178ee1fe1c60 152 if (stateB_1 == 1)
dg0704 3:178ee1fe1c60 153 state_1 = 3;
dg0704 3:178ee1fe1c60 154 else
ChangYuHsuan 4:04702de80697 155 state_1 = 4;
dg0704 3:178ee1fe1c60 156 }
YCTung 0:0971f0666990 157
dg0704 3:178ee1fe1c60 158 //Forward: v1Count +1
dg0704 3:178ee1fe1c60 159 //Inverse: v1Count -1
dg0704 3:178ee1fe1c60 160 if ( (state_1 == (state_1_old + 1)) || (state_1 == (state_1_old - 3)) )
dg0704 3:178ee1fe1c60 161 v1Count++;
dg0704 3:178ee1fe1c60 162 else if ( (state_1 == (state_1_old - 1)) || (state_1 == (state_1_old + 3)))
dg0704 3:178ee1fe1c60 163 v1Count--;
dg0704 3:178ee1fe1c60 164
dg0704 3:178ee1fe1c60 165 state_1_old = state_1;
YCTung 0:0971f0666990 166
YCTung 0:0971f0666990 167 //Motor 2
YCTung 0:0971f0666990 168 stateA_2 = HallA_2.read();
YCTung 0:0971f0666990 169 stateB_2 = HallB_2.read();
YCTung 0:0971f0666990 170
YCTung 0:0971f0666990 171 ///code for state determination///
dg0704 3:178ee1fe1c60 172 if (stateA_2 == 0)
dg0704 3:178ee1fe1c60 173 {
dg0704 3:178ee1fe1c60 174 if (stateB_2 == 0)
dg0704 3:178ee1fe1c60 175 state_2 = 1;
dg0704 3:178ee1fe1c60 176 else
ChangYuHsuan 4:04702de80697 177 state_2 = 2;
dg0704 3:178ee1fe1c60 178 }
dg0704 3:178ee1fe1c60 179 else
dg0704 3:178ee1fe1c60 180 {
dg0704 3:178ee1fe1c60 181 if (stateB_2 == 1)
dg0704 3:178ee1fe1c60 182 state_2 = 3;
dg0704 3:178ee1fe1c60 183 else
ChangYuHsuan 4:04702de80697 184 state_2 = 4;
dg0704 3:178ee1fe1c60 185 }
YCTung 0:0971f0666990 186
dg0704 3:178ee1fe1c60 187 //Forward: v2Count +1
dg0704 3:178ee1fe1c60 188 //Inverse: v2Count -1
dg0704 3:178ee1fe1c60 189 if ( (state_2 == (state_2_old + 1)) || (state_2 == (state_2_old - 3)) )
dg0704 3:178ee1fe1c60 190 v1Count++;
dg0704 3:178ee1fe1c60 191 else if ( (state_2 == (state_2_old - 1)) || (state_2 == (state_2_old + 3)))
dg0704 3:178ee1fe1c60 192 v1Count--;
YCTung 0:0971f0666990 193 }
YCTung 0:0971f0666990 194
YCTung 0:0971f0666990 195 void init_TIMER(void)
YCTung 0:0971f0666990 196 {
YCTung 0:0971f0666990 197 timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
YCTung 0:0971f0666990 198 }
YCTung 0:0971f0666990 199
YCTung 0:0971f0666990 200 void init_PWM(void)
YCTung 0:0971f0666990 201 {
YCTung 0:0971f0666990 202 pwm1.period_us(50);
YCTung 0:0971f0666990 203 pwm1.write(0.5);
YCTung 0:0971f0666990 204 TIM1->CCER |= 0x4;
YCTung 0:0971f0666990 205
YCTung 0:0971f0666990 206 pwm2.period_us(50);
YCTung 0:0971f0666990 207 pwm2.write(0.5);
YCTung 0:0971f0666990 208 TIM1->CCER |= 0x40;
YCTung 0:0971f0666990 209 }
YCTung 0:0971f0666990 210
YCTung 0:0971f0666990 211 void init_CN(void)
YCTung 0:0971f0666990 212 {
YCTung 0:0971f0666990 213 HallA_1.rise(&CN_interrupt);
YCTung 0:0971f0666990 214 HallA_1.fall(&CN_interrupt);
YCTung 0:0971f0666990 215 HallB_1.rise(&CN_interrupt);
YCTung 0:0971f0666990 216 HallB_1.fall(&CN_interrupt);
YCTung 0:0971f0666990 217
YCTung 0:0971f0666990 218 HallA_2.rise(&CN_interrupt);
YCTung 0:0971f0666990 219 HallA_2.fall(&CN_interrupt);
YCTung 0:0971f0666990 220 HallB_2.rise(&CN_interrupt);
YCTung 0:0971f0666990 221 HallB_2.fall(&CN_interrupt);
YCTung 0:0971f0666990 222
YCTung 0:0971f0666990 223 stateA_1 = HallA_1.read();
YCTung 0:0971f0666990 224 stateB_1 = HallB_1.read();
YCTung 0:0971f0666990 225 stateA_2 = HallA_2.read();
YCTung 0:0971f0666990 226 stateB_2 = HallB_2.read();
ChangYuHsuan 4:04702de80697 227 }
ChangYuHsuan 4:04702de80697 228
ChangYuHsuan 4:04702de80697 229 void init_BLUETOOTH(void)
ChangYuHsuan 4:04702de80697 230 {
ChangYuHsuan 4:04702de80697 231 bluetooth.baud(115200);
YCTung 0:0971f0666990 232 }