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