STM32F302R8 with Konrad's inverter boards for senior design.

Dependencies:   mbed

Fork of Blue_Board_Ticker by Brad VanderWilp

Committer:
vicyap
Date:
Wed Apr 13 19:22:34 2016 +0000
Revision:
17:5e27edd3d8e6
Parent:
16:792055c232a5
Child:
18:d7033a38f20b
reverse working

Who changed what in which revision?

UserRevisionLine numberNew 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 5:6110655353ad 10 AnalogIn pot(PB_1);
BVanderWilp 5:6110655353ad 11
BVanderWilp 0:d445abf9a8e9 12 InterruptIn button(USER_BUTTON);
BVanderWilp 0:d445abf9a8e9 13
BVanderWilp 0:d445abf9a8e9 14 DigitalOut redLed(PB_2);
BVanderWilp 0:d445abf9a8e9 15 DigitalOut led(LED1);
BVanderWilp 1:69c06d3676fd 16 InterruptIn hallA(PA_15); //H1, Green
BVanderWilp 1:69c06d3676fd 17 InterruptIn hallB(PB_3); //H2, Blue
BVanderWilp 1:69c06d3676fd 18 InterruptIn hallC(PB_10); //H3, White
BVanderWilp 0:d445abf9a8e9 19
BVanderWilp 6:e401c592d2c3 20 Ticker rpmInterrupt;
vicyap 12:d34b96d7f997 21 Ticker spinTicker;
BVanderWilp 6:e401c592d2c3 22 int revCount = 0;
BVanderWilp 6:e401c592d2c3 23 int rpmPrintFlag = 0;
BVanderWilp 6:e401c592d2c3 24 int currentRPM;
BVanderWilp 0:d445abf9a8e9 25
BVanderWilp 5:6110655353ad 26 float pwmMax = 0.9;
BVanderWilp 5:6110655353ad 27 float pwmDuty;
BVanderWilp 0:d445abf9a8e9 28 int stall = 0;
BVanderWilp 7:9d90184335aa 29 int reverse = 0;
BVanderWilp 6:e401c592d2c3 30
BVanderWilp 6:e401c592d2c3 31 void rpmCalc()
BVanderWilp 6:e401c592d2c3 32 {
BVanderWilp 7:9d90184335aa 33 currentRPM = revCount * 60; //account for elec vs mech rpm
BVanderWilp 6:e401c592d2c3 34 revCount = 0;
BVanderWilp 6:e401c592d2c3 35 rpmPrintFlag = 1;
BVanderWilp 6:e401c592d2c3 36 }
BVanderWilp 9:af60c737a93e 37 //original names: CBA CBA, new names: BAC BAC
BVanderWilp 9:af60c737a93e 38 void Brise() //state1, A0 B- C+
vicyap 15:dddb511c39b4 39 {
vicyap 17:5e27edd3d8e6 40 phaseA.write(0);
vicyap 17:5e27edd3d8e6 41 phaseB.write(0);
vicyap 17:5e27edd3d8e6 42 phaseC.write(pwmDuty);
BVanderWilp 0:d445abf9a8e9 43 phaseAEN = 0;
BVanderWilp 1:69c06d3676fd 44 phaseBEN = 1;
BVanderWilp 3:2bcc36fe4de5 45 phaseCEN = 1;
BVanderWilp 1:69c06d3676fd 46 }
BVanderWilp 1:69c06d3676fd 47
BVanderWilp 9:af60c737a93e 48 void Afall() //state2, A+ B- C0
BVanderWilp 1:69c06d3676fd 49 {
vicyap 17:5e27edd3d8e6 50 phaseA.write(pwmDuty);
vicyap 17:5e27edd3d8e6 51 phaseB.write(0);
BVanderWilp 1:69c06d3676fd 52 phaseC.write(0);
BVanderWilp 1:69c06d3676fd 53 phaseAEN = 1;
BVanderWilp 3:2bcc36fe4de5 54 phaseBEN = 1;
vicyap 17:5e27edd3d8e6 55 phaseCEN = 0;
BVanderWilp 1:69c06d3676fd 56 }
BVanderWilp 1:69c06d3676fd 57
BVanderWilp 9:af60c737a93e 58 void Crise() //state3, A+ B0 C-
BVanderWilp 1:69c06d3676fd 59 {
vicyap 17:5e27edd3d8e6 60 phaseA.write(pwmDuty);
vicyap 17:5e27edd3d8e6 61 phaseB.write(0);
vicyap 17:5e27edd3d8e6 62 phaseC.write(0);
vicyap 17:5e27edd3d8e6 63 phaseAEN = 1;
BVanderWilp 1:69c06d3676fd 64 phaseBEN = 0;
BVanderWilp 1:69c06d3676fd 65 phaseCEN = 1;
BVanderWilp 1:69c06d3676fd 66 }
BVanderWilp 1:69c06d3676fd 67
BVanderWilp 9:af60c737a93e 68 void Bfall() //state4, A0 B+ C-
BVanderWilp 1:69c06d3676fd 69 {
BVanderWilp 0:d445abf9a8e9 70 phaseA.write(0);
BVanderWilp 0:d445abf9a8e9 71 phaseB.write(pwmDuty);
vicyap 17:5e27edd3d8e6 72 phaseC.write(0);
vicyap 17:5e27edd3d8e6 73 phaseAEN = 0;
BVanderWilp 0:d445abf9a8e9 74 phaseBEN = 1;
BVanderWilp 3:2bcc36fe4de5 75 phaseCEN = 1;
BVanderWilp 0:d445abf9a8e9 76 }
BVanderWilp 0:d445abf9a8e9 77
BVanderWilp 9:af60c737a93e 78 void Arise() //state5, A- B+ C0
BVanderWilp 0:d445abf9a8e9 79 {
vicyap 17:5e27edd3d8e6 80 phaseA.write(0);
vicyap 17:5e27edd3d8e6 81 phaseB.write(pwmDuty);
vicyap 17:5e27edd3d8e6 82 phaseC.write(0);
BVanderWilp 0:d445abf9a8e9 83 phaseAEN = 1;
vicyap 17:5e27edd3d8e6 84 phaseCEN = 0;
BVanderWilp 3:2bcc36fe4de5 85 phaseBEN = 1;
BVanderWilp 0:d445abf9a8e9 86 }
BVanderWilp 0:d445abf9a8e9 87
BVanderWilp 9:af60c737a93e 88 void Cfall() //state6, A- B0 C+
BVanderWilp 0:d445abf9a8e9 89 {
vicyap 17:5e27edd3d8e6 90 phaseAEN = 1;
BVanderWilp 0:d445abf9a8e9 91 phaseBEN = 0;
vicyap 17:5e27edd3d8e6 92 phaseCEN = 1;
vicyap 17:5e27edd3d8e6 93 phaseA.write(0);
BVanderWilp 0:d445abf9a8e9 94 phaseB.write(0);
BVanderWilp 0:d445abf9a8e9 95 phaseC.write(pwmDuty);
BVanderWilp 0:d445abf9a8e9 96 }
BVanderWilp 0:d445abf9a8e9 97
vicyap 14:0f50e85bd463 98 void toggleRedLed()
vicyap 14:0f50e85bd463 99 {
vicyap 15:dddb511c39b4 100 redLed = !redLed;
vicyap 14:0f50e85bd463 101 }
BVanderWilp 0:d445abf9a8e9 102
BVanderWilp 7:9d90184335aa 103 void jumpStart()
BVanderWilp 7:9d90184335aa 104 {
BVanderWilp 3:2bcc36fe4de5 105 int h1 = hallA.read();
BVanderWilp 3:2bcc36fe4de5 106 int h2 = hallB.read();
BVanderWilp 3:2bcc36fe4de5 107 int h3 = hallC.read();
BVanderWilp 3:2bcc36fe4de5 108 //check where we start
vicyap 16:792055c232a5 109
vicyap 16:792055c232a5 110 if (reverse == 0)
vicyap 16:792055c232a5 111 {
vicyap 16:792055c232a5 112 if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
vicyap 16:792055c232a5 113 Afall();
vicyap 16:792055c232a5 114 } else if(h1 == 0 && h2 == 0 && h3 == 1) { //state2
vicyap 16:792055c232a5 115 Crise();
vicyap 16:792055c232a5 116 } else if(h1 == 1 && h2 == 0 && h3 == 1) { //state3
vicyap 16:792055c232a5 117 Bfall();
vicyap 16:792055c232a5 118 } else if(h1 == 1 && h2 == 0 && h3 == 0) { //state4
vicyap 16:792055c232a5 119 Arise();
vicyap 16:792055c232a5 120 } else if(h1 == 1 && h2 == 1 && h3 == 0) { //state5
vicyap 16:792055c232a5 121 Cfall();
vicyap 16:792055c232a5 122 } else { //(h1 == 0 && h2 == 1 && h3 == 0)state6
vicyap 16:792055c232a5 123 Brise();
vicyap 16:792055c232a5 124 }
vicyap 16:792055c232a5 125 }
vicyap 16:792055c232a5 126 else if (reverse == 1)
vicyap 16:792055c232a5 127 {
vicyap 16:792055c232a5 128 // A -> C
vicyap 16:792055c232a5 129 // B -> A
vicyap 16:792055c232a5 130 // C -> B
vicyap 16:792055c232a5 131 if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
vicyap 16:792055c232a5 132 Cfall();
vicyap 16:792055c232a5 133 } else if(h1 == 0 && h2 == 0 && h3 == 1) { //state2
vicyap 16:792055c232a5 134 Brise();
vicyap 16:792055c232a5 135 } else if(h1 == 1 && h2 == 0 && h3 == 1) { //state3
vicyap 16:792055c232a5 136 Afall();
vicyap 16:792055c232a5 137 } else if(h1 == 1 && h2 == 0 && h3 == 0) { //state4
vicyap 16:792055c232a5 138 Crise();
vicyap 16:792055c232a5 139 } else if(h1 == 1 && h2 == 1 && h3 == 0) { //state5
vicyap 16:792055c232a5 140 Bfall();
vicyap 16:792055c232a5 141 } else { //(h1 == 0 && h2 == 1 && h3 == 0) state6
vicyap 16:792055c232a5 142 Arise();
vicyap 16:792055c232a5 143 }
vicyap 14:0f50e85bd463 144 }
vicyap 14:0f50e85bd463 145 toggleRedLed();
vicyap 17:5e27edd3d8e6 146 revCount++;
BVanderWilp 7:9d90184335aa 147 }
BVanderWilp 7:9d90184335aa 148
BVanderWilp 7:9d90184335aa 149 void activate()
BVanderWilp 7:9d90184335aa 150 {
vicyap 15:dddb511c39b4 151 if(stall == 0) {
BVanderWilp 7:9d90184335aa 152 stall = 1;
vicyap 15:dddb511c39b4 153 } else {
vicyap 17:5e27edd3d8e6 154 if (pwmDuty < 0.4f)
vicyap 16:792055c232a5 155 {
vicyap 16:792055c232a5 156 reverse = reverse ^ 1;
vicyap 16:792055c232a5 157 }
BVanderWilp 7:9d90184335aa 158 }
BVanderWilp 7:9d90184335aa 159 }
BVanderWilp 7:9d90184335aa 160
vicyap 15:dddb511c39b4 161 int main()
vicyap 15:dddb511c39b4 162 {
BVanderWilp 7:9d90184335aa 163 //wait until button push to start
BVanderWilp 7:9d90184335aa 164 rpmInterrupt.attach(&rpmCalc, 1);
BVanderWilp 7:9d90184335aa 165 button.rise(&activate);
BVanderWilp 7:9d90184335aa 166 while(stall == 0) {
BVanderWilp 7:9d90184335aa 167 led = !led;
BVanderWilp 7:9d90184335aa 168 wait(1);
BVanderWilp 7:9d90184335aa 169 }
BVanderWilp 7:9d90184335aa 170
BVanderWilp 7:9d90184335aa 171 pwmDuty = pot.read() * pwmMax;
BVanderWilp 7:9d90184335aa 172
BVanderWilp 7:9d90184335aa 173 phaseA.period_us(50);
BVanderWilp 7:9d90184335aa 174 phaseB.period_us(50);
BVanderWilp 7:9d90184335aa 175 phaseC.period_us(50);
vicyap 15:dddb511c39b4 176
BVanderWilp 7:9d90184335aa 177 phaseA.write(0);
BVanderWilp 7:9d90184335aa 178 phaseB.write(0);
vicyap 15:dddb511c39b4 179 phaseC.write(0);
vicyap 15:dddb511c39b4 180
BVanderWilp 7:9d90184335aa 181 phaseAEN = 0;
BVanderWilp 7:9d90184335aa 182 phaseBEN = 0;
BVanderWilp 7:9d90184335aa 183 phaseCEN = 0;
vicyap 15:dddb511c39b4 184
vicyap 13:08c700d71324 185 spinTicker.attach_us(&jumpStart, 50);
vicyap 16:792055c232a5 186 float ADCSum = 0;
BVanderWilp 5:6110655353ad 187 int ADCCount = 0;
BVanderWilp 0:d445abf9a8e9 188 while(1) {
BVanderWilp 0:d445abf9a8e9 189 led = !led;
BVanderWilp 5:6110655353ad 190 ADCSum += pot.read();
BVanderWilp 5:6110655353ad 191 ADCCount++;
vicyap 15:dddb511c39b4 192 if(ADCCount == 20) {
BVanderWilp 5:6110655353ad 193 pwmDuty = (ADCSum/20) * pwmMax;
BVanderWilp 5:6110655353ad 194 ADCSum = 0;
BVanderWilp 5:6110655353ad 195 ADCCount = 0;
BVanderWilp 5:6110655353ad 196 }
vicyap 15:dddb511c39b4 197 if(rpmPrintFlag == 1) {
vicyap 16:792055c232a5 198 printf("%d rpm; %f duty; reverse: %d\r\n", currentRPM, pwmDuty, reverse);
BVanderWilp 6:e401c592d2c3 199 rpmPrintFlag = 0;
BVanderWilp 6:e401c592d2c3 200 }
BVanderWilp 5:6110655353ad 201 wait(0.05);
BVanderWilp 0:d445abf9a8e9 202 }
BVanderWilp 0:d445abf9a8e9 203 }