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