A compass demo using HMC5983

Dependencies:   HMC5983 StepperController mbed

Files at this revision

API Documentation at this revision

Comitter:
acracan
Date:
Fri Jun 15 17:40:27 2018 +0000
Commit message:
A demo with HMC5983

Changed in this revision

HMC5983.lib Show annotated file Show diff for this revision Revisions of this file
StepperController.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /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
--- /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
--- /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
--- /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