blob: de9d4cd310a3822b4d4f0e8c5f0ecfb9ba451823 [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/testing/test_logging.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/time/time.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070012#include "frc971/input/joystick_state_generated.h"
13#include "frc971/input/robot_state_generated.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080014#include "gtest/gtest.h"
Brian Silverman65e49702014-04-30 17:36:40 -070015
James Kuszmaul61750662021-06-21 21:32:33 -070016namespace frc971 {
Brian Silverman65e49702014-04-30 17:36:40 -070017namespace 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 ControlLoopTestTemplated(
James Kuszmaul61750662021-06-21 21:32:33 -070028 aos::FlatbufferDetachedBuffer<aos::Configuration> configuration,
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 ::std::chrono::nanoseconds dt = kTimeTick)
30 : configuration_(std::move(configuration)),
31 event_loop_factory_(&configuration_.message()),
32 dt_(dt),
Austin Schuh08e96eb2020-02-25 23:36:30 -080033 robot_status_event_loop_(MakeEventLoop(
34 "robot_status",
James Kuszmaul61750662021-06-21 21:32:33 -070035 aos::configuration::MultiNode(event_loop_factory_.configuration())
36 ? aos::configuration::GetNode(
37 event_loop_factory_.configuration(), "roborio")
Austin Schuh08e96eb2020-02-25 23:36:30 -080038 : nullptr)) {
James Kuszmaul61750662021-06-21 21:32:33 -070039 aos::testing::EnableTestLogging();
Austin Schuhdf6cbb12019-02-02 13:46:52 -080040 robot_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 robot_status_event_loop_->MakeSender<::aos::RobotState>("/aos");
Austin Schuhdf6cbb12019-02-02 13:46:52 -080042 joystick_state_sender_ =
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 robot_status_event_loop_->MakeSender<::aos::JoystickState>("/aos");
Austin Schuh2001aa42018-10-29 22:57:02 -070044
Austin Schuh9fe68f72019-08-10 19:32:03 -070045 // Schedule the robot status send 1 nanosecond before the loop runs.
46 send_robot_state_phased_loop_ = robot_status_event_loop_->AddPhasedLoop(
47 [this](int) { SendRobotState(); }, dt_,
48 dt - ::std::chrono::nanoseconds(1));
Austin Schuh2001aa42018-10-29 22:57:02 -070049
Austin Schuh9fe68f72019-08-10 19:32:03 -070050 send_joystick_state_timer_ =
51 robot_status_event_loop_->AddTimer([this]() { SendJoystickState(); });
52
53 robot_status_event_loop_->OnRun([this]() {
54 send_joystick_state_timer_->Setup(
55 robot_status_event_loop_->monotonic_now(), dt_);
56 });
Austin Schuh2001aa42018-10-29 22:57:02 -070057 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070058 virtual ~ControlLoopTestTemplated() {}
Brian Silverman65e49702014-04-30 17:36:40 -070059
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000060 void set_team_id(uint16_t team_id) { team_id_ = team_id; }
61 uint16_t team_id() const { return team_id_; }
62
Austin Schuh9fe68f72019-08-10 19:32:03 -070063 // Sets the enabled/disabled bit and (potentially) rebroadcasts the robot
64 // state messages.
65 void SetEnabled(bool enabled) {
66 if (enabled_ != enabled) {
67 enabled_ = enabled;
68 SendJoystickState();
69 SendRobotState();
70 send_joystick_state_timer_->Setup(
71 robot_status_event_loop_->monotonic_now(), dt_);
Austin Schuh2001aa42018-10-29 22:57:02 -070072 }
Brian Silvermane6f64ab2015-02-05 17:03:56 -050073 }
Brian Silverman65e49702014-04-30 17:36:40 -070074
Brian Silverman57cad222015-02-14 20:46:41 -050075 // Simulate a reset of the process reading sensors, which tells loops that all
76 // index counts etc will be reset.
Tyler Chatow67ddb032020-01-12 14:30:04 -080077 void SimulateSensorReset() { ++reader_pid_; }
Brian Silverman57cad222015-02-14 20:46:41 -050078
Austin Schuhe5f064d2016-03-05 17:43:51 -080079 // Sets the battery voltage in robot_state.
80 void set_battery_voltage(double battery_voltage) {
81 battery_voltage_ = battery_voltage;
82 }
83
Austin Schuh08e96eb2020-02-25 23:36:30 -080084 ::std::unique_ptr<::aos::EventLoop> MakeEventLoop(
James Kuszmaul61750662021-06-21 21:32:33 -070085 std::string_view name, const aos::Node *node = nullptr) {
Austin Schuh08e96eb2020-02-25 23:36:30 -080086 return event_loop_factory_.MakeEventLoop(name, node);
Austin Schuh9fe68f72019-08-10 19:32:03 -070087 }
88
Austin Schuh7d87b672019-12-01 20:23:49 -080089 void set_send_delay(std::chrono::nanoseconds send_delay) {
90 event_loop_factory_.set_send_delay(send_delay);
91 }
92
James Kuszmaul61750662021-06-21 21:32:33 -070093 void RunFor(aos::monotonic_clock::duration duration) {
Austin Schuh9fe68f72019-08-10 19:32:03 -070094 event_loop_factory_.RunFor(duration);
95 }
96
97 ::aos::monotonic_clock::time_point monotonic_now() {
Austin Schuha5e14192020-01-06 18:02:41 -080098 return robot_status_event_loop_->monotonic_now();
Austin Schuh9fe68f72019-08-10 19:32:03 -070099 }
100
101 ::std::chrono::nanoseconds dt() const { return dt_; }
102
James Kuszmaul61750662021-06-21 21:32:33 -0700103 const aos::Configuration *configuration() const {
Austin Schuh08e96eb2020-02-25 23:36:30 -0800104 return &configuration_.message();
105 }
106
James Kuszmaul61750662021-06-21 21:32:33 -0700107 aos::SimulatedEventLoopFactory *event_loop_factory() {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800108 return &event_loop_factory_;
109 }
110
Brian Silverman65e49702014-04-30 17:36:40 -0700111 private:
Austin Schuh9fe68f72019-08-10 19:32:03 -0700112 // Sends out all of the required queue messages.
113 void SendJoystickState() {
114 if (monotonic_now() >= kDSPacketTime + last_ds_time_ ||
115 last_enabled_ != enabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116 auto new_state = joystick_state_sender_.MakeBuilder();
117 ::aos::JoystickState::Builder builder =
118 new_state.template MakeBuilder<::aos::JoystickState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 builder.add_fake(true);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700121
Alex Perrycb7da4b2019-08-28 19:35:56 -0700122 builder.add_enabled(enabled_);
123 builder.add_autonomous(false);
124 builder.add_team_id(team_id_);
125
126 new_state.Send(builder.Finish());
Austin Schuh9fe68f72019-08-10 19:32:03 -0700127
128 last_ds_time_ = monotonic_now();
129 last_enabled_ = enabled_;
130 }
131 }
132
133 bool last_enabled_ = false;
134
135 void SendRobotState() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700136 auto new_state = robot_state_sender_.MakeBuilder();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700137
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 ::aos::RobotState::Builder builder =
139 new_state.template MakeBuilder<::aos::RobotState>();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700140
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 builder.add_reader_pid(reader_pid_);
142 builder.add_outputs_enabled(enabled_);
143 builder.add_browned_out(false);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700144
Alex Perrycb7da4b2019-08-28 19:35:56 -0700145 builder.add_is_3v3_active(true);
146 builder.add_is_5v_active(true);
147 builder.add_voltage_3v3(3.3);
148 builder.add_voltage_5v(5.0);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700149
Alex Perrycb7da4b2019-08-28 19:35:56 -0700150 builder.add_voltage_roborio_in(battery_voltage_);
151 builder.add_voltage_battery(battery_voltage_);
152
153 new_state.Send(builder.Finish());
Austin Schuh9fe68f72019-08-10 19:32:03 -0700154 }
155
156 static constexpr ::std::chrono::microseconds kTimeTick{5000};
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800157 static constexpr ::std::chrono::milliseconds kDSPacketTime{20};
Brian Silvermane6f64ab2015-02-05 17:03:56 -0500158
James Kuszmaul61750662021-06-21 21:32:33 -0700159 aos::FlatbufferDetachedBuffer<aos::Configuration> configuration_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700160
James Kuszmaul61750662021-06-21 21:32:33 -0700161 aos::SimulatedEventLoopFactory event_loop_factory_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700162
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 const ::std::chrono::nanoseconds dt_;
164
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000165 uint16_t team_id_ = 971;
Brian Silverman57cad222015-02-14 20:46:41 -0500166 int32_t reader_pid_ = 1;
Austin Schuhe5f064d2016-03-05 17:43:51 -0800167 double battery_voltage_ = 12.4;
Philipp Schraderf75a8bf2015-02-02 05:30:16 +0000168
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800169 ::aos::monotonic_clock::time_point last_ds_time_ =
Austin Schuh9fe68f72019-08-10 19:32:03 -0700170 ::aos::monotonic_clock::min_time;
Brian Silverman65e49702014-04-30 17:36:40 -0700171
Austin Schuh9fe68f72019-08-10 19:32:03 -0700172 bool enabled_ = false;
Campbell Crowley152c7cf2016-02-14 21:20:50 -0800173
Austin Schuh9fe68f72019-08-10 19:32:03 -0700174 ::std::unique_ptr<::aos::EventLoop> robot_status_event_loop_;
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800175
176 ::aos::Sender<::aos::RobotState> robot_state_sender_;
177 ::aos::Sender<::aos::JoystickState> joystick_state_sender_;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700178
179 ::aos::PhasedLoopHandler *send_robot_state_phased_loop_ = nullptr;
180 ::aos::TimerHandler *send_joystick_state_timer_ = nullptr;
Brian Silverman65e49702014-04-30 17:36:40 -0700181};
182
Austin Schuh2001aa42018-10-29 22:57:02 -0700183typedef ControlLoopTestTemplated<::testing::Test> ControlLoopTest;
184
185template <typename TestBaseClass>
Austin Schuh9fe68f72019-08-10 19:32:03 -0700186constexpr ::std::chrono::microseconds
187 ControlLoopTestTemplated<TestBaseClass>::kTimeTick;
Austin Schuh2001aa42018-10-29 22:57:02 -0700188
189template <typename TestBaseClass>
Tyler Chatow67ddb032020-01-12 14:30:04 -0800190constexpr ::std::chrono::milliseconds
191 ControlLoopTestTemplated<TestBaseClass>::kDSPacketTime;
Austin Schuh2001aa42018-10-29 22:57:02 -0700192
Brian Silverman65e49702014-04-30 17:36:40 -0700193} // namespace testing
James Kuszmaul61750662021-06-21 21:32:33 -0700194} // namespace frc971
Brian Silverman65e49702014-04-30 17:36:40 -0700195
John Park33858a32018-09-28 23:05:48 -0700196#endif // AOS_CONTROLS_CONTROL_LOOP_TEST_H_