Stewart Coulden-Smith
/
EquatorStrutDigitalMonitor
Equator Strut Controller.
Revision 2:088eeae4287c, committed 2014-08-07
- Comitter:
- pyrostew
- Date:
- Thu Aug 07 10:37:03 2014 +0000
- Parent:
- 1:a33723b70582
- Commit message:
- Added smoothing algorithm to interrupt period value.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Aug 05 13:09:58 2014 +0000 +++ b/main.cpp Thu Aug 07 10:37:03 2014 +0000 @@ -25,8 +25,33 @@ int interruptPeriod = 0; int lastTime = 0; +int intteruptPeriodArray[15]; +int arrayTotal = 0; +char arrayPos = 0; + char counter = 0; +void SmoothingAdd(int input) +{ + arrayTotal -= intteruptPeriodArray[arrayPos]; + arrayTotal += input; + intteruptPeriodArray[arrayPos] = input; + + if (arrayPos == 14) + { + arrayPos = 0; + } + else + { + arrayTotal++; + } +} + +int SmoothedInterruptPeriod() +{ + return arrayTotal / 15; +} + void RGHSinHandler() { if (PinState == 2) @@ -41,7 +66,7 @@ { direction = 1; position += 0.04 * direction; - interruptPeriod = RunningTime.read_us() - lastTime; + SmoothingAdd(RunningTime.read_us() - lastTime); lastTime = RunningTime.read_us(); } } @@ -65,7 +90,7 @@ { direction = -1; position += 0.04 * direction; - interruptPeriod = RunningTime.read_us() - lastTime; + SmoothingAdd(RunningTime.read_us() - lastTime); lastTime = RunningTime.read_us(); } } @@ -151,7 +176,7 @@ { return 0.0; } - return (direction * 0.04)/((double)interruptPeriod / 1000000.0); + return (direction * 0.04)/((double)SmoothedInterruptPeriod() / 1000000.0); } void SerialTransmit()