blob: 8867266f9a861962fa757dd6e84c0e6c5245c2d7 [file] [log] [blame]
James Kuszmaul9f2f53c2023-02-19 14:08:18 -08001#ifndef FRC971_IMU_READER_IMU_WATCHER_H_
2#define FRC971_IMU_READER_IMU_WATCHER_H_
3
4#include "aos/events/event_loop.h"
5#include "frc971/control_loops/drivetrain/drivetrain_config.h"
6#include "frc971/imu_reader/imu_failures_generated.h"
7#include "frc971/zeroing/imu_zeroer.h"
8#include "frc971/zeroing/wrap.h"
9
10namespace frc971::controls {
11// This class handles listening to an IMUValuesBatch channel sourced off of our
12// ADIS16505 pico board and calling the user-specified callback with the
13// relevant data. This intermediary is used to unwrap encoder readings, check
14// for checksum mismatches, zero the gyro/accelerometer, and translate
15// timestamps between devices.
16// TODO(james): Get unit tests for this class specifically written (we already
17// have tests for the code that exercises this).
18class ImuWatcher {
19 public:
20 // Expected frequency of messages from the pico-based IMU.
21 static constexpr std::chrono::microseconds kNominalDt{500};
22
23 // The callback specified by the user will take:
24 // sample_time_pico: The pico-based timestamp corresponding to the measurement
25 // time. This will be offset by roughly pico_offset_error from the pi's
26 // monotonic clock.
27 // sample_time_pi: Timestamp from the kernel for when the pi observed the
28 // relevant measurement.
29 // encoders: Current encoder values, [left, right]. nullopt if we have faults.
30 // gyro: Current gyro readings, in the raw IMU axes (i.e., these must be
31 // rotated by dt_config.imu_transform before being used). Suitable
32 // for input to the down estimator.
33 // accel: Current accelerometer readings, in the raw IMU axes (i.e., these
34 // must be rotated by dt_config.imu_transform before being used). Suitable
35 // for input to the down estimator.
36 ImuWatcher(
37 aos::EventLoop *event_loop,
38 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config,
39 double drivetrain_distance_per_encoder_tick,
40 std::function<void(
41 aos::monotonic_clock::time_point, aos::monotonic_clock::time_point,
42 std::optional<Eigen::Vector2d>, Eigen::Vector3d, Eigen::Vector3d)>
43 callback);
44
45 const zeroing::ImuZeroer &zeroer() const { return zeroer_; }
46
47 flatbuffers::Offset<ImuFailures> PopulateImuFailures(
48 flatbuffers::FlatBufferBuilder *fbb) const {
49 return ImuFailures::Pack(*fbb, &imu_fault_tracker_);
50 }
51
52 // t = pico_offset + pico_timestamp.
53 // Note that this can drift over sufficiently long time periods!
54 std::optional<std::chrono::nanoseconds> pico_offset() const {
55 return pico_offset_;
56 }
57 // pico_offset_error = actual_time - (pico_offset + pico_timestamp)
58 // If the pico clock and pi clock are exactly in sync, this will always be
59 // zero.
60 aos::monotonic_clock::duration pico_offset_error() const {
61 return pico_offset_error_;
62 }
63
64 private:
65 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
66 std::function<void(
67 aos::monotonic_clock::time_point, aos::monotonic_clock::time_point,
68 std::optional<Eigen::Vector2d>, Eigen::Vector3d, Eigen::Vector3d)>
69 callback_;
70
71 // Last observed pico measurement. Used to track IMU staleness.
72 std::optional<aos::monotonic_clock::time_point> last_pico_timestamp_;
73 // Estimate of the drift between the pi and pico clocks. See
74 // pico_offset_error() for definition.
75 aos::monotonic_clock::duration pico_offset_error_;
76 // Raw offset between the pico and pi clocks. Gets updated to compensate for
77 // wrapping in the pico timestamp.
78 std::optional<std::chrono::nanoseconds> pico_offset_;
79
80 zeroing::ImuZeroer zeroer_;
81
82 ImuFailuresT imu_fault_tracker_;
83 // The first observed data counter. This is used to help us track dropped
84 // messages.
85 std::optional<size_t> first_valid_data_counter_;
86 size_t total_imu_messages_received_ = 0;
87 // added to the current read data counter to allow the data counter to
88 // increase monotonically. Will be a multiple of 2 ** 16.
89 size_t data_counter_offset_ = 0;
90 // PRevious data counter value (data_counter_offset_ not included).
91 int last_data_counter_ = 0;
92
93 // Unwrappers for the left and right encoders (necessary because the pico only
94 // sends out 16-bit encoder counts).
95 zeroing::UnwrapSensor left_encoder_;
96 zeroing::UnwrapSensor right_encoder_;
97
98 // When we lose IMU readings (e.g., due to checksum mismatches), we perform a
99 // zero-order hold on gyro readings; in order to do this, store the most
100 // recent gyro readings.
101 Eigen::Vector3d last_gyro_ = Eigen::Vector3d::Zero();
102};
103} // namespace frc971::controls
104#endif // FRC971_IMU_READER_IMU_WATCHER_H_