diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
commit | bcdce9633dc351d3bc7f347a165348b8fab87cd9 (patch) | |
tree | 88c2f5d8f0c5fac2bca28e4b543e5209f3bc98fb /src/sensor/calibration.hpp | |
parent | 917adc6a2b6b166e37fc3d4f94b41488f0c245a5 (diff) |
refactor: reorganize files & improve classes
Diffstat (limited to 'src/sensor/calibration.hpp')
-rw-r--r-- | src/sensor/calibration.hpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/sensor/calibration.hpp b/src/sensor/calibration.hpp new file mode 100644 index 0000000..03e2e9c --- /dev/null +++ b/src/sensor/calibration.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include "sensor/sensor.hpp" +#include "serial.hpp" +#include "std/memory.hpp" + +// Calibration precision +constexpr float ACCEL_CAL_X_MAX = 0.006; +constexpr float ACCEL_CAL_X_MIN = -0.006; + +constexpr float ACCEL_CAL_Y_MAX = 0.006; +constexpr float ACCEL_CAL_Y_MIN = -0.006; + +constexpr float ACCEL_CAL_Z_MAX = 0.006; +constexpr float ACCEL_CAL_Z_MIN = -0.006; + +constexpr float GYRO_CAL_X_MAX = 0.06; +constexpr float GYRO_CAL_X_MIN = -0.06; + +constexpr float GYRO_CAL_Y_MAX = 0.06; +constexpr float GYRO_CAL_Y_MIN = -0.06; + +constexpr float GYRO_CAL_Z_MAX = 0.06; +constexpr float GYRO_CAL_Z_MIN = -0.06; + +constexpr uint32_t CALIBRATION_TIMEOUT = 120000; // Milliseconds + +constexpr unsigned int SENSOR_READ_CNT = 20; +constexpr float SENSOR_VAL_ADJUST = 0.05; + +/** + * Sensor calibrator. + */ +class SensorCalibrator +{ +public: + /** + * Sensor calibrator. + * + * @param sensor A sensor to calibrate + * @param sout A Serial output stream + */ + SensorCalibrator(UniquePtr<Sensor> &sensor, SerialStream sout); + + /** + * Calibrates the sensor. + * + * @param throttle_time The sensor's throttle time + * @returns Whether or not the calibration succeeded. Will return false on + * timeout. + */ + bool calibrate(unsigned int throttle_time); + +private: + void _resetValues(); + void _updateValues(); + void _adjustValues(); + bool _isValuesInRange() const; + + void _adjustCalibration(); + + UniquePtr<Sensor> &_sensor; + + SerialStream _sout; + + double _accel_x = 0; + double _accel_y = 0; + double _accel_z = 0; + + double _gyro_x = 0; + double _gyro_y = 0; + double _gyro_z = 0; +}; |