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 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 8 | #include "aos/events/simulated-event-loop.h" |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 9 | #include "aos/logging/queue_logging.h" |
| 10 | #include "aos/robot_state/robot_state.q.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 11 | #include "aos/testing/test_shm.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/time/time.h" |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | namespace testing { |
| 16 | |
| 17 | // Handles setting up the environment that all control loops need to actually |
| 18 | // run. |
| 19 | // This includes sending the queue messages and Clear()ing the queues when |
| 20 | // appropriate. |
| 21 | // It also includes dealing with ::aos::time. |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 22 | template <typename TestBaseClass> |
| 23 | class ControlLoopTestTemplated : public TestBaseClass { |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 24 | public: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 25 | ControlLoopTestTemplated(::std::chrono::nanoseconds dt = kTimeTick) |
| 26 | : dt_(dt), robot_status_event_loop_(MakeEventLoop()) { |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 27 | robot_state_sender_ = |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 28 | robot_status_event_loop_->MakeSender<::aos::RobotState>( |
| 29 | ".aos.robot_state"); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 30 | joystick_state_sender_ = |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 31 | robot_status_event_loop_->MakeSender<::aos::JoystickState>( |
| 32 | ".aos.joystick_state"); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 33 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 34 | // Schedule the robot status send 1 nanosecond before the loop runs. |
| 35 | send_robot_state_phased_loop_ = robot_status_event_loop_->AddPhasedLoop( |
| 36 | [this](int) { SendRobotState(); }, dt_, |
| 37 | dt - ::std::chrono::nanoseconds(1)); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 38 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 39 | send_joystick_state_timer_ = |
| 40 | robot_status_event_loop_->AddTimer([this]() { SendJoystickState(); }); |
| 41 | |
| 42 | robot_status_event_loop_->OnRun([this]() { |
| 43 | send_joystick_state_timer_->Setup( |
| 44 | robot_status_event_loop_->monotonic_now(), dt_); |
| 45 | }); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 46 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 47 | virtual ~ControlLoopTestTemplated() {} |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 48 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 49 | void set_team_id(uint16_t team_id) { team_id_ = team_id; } |
| 50 | uint16_t team_id() const { return team_id_; } |
| 51 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 52 | // Sets the enabled/disabled bit and (potentially) rebroadcasts the robot |
| 53 | // state messages. |
| 54 | void SetEnabled(bool enabled) { |
| 55 | if (enabled_ != enabled) { |
| 56 | enabled_ = enabled; |
| 57 | SendJoystickState(); |
| 58 | SendRobotState(); |
| 59 | send_joystick_state_timer_->Setup( |
| 60 | robot_status_event_loop_->monotonic_now(), dt_); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 61 | } |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 62 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 63 | |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 64 | // Simulate a reset of the process reading sensors, which tells loops that all |
| 65 | // index counts etc will be reset. |
| 66 | void SimulateSensorReset() { |
| 67 | ++reader_pid_; |
| 68 | } |
| 69 | |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 70 | // Sets the battery voltage in robot_state. |
| 71 | void set_battery_voltage(double battery_voltage) { |
| 72 | battery_voltage_ = battery_voltage; |
| 73 | } |
| 74 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 75 | ::std::unique_ptr<::aos::EventLoop> MakeEventLoop() { |
| 76 | return event_loop_factory_.MakeEventLoop(); |
| 77 | } |
| 78 | |
| 79 | void RunFor(monotonic_clock::duration duration) { |
| 80 | event_loop_factory_.RunFor(duration); |
| 81 | } |
| 82 | |
| 83 | ::aos::monotonic_clock::time_point monotonic_now() { |
| 84 | return event_loop_factory_.monotonic_now(); |
| 85 | } |
| 86 | |
| 87 | ::std::chrono::nanoseconds dt() const { return dt_; } |
| 88 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 89 | private: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 90 | // Sends out all of the required queue messages. |
| 91 | void SendJoystickState() { |
| 92 | if (monotonic_now() >= kDSPacketTime + last_ds_time_ || |
| 93 | last_enabled_ != enabled_) { |
| 94 | auto new_state = joystick_state_sender_.MakeMessage(); |
| 95 | new_state->fake = true; |
| 96 | |
| 97 | new_state->enabled = enabled_; |
| 98 | new_state->autonomous = false; |
| 99 | new_state->team_id = team_id_; |
| 100 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 101 | AOS_LOG_STRUCT(INFO, "joystick_state", *new_state); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 102 | new_state.Send(); |
| 103 | |
| 104 | last_ds_time_ = monotonic_now(); |
| 105 | last_enabled_ = enabled_; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | bool last_enabled_ = false; |
| 110 | |
| 111 | void SendRobotState() { |
| 112 | auto new_state = robot_state_sender_.MakeMessage(); |
| 113 | |
| 114 | new_state->reader_pid = reader_pid_; |
| 115 | new_state->outputs_enabled = enabled_; |
| 116 | new_state->browned_out = false; |
| 117 | |
| 118 | new_state->is_3v3_active = true; |
| 119 | new_state->is_5v_active = true; |
| 120 | new_state->voltage_3v3 = 3.3; |
| 121 | new_state->voltage_5v = 5.0; |
| 122 | |
| 123 | new_state->voltage_roborio_in = battery_voltage_; |
| 124 | new_state->voltage_battery = battery_voltage_; |
| 125 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 126 | AOS_LOG_STRUCT(INFO, "robot_state", *new_state); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 127 | new_state.Send(); |
| 128 | } |
| 129 | |
| 130 | static constexpr ::std::chrono::microseconds kTimeTick{5000}; |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 131 | static constexpr ::std::chrono::milliseconds kDSPacketTime{20}; |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 132 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 133 | const ::std::chrono::nanoseconds dt_; |
| 134 | |
| 135 | SimulatedEventLoopFactory event_loop_factory_; |
| 136 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 137 | uint16_t team_id_ = 971; |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 138 | int32_t reader_pid_ = 1; |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 139 | double battery_voltage_ = 12.4; |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 140 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 141 | ::aos::monotonic_clock::time_point last_ds_time_ = |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 142 | ::aos::monotonic_clock::min_time; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 143 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 144 | bool enabled_ = false; |
Campbell Crowley | 152c7cf | 2016-02-14 21:20:50 -0800 | [diff] [blame] | 145 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 146 | ::std::unique_ptr<::aos::EventLoop> robot_status_event_loop_; |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 147 | |
| 148 | ::aos::Sender<::aos::RobotState> robot_state_sender_; |
| 149 | ::aos::Sender<::aos::JoystickState> joystick_state_sender_; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 150 | |
| 151 | ::aos::PhasedLoopHandler *send_robot_state_phased_loop_ = nullptr; |
| 152 | ::aos::TimerHandler *send_joystick_state_timer_ = nullptr; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 155 | typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest; |
| 156 | |
| 157 | template <typename TestBaseClass> |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 158 | constexpr ::std::chrono::microseconds |
| 159 | ControlLoopTestTemplated<TestBaseClass>::kTimeTick; |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 160 | |
| 161 | template <typename TestBaseClass> |
| 162 | constexpr ::std::chrono::milliseconds ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime; |
| 163 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 164 | } // namespace testing |
| 165 | } // namespace aos |
| 166 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 167 | #endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |