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