|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <Eigen/Dense> |
4 | | -#include <utility> |
5 | | -#include <numbers> |
6 | 4 | #include <climits> |
| 5 | +#include <numbers> |
| 6 | +#include <utility> |
7 | 7 |
|
8 | 8 | #include "proto/tbots_software_msgs.pb.h" |
9 | 9 | #include "software/geom/angular_velocity.h" |
|
13 | 13 | */ |
14 | 14 | class ImuService |
15 | 15 | { |
16 | | - public: |
17 | | - /** |
18 | | - * Constructs and initializes a new IMU service object. |
19 | | - * |
20 | | - * If successfully initialized, will try to do a simple calibration of the IMU. |
21 | | - */ |
22 | | - ImuService(); |
23 | | - /* |
24 | | - * Polls the latest IMU reading of the angular velocity of the robot on the z axis |
25 | | - * @return the current angular velocty of the robot on the z axis |
26 | | - */ |
27 | | - std::optional<AngularVelocity> pollHeadingVelocity(); |
28 | | - /* |
29 | | - * Polls the latest IMU reading of the linear acceleration of the robot on the z plane |
30 | | - * @return the current linear acceleration of the robot on the z plane |
31 | | - */ |
32 | | - std::optional<Eigen::Vector2d> pollLinearAcceleration(); |
33 | | - |
34 | | - |
35 | | - // Variance from datasheet (in rad^2/s^2) |
36 | | - static constexpr double IMU_VARIANCE = |
37 | | - (4.0 * 14.4222 / 1000.0 * std::numbers::pi / 180.0) * |
38 | | - (4.0 * 14.4222 / 1000.0 * std::numbers::pi / 180.0); |
39 | | - |
40 | | - private: |
41 | | - |
42 | | - /* |
43 | | - * Reads byte data from two registers, and combine them into a single value |
44 | | - * @parama ls_reg register of the least significant register |
45 | | - * @parama ms_reg register of the most significant register |
46 | | - * @return the combined integer value of the two registers |
47 | | - */ |
48 | | - |
49 | | - std::optional<int16_t> readAndCombineByteData(uint8_t ls_reg, uint8_t ms_reg); |
50 | | - |
51 | | - bool initialized_=false; |
52 | | - |
53 | | - int file_descriptor_=0; |
54 | | - |
55 | | - double degrees_error_; |
56 | | - |
57 | | - // Maps the maximum raw reading from 16-bit integer to be 2 times gravity |
58 | | - static constexpr double ACCELEROMETER_FULL_SCALE_G = 2.0; |
59 | | - // Same for gyroscope, 1000 degrees per second |
60 | | - static constexpr double IMU_FULL_SCALE_DPS=1000.0; |
61 | | - |
62 | | - /// Various i2c registers. |
63 | | - static const uint8_t WHOAMI_REG = 0xf; |
64 | | - static const uint8_t ACCEL_CONTROL_REG = 0x10; |
65 | | - static const uint8_t GYRO_CONTROL_REG = 0x11; |
66 | | - static const uint8_t CTRL4_C = 0x13; |
67 | | - static const uint8_t CTRL6_C = 0x15; |
68 | | - static const uint8_t CTRL8_XL = 0x17; |
69 | | - |
70 | | - // Device path for the IMU |
71 | | - inline static const std::string IMU_DEVICE = "/dev/i2c-1"; |
72 | | - |
73 | | - // Gyroscope Z-axis (Yaw) Output Data Registers |
74 | | - static constexpr uint8_t HEADING_LEAST_SIG_REG = 0x26; // OUTZ_L_G |
75 | | - static constexpr uint8_t HEADING_MOST_SIG_REG = 0x27; // OUTZ_H_G |
76 | | - |
77 | | - // Accelerometer X-axis Output Data Registers |
78 | | - static constexpr uint8_t ACCEL_X_LEAST_SIG_REG = 0x28; // OUTX_L_XL |
79 | | - static constexpr uint8_t ACCEL_X_MOST_SIG_REG = 0x29; // OUTX_H_XL |
80 | | - |
81 | | - // Accelerometer Y-axis Output Data Registers |
82 | | - static constexpr uint8_t ACCEL_Y_LEAST_SIG_REG = 0x2A; // OUTY_L_XL |
83 | | - static constexpr uint8_t ACCEL_Y_MOST_SIG_REG = 0x2B; // OUTY_H_XL |
| 16 | + public: |
| 17 | + /** |
| 18 | + * Constructs and initializes a new IMU service object. |
| 19 | + * |
| 20 | + * If successfully initialized, will try to do a simple calibration of the IMU. |
| 21 | + */ |
| 22 | + ImuService(); |
| 23 | + /* |
| 24 | + * Polls the latest IMU reading of the angular velocity of the robot on the z axis |
| 25 | + * @return the current angular velocty of the robot on the z axis |
| 26 | + */ |
| 27 | + std::optional<AngularVelocity> pollHeadingVelocity(); |
| 28 | + /* |
| 29 | + * Polls the latest IMU reading of the linear acceleration of the robot on the z plane |
| 30 | + * @return the current linear acceleration of the robot on the z plane |
| 31 | + */ |
| 32 | + std::optional<Eigen::Vector2d> pollLinearAcceleration(); |
| 33 | + |
| 34 | + |
| 35 | + // Variance from datasheet (in rad^2/s^2) |
| 36 | + static constexpr double IMU_VARIANCE = |
| 37 | + (4.0 * 14.4222 / 1000.0 * std::numbers::pi / 180.0) * |
| 38 | + (4.0 * 14.4222 / 1000.0 * std::numbers::pi / 180.0); |
| 39 | + |
| 40 | + private: |
| 41 | + /* |
| 42 | + * Reads byte data from two registers, and combine them into a single value |
| 43 | + * @parama ls_reg register of the least significant register |
| 44 | + * @parama ms_reg register of the most significant register |
| 45 | + * @return the combined integer value of the two registers |
| 46 | + */ |
| 47 | + |
| 48 | + std::optional<int16_t> readAndCombineByteData(uint8_t ls_reg, uint8_t ms_reg); |
| 49 | + |
| 50 | + bool initialized_ = false; |
| 51 | + |
| 52 | + int file_descriptor_ = 0; |
| 53 | + |
| 54 | + double degrees_error_; |
| 55 | + |
| 56 | + // Maps the maximum raw reading from 16-bit integer to be 2 times gravity |
| 57 | + static constexpr double ACCELEROMETER_FULL_SCALE_G = 2.0; |
| 58 | + // Same for gyroscope, 1000 degrees per second |
| 59 | + static constexpr double IMU_FULL_SCALE_DPS = 1000.0; |
| 60 | + |
| 61 | + /// Various i2c registers. |
| 62 | + static const uint8_t WHOAMI_REG = 0xf; |
| 63 | + static const uint8_t ACCEL_CONTROL_REG = 0x10; |
| 64 | + static const uint8_t GYRO_CONTROL_REG = 0x11; |
| 65 | + static const uint8_t CTRL4_C = 0x13; |
| 66 | + static const uint8_t CTRL6_C = 0x15; |
| 67 | + static const uint8_t CTRL8_XL = 0x17; |
| 68 | + |
| 69 | + // Device path for the IMU |
| 70 | + inline static const std::string IMU_DEVICE = "/dev/i2c-1"; |
| 71 | + |
| 72 | + // Gyroscope Z-axis (Yaw) Output Data Registers |
| 73 | + static constexpr uint8_t HEADING_LEAST_SIG_REG = 0x26; // OUTZ_L_G |
| 74 | + static constexpr uint8_t HEADING_MOST_SIG_REG = 0x27; // OUTZ_H_G |
| 75 | + |
| 76 | + // Accelerometer X-axis Output Data Registers |
| 77 | + static constexpr uint8_t ACCEL_X_LEAST_SIG_REG = 0x28; // OUTX_L_XL |
| 78 | + static constexpr uint8_t ACCEL_X_MOST_SIG_REG = 0x29; // OUTX_H_XL |
| 79 | + |
| 80 | + // Accelerometer Y-axis Output Data Registers |
| 81 | + static constexpr uint8_t ACCEL_Y_LEAST_SIG_REG = 0x2A; // OUTY_L_XL |
| 82 | + static constexpr uint8_t ACCEL_Y_MOST_SIG_REG = 0x2B; // OUTY_H_XL |
84 | 83 | } |
0 commit comments