John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |
| 2 | #define AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 3 | |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 4 | #include <chrono> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 5 | #include <string_view> |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/simulated_event_loop.h" |
| 8 | #include "aos/flatbuffers.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/testing/test_logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 12 | #include "frc971/input/joystick_state_generated.h" |
| 13 | #include "frc971/input/robot_state_generated.h" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 15 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 16 | namespace frc971 { |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 17 | namespace testing { |
| 18 | |
| 19 | // Handles setting up the environment that all control loops need to actually |
| 20 | // run. |
| 21 | // This includes sending the queue messages and Clear()ing the queues when |
| 22 | // appropriate. |
| 23 | // It also includes dealing with ::aos::time. |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 24 | template <typename TestBaseClass> |
| 25 | class ControlLoopTestTemplated : public TestBaseClass { |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 26 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | ControlLoopTestTemplated( |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 28 | aos::FlatbufferDetachedBuffer<aos::Configuration> configuration, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | ::std::chrono::nanoseconds dt = kTimeTick) |
| 30 | : configuration_(std::move(configuration)), |
| 31 | event_loop_factory_(&configuration_.message()), |
| 32 | dt_(dt), |
Austin Schuh | 08e96eb | 2020-02-25 23:36:30 -0800 | [diff] [blame] | 33 | robot_status_event_loop_(MakeEventLoop( |
| 34 | "robot_status", |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 35 | aos::configuration::MultiNode(event_loop_factory_.configuration()) |
| 36 | ? aos::configuration::GetNode( |
| 37 | event_loop_factory_.configuration(), "roborio") |
Austin Schuh | 08e96eb | 2020-02-25 23:36:30 -0800 | [diff] [blame] | 38 | : nullptr)) { |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 39 | aos::testing::EnableTestLogging(); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 40 | robot_state_sender_ = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | robot_status_event_loop_->MakeSender<::aos::RobotState>("/aos"); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 42 | joystick_state_sender_ = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | robot_status_event_loop_->MakeSender<::aos::JoystickState>("/aos"); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 44 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 45 | // Schedule the robot status send 1 nanosecond before the loop runs. |
| 46 | send_robot_state_phased_loop_ = robot_status_event_loop_->AddPhasedLoop( |
| 47 | [this](int) { SendRobotState(); }, dt_, |
| 48 | dt - ::std::chrono::nanoseconds(1)); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 49 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 50 | send_joystick_state_timer_ = |
| 51 | robot_status_event_loop_->AddTimer([this]() { SendJoystickState(); }); |
| 52 | |
| 53 | robot_status_event_loop_->OnRun([this]() { |
| 54 | send_joystick_state_timer_->Setup( |
| 55 | robot_status_event_loop_->monotonic_now(), dt_); |
| 56 | }); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 57 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 58 | virtual ~ControlLoopTestTemplated() {} |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 59 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 60 | void set_team_id(uint16_t team_id) { team_id_ = team_id; } |
| 61 | uint16_t team_id() const { return team_id_; } |
| 62 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 63 | // Sets the enabled/disabled bit and (potentially) rebroadcasts the robot |
| 64 | // state messages. |
| 65 | void SetEnabled(bool enabled) { |
| 66 | if (enabled_ != enabled) { |
| 67 | enabled_ = enabled; |
| 68 | SendJoystickState(); |
| 69 | SendRobotState(); |
| 70 | send_joystick_state_timer_->Setup( |
| 71 | robot_status_event_loop_->monotonic_now(), dt_); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 72 | } |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 73 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 74 | |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 75 | // Simulate a reset of the process reading sensors, which tells loops that all |
| 76 | // index counts etc will be reset. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 77 | void SimulateSensorReset() { ++reader_pid_; } |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 78 | |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 79 | // Sets the battery voltage in robot_state. |
| 80 | void set_battery_voltage(double battery_voltage) { |
| 81 | battery_voltage_ = battery_voltage; |
| 82 | } |
| 83 | |
Austin Schuh | 08e96eb | 2020-02-25 23:36:30 -0800 | [diff] [blame] | 84 | ::std::unique_ptr<::aos::EventLoop> MakeEventLoop( |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 85 | std::string_view name, const aos::Node *node = nullptr) { |
Austin Schuh | 08e96eb | 2020-02-25 23:36:30 -0800 | [diff] [blame] | 86 | return event_loop_factory_.MakeEventLoop(name, node); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 89 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 90 | event_loop_factory_.set_send_delay(send_delay); |
| 91 | } |
| 92 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 93 | void RunFor(aos::monotonic_clock::duration duration) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 94 | event_loop_factory_.RunFor(duration); |
| 95 | } |
| 96 | |
| 97 | ::aos::monotonic_clock::time_point monotonic_now() { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 98 | return robot_status_event_loop_->monotonic_now(); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | ::std::chrono::nanoseconds dt() const { return dt_; } |
| 102 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 103 | const aos::Configuration *configuration() const { |
Austin Schuh | 08e96eb | 2020-02-25 23:36:30 -0800 | [diff] [blame] | 104 | return &configuration_.message(); |
| 105 | } |
| 106 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 107 | aos::SimulatedEventLoopFactory *event_loop_factory() { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 108 | return &event_loop_factory_; |
| 109 | } |
| 110 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 111 | private: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 112 | // Sends out all of the required queue messages. |
| 113 | void SendJoystickState() { |
| 114 | if (monotonic_now() >= kDSPacketTime + last_ds_time_ || |
| 115 | last_enabled_ != enabled_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | auto new_state = joystick_state_sender_.MakeBuilder(); |
| 117 | ::aos::JoystickState::Builder builder = |
| 118 | new_state.template MakeBuilder<::aos::JoystickState>(); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 119 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | builder.add_fake(true); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 121 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 122 | builder.add_enabled(enabled_); |
| 123 | builder.add_autonomous(false); |
| 124 | builder.add_team_id(team_id_); |
| 125 | |
| 126 | new_state.Send(builder.Finish()); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 127 | |
| 128 | last_ds_time_ = monotonic_now(); |
| 129 | last_enabled_ = enabled_; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool last_enabled_ = false; |
| 134 | |
| 135 | void SendRobotState() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | auto new_state = robot_state_sender_.MakeBuilder(); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 137 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | ::aos::RobotState::Builder builder = |
| 139 | new_state.template MakeBuilder<::aos::RobotState>(); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 140 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 141 | builder.add_reader_pid(reader_pid_); |
| 142 | builder.add_outputs_enabled(enabled_); |
| 143 | builder.add_browned_out(false); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 144 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 145 | builder.add_is_3v3_active(true); |
| 146 | builder.add_is_5v_active(true); |
| 147 | builder.add_voltage_3v3(3.3); |
| 148 | builder.add_voltage_5v(5.0); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 149 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 150 | builder.add_voltage_roborio_in(battery_voltage_); |
| 151 | builder.add_voltage_battery(battery_voltage_); |
| 152 | |
| 153 | new_state.Send(builder.Finish()); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static constexpr ::std::chrono::microseconds kTimeTick{5000}; |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 157 | static constexpr ::std::chrono::milliseconds kDSPacketTime{20}; |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 158 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 159 | aos::FlatbufferDetachedBuffer<aos::Configuration> configuration_; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 160 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 161 | aos::SimulatedEventLoopFactory event_loop_factory_; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 162 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | const ::std::chrono::nanoseconds dt_; |
| 164 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 165 | uint16_t team_id_ = 971; |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 166 | int32_t reader_pid_ = 1; |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 167 | double battery_voltage_ = 12.4; |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 168 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 169 | ::aos::monotonic_clock::time_point last_ds_time_ = |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 170 | ::aos::monotonic_clock::min_time; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 171 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 172 | bool enabled_ = false; |
Campbell Crowley | 152c7cf | 2016-02-14 21:20:50 -0800 | [diff] [blame] | 173 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 174 | ::std::unique_ptr<::aos::EventLoop> robot_status_event_loop_; |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 175 | |
| 176 | ::aos::Sender<::aos::RobotState> robot_state_sender_; |
| 177 | ::aos::Sender<::aos::JoystickState> joystick_state_sender_; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 178 | |
| 179 | ::aos::PhasedLoopHandler *send_robot_state_phased_loop_ = nullptr; |
| 180 | ::aos::TimerHandler *send_joystick_state_timer_ = nullptr; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 183 | typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest; |
| 184 | |
| 185 | template <typename TestBaseClass> |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 186 | constexpr ::std::chrono::microseconds |
| 187 | ControlLoopTestTemplated<TestBaseClass>::kTimeTick; |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 188 | |
| 189 | template <typename TestBaseClass> |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 190 | constexpr ::std::chrono::milliseconds |
| 191 | ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime; |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 192 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 193 | } // namespace testing |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 194 | } // namespace frc971 |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 195 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 196 | #endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |