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