Fix compass heading direction, add magnetic declination and heading offset

- Fix atan2 sign bug: atan2(-yHeading, xHeading) per Freescale AN4248 eq.22.
  The previous atan2(yHeading, xHeading) produced counter-clockwise angles,
  causing East/West to be swapped relative to compass convention.

- Add imuSetMagneticDeclination() (T:146, field "decl") to correct the
  offset between magnetic north and true north (iPhone shows true north).
  Value is persisted in imuConfig.json across reboots.

- Add imuSetHeadingOffset() (T:147, field "off") to compensate for sensor
  mounting orientation on the rover. Also persisted.

- Persist decl and hOff in imuConfig.json (version 2, backwards compatible).

- Fix calibration feedback for web interface: T:126 now includes calDone=1
  (success) or calDone=0 (fail) once after calibration completes, plus
  decl and hOff fields. Calibration start message explains polling procedure.

- Remove duplicate #include <nvs_flash.h> and unused Adafruit ICM20948
  includes that caused build failures when libraries were not installed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Josh
2026-04-23 21:46:00 +02:00
co-authored by Claude Sonnet 4.6
parent 6f1391d1e9
commit 9b0a5a9926
6 changed files with 296 additions and 188 deletions
+6 -10
View File
@@ -1,4 +1,4 @@
#include <ArduinoJson.h>
#include <ArduinoJson.h>
StaticJsonDocument<256> jsonCmdReceive;
StaticJsonDocument<256> jsonInfoSend;
StaticJsonDocument<512> jsonInfoHttp;
@@ -12,16 +12,12 @@ StaticJsonDocument<512> jsonInfoHttp;
#include <WiFi.h>
#include <WebServer.h>
#include <esp_now.h>
#include <nvs_flash.h>
#include <Adafruit_SSD1306.h>
#include <INA219_WE.h>
#include <ESP32Encoder.h>
#include <PID_v2.h>
#include <SimpleKalmanFilter.h>
#include <math.h>
#include <Adafruit_ICM20X.h>
#include <Adafruit_ICM20948.h>
#include <Adafruit_Sensor.h>
// functions for barrery info.
@@ -119,7 +115,7 @@ void setup() {
screenLine_0 = "UGV";
}
screenLine_1 = "version: 1.00";
screenLine_1 = "version: 1.00";
screenLine_2 = "starting...";
screenLine_3 = "";
oled_update();
@@ -136,9 +132,9 @@ void setup() {
screenLine_2 = screenLine_3;
screenLine_3 = "Initialize LittleFS";
oled_update();
if(InfoPrint == 1){Serial.println("Initialize LittleFS for Flash files ctrl.");}
initFS();
imuLoadMagnCalibration();
if(InfoPrint == 1){Serial.println("Initialize LittleFS for Flash files ctrl.");}
initFS();
imuLoadMagnCalibration();
// init the funcs in switch_module.h
screenLine_2 = screenLine_3;
@@ -265,4 +261,4 @@ void loop() {
heartBeatCtrl();
size_t freeHeap = esp_get_free_heap_size();
}
}