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