STM32F302R8 with Konrad's inverter boards for senior design.
Fork of Blue_Board_Ticker by
Diff: main.cpp
- Revision:
- 20:029a58eb24ab
- Parent:
- 19:085eb0579185
- Child:
- 21:2bf65c29a3c6
--- a/main.cpp Fri Apr 15 21:35:50 2016 +0000 +++ b/main.cpp Fri Apr 15 21:54:18 2016 +0000 @@ -23,16 +23,18 @@ int rpmPrintFlag = 0; int currentRPM; -float pwmMax = 0.9; +int ADCCountMax = 1000; // tune for responsiveness vs. stability, higher value -> less responsive -> more stable +float pwmMax = 0.9; // tune for max pwmDuty. higher value -> more power available float pwmDuty; int stall = 0; int reverse = 0; int spinTickerCounter = 0; +int spinTickerPeriod_us = 50; // tune for how often hall sensors are checked int ticksPerHall = 0; int prevHallState = 0; -int pole_pairs = 2; +int pole_pairs = 2; // tune for proper mech rpm calculation void rpmCalc() { @@ -203,22 +205,24 @@ int h3 = hallC.read(); prevHallState = (h1 << 2) + (h2 << 1) + h3; - spinTicker.attach_us(&jumpStart, 50); + spinTicker.attach_us(&jumpStart, spinTickerPeriod_us); float ADCSum = 0; int ADCCount = 0; while(1) { led = !led; - ADCSum += pot.read(); - ADCCount++; - if(ADCCount == 20) { - pwmDuty = (ADCSum/20) * pwmMax; - ADCSum = 0; - ADCCount = 0; + if(ADCCount == ADCCountMax) { + ADCSum = ADCSum - (ADCSum / ADCCountMax); + ADCSum += pot.read(); + pwmDuty = (ADCSum/ADCCountMax) * pwmMax; + } + else + { + ADCSum += pot.read(); + ADCCount++; } if(rpmPrintFlag == 1) { printf("%d rpm; %f duty; reverse: %d; ticksPerHall = %d\r\n", currentRPM, pwmDuty, reverse, ticksPerHall); rpmPrintFlag = 0; } - wait(0.05); } }