blob: c6675688873658fde11f0dbc6ad4ef4b23d1f4c6 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "glog/logging.h"
8#include "gtest/gtest.h"
9
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/events/simulated_event_loop.h"
11#include "aos/flatbuffers.h"
12#include "aos/json_to_flatbuffer.h"
Austin Schuh0debde12022-08-17 16:25:17 -070013#include "aos/network/testing_time_converter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/testing/test_logging.h"
John Park33858a32018-09-28 23:05:48 -070015#include "aos/time/time.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070016#include "frc971/input/joystick_state_generated.h"
17#include "frc971/input/robot_state_generated.h"
Brian Silverman65e49702014-04-30 17:36:40 -070018
James Kuszmaul61750662021-06-21 21:32:33 -070019namespace frc971 {
Brian Silverman65e49702014-04-30 17:36:40 -070020namespace testing {
21
22// Handles setting up the environment that all control loops need to actually
23// run.
24// This includes sending the queue messages and Clear()ing the queues when
25// appropriate.
26// It also includes dealing with ::aos::time.
Austin Schuh2001aa42018-10-29 22:57:02 -070027template <typename TestBaseClass>
28class ControlLoopTestTemplated : public TestBaseClass {
Brian Silverman65e49702014-04-30 17:36:40 -070029 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 ControlLoopTestTemplated(
James Kuszmaul61750662021-06-21 21:32:33 -070031 aos::FlatbufferDetachedBuffer<aos::Configuration> configuration,
Austin Schuh0debde12022-08-17 16:25:17 -070032 ::std::chrono::nanoseconds dt = kTimeTick,
33 std::vector<std::vector<aos::logger::BootTimestamp>> times = {})
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 : configuration_(std::move(configuration)),
Austin Schuh0debde12022-08-17 16:25:17 -070035 time_converter_(
36 aos::configuration::NodesCount(&configuration_.message())),
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 event_loop_factory_(&configuration_.message()),
Austin Schuh0debde12022-08-17 16:25:17 -070038 dt_(dt) {
39 event_loop_factory()->SetTimeConverter(&time_converter_);
40
41 // We need to setup the time converter before any event loop has been
42 // created. Otherwise, the event loop will read the boot uuid and we'll be
43 // unable to change it.
44 if (times.empty()) {
45 time_converter_.StartEqual();
46 } else {
47 for (const std::vector<aos::logger::BootTimestamp> &time : times) {
48 time_converter_.AddMonotonic(time);
49 }
50 }
51 robot_status_event_loop_ = MakeEventLoop(
52 "robot_status",
53 aos::configuration::MultiNode(event_loop_factory_.configuration())
54 ? aos::configuration::GetNode(event_loop_factory_.configuration(),
55 "roborio")
56 : nullptr);
James Kuszmaul61750662021-06-21 21:32:33 -070057 aos::testing::EnableTestLogging();
Austin Schuhdf6cbb12019-02-02 13:46:52 -080058 robot_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 robot_status_event_loop_->MakeSender<::aos::RobotState>("/aos");
Austin Schuhdf6cbb12019-02-02 13:46:52 -080060 joystick_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 robot_status_event_loop_->MakeSender<::aos::JoystickState>("/aos");
Austin Schuh2001aa42018-10-29 22:57:02 -070062
Austin Schuh9fe68f72019-08-10 19:32:03 -070063 // Schedule the robot status send 1 nanosecond before the loop runs.
64 send_robot_state_phased_loop_ = robot_status_event_loop_->AddPhasedLoop(
65 [this](int) { SendRobotState(); }, dt_,
66 dt - ::std::chrono::nanoseconds(1));
Austin Schuh2001aa42018-10-29 22:57:02 -070067
Austin Schuh9fe68f72019-08-10 19:32:03 -070068 send_joystick_state_timer_ =
69 robot_status_event_loop_->AddTimer([this]() { SendJoystickState(); });
70
71 robot_status_event_loop_->OnRun([this]() {
72 send_joystick_state_timer_->Setup(
73 robot_status_event_loop_->monotonic_now(), dt_);
74 });
Austin Schuh2001aa42018-10-29 22:57:02 -070075 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070076 virtual ~ControlLoopTestTemplated() {}
Brian Silverman65e49702014-04-30 17:36:40 -070077
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000078 void set_team_id(uint16_t team_id) { team_id_ = team_id; }
79 uint16_t team_id() const { return team_id_; }
80
Ravago Jonesd51af7a2022-03-26 21:44:20 -070081 void set_alliance(aos::Alliance alliance) { alliance_ = alliance; }
82 aos::Alliance alliance() const { return alliance_; }
83
Austin Schuh9fe68f72019-08-10 19:32:03 -070084 // Sets the enabled/disabled bit and (potentially) rebroadcasts the robot
85 // state messages.
86 void SetEnabled(bool enabled) {
87 if (enabled_ != enabled) {
88 enabled_ = enabled;
89 SendJoystickState();
90 SendRobotState();
91 send_joystick_state_timer_->Setup(
92 robot_status_event_loop_->monotonic_now(), dt_);
Austin Schuh2001aa42018-10-29 22:57:02 -070093 }
Brian Silvermane6f64ab2015-02-05 17:03:56 -050094 }
Brian Silverman65e49702014-04-30 17:36:40 -070095
Brian Silverman57cad222015-02-14 20:46:41 -050096 // Simulate a reset of the process reading sensors, which tells loops that all
97 // index counts etc will be reset.
Tyler Chatow67ddb032020-01-12 14:30:04 -080098 void SimulateSensorReset() { ++reader_pid_; }
Brian Silverman57cad222015-02-14 20:46:41 -050099
Austin Schuhe5f064d2016-03-05 17:43:51 -0800100 // Sets the battery voltage in robot_state.
101 void set_battery_voltage(double battery_voltage) {
102 battery_voltage_ = battery_voltage;
103 }
104
Austin Schuh08e96eb2020-02-25 23:36:30 -0800105 ::std::unique_ptr<::aos::EventLoop> MakeEventLoop(
James Kuszmaul61750662021-06-21 21:32:33 -0700106 std::string_view name, const aos::Node *node = nullptr) {
Austin Schuh08e96eb2020-02-25 23:36:30 -0800107 return event_loop_factory_.MakeEventLoop(name, node);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700108 }
109
Austin Schuh7d87b672019-12-01 20:23:49 -0800110 void set_send_delay(std::chrono::nanoseconds send_delay) {
111 event_loop_factory_.set_send_delay(send_delay);
112 }
113
James Kuszmaul61750662021-06-21 21:32:33 -0700114 void RunFor(aos::monotonic_clock::duration duration) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700115 event_loop_factory_.RunFor(duration);
116 }
117
118 ::aos::monotonic_clock::time_point monotonic_now() {
Austin Schuha5e14192020-01-06 18:02:41 -0800119 return robot_status_event_loop_->monotonic_now();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700120 }
121
122 ::std::chrono::nanoseconds dt() const { return dt_; }
123
James Kuszmaul61750662021-06-21 21:32:33 -0700124 const aos::Configuration *configuration() const {
Austin Schuh08e96eb2020-02-25 23:36:30 -0800125 return &configuration_.message();
126 }
127
James Kuszmaul61750662021-06-21 21:32:33 -0700128 aos::SimulatedEventLoopFactory *event_loop_factory() {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800129 return &event_loop_factory_;
130 }
131
Austin Schuh0debde12022-08-17 16:25:17 -0700132 aos::message_bridge::TestingTimeConverter *time_converter() {
133 return &time_converter_;
134 }
135
Brian Silverman65e49702014-04-30 17:36:40 -0700136 private:
Austin Schuh9fe68f72019-08-10 19:32:03 -0700137 // Sends out all of the required queue messages.
138 void SendJoystickState() {
139 if (monotonic_now() >= kDSPacketTime + last_ds_time_ ||
140 last_enabled_ != enabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 auto new_state = joystick_state_sender_.MakeBuilder();
142 ::aos::JoystickState::Builder builder =
143 new_state.template MakeBuilder<::aos::JoystickState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700144
Alex Perrycb7da4b2019-08-28 19:35:56 -0700145 builder.add_fake(true);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700146
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 builder.add_enabled(enabled_);
148 builder.add_autonomous(false);
149 builder.add_team_id(team_id_);
Ravago Jonesd51af7a2022-03-26 21:44:20 -0700150 builder.add_alliance(alliance_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151
Ravago Jonesd51af7a2022-03-26 21:44:20 -0700152 CHECK_EQ(new_state.Send(builder.Finish()), aos::RawSender::Error::kOk);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700153
154 last_ds_time_ = monotonic_now();
155 last_enabled_ = enabled_;
156 }
157 }
158
159 bool last_enabled_ = false;
160
161 void SendRobotState() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 auto new_state = robot_state_sender_.MakeBuilder();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700163
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 ::aos::RobotState::Builder builder =
165 new_state.template MakeBuilder<::aos::RobotState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700166
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 builder.add_reader_pid(reader_pid_);
168 builder.add_outputs_enabled(enabled_);
169 builder.add_browned_out(false);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700170
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 builder.add_is_3v3_active(true);
172 builder.add_is_5v_active(true);
173 builder.add_voltage_3v3(3.3);
174 builder.add_voltage_5v(5.0);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700175
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 builder.add_voltage_roborio_in(battery_voltage_);
177 builder.add_voltage_battery(battery_voltage_);
178
milind1f1dca32021-07-03 13:50:07 -0700179 new_state.CheckOk(new_state.Send(builder.Finish()));
Austin Schuh9fe68f72019-08-10 19:32:03 -0700180 }
181
182 static constexpr ::std::chrono::microseconds kTimeTick{5000};
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800183 static constexpr ::std::chrono::milliseconds kDSPacketTime{20};
Brian Silvermane6f64ab2015-02-05 17:03:56 -0500184
James Kuszmaul61750662021-06-21 21:32:33 -0700185 aos::FlatbufferDetachedBuffer<aos::Configuration> configuration_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700186
Austin Schuh0debde12022-08-17 16:25:17 -0700187 aos::message_bridge::TestingTimeConverter time_converter_;
188
James Kuszmaul61750662021-06-21 21:32:33 -0700189 aos::SimulatedEventLoopFactory event_loop_factory_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700190
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191 const ::std::chrono::nanoseconds dt_;
192
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000193 uint16_t team_id_ = 971;
Ravago Jonesd51af7a2022-03-26 21:44:20 -0700194 aos::Alliance alliance_ = aos::Alliance::kInvalid;
Brian Silverman57cad222015-02-14 20:46:41 -0500195 int32_t reader_pid_ = 1;
Austin Schuhe5f064d2016-03-05 17:43:51 -0800196 double battery_voltage_ = 12.4;
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000197
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800198 ::aos::monotonic_clock::time_point last_ds_time_ =
Austin Schuh9fe68f72019-08-10 19:32:03 -0700199 ::aos::monotonic_clock::min_time;
Brian Silverman65e49702014-04-30 17:36:40 -0700200
Austin Schuh9fe68f72019-08-10 19:32:03 -0700201 bool enabled_ = false;
Campbell Crowley152c7cf2016-02-14 21:20:50 -0800202
Austin Schuh9fe68f72019-08-10 19:32:03 -0700203 ::std::unique_ptr<::aos::EventLoop> robot_status_event_loop_;
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800204
205 ::aos::Sender<::aos::RobotState> robot_state_sender_;
206 ::aos::Sender<::aos::JoystickState> joystick_state_sender_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700207
208 ::aos::PhasedLoopHandler *send_robot_state_phased_loop_ = nullptr;
209 ::aos::TimerHandler *send_joystick_state_timer_ = nullptr;
Brian Silverman65e49702014-04-30 17:36:40 -0700210};
211
Austin Schuh2001aa42018-10-29 22:57:02 -0700212typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest;
213
214template <typename TestBaseClass>
Austin Schuh9fe68f72019-08-10 19:32:03 -0700215constexpr ::std::chrono::microseconds
216 ControlLoopTestTemplated<TestBaseClass>::kTimeTick;
Austin Schuh2001aa42018-10-29 22:57:02 -0700217
218template <typename TestBaseClass>
Tyler Chatow67ddb032020-01-12 14:30:04 -0800219constexpr ::std::chrono::milliseconds
220 ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime;
Austin Schuh2001aa42018-10-29 22:57:02 -0700221
Brian Silverman65e49702014-04-30 17:36:40 -0700222} // namespace testing
James Kuszmaul61750662021-06-21 21:32:33 -0700223} // namespace frc971
Brian Silverman65e49702014-04-30 17:36:40 -0700224
John Park33858a32018-09-28 23:05:48 -0700225#endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_