Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_GYRO_H_ |
| 2 | #define FRC971_WPILIB_GYRO_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include <atomic> |
| 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/robot_state/robot_state_generated.h" |
| 10 | #include "frc971/queues/gyro_generated.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 11 | #include "frc971/wpilib/gyro_interface.h" |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 12 | #include "frc971/zeroing/averager.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 13 | |
| 14 | namespace frc971 { |
| 15 | namespace wpilib { |
| 16 | |
| 17 | // Handles reading the gyro over SPI and sending out angles on a queue. |
| 18 | // |
| 19 | // This is designed to be passed into ::std::thread's constructor so it will run |
| 20 | // as a separate thread. |
| 21 | class GyroSender { |
| 22 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 23 | GyroSender(::aos::EventLoop *event_loop); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 24 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 25 | enum class State { INITIALIZING, RUNNING }; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 26 | |
| 27 | private: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 28 | // Initializes the gyro and then loops until Exit() is called on the event |
| 29 | // loop, taking readings. |
| 30 | void Loop(const int iterations); |
| 31 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 32 | ::aos::EventLoop *event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 33 | ::aos::Fetcher<::aos::RobotState> joystick_state_fetcher_; |
Austin Schuh | cc1010e | 2019-05-12 20:38:01 -0700 | [diff] [blame] | 34 | ::aos::Sender<::frc971::sensors::Uid> uid_sender_; |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 35 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_reading_sender_; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 36 | |
| 37 | // Readings per second. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 38 | static constexpr int kReadingRate = 200; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 39 | |
| 40 | GyroInterface gyro_; |
| 41 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 42 | State state_ = State::INITIALIZING; |
| 43 | |
| 44 | // In radians, ready to send out. |
| 45 | double angle_ = 0; |
| 46 | // Calibrated offset. |
| 47 | double zero_offset_ = 0; |
| 48 | |
| 49 | ::aos::monotonic_clock::time_point last_initialize_time_ = |
| 50 | ::aos::monotonic_clock::min_time; |
| 51 | int startup_cycles_left_ = 2 * kReadingRate; |
| 52 | |
| 53 | zeroing::Averager<double, 6 * kReadingRate> zeroing_data_; |
| 54 | |
| 55 | bool zeroed_ = false; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | } // namespace wpilib |
| 59 | } // namespace frc971 |
| 60 | |
| 61 | #endif // FRC971_WPILIB_GYRO_H_ |