summaryrefslogtreecommitdiff
path: root/src/sensor/calibration.hpp
blob: a2da58b8908d66352c9236daf74b3faf268f0bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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;

class SensorCalibratorValues
{
public:
	double accel_x = 0;
	double accel_y = 0;
	double accel_z = 0;

	double gyro_x = 0;
	double gyro_y = 0;
	double gyro_z = 0;
};

/**
 * 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 _updateValues(SensorCalibratorValues &values);
	void _adjustCalibrationWithValues(const SensorCalibratorValues &values);

	static void _adjustValues(SensorCalibratorValues &values);
	static bool _isValuesInRange(const SensorCalibratorValues &values);

	UniquePtr<Sensor> _sensor;

	SerialStream _sout;
};