From b3c098fbb5bc0f64feda8d20e0dafb91f00aa20a Mon Sep 17 00:00:00 2001 From: Abel Tim Date: Sun, 30 Jun 2024 17:38:05 +0200 Subject: motor drivers --- debug/IMU_comp/IMU_comp.ino | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'debug/IMU_comp/IMU_comp.ino') diff --git a/debug/IMU_comp/IMU_comp.ino b/debug/IMU_comp/IMU_comp.ino index 40d99b6..0c3928d 100644 --- a/debug/IMU_comp/IMU_comp.ino +++ b/debug/IMU_comp/IMU_comp.ino @@ -1,11 +1,9 @@ -//https://www.pololu.com/product/2862 #include #include +#include LIS3MDL mag; -char report[80]; - void setup() { Serial.begin(9600); @@ -24,9 +22,24 @@ void loop() { mag.read(); - snprintf(report, sizeof(report), "M: %6d %6d %6d", - mag.m.x, mag.m.y, mag.m.z); - Serial.println(report); + // Calculate heading + float heading = atan2(mag.m.y, mag.m.x); + if (heading < 0) + heading += 2 * PI; // Normalize heading to positive angles + + // Convert from radians to degrees + float headingDegrees = heading * 180.0 / PI; + + // Adjust for magnetic declination if needed + // headingDegrees += magneticDeclination; + + // Normalize headingDegrees to [0, 360) range + if (headingDegrees >= 360.0) + headingDegrees -= 360.0; + + // Print heading + Serial.print("Heading: "); + Serial.println(headingDegrees); delay(100); } -- cgit v1.2.3