From 97eb0c58b13ab0a362c1c79d5d62ad4ec64f2a47 Mon Sep 17 00:00:00 2001 From: Theodore Ng Date: Sat, 31 Jan 2026 17:41:46 -0600 Subject: [PATCH 1/3] Changing magnetometer driver --- MIDAS/platformio.ini | 4 ++-- MIDAS/src/hardware/Magnetometer.cpp | 24 +++++++++++++----------- MIDAS/src/hardware/main.cpp | 4 ++-- MIDAS/src/hardware/pins.h | 5 +++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/MIDAS/platformio.ini b/MIDAS/platformio.ini index 2f52aeb4..3f911218 100644 --- a/MIDAS/platformio.ini +++ b/MIDAS/platformio.ini @@ -13,7 +13,7 @@ build_flags = build_unflags = -std=gnu++11 lib_deps = - adafruit/Adafruit LIS3MDL@^1.2.1 ; Magnetometer driver + sparkfun/SparkFun MMC5983MA Magnetometer Arduino Library@^1.1.4 ; Magnetometer driver sparkfun/SparkFun u-blox GNSS v3@^3.1.8 ; GPS build_src_filter = +<*> - + - @@ -33,7 +33,7 @@ build_flags = build_unflags = -std=gnu++11 lib_deps = - adafruit/Adafruit LIS3MDL@^1.2.1 ; Magnetometer driver + sparkfun/SparkFun MMC5983MA Magnetometer Arduino Library@^1.1.4 ; Magnetometer driver sparkfun/SparkFun u-blox GNSS v3@^3.1.8 ; GPS build_src_filter = +<*> - + - diff --git a/MIDAS/src/hardware/Magnetometer.cpp b/MIDAS/src/hardware/Magnetometer.cpp index 23ef322d..a44d0360 100644 --- a/MIDAS/src/hardware/Magnetometer.cpp +++ b/MIDAS/src/hardware/Magnetometer.cpp @@ -1,27 +1,29 @@ -#include +#include #include "sensors.h" #include "hal.h" -Adafruit_LIS3MDL LIS3MDL; // global static instance of the sensor +SFE_MMC5983MA MMC5983; // global static instance of the sensor ErrorCode MagnetometerSensor::init() { - if (!LIS3MDL.begin_SPI(LIS3MDL_CS)) { // Checks if sensor is connected + if (MMC5983.begin(MMC5983_CS) == false) { // Checks if sensor is connected return ErrorCode::MagnetometerCouldNotBeInitialized; } - LIS3MDL.setOperationMode(LIS3MDL_CONTINUOUSMODE); // Reading continuously, instead of single-shot or off - LIS3MDL.setDataRate(LIS3MDL_DATARATE_155_HZ); - LIS3MDL.setRange(LIS3MDL_RANGE_4_GAUSS); // Earth's magnetic field is 1/2 gauss, can detect high current return ErrorCode::NoError; } Magnetometer MagnetometerSensor::read() { // read from aforementioned global instance of sensor - LIS3MDL.read(); + uint32_t cx, cy, cz; + double X, Y, Z; - float mx = LIS3MDL.x_gauss; - float my = LIS3MDL.y_gauss; - float mz = LIS3MDL.z_gauss; - Magnetometer reading{mx, my, mz}; + MMC5983.getMeasurementXYZ(&cx, &cy, &cz); + + double sf = (double)(1 << 17); + X = ((double)cx - sf)/sf; + Y = ((double)cy - sf)/sf; + Z = ((double)cz - sf)/sf; + + Magnetometer reading{X, Y, Z}; return reading; } diff --git a/MIDAS/src/hardware/main.cpp b/MIDAS/src/hardware/main.cpp index 4985d40f..fc8e7a60 100644 --- a/MIDAS/src/hardware/main.cpp +++ b/MIDAS/src/hardware/main.cpp @@ -78,7 +78,7 @@ void setup() pinMode(LSM6DS3_CS, OUTPUT); pinMode(KX134_CS, OUTPUT); pinMode(ADXL355_CS, OUTPUT); - pinMode(LIS3MDL_CS, OUTPUT); + pinMode(MMC5983_CS, OUTPUT); pinMode(BNO086_CS, OUTPUT); pinMode(BNO086_RESET, OUTPUT); pinMode(CAN_CS, OUTPUT); @@ -89,7 +89,7 @@ void setup() digitalWrite(LSM6DS3_CS, HIGH); digitalWrite(KX134_CS, HIGH); digitalWrite(ADXL355_CS, HIGH); - digitalWrite(LIS3MDL_CS, HIGH); + digitalWrite(MMC5983_CS, HIGH); digitalWrite(BNO086_CS, HIGH); digitalWrite(CAN_CS, HIGH); digitalWrite(E22_CS, HIGH); diff --git a/MIDAS/src/hardware/pins.h b/MIDAS/src/hardware/pins.h index 60724c40..ec7ab690 100644 --- a/MIDAS/src/hardware/pins.h +++ b/MIDAS/src/hardware/pins.h @@ -18,7 +18,8 @@ #define ADXL355_CS 44 // magnetometer chip select -#define LIS3MDL_CS 35 +#define MMC5983_CS 4 +#define MMC5983_INT 7 // orientation chip select, interrupt #define BNO086_CS 48 @@ -92,4 +93,4 @@ #define FLASH_DAT2 15 #define FLASH_DAT3 16 -#define GPIO_RESET 47 \ No newline at end of file +#define GPIO_RESET 47 From feb28ae1ddd744926fc384a25ecd02d01ef61074 Mon Sep 17 00:00:00 2001 From: Theodore Ng Date: Tue, 10 Feb 2026 19:27:54 -0600 Subject: [PATCH 2/3] fix narrow conversion --- MIDAS/src/sensor_data.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MIDAS/src/sensor_data.h b/MIDAS/src/sensor_data.h index 47358997..92d8d1f4 100644 --- a/MIDAS/src/sensor_data.h +++ b/MIDAS/src/sensor_data.h @@ -166,9 +166,9 @@ struct GPS { * @brief data from the magnetometer */ struct Magnetometer { - float mx; - float my; - float mz; + double mx; + double my; + double mz; }; struct Quaternion { @@ -275,4 +275,4 @@ struct PyroState { struct CameraData { uint8_t camera_state = 255; float camera_voltage = 0; -}; \ No newline at end of file +}; From 3e99349ba8fe78c32b1e64b0c688e000b061d269 Mon Sep 17 00:00:00 2001 From: Theodore Ng Date: Tue, 10 Feb 2026 19:39:25 -0600 Subject: [PATCH 3/3] Fixed build error with Adafruit --- MIDAS/platformio.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MIDAS/platformio.ini b/MIDAS/platformio.ini index 3f911218..c35ad6bc 100644 --- a/MIDAS/platformio.ini +++ b/MIDAS/platformio.ini @@ -13,6 +13,7 @@ build_flags = build_unflags = -std=gnu++11 lib_deps = + adafruit/Adafruit LIS3MDL@^1.2.1 ; Old magnetometer driver, Adafruit_BNO08x fails or else sparkfun/SparkFun MMC5983MA Magnetometer Arduino Library@^1.1.4 ; Magnetometer driver sparkfun/SparkFun u-blox GNSS v3@^3.1.8 ; GPS @@ -33,6 +34,7 @@ build_flags = build_unflags = -std=gnu++11 lib_deps = + adafruit/Adafruit LIS3MDL@^1.2.1 ; Old magnetometer driver, Adafruit_BNO08x fails or else sparkfun/SparkFun MMC5983MA Magnetometer Arduino Library@^1.1.4 ; Magnetometer driver sparkfun/SparkFun u-blox GNSS v3@^3.1.8 ; GPS