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