add calibration routine for analog lever
This commit is contained in:
parent
4b0959ecaa
commit
baf657a5f4
|
@ -43,7 +43,7 @@ union NonVolatileMemory {
|
|||
struct Calibration {
|
||||
uint32_t validMarker = VALID_MARKER_DEFAULT;
|
||||
JoystickCalibration joystickCal;
|
||||
LinearAnalogCalibration linearCal;
|
||||
LinearAnalogCalibration lever0;
|
||||
} __attribute__((packed)) calData;
|
||||
|
||||
uint8_t rawBytes[sizeof(NonVolatileMemory::Calibration)] = {};
|
||||
|
|
24
lib/single_axis_control/SingleAxis.cpp
Normal file
24
lib/single_axis_control/SingleAxis.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
/** \File SingleAxis.cpp
|
||||
* \copyright (c) 2025 len0rd
|
||||
* \date 2025-01-14
|
||||
*/
|
||||
#include "SingleAxis.hpp"
|
||||
|
||||
LinearSingleAxis::LinearSingleAxis(uint8_t analogPin)
|
||||
: m_pin{analogPin},
|
||||
m_cal{
|
||||
.min = 0,
|
||||
.max = 1 << 10,
|
||||
} {}
|
||||
|
||||
uint16_t LinearSingleAxis::readRaw() {
|
||||
return analogRead(m_pin);
|
||||
}
|
||||
|
||||
uint8_t LinearSingleAxis::readPercent() {
|
||||
return map(readRaw(), m_cal.min, m_cal.max, 0, 100);
|
||||
}
|
||||
|
||||
void LinearSingleAxis::applyCalibration(const LinearAnalogCalibration& cal) {
|
||||
m_cal = cal;
|
||||
}
|
31
lib/single_axis_control/SingleAxis.hpp
Normal file
31
lib/single_axis_control/SingleAxis.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/** \File SingleAxis.hpp
|
||||
* \copyright (c) 2025 len0rd
|
||||
* \date 2025-01-14
|
||||
*
|
||||
* Simple utility class for reading a 'single axis' analog device. ie: a potentiometer
|
||||
*/
|
||||
#ifndef __SINGLEAXIS_H__
|
||||
#define __SINGLEAXIS_H__
|
||||
#include "Arduino.h"
|
||||
#include "NvmEeprom.hpp"
|
||||
|
||||
class LinearSingleAxis {
|
||||
public:
|
||||
LinearSingleAxis(uint8_t analogPin);
|
||||
|
||||
uint16_t readRaw();
|
||||
|
||||
/// read as a percentage between calbirated min/max
|
||||
/// Return a value 0-100
|
||||
uint8_t readPercent();
|
||||
|
||||
/// Apply the provided calibration and use on subsequent @ref readPercent calls
|
||||
void applyCalibration(const LinearAnalogCalibration& cal);
|
||||
|
||||
private:
|
||||
uint8_t m_pin;
|
||||
|
||||
LinearAnalogCalibration m_cal;
|
||||
};
|
||||
|
||||
#endif /* __SINGLEAXIS_H__ */
|
29
src/main.cpp
29
src/main.cpp
|
@ -2,6 +2,7 @@
|
|||
#include <Arduino.h>
|
||||
#include "Arduino_LED_Matrix.h"
|
||||
#include "NvmEeprom.hpp"
|
||||
#include "SingleAxis.hpp"
|
||||
|
||||
// joystick calibration
|
||||
#define TIME_CAL_1 2000
|
||||
|
@ -10,6 +11,7 @@
|
|||
// static objects
|
||||
AlignedJoy joystick_1(0, 1); /// X on A0, Y on A1
|
||||
ArduinoLEDMatrix ledMatrix;
|
||||
LinearSingleAxis lever0(2);
|
||||
|
||||
/// Run calibration on the provided joystick. Save the results to the provided calibration
|
||||
bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
||||
|
@ -95,6 +97,22 @@ bool runJoystickCalibrationRoutine(AlignedJoy& joy, JoystickCalibration& cal) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool runLeverCalibration(LinearSingleAxis& lever, LinearAnalogCalibration& cal) {
|
||||
Serial.println(" Calibrate lever minimum. Move lever to the min position");
|
||||
delay(2000);
|
||||
Serial.println(" Capturing min now!");
|
||||
cal.min = lever.readRaw();
|
||||
delay(500);
|
||||
|
||||
Serial.println(" Calibrate lever maximum. Move lever to the max position");
|
||||
delay(2000);
|
||||
Serial.println(" Capturing max now!");
|
||||
cal.max = lever.readRaw();
|
||||
|
||||
Serial.println(" Calibration succesfull!");
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// SERIAL INITIALIZE
|
||||
Serial.begin(115200);
|
||||
|
@ -109,7 +127,7 @@ void setup() {
|
|||
readNvm(nvm);
|
||||
|
||||
// run joystick calibration if needed
|
||||
bool calibrationInvalid = nvm.valid();
|
||||
bool calibrationInvalid = !nvm.valid();
|
||||
do {
|
||||
calibrationInvalid |=
|
||||
!nvm.calData.joystickCal.x.valid() || !nvm.calData.joystickCal.y.valid();
|
||||
|
@ -129,6 +147,15 @@ void setup() {
|
|||
joystick_1.setCalibratedPoint(axis_t::Y, point_t::MAX, nvm.calData.joystickCal.y.max);
|
||||
|
||||
// run analog lever calibration if needed
|
||||
calibrationInvalid = !nvm.valid();
|
||||
do {
|
||||
calibrationInvalid |= !nvm.calData.lever0.valid();
|
||||
if (calibrationInvalid) {
|
||||
Serial.println("Lever0 calibration required\n===========================");
|
||||
calibrationInvalid |= runLeverCalibration(lever0, nvm.calData.lever0);
|
||||
}
|
||||
} while (calibrationInvalid);
|
||||
lever0.applyCalibration(nvm.calData.lever0);
|
||||
|
||||
// write NVM in the event any values were updated
|
||||
writeNvm(nvm);
|
||||
|
|
Loading…
Reference in a new issue