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