David's dead reckoning code for the LVBots competition on March 6th. Uses the mbed LPC1768, DRV8835, QTR-3RC, and two DC motors with encoders.

Dependencies:   PololuEncoder Pacer mbed GeneralDebouncer

Revision:
40:6fa672be85ec
Child:
42:96671b71aac5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/turn_sensor.h	Thu Jul 25 02:53:34 2019 +0000
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <mbed.h>
+
+class TurnSensor
+{
+    // TODO: for production code, you would want a way to set the gyro offset
+
+    public:
+
+    void reset();
+    void start();
+    void update();
+    
+    int32_t getAngle()
+    {
+        return (int32_t)angleUnsigned;
+    }
+    
+    uint32_t getAngleUnsigned()
+    {
+        return angleUnsigned;
+    }
+    
+    int16_t getAngleDegrees()
+    {
+        return (((int32_t)angleUnsigned >> 16) * 360) >> 16;
+    }
+    
+    int16_t getRate()
+    {
+        return rate;
+    }
+    
+    private:
+
+    Timer timer;
+    uint32_t angleUnsigned;
+    int16_t rate;
+    uint16_t gyroLastUpdate;
+};
+
+
+// This constant represents a turn of 45 degrees.
+const int32_t turnAngle45 = 0x20000000;
+
+// This constant represents a turn of 90 degrees.
+const int32_t turnAngle90 = turnAngle45 * 2;
+
+// This constant represents a turn of approximately 1 degree.
+const int32_t turnAngle1 = (turnAngle45 + 22) / 45;
\ No newline at end of file