blob: 536df026582a5c4f7cb29a48c052e3503f231993 [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
Austin Schuh2001aa42018-10-29 22:57:02 -07004#include <chrono>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08005#include <string_view>
Austin Schuh2001aa42018-10-29 22:57:02 -07006
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/simulated_event_loop.h"
8#include "aos/flatbuffers.h"
9#include "aos/json_to_flatbuffer.h"
10#include "aos/robot_state/joystick_state_generated.h"
11#include "aos/robot_state/robot_state_generated.h"
12#include "aos/testing/test_logging.h"
John Park33858a32018-09-28 23:05:48 -070013#include "aos/time/time.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080014#include "gtest/gtest.h"
Brian Silverman65e49702014-04-30 17:36:40 -070015
16namespace aos {
17namespace testing {
18
19// Handles setting up the environment that all control loops need to actually
20// run.
21// This includes sending the queue messages and Clear()ing the queues when
22// appropriate.
23// It also includes dealing with ::aos::time.
Austin Schuh2001aa42018-10-29 22:57:02 -070024template <typename TestBaseClass>
25class ControlLoopTestTemplated : public TestBaseClass {
Brian Silverman65e49702014-04-30 17:36:40 -070026 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 // Builds a control loop test with the provided configuration. Note: this
28 // merges and sorts the config to reduce user errors.
James Kuszmaul3ae42262019-11-08 12:33:41 -080029 ControlLoopTestTemplated(std::string_view configuration,
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 ::std::chrono::nanoseconds dt = kTimeTick)
31 : ControlLoopTestTemplated(
32 configuration::MergeConfiguration(
33 aos::FlatbufferDetachedBuffer<Configuration>(JsonToFlatbuffer(
34 configuration, Configuration::MiniReflectTypeTable()))),
35 dt) {}
36
37 ControlLoopTestTemplated(
38 FlatbufferDetachedBuffer<Configuration> configuration,
39 ::std::chrono::nanoseconds dt = kTimeTick)
40 : configuration_(std::move(configuration)),
41 event_loop_factory_(&configuration_.message()),
42 dt_(dt),
Austin Schuh08e96eb2020-02-25 23:36:30 -080043 robot_status_event_loop_(MakeEventLoop(
44 "robot_status",
45 configuration::MultiNode(event_loop_factory_.configuration())
46 ? configuration::GetNode(event_loop_factory_.configuration(),
47 "roborio")
48 : nullptr)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 testing::EnableTestLogging();
Austin Schuhdf6cbb12019-02-02 13:46:52 -080050 robot_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 robot_status_event_loop_->MakeSender<::aos::RobotState>("/aos");
Austin Schuhdf6cbb12019-02-02 13:46:52 -080052 joystick_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 robot_status_event_loop_->MakeSender<::aos::JoystickState>("/aos");
Austin Schuh2001aa42018-10-29 22:57:02 -070054
Austin Schuh9fe68f72019-08-10 19:32:03 -070055 // Schedule the robot status send 1 nanosecond before the loop runs.
56 send_robot_state_phased_loop_ = robot_status_event_loop_->AddPhasedLoop(
57 [this](int) { SendRobotState(); }, dt_,
58 dt - ::std::chrono::nanoseconds(1));
Austin Schuh2001aa42018-10-29 22:57:02 -070059
Austin Schuh9fe68f72019-08-10 19:32:03 -070060 send_joystick_state_timer_ =
61 robot_status_event_loop_->AddTimer([this]() { SendJoystickState(); });
62
63 robot_status_event_loop_->OnRun([this]() {
64 send_joystick_state_timer_->Setup(
65 robot_status_event_loop_->monotonic_now(), dt_);
66 });
Austin Schuh2001aa42018-10-29 22:57:02 -070067 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070068 virtual ~ControlLoopTestTemplated() {}
Brian Silverman65e49702014-04-30 17:36:40 -070069
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000070 void set_team_id(uint16_t team_id) { team_id_ = team_id; }
71 uint16_t team_id() const { return team_id_; }
72
Austin Schuh9fe68f72019-08-10 19:32:03 -070073 // Sets the enabled/disabled bit and (potentially) rebroadcasts the robot
74 // state messages.
75 void SetEnabled(bool enabled) {
76 if (enabled_ != enabled) {
77 enabled_ = enabled;
78 SendJoystickState();
79 SendRobotState();
80 send_joystick_state_timer_->Setup(
81 robot_status_event_loop_->monotonic_now(), dt_);
Austin Schuh2001aa42018-10-29 22:57:02 -070082 }
Brian Silvermane6f64ab2015-02-05 17:03:56 -050083 }
Brian Silverman65e49702014-04-30 17:36:40 -070084
Brian Silverman57cad222015-02-14 20:46:41 -050085 // Simulate a reset of the process reading sensors, which tells loops that all
86 // index counts etc will be reset.
Tyler Chatow67ddb032020-01-12 14:30:04 -080087 void SimulateSensorReset() { ++reader_pid_; }
Brian Silverman57cad222015-02-14 20:46:41 -050088
Austin Schuhe5f064d2016-03-05 17:43:51 -080089 // Sets the battery voltage in robot_state.
90 void set_battery_voltage(double battery_voltage) {
91 battery_voltage_ = battery_voltage;
92 }
93
Austin Schuh08e96eb2020-02-25 23:36:30 -080094 ::std::unique_ptr<::aos::EventLoop> MakeEventLoop(
95 std::string_view name, const Node *node = nullptr) {
96 return event_loop_factory_.MakeEventLoop(name, node);
Austin Schuh9fe68f72019-08-10 19:32:03 -070097 }
98
Austin Schuh7d87b672019-12-01 20:23:49 -080099 void set_send_delay(std::chrono::nanoseconds send_delay) {
100 event_loop_factory_.set_send_delay(send_delay);
101 }
102
Austin Schuh9fe68f72019-08-10 19:32:03 -0700103 void RunFor(monotonic_clock::duration duration) {
104 event_loop_factory_.RunFor(duration);
105 }
106
107 ::aos::monotonic_clock::time_point monotonic_now() {
Austin Schuha5e14192020-01-06 18:02:41 -0800108 return robot_status_event_loop_->monotonic_now();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700109 }
110
111 ::std::chrono::nanoseconds dt() const { return dt_; }
112
Austin Schuh08e96eb2020-02-25 23:36:30 -0800113 const Configuration *configuration() const {
114 return &configuration_.message();
115 }
116
James Kuszmaul958b21e2020-02-26 21:51:40 -0800117 SimulatedEventLoopFactory *event_loop_factory() {
118 return &event_loop_factory_;
119 }
120
Brian Silverman65e49702014-04-30 17:36:40 -0700121 private:
Austin Schuh9fe68f72019-08-10 19:32:03 -0700122 // Sends out all of the required queue messages.
123 void SendJoystickState() {
124 if (monotonic_now() >= kDSPacketTime + last_ds_time_ ||
125 last_enabled_ != enabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 auto new_state = joystick_state_sender_.MakeBuilder();
127 ::aos::JoystickState::Builder builder =
128 new_state.template MakeBuilder<::aos::JoystickState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700129
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 builder.add_fake(true);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700131
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 builder.add_enabled(enabled_);
133 builder.add_autonomous(false);
134 builder.add_team_id(team_id_);
135
136 new_state.Send(builder.Finish());
Austin Schuh9fe68f72019-08-10 19:32:03 -0700137
138 last_ds_time_ = monotonic_now();
139 last_enabled_ = enabled_;
140 }
141 }
142
143 bool last_enabled_ = false;
144
145 void SendRobotState() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 auto new_state = robot_state_sender_.MakeBuilder();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 ::aos::RobotState::Builder builder =
149 new_state.template MakeBuilder<::aos::RobotState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700150
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 builder.add_reader_pid(reader_pid_);
152 builder.add_outputs_enabled(enabled_);
153 builder.add_browned_out(false);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700154
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 builder.add_is_3v3_active(true);
156 builder.add_is_5v_active(true);
157 builder.add_voltage_3v3(3.3);
158 builder.add_voltage_5v(5.0);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700159
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 builder.add_voltage_roborio_in(battery_voltage_);
161 builder.add_voltage_battery(battery_voltage_);
162
163 new_state.Send(builder.Finish());
Austin Schuh9fe68f72019-08-10 19:32:03 -0700164 }
165
166 static constexpr ::std::chrono::microseconds kTimeTick{5000};
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800167 static constexpr ::std::chrono::milliseconds kDSPacketTime{20};
Brian Silvermane6f64ab2015-02-05 17:03:56 -0500168
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 FlatbufferDetachedBuffer<Configuration> configuration_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700170
171 SimulatedEventLoopFactory event_loop_factory_;
172
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 const ::std::chrono::nanoseconds dt_;
174
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000175 uint16_t team_id_ = 971;
Brian Silverman57cad222015-02-14 20:46:41 -0500176 int32_t reader_pid_ = 1;
Austin Schuhe5f064d2016-03-05 17:43:51 -0800177 double battery_voltage_ = 12.4;
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000178
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800179 ::aos::monotonic_clock::time_point last_ds_time_ =
Austin Schuh9fe68f72019-08-10 19:32:03 -0700180 ::aos::monotonic_clock::min_time;
Brian Silverman65e49702014-04-30 17:36:40 -0700181
Austin Schuh9fe68f72019-08-10 19:32:03 -0700182 bool enabled_ = false;
Campbell Crowley152c7cf2016-02-14 21:20:50 -0800183
Austin Schuh9fe68f72019-08-10 19:32:03 -0700184 ::std::unique_ptr<::aos::EventLoop> robot_status_event_loop_;
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800185
186 ::aos::Sender<::aos::RobotState> robot_state_sender_;
187 ::aos::Sender<::aos::JoystickState> joystick_state_sender_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700188
189 ::aos::PhasedLoopHandler *send_robot_state_phased_loop_ = nullptr;
190 ::aos::TimerHandler *send_joystick_state_timer_ = nullptr;
Brian Silverman65e49702014-04-30 17:36:40 -0700191};
192
Austin Schuh2001aa42018-10-29 22:57:02 -0700193typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest;
194
195template <typename TestBaseClass>
Austin Schuh9fe68f72019-08-10 19:32:03 -0700196constexpr ::std::chrono::microseconds
197 ControlLoopTestTemplated<TestBaseClass>::kTimeTick;
Austin Schuh2001aa42018-10-29 22:57:02 -0700198
199template <typename TestBaseClass>
Tyler Chatow67ddb032020-01-12 14:30:04 -0800200constexpr ::std::chrono::milliseconds
201 ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime;
Austin Schuh2001aa42018-10-29 22:57:02 -0700202
Brian Silverman65e49702014-04-30 17:36:40 -0700203} // namespace testing
204} // namespace aos
205
John Park33858a32018-09-28 23:05:48 -0700206#endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_