summaryrefslogtreecommitdiff
path: root/src/sensor.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-12-24 20:07:16 +0100
committerHampusM <hampus@hampusmat.com>2021-12-26 20:36:34 +0100
commite5b34cbb3d6764cc9a7d3e6d4c27da468f16246f (patch)
treedd6da3fb64a9a8f355ad819061efcdaa8aa305a3 /src/sensor.hpp
parent8969ebfa45b593e0c59f6998fdf8bde42324089f (diff)
refactor: improve whole project
Diffstat (limited to 'src/sensor.hpp')
-rw-r--r--src/sensor.hpp137
1 files changed, 33 insertions, 104 deletions
diff --git a/src/sensor.hpp b/src/sensor.hpp
index 3fbc544..07b8f95 100644
--- a/src/sensor.hpp
+++ b/src/sensor.hpp
@@ -3,15 +3,7 @@
#include "Arduino.h"
#include "Wire.h"
-
-#define DEFAULT_THROTTLE_TIME 10 // milliseconds
-
-// Status codes
-#define SENSOR_OK 0
-#define SENSOR_THROTTLED 1
-#define SENSOR_ERR_READ -1
-#define SENSOR_ERR_WRITE -2
-#define SENSOR_ERR_NOT_CONNECTED -3
+#include "time_utils.hpp"
#define SENSOR_WAKEUP 0x00
@@ -19,35 +11,33 @@
#define RAW_TO_DPS_FACTOR (1.0 / 131.0)
#define RAW_TO_G_FACTOR (1.0 / 16384.0)
-// Automatic calibration precision
-#define ACCEL_CAL_X_MAX 0.002
-#define ACCEL_CAL_X_MIN -0.002
-
-#define ACCEL_CAL_Y_MAX 0.002
-#define ACCEL_CAL_Y_MIN -0.002
-
-#define ACCEL_CAL_Z_MAX 0.002
-#define ACCEL_CAL_Z_MIN -0.002
-
-#define GYRO_CAL_X_MAX 0.2
-#define GYRO_CAL_X_MIN -0.2
-
-#define GYRO_CAL_Y_MAX 0.02
-#define GYRO_CAL_Y_MIN -0.02
-
-#define GYRO_CAL_Z_MAX 0.08
-#define GYRO_CAL_Z_MIN -0.08
-
-#define CALIBRATION_TIMEOUT 120000 // Milliseconds
+enum class SensorStatus
+{
+ OK = 0,
+ THROTTLED = 1,
+ ERR_READ = -1,
+ ERR_WRITE = -2,
+ ERR_NOT_CONNECTED = -3
+};
+/**
+ * A GY521 gyro and accelerometer sensor.
+ */
class Sensor
{
public:
- Sensor(uint8_t address, TwoWire *wire);
+ /**
+ * A GY521 gyro and accelerometer sensor.
+ *
+ * @param address The address of the sensor
+ * @param wire A Wire instance
+ * @param throttle_time A minumum time between sensor reads for the sensor to throttle
+ */
+ Sensor(uint8_t address, TwoWire *wire, unsigned int throttle_time);
/**
* Initializes communication with the sensor.
- *
+ *
* @returns Whether or not the sensor can be communicated with.
*/
bool begin();
@@ -63,59 +53,21 @@ public:
void reset();
/**
- * Returns whether or not throttling is enabled.
- */
- bool getThrottleEnabled();
-
- /**
- * Sets whether or not throttling is enabled.
- *
- * @param throttle Throttling enable/disable
- */
- void setThrottleEnabled(bool throttle);
-
- /**
- * Returns the throttle time.
- */
- uint16_t getThrottleTime();
-
- /**
- * Sets the throttle time.
- *
- * @param time_ms Time in milliseconds to throttle
- */
- void setThrottleTime(uint16_t time_ms);
-
- /**
* Reads from the sensor.
- *
- * @returns The sensor status.
- */
- int16_t read();
-
- /**
- * Returns the accelerometer sensitivity.
- *
- * 0,1,2,3 ==> 2g 4g 8g 16g
+ *
+ * @returns Whether or not it succeeded.
*/
- uint8_t getAccelSensitivity();
+ bool read();
/**
* Sets the accelerometer sensitivity.
- *
+ *
* @param sensitivity Accelerometer sensitivity. 0, 1, 2, 3 => 2g 4g 8g 16g
* @returns Whether or not it succeeded.
*/
bool setAccelSensitivity(uint8_t sensitivity);
/**
- * Returns the gyro sensitivity.
- *
- * 0, 1, 2, 3 => 250, 500, 1000, 2000 degrees/s
- */
- uint8_t getGyroSensitivity();
-
- /**
* Sets the gyro sensitivity.
*
* @param sensitivity Gyro sensitivity.
@@ -150,11 +102,6 @@ public:
float getAngleY();
/**
- * Returns the current temperature.
- */
- float getTemperature();
-
- /**
* Returns the current X axis in degrees/s.
*/
float getGyroX();
@@ -180,22 +127,17 @@ public:
float getRoll();
/**
- * Returns the last time the sensor was read.
- */
- uint32_t lastTime();
-
- /**
* Sets the value of a key in the sensor's register.
- *
+ *
* @param reg The address of a registry key
* @param value A new value
- * @returns The sensor status
+ * @returns Whether or not it succeeded.
*/
- uint8_t setRegister(uint8_t reg, uint8_t value);
+ bool setRegister(uint8_t reg, uint8_t value);
/**
* Returns the value of a key in the sensor's registry.
- *
+ *
* @param reg The address of a registry key
*/
uint8_t getRegister(uint8_t reg);
@@ -203,17 +145,7 @@ public:
/**
* Returns the last sensor status.
*/
- int16_t getStatus();
-
- /**
- * Automatically calibrates the sensor.
- *
- * @returns Whether or not the calibration succeeded.
- * Will return false if it times out.
- */
- bool autoCalibrate();
-
- bool is_calibrated;
+ SensorStatus getStatus();
// Accelerometer calibration values
float accel_cal_x;
@@ -229,12 +161,11 @@ private:
uint8_t _address;
bool _throttle_enabled;
- uint16_t _throttleTime;
+ unsigned int _throttle_time;
- uint32_t _lastTime;
- uint32_t _lastMicros;
+ Time _last_time;
- int16_t _status;
+ SensorStatus _status;
uint8_t _accel_sensitivity;
float _accel_to_g_force;
@@ -250,8 +181,6 @@ private:
float _pitch, _roll, _yaw;
- float _temperature;
-
int16_t _readTwoBytes();
TwoWire *_wire;