A compass demo using HMC5983
Dependencies: HMC5983 StepperController mbed
Revision 0:7c80cce5d0d3, committed 2018-06-15
- Comitter:
- acracan
- Date:
- Fri Jun 15 17:40:27 2018 +0000
- Commit message:
- A demo with HMC5983
Changed in this revision
diff -r 000000000000 -r 7c80cce5d0d3 HMC5983.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HMC5983.lib Fri Jun 15 17:40:27 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/acracan/code/HMC5983/#cffff4c45a14
diff -r 000000000000 -r 7c80cce5d0d3 StepperController.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StepperController.lib Fri Jun 15 17:40:27 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/acracan/code/StepperController/#d589fc047bc9
diff -r 000000000000 -r 7c80cce5d0d3 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 15 17:40:27 2018 +0000 @@ -0,0 +1,59 @@ +#include "HMC5983.h" +#include "mbed.h" +#include "steppercontroller.h" + + +Serial pc(USBTX, USBRX); + +I2C i2c(D14, D15); +HMC5983 compass(i2c); + +Timer displayTimer; +Timer stepperRelaxTimer; +Timer compassPollTimer; +int crtState, prvState; +StepperController stepper(D7, D6, D5, D4); +double desiredAngle, actualAngle; + +double angleDiff(double a, double b); + +int main() +{ + prvState = 0; + stepper.setSequenceType(StepperController::Interleaved); + displayTimer.start(); + stepperRelaxTimer.start(); + compassPollTimer.start(); + stepper.setPulseWidth(0.2); + desiredAngle = 0.0; + while (1) { + if (compassPollTimer.read() > 1) { + compassPollTimer.reset(); + actualAngle = compass.read(); + + pc.printf("Compass: %f\n", actualAngle); + pc.printf("Difference: %f\n", angleDiff(actualAngle, desiredAngle)); + if (abs(angleDiff(actualAngle, desiredAngle)) > 5) { + if (angleDiff(actualAngle, desiredAngle) > 0) { + stepper.setDirection(StepperController::DirectionCCW); + stepper.advance(); + } + else { + stepper.setDirection(StepperController::DirectionCW); + stepper.advance(); + } + } + } + } +} + +double angleDiff(double a, double b) +{ + double diff = a - b; + + if (diff > 180) + diff -= 360; + if (diff < -180) + diff += 360; + return diff; +} \ No newline at end of file
diff -r 000000000000 -r 7c80cce5d0d3 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 15 17:40:27 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/5aab5a7997ee \ No newline at end of file