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 | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 8 | #include "aos/logging/queue_logging.h" |
| 9 | #include "aos/robot_state/robot_state.q.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 10 | #include "aos/testing/test_shm.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace testing { |
| 15 | |
| 16 | // Handles setting up the environment that all control loops need to actually |
| 17 | // run. |
| 18 | // This includes sending the queue messages and Clear()ing the queues when |
| 19 | // appropriate. |
| 20 | // It also includes dealing with ::aos::time. |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 21 | template <typename TestBaseClass> |
| 22 | class ControlLoopTestTemplated : public TestBaseClass { |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 23 | public: |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 24 | ControlLoopTestTemplated() { |
| 25 | ::aos::joystick_state.Clear(); |
| 26 | ::aos::robot_state.Clear(); |
| 27 | |
| 28 | ::aos::time::EnableMockTime(current_time_); |
| 29 | |
| 30 | SendMessages(false); |
| 31 | } |
| 32 | virtual ~ControlLoopTestTemplated() { |
| 33 | ::aos::joystick_state.Clear(); |
| 34 | ::aos::robot_state.Clear(); |
| 35 | |
| 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_; |
| 47 | auto new_state = ::aos::joystick_state.MakeMessage(); |
| 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 | { |
| 59 | auto new_state = ::aos::robot_state.MakeMessage(); |
| 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. |
| 83 | void SimulateTimestep(bool enabled) { |
| 84 | SendMessages(enabled); |
| 85 | TickTime(); |
| 86 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 87 | |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 88 | // Simulate a reset of the process reading sensors, which tells loops that all |
| 89 | // index counts etc will be reset. |
| 90 | void SimulateSensorReset() { |
| 91 | ++reader_pid_; |
| 92 | } |
| 93 | |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 94 | // Sets the battery voltage in robot_state. |
| 95 | void set_battery_voltage(double battery_voltage) { |
| 96 | battery_voltage_ = battery_voltage; |
| 97 | } |
| 98 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 99 | private: |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 100 | static constexpr ::std::chrono::milliseconds kTimeTick{5}; |
| 101 | static constexpr ::std::chrono::milliseconds kDSPacketTime{20}; |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 102 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 103 | uint16_t team_id_ = 971; |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 104 | int32_t reader_pid_ = 1; |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 105 | double battery_voltage_ = 12.4; |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 106 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 107 | ::aos::monotonic_clock::time_point last_ds_time_ = |
| 108 | ::aos::monotonic_clock::epoch(); |
| 109 | ::aos::monotonic_clock::time_point current_time_ = |
| 110 | ::aos::monotonic_clock::epoch(); |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 111 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 112 | ::aos::testing::TestSharedMemory my_shm_; |
Campbell Crowley | 152c7cf | 2016-02-14 21:20:50 -0800 | [diff] [blame] | 113 | |
| 114 | bool last_enabled_ = false; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Austin Schuh | 2001aa4 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 117 | typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest; |
| 118 | |
| 119 | template <typename TestBaseClass> |
| 120 | constexpr ::std::chrono::milliseconds ControlLoopTestTemplated<TestBaseClass>::kTimeTick; |
| 121 | |
| 122 | template <typename TestBaseClass> |
| 123 | constexpr ::std::chrono::milliseconds ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime; |
| 124 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 125 | } // namespace testing |
| 126 | } // namespace aos |
| 127 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 128 | #endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |