Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 1 | #include "frc971/codelab/basic.h" |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <memory> |
| 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/events/shm_event_loop.h" |
James Kuszmaul | 78e29ac | 2020-07-28 21:07:03 -0700 | [diff] [blame] | 9 | #include "frc971/codelab/basic_goal_generated.h" |
| 10 | #include "frc971/codelab/basic_output_generated.h" |
| 11 | #include "frc971/codelab/basic_position_generated.h" |
| 12 | #include "frc971/codelab/basic_status_generated.h" |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 13 | #include "frc971/control_loops/control_loop_test.h" |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/team_number_test_environment.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | namespace frc971 { |
| 18 | namespace codelab { |
| 19 | namespace testing { |
| 20 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 21 | namespace chrono = ::std::chrono; |
| 22 | using aos::monotonic_clock; |
| 23 | |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 24 | // Class which simulates stuff and sends out queue messages with the |
| 25 | // position. |
| 26 | class BasicSimulation { |
| 27 | public: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 28 | BasicSimulation(::aos::EventLoop *event_loop, chrono::nanoseconds dt) |
| 29 | : event_loop_(event_loop), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | position_sender_(event_loop_->MakeSender<Position>("/codelab")), |
| 31 | status_fetcher_(event_loop_->MakeFetcher<Status>("/codelab")), |
| 32 | output_fetcher_(event_loop_->MakeFetcher<Output>("/codelab")) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 33 | event_loop_->AddPhasedLoop( |
| 34 | [this](int) { |
| 35 | // Skip this the first time. |
| 36 | if (!first_) { |
| 37 | Simulate(); |
| 38 | } |
| 39 | first_ = false; |
| 40 | SendPositionMessage(); |
| 41 | }, |
| 42 | dt); |
| 43 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 44 | |
| 45 | // Sends a queue message with the position data. |
| 46 | void SendPositionMessage() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | auto builder = position_sender_.MakeBuilder(); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 48 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | Position::Builder position_builder = builder.MakeBuilder<Position>(); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 50 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | position_builder.add_limit_sensor(limit_sensor_); |
| 52 | |
| 53 | builder.Send(position_builder.Finish()); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void VerifyResults(double voltage, bool status) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 57 | output_fetcher_.Fetch(); |
| 58 | status_fetcher_.Fetch(); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 59 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(output_fetcher_.get() != nullptr); |
| 61 | ASSERT_TRUE(status_fetcher_.get() != nullptr); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 62 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | EXPECT_EQ(output_fetcher_->intake_voltage(), voltage); |
| 64 | EXPECT_EQ(status_fetcher_->intake_complete(), status); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void set_limit_sensor(bool value) { limit_sensor_ = value; } |
| 68 | |
| 69 | // Simulates basic control loop for a single timestep. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 70 | void Simulate() { EXPECT_TRUE(output_fetcher_.Fetch()); } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 73 | ::aos::EventLoop *event_loop_; |
| 74 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | ::aos::Sender<Position> position_sender_; |
| 76 | ::aos::Fetcher<Status> status_fetcher_; |
| 77 | ::aos::Fetcher<Output> output_fetcher_; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 78 | |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 79 | bool limit_sensor_ = false; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 80 | |
| 81 | bool first_ = true; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 84 | class BasicControlLoopTest : public ::frc971::testing::ControlLoopTest { |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 85 | public: |
| 86 | BasicControlLoopTest() |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 87 | : ::frc971::testing::ControlLoopTest( |
James Kuszmaul | 78e29ac | 2020-07-28 21:07:03 -0700 | [diff] [blame] | 88 | aos::configuration::ReadConfig("frc971/codelab/config.json"), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | chrono::microseconds(5050)), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 90 | test_event_loop_(MakeEventLoop("test")), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | goal_sender_(test_event_loop_->MakeSender<Goal>("/codelab")), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 92 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 93 | basic_event_loop_(MakeEventLoop("basic")), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | basic_(basic_event_loop_.get(), "/codelab"), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 95 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 96 | basic_simulation_event_loop_(MakeEventLoop("simulation")), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 97 | basic_simulation_(basic_simulation_event_loop_.get(), dt()) { |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 98 | set_team_id(control_loops::testing::kTeamNumber); |
James Kuszmaul | 78e29ac | 2020-07-28 21:07:03 -0700 | [diff] [blame] | 99 | SetEnabled(true); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 102 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | ::aos::Sender<Goal> goal_sender_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 104 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 105 | ::std::unique_ptr<::aos::EventLoop> basic_event_loop_; |
| 106 | Basic basic_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 107 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 108 | ::std::unique_ptr<::aos::EventLoop> basic_simulation_event_loop_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 109 | BasicSimulation basic_simulation_; |
| 110 | }; |
| 111 | |
| 112 | // Tests that when the motor has finished intaking it stops. |
| 113 | TEST_F(BasicControlLoopTest, IntakeLimitTransitionsToTrue) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 114 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | auto builder = goal_sender_.MakeBuilder(); |
| 116 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 117 | goal_builder.add_intake(true); |
| 118 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 121 | basic_simulation_.set_limit_sensor(false); |
| 122 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 123 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 124 | |
| 125 | basic_simulation_.VerifyResults(12.0, false); |
| 126 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 127 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | auto builder = goal_sender_.MakeBuilder(); |
| 129 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 130 | goal_builder.add_intake(true); |
| 131 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 132 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 133 | basic_simulation_.set_limit_sensor(true); |
| 134 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 135 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 136 | |
| 137 | basic_simulation_.VerifyResults(0.0, true); |
| 138 | } |
| 139 | |
| 140 | // Tests that the intake goes on if the sensor is not pressed |
| 141 | // and intake is requested. |
| 142 | TEST_F(BasicControlLoopTest, IntakeLimitNotSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 143 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | auto builder = goal_sender_.MakeBuilder(); |
| 145 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 146 | goal_builder.add_intake(true); |
| 147 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 148 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 149 | basic_simulation_.set_limit_sensor(false); |
| 150 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 151 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 152 | |
| 153 | basic_simulation_.VerifyResults(12.0, false); |
| 154 | } |
| 155 | |
| 156 | // Tests that the intake is off if no intake is requested, |
| 157 | // even if the limit sensor is off. |
| 158 | TEST_F(BasicControlLoopTest, NoIntakeLimitNotSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 159 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | auto builder = goal_sender_.MakeBuilder(); |
| 161 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 162 | goal_builder.add_intake(false); |
| 163 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 164 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 165 | basic_simulation_.set_limit_sensor(false); |
| 166 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 167 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 168 | |
| 169 | basic_simulation_.VerifyResults(0.0, false); |
| 170 | } |
| 171 | |
| 172 | // Tests that the intake is off even if the limit sensor |
| 173 | // is pressed and intake is requested. |
| 174 | TEST_F(BasicControlLoopTest, IntakeLimitSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 175 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | auto builder = goal_sender_.MakeBuilder(); |
| 177 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 178 | goal_builder.add_intake(true); |
| 179 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 180 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 181 | basic_simulation_.set_limit_sensor(true); |
| 182 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 183 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 184 | |
| 185 | basic_simulation_.VerifyResults(0.0, true); |
| 186 | } |
| 187 | |
| 188 | // Tests that the intake is off if no intake is requested, |
| 189 | TEST_F(BasicControlLoopTest, NoIntakeLimitSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 190 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | auto builder = goal_sender_.MakeBuilder(); |
| 192 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 193 | goal_builder.add_intake(false); |
| 194 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 195 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 196 | basic_simulation_.set_limit_sensor(true); |
| 197 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 198 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 199 | |
| 200 | basic_simulation_.VerifyResults(0.0, false); |
| 201 | } |
| 202 | |
| 203 | // Tests that the control loop handles things properly if no goal is set. |
| 204 | TEST_F(BasicControlLoopTest, NoGoalSet) { |
| 205 | basic_simulation_.set_limit_sensor(true); |
| 206 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 207 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 208 | |
| 209 | basic_simulation_.VerifyResults(0.0, false); |
| 210 | } |
| 211 | |
| 212 | // Tests that the control loop handles things properly if disabled. |
| 213 | TEST_F(BasicControlLoopTest, Disabled) { |
| 214 | basic_simulation_.set_limit_sensor(true); |
| 215 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 216 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 217 | auto builder = goal_sender_.MakeBuilder(); |
| 218 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 219 | goal_builder.add_intake(true); |
| 220 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 221 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 222 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 223 | SetEnabled(false); |
| 224 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 225 | |
| 226 | basic_simulation_.VerifyResults(0.0, false); |
| 227 | } |
| 228 | |
| 229 | } // namespace testing |
| 230 | } // namespace codelab |
| 231 | } // namespace frc971 |