Brad VanderWilp
/
Blue_Board_Test_2
Senior design sensored code to run freescale motor with IM07M1
main.cpp@4:ab4d51b5b1c8, 2016-04-06 (annotated)
- Committer:
- BVanderWilp
- Date:
- Wed Apr 06 20:10:39 2016 +0000
- Revision:
- 4:ab4d51b5b1c8
- Parent:
- 3:2bcc36fe4de5
- Child:
- 5:6110655353ad
fixed bug in sensorless startup;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BVanderWilp | 0:d445abf9a8e9 | 1 | #include "mbed.h" |
BVanderWilp | 0:d445abf9a8e9 | 2 | |
BVanderWilp | 1:69c06d3676fd | 3 | PwmOut phaseA(PA_8); //Out1, Green |
BVanderWilp | 0:d445abf9a8e9 | 4 | DigitalOut phaseAEN(PC_10); |
BVanderWilp | 1:69c06d3676fd | 5 | PwmOut phaseB(PA_9); //Out2, Blue |
BVanderWilp | 0:d445abf9a8e9 | 6 | DigitalOut phaseBEN(PC_11); |
BVanderWilp | 1:69c06d3676fd | 7 | PwmOut phaseC(PA_10); //Out3, White |
BVanderWilp | 0:d445abf9a8e9 | 8 | DigitalOut phaseCEN(PC_12); |
BVanderWilp | 0:d445abf9a8e9 | 9 | |
BVanderWilp | 0:d445abf9a8e9 | 10 | InterruptIn button(USER_BUTTON); |
BVanderWilp | 0:d445abf9a8e9 | 11 | |
BVanderWilp | 0:d445abf9a8e9 | 12 | DigitalOut redLed(PB_2); |
BVanderWilp | 0:d445abf9a8e9 | 13 | DigitalOut led(LED1); |
BVanderWilp | 1:69c06d3676fd | 14 | InterruptIn hallA(PA_15); //H1, Green |
BVanderWilp | 1:69c06d3676fd | 15 | InterruptIn hallB(PB_3); //H2, Blue |
BVanderWilp | 1:69c06d3676fd | 16 | InterruptIn hallC(PB_10); //H3, White |
BVanderWilp | 0:d445abf9a8e9 | 17 | |
BVanderWilp | 1:69c06d3676fd | 18 | Ticker interrupt; |
BVanderWilp | 0:d445abf9a8e9 | 19 | |
BVanderWilp | 1:69c06d3676fd | 20 | float pwmDuty = 0.5; |
BVanderWilp | 0:d445abf9a8e9 | 21 | int stall = 0; |
BVanderWilp | 0:d445abf9a8e9 | 22 | void activate() |
BVanderWilp | 0:d445abf9a8e9 | 23 | { |
BVanderWilp | 0:d445abf9a8e9 | 24 | stall = 1; |
BVanderWilp | 0:d445abf9a8e9 | 25 | } |
BVanderWilp | 0:d445abf9a8e9 | 26 | |
BVanderWilp | 1:69c06d3676fd | 27 | void Crise() //state1, A0 B- C+ |
BVanderWilp | 0:d445abf9a8e9 | 28 | { |
BVanderWilp | 0:d445abf9a8e9 | 29 | phaseAEN = 0; |
BVanderWilp | 1:69c06d3676fd | 30 | phaseBEN = 1; |
BVanderWilp | 1:69c06d3676fd | 31 | |
BVanderWilp | 3:2bcc36fe4de5 | 32 | phaseC.write(pwmDuty); |
BVanderWilp | 3:2bcc36fe4de5 | 33 | phaseCEN = 1; |
BVanderWilp | 1:69c06d3676fd | 34 | redLed = ! redLed; |
BVanderWilp | 1:69c06d3676fd | 35 | } |
BVanderWilp | 1:69c06d3676fd | 36 | |
BVanderWilp | 1:69c06d3676fd | 37 | void Bfall() //state2, A+ B- C0 |
BVanderWilp | 1:69c06d3676fd | 38 | { |
BVanderWilp | 1:69c06d3676fd | 39 | phaseCEN = 0; |
BVanderWilp | 1:69c06d3676fd | 40 | phaseC.write(0); |
BVanderWilp | 1:69c06d3676fd | 41 | phaseA.write(pwmDuty); |
BVanderWilp | 1:69c06d3676fd | 42 | phaseAEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 43 | |
BVanderWilp | 3:2bcc36fe4de5 | 44 | phaseBEN = 1; |
BVanderWilp | 1:69c06d3676fd | 45 | } |
BVanderWilp | 1:69c06d3676fd | 46 | |
BVanderWilp | 1:69c06d3676fd | 47 | void Arise() //state3, A+ B0 C- |
BVanderWilp | 1:69c06d3676fd | 48 | { |
BVanderWilp | 1:69c06d3676fd | 49 | phaseBEN = 0; |
BVanderWilp | 1:69c06d3676fd | 50 | phaseCEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 51 | |
BVanderWilp | 3:2bcc36fe4de5 | 52 | phaseA.write(pwmDuty); |
BVanderWilp | 3:2bcc36fe4de5 | 53 | phaseAEN = 1; |
BVanderWilp | 1:69c06d3676fd | 54 | } |
BVanderWilp | 1:69c06d3676fd | 55 | |
BVanderWilp | 1:69c06d3676fd | 56 | void Cfall() //state4, A0 B+ C- |
BVanderWilp | 1:69c06d3676fd | 57 | { |
BVanderWilp | 1:69c06d3676fd | 58 | phaseAEN = 0; |
BVanderWilp | 0:d445abf9a8e9 | 59 | phaseA.write(0); |
BVanderWilp | 0:d445abf9a8e9 | 60 | phaseB.write(pwmDuty); |
BVanderWilp | 0:d445abf9a8e9 | 61 | phaseBEN = 1; |
BVanderWilp | 1:69c06d3676fd | 62 | |
BVanderWilp | 3:2bcc36fe4de5 | 63 | phaseCEN = 1; |
BVanderWilp | 1:69c06d3676fd | 64 | redLed = ! redLed; |
BVanderWilp | 0:d445abf9a8e9 | 65 | } |
BVanderWilp | 0:d445abf9a8e9 | 66 | |
BVanderWilp | 1:69c06d3676fd | 67 | void Brise() //state5, A- B+ C0 |
BVanderWilp | 0:d445abf9a8e9 | 68 | { |
BVanderWilp | 0:d445abf9a8e9 | 69 | phaseCEN = 0; |
BVanderWilp | 0:d445abf9a8e9 | 70 | phaseAEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 71 | |
BVanderWilp | 3:2bcc36fe4de5 | 72 | phaseB.write(pwmDuty); |
BVanderWilp | 3:2bcc36fe4de5 | 73 | phaseBEN = 1; |
BVanderWilp | 0:d445abf9a8e9 | 74 | } |
BVanderWilp | 0:d445abf9a8e9 | 75 | |
BVanderWilp | 1:69c06d3676fd | 76 | void Afall() //state5, A- B0 C+ |
BVanderWilp | 0:d445abf9a8e9 | 77 | { |
BVanderWilp | 0:d445abf9a8e9 | 78 | phaseBEN = 0; |
BVanderWilp | 0:d445abf9a8e9 | 79 | phaseB.write(0); |
BVanderWilp | 0:d445abf9a8e9 | 80 | phaseC.write(pwmDuty); |
BVanderWilp | 0:d445abf9a8e9 | 81 | phaseCEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 82 | |
BVanderWilp | 3:2bcc36fe4de5 | 83 | phaseAEN = 1; |
BVanderWilp | 0:d445abf9a8e9 | 84 | } |
BVanderWilp | 0:d445abf9a8e9 | 85 | |
BVanderWilp | 1:69c06d3676fd | 86 | int count = 0; |
BVanderWilp | 1:69c06d3676fd | 87 | void intcall(){ |
BVanderWilp | 1:69c06d3676fd | 88 | count = (count + 1) % 6; |
BVanderWilp | 1:69c06d3676fd | 89 | |
BVanderWilp | 1:69c06d3676fd | 90 | if(count == 0) //B+, C- |
BVanderWilp | 1:69c06d3676fd | 91 | { |
BVanderWilp | 1:69c06d3676fd | 92 | phaseAEN = 0; |
BVanderWilp | 1:69c06d3676fd | 93 | phaseA.write(0); |
BVanderWilp | 1:69c06d3676fd | 94 | phaseB.write(pwmDuty); |
BVanderWilp | 1:69c06d3676fd | 95 | phaseBEN = 1; |
BVanderWilp | 1:69c06d3676fd | 96 | } |
BVanderWilp | 1:69c06d3676fd | 97 | else if(count == 1) //B+, A- |
BVanderWilp | 1:69c06d3676fd | 98 | { |
BVanderWilp | 1:69c06d3676fd | 99 | phaseCEN = 0; |
BVanderWilp | 1:69c06d3676fd | 100 | phaseAEN = 1; |
BVanderWilp | 1:69c06d3676fd | 101 | } |
BVanderWilp | 1:69c06d3676fd | 102 | else if(count == 2) //C+, A- |
BVanderWilp | 1:69c06d3676fd | 103 | { |
BVanderWilp | 1:69c06d3676fd | 104 | phaseBEN = 0; |
BVanderWilp | 1:69c06d3676fd | 105 | phaseB.write(0); |
BVanderWilp | 1:69c06d3676fd | 106 | phaseC.write(pwmDuty); |
BVanderWilp | 1:69c06d3676fd | 107 | phaseCEN = 1; |
BVanderWilp | 1:69c06d3676fd | 108 | } |
BVanderWilp | 1:69c06d3676fd | 109 | else if(count == 3) //C+, B- |
BVanderWilp | 1:69c06d3676fd | 110 | { |
BVanderWilp | 1:69c06d3676fd | 111 | phaseAEN = 0; |
BVanderWilp | 1:69c06d3676fd | 112 | phaseBEN = 1; |
BVanderWilp | 1:69c06d3676fd | 113 | } |
BVanderWilp | 1:69c06d3676fd | 114 | else if(count == 4) //A+, B- |
BVanderWilp | 1:69c06d3676fd | 115 | { |
BVanderWilp | 1:69c06d3676fd | 116 | phaseCEN = 0; |
BVanderWilp | 1:69c06d3676fd | 117 | phaseC.write(0); |
BVanderWilp | 1:69c06d3676fd | 118 | phaseA.write(pwmDuty); |
BVanderWilp | 1:69c06d3676fd | 119 | phaseAEN = 1; |
BVanderWilp | 1:69c06d3676fd | 120 | } |
BVanderWilp | 1:69c06d3676fd | 121 | else if(count == 5) //A+, C- |
BVanderWilp | 1:69c06d3676fd | 122 | { |
BVanderWilp | 1:69c06d3676fd | 123 | phaseBEN = 0; |
BVanderWilp | 1:69c06d3676fd | 124 | phaseCEN = 1; |
BVanderWilp | 1:69c06d3676fd | 125 | } |
BVanderWilp | 0:d445abf9a8e9 | 126 | } |
BVanderWilp | 0:d445abf9a8e9 | 127 | |
BVanderWilp | 0:d445abf9a8e9 | 128 | |
BVanderWilp | 0:d445abf9a8e9 | 129 | int main() { |
BVanderWilp | 1:69c06d3676fd | 130 | //wait until button push to start |
BVanderWilp | 0:d445abf9a8e9 | 131 | button.rise(&activate); |
BVanderWilp | 0:d445abf9a8e9 | 132 | while(stall == 0) { |
BVanderWilp | 0:d445abf9a8e9 | 133 | led = !led; |
BVanderWilp | 4:ab4d51b5b1c8 | 134 | wait(1); |
BVanderWilp | 0:d445abf9a8e9 | 135 | } |
BVanderWilp | 1:69c06d3676fd | 136 | |
BVanderWilp | 3:2bcc36fe4de5 | 137 | // //startup with open loop |
BVanderWilp | 3:2bcc36fe4de5 | 138 | // phaseA.period_us(10); |
BVanderWilp | 3:2bcc36fe4de5 | 139 | // phaseB.period_us(10); |
BVanderWilp | 3:2bcc36fe4de5 | 140 | // phaseC.period_us(10); |
BVanderWilp | 3:2bcc36fe4de5 | 141 | // interrupt.attach(&intcall, .01); |
BVanderWilp | 3:2bcc36fe4de5 | 142 | // |
BVanderWilp | 3:2bcc36fe4de5 | 143 | // |
BVanderWilp | 3:2bcc36fe4de5 | 144 | // phaseA.write(0); |
BVanderWilp | 3:2bcc36fe4de5 | 145 | // phaseB.write(pwmDuty); |
BVanderWilp | 3:2bcc36fe4de5 | 146 | // phaseC.write(0); |
BVanderWilp | 3:2bcc36fe4de5 | 147 | // |
BVanderWilp | 3:2bcc36fe4de5 | 148 | // phaseAEN = 0; |
BVanderWilp | 3:2bcc36fe4de5 | 149 | // phaseBEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 150 | // phaseCEN = 1; |
BVanderWilp | 3:2bcc36fe4de5 | 151 | // |
BVanderWilp | 3:2bcc36fe4de5 | 152 | // for(int i = 0; i < 4; i++) |
BVanderWilp | 3:2bcc36fe4de5 | 153 | // { |
BVanderWilp | 3:2bcc36fe4de5 | 154 | // i++; |
BVanderWilp | 3:2bcc36fe4de5 | 155 | // led = !led; |
BVanderWilp | 3:2bcc36fe4de5 | 156 | // wait(0.5); |
BVanderWilp | 3:2bcc36fe4de5 | 157 | // } |
BVanderWilp | 3:2bcc36fe4de5 | 158 | // interrupt.detach(); |
BVanderWilp | 3:2bcc36fe4de5 | 159 | |
BVanderWilp | 0:d445abf9a8e9 | 160 | phaseA.period_us(10); |
BVanderWilp | 0:d445abf9a8e9 | 161 | phaseB.period_us(10); |
BVanderWilp | 0:d445abf9a8e9 | 162 | phaseC.period_us(10); |
BVanderWilp | 0:d445abf9a8e9 | 163 | |
BVanderWilp | 0:d445abf9a8e9 | 164 | phaseA.write(0); |
BVanderWilp | 3:2bcc36fe4de5 | 165 | phaseB.write(0); |
BVanderWilp | 0:d445abf9a8e9 | 166 | phaseC.write(0); |
BVanderWilp | 0:d445abf9a8e9 | 167 | |
BVanderWilp | 0:d445abf9a8e9 | 168 | phaseAEN = 0; |
BVanderWilp | 3:2bcc36fe4de5 | 169 | phaseBEN = 0; |
BVanderWilp | 3:2bcc36fe4de5 | 170 | phaseCEN = 0; |
BVanderWilp | 3:2bcc36fe4de5 | 171 | //begin sensored mode |
BVanderWilp | 1:69c06d3676fd | 172 | hallA.fall(&Afall); |
BVanderWilp | 1:69c06d3676fd | 173 | hallA.rise(&Arise); |
BVanderWilp | 1:69c06d3676fd | 174 | hallB.fall(&Bfall); |
BVanderWilp | 1:69c06d3676fd | 175 | hallB.rise(&Brise); |
BVanderWilp | 1:69c06d3676fd | 176 | hallC.fall(&Cfall); |
BVanderWilp | 1:69c06d3676fd | 177 | hallC.rise(&Crise); |
BVanderWilp | 1:69c06d3676fd | 178 | |
BVanderWilp | 3:2bcc36fe4de5 | 179 | int h1 = hallA.read(); |
BVanderWilp | 3:2bcc36fe4de5 | 180 | int h2 = hallB.read(); |
BVanderWilp | 3:2bcc36fe4de5 | 181 | int h3 = hallC.read(); |
BVanderWilp | 3:2bcc36fe4de5 | 182 | //check where we start |
BVanderWilp | 3:2bcc36fe4de5 | 183 | if(h1 == 0 && h2 == 1 && h3 == 1) //state1 |
BVanderWilp | 3:2bcc36fe4de5 | 184 | { |
BVanderWilp | 3:2bcc36fe4de5 | 185 | Crise(); |
BVanderWilp | 3:2bcc36fe4de5 | 186 | Bfall(); |
BVanderWilp | 3:2bcc36fe4de5 | 187 | } |
BVanderWilp | 3:2bcc36fe4de5 | 188 | else if(h1 == 0 && h2 == 0 && h3 == 1) //state2 |
BVanderWilp | 3:2bcc36fe4de5 | 189 | { |
BVanderWilp | 3:2bcc36fe4de5 | 190 | Bfall(); |
BVanderWilp | 3:2bcc36fe4de5 | 191 | Arise(); |
BVanderWilp | 3:2bcc36fe4de5 | 192 | } |
BVanderWilp | 3:2bcc36fe4de5 | 193 | else if(h1 == 1 && h2 == 0 && h3 == 1) //state3 |
BVanderWilp | 3:2bcc36fe4de5 | 194 | { |
BVanderWilp | 3:2bcc36fe4de5 | 195 | Arise(); |
BVanderWilp | 3:2bcc36fe4de5 | 196 | Cfall(); |
BVanderWilp | 3:2bcc36fe4de5 | 197 | } |
BVanderWilp | 3:2bcc36fe4de5 | 198 | else if(h1 == 1 && h2 == 0 && h3 == 0) //state4 |
BVanderWilp | 3:2bcc36fe4de5 | 199 | { |
BVanderWilp | 3:2bcc36fe4de5 | 200 | Cfall(); |
BVanderWilp | 3:2bcc36fe4de5 | 201 | Brise(); |
BVanderWilp | 3:2bcc36fe4de5 | 202 | } |
BVanderWilp | 3:2bcc36fe4de5 | 203 | else if(h1 == 1 && h2 == 1 && h3 == 0) //state5 |
BVanderWilp | 3:2bcc36fe4de5 | 204 | { |
BVanderWilp | 3:2bcc36fe4de5 | 205 | Brise(); |
BVanderWilp | 4:ab4d51b5b1c8 | 206 | Afall(); |
BVanderWilp | 3:2bcc36fe4de5 | 207 | } |
BVanderWilp | 3:2bcc36fe4de5 | 208 | else //state6 |
BVanderWilp | 3:2bcc36fe4de5 | 209 | { |
BVanderWilp | 3:2bcc36fe4de5 | 210 | Afall(); |
BVanderWilp | 4:ab4d51b5b1c8 | 211 | Crise(); |
BVanderWilp | 3:2bcc36fe4de5 | 212 | } |
BVanderWilp | 3:2bcc36fe4de5 | 213 | |
BVanderWilp | 0:d445abf9a8e9 | 214 | while(1) { |
BVanderWilp | 0:d445abf9a8e9 | 215 | led = !led; |
BVanderWilp | 0:d445abf9a8e9 | 216 | wait(0.2); |
BVanderWilp | 0:d445abf9a8e9 | 217 | } |
BVanderWilp | 0:d445abf9a8e9 | 218 | } |