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 | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 8 | #include "aos/events/shm-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 | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 25 | ControlLoopTestTemplated() { |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 26 | robot_state_sender_ = |
| 27 | test_event_loop_.MakeSender<::aos::RobotState>(".aos.robot_state"); |
| 28 | joystick_state_sender_ = |
| 29 | test_event_loop_.MakeSender<::aos::JoystickState>(".aos.joystick_state"); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 30 | |
| 31 | ::aos::time::EnableMockTime(current_time_); |
| 32 | |
| 33 | SendMessages(false); |
| 34 | } |
| 35 | virtual ~ControlLoopTestTemplated() { |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 36 | ::aos::time::DisableMockTime(); |
| 37 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 38 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 39 | void set_team_id(uint16_t team_id) { team_id_ = team_id; } |
| 40 | uint16_t team_id() const { return team_id_; } |
| 41 | |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 42 | // Sends out all of the required queue messages. |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 43 | void SendMessages(bool enabled) { |
| 44 | if (current_time_ >= kDSPacketTime + last_ds_time_ || |
| 45 | last_enabled_ != enabled) { |
| 46 | last_ds_time_ = current_time_; |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 47 | auto new_state = joystick_state_sender_.MakeMessage(); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 48 | new_state->fake = true; |
| 49 | |
| 50 | new_state->enabled = enabled; |
| 51 | new_state->autonomous = false; |
| 52 | new_state->team_id = team_id_; |
| 53 | |
| 54 | new_state.Send(); |
| 55 | last_enabled_ = enabled; |
| 56 | } |
| 57 | |
| 58 | { |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 59 | auto new_state = robot_state_sender_.MakeMessage(); |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 60 | |
| 61 | new_state->reader_pid = reader_pid_; |
| 62 | new_state->outputs_enabled = enabled; |
| 63 | new_state->browned_out = false; |
| 64 | |
| 65 | new_state->is_3v3_active = true; |
| 66 | new_state->is_5v_active = true; |
| 67 | new_state->voltage_3v3 = 3.3; |
| 68 | new_state->voltage_5v = 5.0; |
| 69 | |
| 70 | new_state->voltage_roborio_in = battery_voltage_; |
| 71 | new_state->voltage_battery = battery_voltage_; |
| 72 | |
| 73 | LOG_STRUCT(INFO, "robot_state", *new_state); |
| 74 | new_state.Send(); |
| 75 | } |
| 76 | } |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 77 | // Ticks time for a single control loop cycle. |
Austin Schuh | 8538704 | 2017-09-13 23:59:21 -0700 | [diff] [blame] | 78 | void TickTime(::std::chrono::nanoseconds dt = kTimeTick) { |
| 79 | ::aos::time::SetMockTime(current_time_ += dt); |
| 80 | } |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 81 | |
| 82 | // Simulates everything that happens during 1 loop time step. |
James Kuszmaul | 109ed8d | 2019-02-17 21:41:04 -0800 | [diff] [blame] | 83 | void SimulateTimestep(bool enabled, |
| 84 | ::std::chrono::nanoseconds dt = kTimeTick) { |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 85 | SendMessages(enabled); |
James Kuszmaul | 109ed8d | 2019-02-17 21:41:04 -0800 | [diff] [blame] | 86 | TickTime(dt); |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 87 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 88 | |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 89 | // Simulate a reset of the process reading sensors, which tells loops that all |
| 90 | // index counts etc will be reset. |
| 91 | void SimulateSensorReset() { |
| 92 | ++reader_pid_; |
| 93 | } |
| 94 | |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 95 | // Sets the battery voltage in robot_state. |
| 96 | void set_battery_voltage(double battery_voltage) { |
| 97 | battery_voltage_ = battery_voltage; |
| 98 | } |
| 99 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 100 | private: |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 101 | static constexpr ::std::chrono::milliseconds kTimeTick{5}; |
| 102 | static constexpr ::std::chrono::milliseconds kDSPacketTime{20}; |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 103 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 104 | uint16_t team_id_ = 971; |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 105 | int32_t reader_pid_ = 1; |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 106 | double battery_voltage_ = 12.4; |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 107 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 108 | ::aos::monotonic_clock::time_point last_ds_time_ = |
| 109 | ::aos::monotonic_clock::epoch(); |
| 110 | ::aos::monotonic_clock::time_point current_time_ = |
| 111 | ::aos::monotonic_clock::epoch(); |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 112 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 113 | ::aos::testing::TestSharedMemory my_shm_; |
Campbell Crowley | 152c7cf | 2016-02-14 21:20:50 -0800 | [diff] [blame] | 114 | |
| 115 | bool last_enabled_ = false; |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 116 | |
| 117 | ::aos::ShmEventLoop test_event_loop_; |
| 118 | |
| 119 | ::aos::Sender<::aos::RobotState> robot_state_sender_; |
| 120 | ::aos::Sender<::aos::JoystickState> joystick_state_sender_; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 123 | typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest; |
| 124 | |
| 125 | template <typename TestBaseClass> |
| 126 | constexpr ::std::chrono::milliseconds ControlLoopTestTemplated<TestBaseClass>::kTimeTick; |
| 127 | |
| 128 | template <typename TestBaseClass> |
| 129 | constexpr ::std::chrono::milliseconds ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime; |
| 130 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 131 | } // namespace testing |
| 132 | } // namespace aos |
| 133 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 134 | #endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |