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