Brad VanderWilp
/
Blue_Board_Test
Senior Design open loop control for FreeScale motor using IHM07M1
main.cpp@1:4a4e44a7564a, 2016-04-06 (annotated)
- Committer:
- vicyap
- Date:
- Wed Apr 06 17:54:18 2016 +0000
- Revision:
- 1:4a4e44a7564a
- Parent:
- 0:bacbeb54a4dd
changed minDelay to 0.15
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BVanderWilp | 0:bacbeb54a4dd | 1 | #include "mbed.h" |
BVanderWilp | 0:bacbeb54a4dd | 2 | |
BVanderWilp | 0:bacbeb54a4dd | 3 | PwmOut phaseA(PA_8); |
BVanderWilp | 0:bacbeb54a4dd | 4 | DigitalOut phaseAEN(PC_10); |
BVanderWilp | 0:bacbeb54a4dd | 5 | PwmOut phaseB(PA_9); |
BVanderWilp | 0:bacbeb54a4dd | 6 | DigitalOut phaseBEN(PC_11); |
BVanderWilp | 0:bacbeb54a4dd | 7 | PwmOut phaseC(PA_10); |
BVanderWilp | 0:bacbeb54a4dd | 8 | DigitalOut phaseCEN(PC_12); |
BVanderWilp | 0:bacbeb54a4dd | 9 | |
BVanderWilp | 0:bacbeb54a4dd | 10 | InterruptIn button(USER_BUTTON); |
BVanderWilp | 0:bacbeb54a4dd | 11 | |
BVanderWilp | 0:bacbeb54a4dd | 12 | DigitalOut led(LED1); |
BVanderWilp | 0:bacbeb54a4dd | 13 | Ticker interrupt; |
BVanderWilp | 0:bacbeb54a4dd | 14 | |
BVanderWilp | 0:bacbeb54a4dd | 15 | int count = 0; |
BVanderWilp | 0:bacbeb54a4dd | 16 | float pwmDuty = 0.3; |
BVanderWilp | 0:bacbeb54a4dd | 17 | |
BVanderWilp | 0:bacbeb54a4dd | 18 | void intcall(){ |
BVanderWilp | 0:bacbeb54a4dd | 19 | count = (count + 1) % 6; |
BVanderWilp | 0:bacbeb54a4dd | 20 | |
BVanderWilp | 0:bacbeb54a4dd | 21 | if(count == 0) //B+, C- |
BVanderWilp | 0:bacbeb54a4dd | 22 | { |
BVanderWilp | 0:bacbeb54a4dd | 23 | // phaseA = 0; |
BVanderWilp | 0:bacbeb54a4dd | 24 | // phaseAP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 25 | // phaseB = 1; |
BVanderWilp | 0:bacbeb54a4dd | 26 | // phaseBP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 27 | // phaseC = 0; |
BVanderWilp | 0:bacbeb54a4dd | 28 | // phaseCP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 29 | phaseAEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 30 | phaseA.write(0); |
BVanderWilp | 0:bacbeb54a4dd | 31 | phaseB.write(pwmDuty); |
BVanderWilp | 0:bacbeb54a4dd | 32 | phaseBEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 33 | } |
BVanderWilp | 0:bacbeb54a4dd | 34 | else if(count == 1) //B+, A- |
BVanderWilp | 0:bacbeb54a4dd | 35 | { |
BVanderWilp | 0:bacbeb54a4dd | 36 | // phaseA = 0; |
BVanderWilp | 0:bacbeb54a4dd | 37 | // phaseAP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 38 | // phaseB = 1; |
BVanderWilp | 0:bacbeb54a4dd | 39 | // phaseBP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 40 | // phaseC = 0; |
BVanderWilp | 0:bacbeb54a4dd | 41 | // phaseCP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 42 | phaseCEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 43 | phaseAEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 44 | } |
BVanderWilp | 0:bacbeb54a4dd | 45 | else if(count == 2) //C+, A- |
BVanderWilp | 0:bacbeb54a4dd | 46 | { |
BVanderWilp | 0:bacbeb54a4dd | 47 | // phaseA = 0; |
BVanderWilp | 0:bacbeb54a4dd | 48 | // phaseAP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 49 | // phaseB = 0; |
BVanderWilp | 0:bacbeb54a4dd | 50 | // phaseBP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 51 | // phaseC = 1; |
BVanderWilp | 0:bacbeb54a4dd | 52 | // phaseCP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 53 | phaseBEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 54 | phaseB.write(0); |
BVanderWilp | 0:bacbeb54a4dd | 55 | phaseC.write(pwmDuty); |
BVanderWilp | 0:bacbeb54a4dd | 56 | phaseCEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 57 | } |
BVanderWilp | 0:bacbeb54a4dd | 58 | else if(count == 3) //C+, B- |
BVanderWilp | 0:bacbeb54a4dd | 59 | { |
BVanderWilp | 0:bacbeb54a4dd | 60 | // phaseA = 0; |
BVanderWilp | 0:bacbeb54a4dd | 61 | // phaseAP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 62 | // phaseB = 0; |
BVanderWilp | 0:bacbeb54a4dd | 63 | // phaseBP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 64 | // phaseC = 1; |
BVanderWilp | 0:bacbeb54a4dd | 65 | // phaseCP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 66 | phaseAEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 67 | phaseBEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 68 | } |
BVanderWilp | 0:bacbeb54a4dd | 69 | else if(count == 4) //A+, B- |
BVanderWilp | 0:bacbeb54a4dd | 70 | { |
BVanderWilp | 0:bacbeb54a4dd | 71 | // phaseA = 1; |
BVanderWilp | 0:bacbeb54a4dd | 72 | // phaseAP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 73 | // phaseB = 0; |
BVanderWilp | 0:bacbeb54a4dd | 74 | // phaseBP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 75 | // phaseC = 0; |
BVanderWilp | 0:bacbeb54a4dd | 76 | // phaseCP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 77 | phaseCEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 78 | phaseC.write(0); |
BVanderWilp | 0:bacbeb54a4dd | 79 | phaseA.write(pwmDuty); |
BVanderWilp | 0:bacbeb54a4dd | 80 | phaseAEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 81 | } |
BVanderWilp | 0:bacbeb54a4dd | 82 | else if(count == 5) //A+, C- |
BVanderWilp | 0:bacbeb54a4dd | 83 | { |
BVanderWilp | 0:bacbeb54a4dd | 84 | // phaseA = 1; |
BVanderWilp | 0:bacbeb54a4dd | 85 | // phaseAP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 86 | // phaseB = 0; |
BVanderWilp | 0:bacbeb54a4dd | 87 | // phaseBP = 0; |
BVanderWilp | 0:bacbeb54a4dd | 88 | // phaseC = 0; |
BVanderWilp | 0:bacbeb54a4dd | 89 | // phaseCP = 1; |
BVanderWilp | 0:bacbeb54a4dd | 90 | phaseBEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 91 | phaseCEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 92 | } |
BVanderWilp | 0:bacbeb54a4dd | 93 | } |
BVanderWilp | 0:bacbeb54a4dd | 94 | |
BVanderWilp | 0:bacbeb54a4dd | 95 | int stall = 0; |
BVanderWilp | 0:bacbeb54a4dd | 96 | void activate() |
BVanderWilp | 0:bacbeb54a4dd | 97 | { |
BVanderWilp | 0:bacbeb54a4dd | 98 | stall = 1; |
BVanderWilp | 0:bacbeb54a4dd | 99 | } |
BVanderWilp | 0:bacbeb54a4dd | 100 | |
BVanderWilp | 0:bacbeb54a4dd | 101 | int main() { |
BVanderWilp | 0:bacbeb54a4dd | 102 | float intDelay = .05; |
BVanderWilp | 0:bacbeb54a4dd | 103 | // float minDelay = .005; |
vicyap | 1:4a4e44a7564a | 104 | float minDelay = .015; |
BVanderWilp | 0:bacbeb54a4dd | 105 | button.rise(&activate); |
BVanderWilp | 0:bacbeb54a4dd | 106 | while(stall == 0) { |
BVanderWilp | 0:bacbeb54a4dd | 107 | led = !led; |
BVanderWilp | 0:bacbeb54a4dd | 108 | wait(2); |
BVanderWilp | 0:bacbeb54a4dd | 109 | } |
BVanderWilp | 0:bacbeb54a4dd | 110 | interrupt.attach(&intcall, intDelay); |
BVanderWilp | 0:bacbeb54a4dd | 111 | phaseA.period_us(10); |
BVanderWilp | 0:bacbeb54a4dd | 112 | phaseB.period_us(10); |
BVanderWilp | 0:bacbeb54a4dd | 113 | phaseC.period_us(10); |
BVanderWilp | 0:bacbeb54a4dd | 114 | |
BVanderWilp | 0:bacbeb54a4dd | 115 | phaseA.write(0); |
BVanderWilp | 0:bacbeb54a4dd | 116 | phaseB.write(pwmDuty); |
BVanderWilp | 0:bacbeb54a4dd | 117 | phaseC.write(0); |
BVanderWilp | 0:bacbeb54a4dd | 118 | |
BVanderWilp | 0:bacbeb54a4dd | 119 | phaseAEN = 0; |
BVanderWilp | 0:bacbeb54a4dd | 120 | phaseBEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 121 | phaseCEN = 1; |
BVanderWilp | 0:bacbeb54a4dd | 122 | |
BVanderWilp | 0:bacbeb54a4dd | 123 | while(1) { |
BVanderWilp | 0:bacbeb54a4dd | 124 | led = !led; |
BVanderWilp | 0:bacbeb54a4dd | 125 | wait(0.2); |
BVanderWilp | 0:bacbeb54a4dd | 126 | if(intDelay > minDelay) |
BVanderWilp | 0:bacbeb54a4dd | 127 | { |
BVanderWilp | 0:bacbeb54a4dd | 128 | intDelay -= .001; |
BVanderWilp | 0:bacbeb54a4dd | 129 | interrupt.detach(); |
BVanderWilp | 0:bacbeb54a4dd | 130 | interrupt.attach(&intcall, intDelay); |
BVanderWilp | 0:bacbeb54a4dd | 131 | // pwmDuty += .01; |
BVanderWilp | 0:bacbeb54a4dd | 132 | pwmDuty += .007; |
BVanderWilp | 0:bacbeb54a4dd | 133 | } |
BVanderWilp | 0:bacbeb54a4dd | 134 | } |
BVanderWilp | 0:bacbeb54a4dd | 135 | } |