blob: 1633daf48c2693ff2d8d5abb282f0c6da6ce610c [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_CONTROLS_CONTROL_LOOP_TEST_H_
2#define AOS_CONTROLS_CONTROL_LOOP_TEST_H_
Brian Silverman65e49702014-04-30 17:36:40 -07003
4#include "gtest/gtest.h"
5
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05006#include "aos/testing/test_shm.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/time/time.h"
Brian Silverman65e49702014-04-30 17:36:40 -07008
9namespace aos {
10namespace 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.
17class ControlLoopTest : public ::testing::Test {
18 public:
19 ControlLoopTest();
Brian Silverman65e49702014-04-30 17:36:40 -070020 virtual ~ControlLoopTest();
21
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000022 void set_team_id(uint16_t team_id) { team_id_ = team_id; }
23 uint16_t team_id() const { return team_id_; }
24
Brian Silvermane6f64ab2015-02-05 17:03:56 -050025 // Sends out all of the required queue messages.
26 void SendMessages(bool enabled);
27 // Ticks time for a single control loop cycle.
Austin Schuh85387042017-09-13 23:59:21 -070028 void TickTime(::std::chrono::nanoseconds dt = kTimeTick) {
29 ::aos::time::SetMockTime(current_time_ += dt);
30 }
Brian Silvermane6f64ab2015-02-05 17:03:56 -050031
32 // Simulates everything that happens during 1 loop time step.
33 void SimulateTimestep(bool enabled) {
34 SendMessages(enabled);
35 TickTime();
36 }
Brian Silverman65e49702014-04-30 17:36:40 -070037
Brian Silverman57cad222015-02-14 20:46:41 -050038 // 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 Schuhe5f064d2016-03-05 17:43:51 -080044 // Sets the battery voltage in robot_state.
45 void set_battery_voltage(double battery_voltage) {
46 battery_voltage_ = battery_voltage;
47 }
48
Brian Silverman65e49702014-04-30 17:36:40 -070049 private:
Austin Schuh6a6f90c2016-11-25 21:36:42 -080050 static constexpr ::std::chrono::milliseconds kTimeTick{5};
51 static constexpr ::std::chrono::milliseconds kDSPacketTime{20};
Brian Silvermane6f64ab2015-02-05 17:03:56 -050052
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000053 uint16_t team_id_ = 971;
Brian Silverman57cad222015-02-14 20:46:41 -050054 int32_t reader_pid_ = 1;
Austin Schuhe5f064d2016-03-05 17:43:51 -080055 double battery_voltage_ = 12.4;
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000056
Austin Schuh6a6f90c2016-11-25 21:36:42 -080057 ::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 Silverman65e49702014-04-30 17:36:40 -070061
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050062 ::aos::testing::TestSharedMemory my_shm_;
Campbell Crowley152c7cf2016-02-14 21:20:50 -080063
64 bool last_enabled_ = false;
Brian Silverman65e49702014-04-30 17:36:40 -070065};
66
67} // namespace testing
68} // namespace aos
69
John Park33858a32018-09-28 23:05:48 -070070#endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_