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 | |
| 4 | #include "gtest/gtest.h" |
| 5 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 6 | #include "aos/testing/test_shm.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/time/time.h" |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | // Handles setting up the environment that all control loops need to actually |
| 13 | // run. |
| 14 | // This includes sending the queue messages and Clear()ing the queues when |
| 15 | // appropriate. |
| 16 | // It also includes dealing with ::aos::time. |
| 17 | class ControlLoopTest : public ::testing::Test { |
| 18 | public: |
| 19 | ControlLoopTest(); |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 20 | virtual ~ControlLoopTest(); |
| 21 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 22 | void set_team_id(uint16_t team_id) { team_id_ = team_id; } |
| 23 | uint16_t team_id() const { return team_id_; } |
| 24 | |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 25 | // Sends out all of the required queue messages. |
| 26 | void SendMessages(bool enabled); |
| 27 | // Ticks time for a single control loop cycle. |
Austin Schuh | 8538704 | 2017-09-13 23:59:21 -0700 | [diff] [blame] | 28 | void TickTime(::std::chrono::nanoseconds dt = kTimeTick) { |
| 29 | ::aos::time::SetMockTime(current_time_ += dt); |
| 30 | } |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 31 | |
| 32 | // Simulates everything that happens during 1 loop time step. |
| 33 | void SimulateTimestep(bool enabled) { |
| 34 | SendMessages(enabled); |
| 35 | TickTime(); |
| 36 | } |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 37 | |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 38 | // Simulate a reset of the process reading sensors, which tells loops that all |
| 39 | // index counts etc will be reset. |
| 40 | void SimulateSensorReset() { |
| 41 | ++reader_pid_; |
| 42 | } |
| 43 | |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 44 | // Sets the battery voltage in robot_state. |
| 45 | void set_battery_voltage(double battery_voltage) { |
| 46 | battery_voltage_ = battery_voltage; |
| 47 | } |
| 48 | |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 49 | private: |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 50 | static constexpr ::std::chrono::milliseconds kTimeTick{5}; |
| 51 | static constexpr ::std::chrono::milliseconds kDSPacketTime{20}; |
Brian Silverman | e6f64ab | 2015-02-05 17:03:56 -0500 | [diff] [blame] | 52 | |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 53 | uint16_t team_id_ = 971; |
Brian Silverman | 57cad22 | 2015-02-14 20:46:41 -0500 | [diff] [blame] | 54 | int32_t reader_pid_ = 1; |
Austin Schuh | e5f064d | 2016-03-05 17:43:51 -0800 | [diff] [blame] | 55 | double battery_voltage_ = 12.4; |
Philipp Schrader | f75a8bf | 2015-02-02 05:30:16 +0000 | [diff] [blame] | 56 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 57 | ::aos::monotonic_clock::time_point last_ds_time_ = |
| 58 | ::aos::monotonic_clock::epoch(); |
| 59 | ::aos::monotonic_clock::time_point current_time_ = |
| 60 | ::aos::monotonic_clock::epoch(); |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 61 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 62 | ::aos::testing::TestSharedMemory my_shm_; |
Campbell Crowley | 152c7cf | 2016-02-14 21:20:50 -0800 | [diff] [blame] | 63 | |
| 64 | bool last_enabled_ = false; |
Brian Silverman | 65e4970 | 2014-04-30 17:36:40 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | } // namespace testing |
| 68 | } // namespace aos |
| 69 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 70 | #endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_ |