Initiate magnetometer based compass calibration from button presses (#5553)

* Initiate magenetometer based compass calibration from button presses

- only active for BMX160 accelerometers on RAK_4631
- replace automatic calibration on power on with button triggered
  calibration
- set 5 presses to trigger 30s calibration
- set 6 presses to trigger 60s calibration (useful if unit is not
  handheld, ie vehicle mounted)
- show calibration time remaining on calibration alert screen

* Fix non RAK 4631 builds

- exclude changes from non RAK 4631 builds
- remove calls to screen when not present

* Fix build on RAK4631_eth_gw

- exclude all compass heading updates on variant without screen

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
danwelch3 2025-01-16 16:22:27 -07:00 committed by GitHub
parent 7acd72ede1
commit a085614aaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 71 additions and 6 deletions

View File

@ -190,6 +190,20 @@ int32_t ButtonThread::runOnce()
case 4:
digitalWrite(PIN_EINK_EN, digitalRead(PIN_EINK_EN) == LOW);
break;
#endif
#if defined(RAK_4631)
// 5 clicks: start accelerometer/magenetometer calibration for 30 seconds
case 5:
if (accelerometerThread) {
accelerometerThread->calibrate(30);
}
break;
// 6 clicks: start accelerometer/magenetometer calibration for 60 seconds
case 6:
if (accelerometerThread) {
accelerometerThread->calibrate(60);
}
break;
#endif
// No valid multipress action
default:

View File

@ -278,6 +278,10 @@ class Screen : public concurrency::OSThread
bool hasHeading() { return hasCompass; }
long getHeading() { return compassHeading; }
void setEndCalibration(uint32_t _endCalibrationAt) { endCalibrationAt = _endCalibrationAt; }
uint32_t getEndCalibration() { return endCalibrationAt; }
// functions for display brightness
void increaseBrightness();
void decreaseBrightness();
@ -673,6 +677,8 @@ class Screen : public concurrency::OSThread
bool hasCompass = false;
float compassHeading;
uint32_t endCalibrationAt;
/// Holds state for debug information
DebugInfo debugInfo;

View File

@ -48,6 +48,13 @@ class AccelerometerThread : public concurrency::OSThread
setIntervalFromNow(0);
};
void calibrate(uint16_t forSeconds)
{
if (sensor) {
sensor->calibrate(forSeconds);
}
}
protected:
int32_t runOnce() override
{

View File

@ -25,19 +25,21 @@ bool BMX160Sensor::init()
int32_t BMX160Sensor::runOnce()
{
#if !defined(MESHTASTIC_EXCLUDE_SCREEN)
sBmx160SensorData_t magAccel;
sBmx160SensorData_t gAccel;
/* Get a new sensor event */
sensor.getAllData(&magAccel, NULL, &gAccel);
#if !defined(MESHTASTIC_EXCLUDE_SCREEN)
// experimental calibrate routine. Limited to between 10 and 30 seconds after boot
if (millis() > 12 * 1000 && millis() < 30 * 1000) {
if (doCalibration) {
if (!showingScreen) {
powerFSM.trigger(EVENT_PRESS); // keep screen alive during calibration
showingScreen = true;
screen->startAlert((FrameCallback)drawFrameCalibration);
}
if (magAccel.x > highestX)
highestX = magAccel.x;
if (magAccel.x < lowestX)
@ -50,9 +52,17 @@ int32_t BMX160Sensor::runOnce()
highestZ = magAccel.z;
if (magAccel.z < lowestZ)
lowestZ = magAccel.z;
} else if (showingScreen && millis() >= 30 * 1000) {
showingScreen = false;
screen->endAlert();
uint32_t now = millis();
if (now > endCalibrationAt) {
doCalibration = false;
endCalibrationAt = 0;
showingScreen = false;
screen->endAlert();
}
// LOG_DEBUG("BMX160 min_x: %.4f, max_X: %.4f, min_Y: %.4f, max_Y: %.4f, min_Z: %.4f, max_Z: %.4f", lowestX, highestX,
// lowestY, highestY, lowestZ, highestZ);
}
int highestRealX = highestX - (highestX + lowestX) / 2;
@ -93,12 +103,25 @@ int32_t BMX160Sensor::runOnce()
heading += 270;
break;
}
screen->setHeading(heading);
#endif
return MOTION_SENSOR_CHECK_INTERVAL_MS;
}
void BMX160Sensor::calibrate(uint16_t forSeconds)
{
#if !defined(MESHTASTIC_EXCLUDE_SCREEN)
LOG_DEBUG("BMX160 calibration started for %is", forSeconds);
doCalibration = true;
uint16_t calibrateFor = forSeconds * 1000; // calibrate for seconds provided
endCalibrationAt = millis() + calibrateFor;
screen->setEndCalibration(endCalibrationAt);
#endif
}
#endif
#endif

View File

@ -23,6 +23,7 @@ class BMX160Sensor : public MotionSensor
explicit BMX160Sensor(ScanI2C::FoundDevice foundDevice);
virtual bool init() override;
virtual int32_t runOnce() override;
virtual void calibrate(uint16_t forSeconds) override;
};
#else

View File

@ -2,6 +2,8 @@
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_I2C
char timeRemainingBuffer[12];
// screen is defined in main.cpp
extern graphics::Screen *screen;
@ -37,6 +39,12 @@ void MotionSensor::drawFrameCalibration(OLEDDisplay *display, OLEDDisplayUiState
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(FONT_MEDIUM);
display->drawString(x, y, "Calibrating\nCompass");
uint8_t timeRemaining = (screen->getEndCalibration() - millis()) / 1000;
sprintf(timeRemainingBuffer, "( %02d )", timeRemaining);
display->setFont(FONT_SMALL);
display->drawString(x, y + 40, timeRemainingBuffer);
int16_t compassX = 0, compassY = 0;
uint16_t compassDiam = graphics::Screen::getCompassDiam(display->getWidth(), display->getHeight());

View File

@ -40,6 +40,8 @@ class MotionSensor
// Refer to /src/concurrency/OSThread.h for more information
inline virtual int32_t runOnce() { return MOTION_SENSOR_CHECK_INTERVAL_MS; };
virtual void calibrate(uint16_t forSeconds){};
protected:
// Turn on the screen when a tap or motion is detected
virtual void wakeScreen();
@ -53,6 +55,10 @@ class MotionSensor
#endif
ScanI2C::FoundDevice device;
// Do calibration if true
bool doCalibration = false;
uint32_t endCalibrationAt = 0;
};
namespace MotionSensorI2C