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" |
| 93 | " \"type\": \"aos.RobotState\"\n" |
| 94 | " },\n" |
| 95 | " {\n" |
| 96 | " \"name\": \"/codelab\",\n" |
| 97 | " \"type\": \"frc971.codelab.Goal\"\n" |
| 98 | " },\n" |
| 99 | " {\n" |
| 100 | " \"name\": \"/codelab\",\n" |
| 101 | " \"type\": \"frc971.codelab.Output\"\n" |
| 102 | " },\n" |
| 103 | " {\n" |
| 104 | " \"name\": \"/codelab\",\n" |
| 105 | " \"type\": \"frc971.codelab.Status\"\n" |
| 106 | " },\n" |
| 107 | " {\n" |
| 108 | " \"name\": \"/codelab\",\n" |
| 109 | " \"type\": \"frc971.codelab.Position\"\n" |
| 110 | " }\n" |
| 111 | " ]\n" |
| 112 | "}\n", |
| 113 | chrono::microseconds(5050)), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 114 | test_event_loop_(MakeEventLoop()), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 115 | goal_sender_(test_event_loop_->MakeSender<Goal>("/codelab")), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 116 | |
| 117 | basic_event_loop_(MakeEventLoop()), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 118 | basic_(basic_event_loop_.get(), "/codelab"), |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 119 | |
| 120 | basic_simulation_event_loop_(MakeEventLoop()), |
| 121 | basic_simulation_(basic_simulation_event_loop_.get(), dt()) { |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 122 | set_team_id(control_loops::testing::kTeamNumber); |
| 123 | } |
| 124 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 125 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 126 | ::aos::Sender<Goal> goal_sender_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 127 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 128 | ::std::unique_ptr<::aos::EventLoop> basic_event_loop_; |
| 129 | Basic basic_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 130 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 131 | ::std::unique_ptr<::aos::EventLoop> basic_simulation_event_loop_; |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 132 | BasicSimulation basic_simulation_; |
| 133 | }; |
| 134 | |
| 135 | // Tests that when the motor has finished intaking it stops. |
| 136 | TEST_F(BasicControlLoopTest, IntakeLimitTransitionsToTrue) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 137 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 138 | auto builder = goal_sender_.MakeBuilder(); |
| 139 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 140 | goal_builder.add_intake(true); |
| 141 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 144 | basic_simulation_.set_limit_sensor(false); |
| 145 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 146 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 147 | |
| 148 | basic_simulation_.VerifyResults(12.0, false); |
| 149 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 150 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 151 | auto builder = goal_sender_.MakeBuilder(); |
| 152 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 153 | goal_builder.add_intake(true); |
| 154 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 155 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 156 | basic_simulation_.set_limit_sensor(true); |
| 157 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 158 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 159 | |
| 160 | basic_simulation_.VerifyResults(0.0, true); |
| 161 | } |
| 162 | |
| 163 | // Tests that the intake goes on if the sensor is not pressed |
| 164 | // and intake is requested. |
| 165 | TEST_F(BasicControlLoopTest, IntakeLimitNotSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 166 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 167 | auto builder = goal_sender_.MakeBuilder(); |
| 168 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 169 | goal_builder.add_intake(true); |
| 170 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 171 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 172 | basic_simulation_.set_limit_sensor(false); |
| 173 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 174 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 175 | |
| 176 | basic_simulation_.VerifyResults(12.0, false); |
| 177 | } |
| 178 | |
| 179 | // Tests that the intake is off if no intake is requested, |
| 180 | // even if the limit sensor is off. |
| 181 | TEST_F(BasicControlLoopTest, NoIntakeLimitNotSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 182 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 183 | auto builder = goal_sender_.MakeBuilder(); |
| 184 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 185 | goal_builder.add_intake(false); |
| 186 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 187 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 188 | basic_simulation_.set_limit_sensor(false); |
| 189 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 190 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 191 | |
| 192 | basic_simulation_.VerifyResults(0.0, false); |
| 193 | } |
| 194 | |
| 195 | // Tests that the intake is off even if the limit sensor |
| 196 | // is pressed and intake is requested. |
| 197 | TEST_F(BasicControlLoopTest, IntakeLimitSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 198 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 199 | auto builder = goal_sender_.MakeBuilder(); |
| 200 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 201 | goal_builder.add_intake(true); |
| 202 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 203 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 204 | basic_simulation_.set_limit_sensor(true); |
| 205 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 206 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 207 | |
| 208 | basic_simulation_.VerifyResults(0.0, true); |
| 209 | } |
| 210 | |
| 211 | // Tests that the intake is off if no intake is requested, |
| 212 | TEST_F(BasicControlLoopTest, NoIntakeLimitSet) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 213 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 214 | auto builder = goal_sender_.MakeBuilder(); |
| 215 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 216 | goal_builder.add_intake(false); |
| 217 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 218 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 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 no goal is set. |
| 227 | TEST_F(BasicControlLoopTest, NoGoalSet) { |
| 228 | basic_simulation_.set_limit_sensor(true); |
| 229 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 230 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 231 | |
| 232 | basic_simulation_.VerifyResults(0.0, false); |
| 233 | } |
| 234 | |
| 235 | // Tests that the control loop handles things properly if disabled. |
| 236 | TEST_F(BasicControlLoopTest, Disabled) { |
| 237 | basic_simulation_.set_limit_sensor(true); |
| 238 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 239 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 240 | auto builder = goal_sender_.MakeBuilder(); |
| 241 | Goal::Builder goal_builder = builder.MakeBuilder<Goal>(); |
| 242 | goal_builder.add_intake(true); |
| 243 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 244 | } |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 245 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 246 | SetEnabled(false); |
| 247 | RunFor(dt() * 2); |
Parker Schuh | 18dbbb4 | 2017-10-18 21:45:33 -0700 | [diff] [blame] | 248 | |
| 249 | basic_simulation_.VerifyResults(0.0, false); |
| 250 | } |
| 251 | |
| 252 | } // namespace testing |
| 253 | } // namespace codelab |
| 254 | } // namespace frc971 |