Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame^] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | #include "aos/common/queue.h" |
| 7 | #include "aos/common/queue_testutils.h" |
| 8 | #include "frc971/control_loops/wrist_motor.q.h" |
| 9 | #include "frc971/control_loops/wrist.h" |
| 10 | |
| 11 | |
| 12 | using ::aos::time::Time; |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | namespace testing { |
| 17 | |
| 18 | class WristTest : public ::testing::Test { |
| 19 | protected: |
| 20 | ::aos::common::testing::GlobalCoreInstance my_core; |
| 21 | |
| 22 | // Create a new instance of the test queue so that it invalidates the queue |
| 23 | // that it points to. Otherwise, we will have a pointer to shared memory that |
| 24 | // is no longer valid. |
| 25 | WristLoop my_wrist_loop_; |
| 26 | |
| 27 | WristMotor wrist_motor_; |
| 28 | ::std::unique_ptr<StateFeedbackPlant<2, 1, 1>> wrist_plant_; |
| 29 | |
| 30 | WristTest() : my_wrist_loop_(".frc971.control_loops.wrist", |
| 31 | 0x1a7b7094, ".frc971.control_loops.wrist.goal", |
| 32 | ".frc971.control_loops.wrist.position", |
| 33 | ".frc971.control_loops.wrist.output", |
| 34 | ".frc971.control_loops.wrist.status"), |
| 35 | wrist_motor_(&my_wrist_loop_), |
| 36 | wrist_plant_(new StateFeedbackPlant<2, 1, 1>(MakeWristPlant())) { |
| 37 | // Flush the robot state queue so we can recreate shared memory for this |
| 38 | // test. |
| 39 | ::aos::robot_state.Clear(); |
| 40 | ::aos::robot_state.MakeWithBuilder().enabled(true) |
| 41 | .autonomous(false) |
| 42 | .team_id(971).Send(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | // Tests that we can send a message to another thread and it blocking receives |
| 48 | // it at the correct time. |
| 49 | TEST_F(WristTest, Stable) { |
| 50 | my_wrist_loop_.goal.MakeWithBuilder().goal(0.0).Send(); |
| 51 | for (int i = 0; i < 1000; ++i) { |
| 52 | my_wrist_loop_.position.MakeWithBuilder() |
| 53 | .pos(wrist_plant_->Y(0, 0)) |
| 54 | .hall_effect(false) |
| 55 | .calibration(0.0).Send(); |
| 56 | |
| 57 | wrist_motor_.Iterate(); |
| 58 | |
| 59 | EXPECT_TRUE(my_wrist_loop_.output.FetchLatest()); |
| 60 | wrist_plant_->U << my_wrist_loop_.output->voltage; |
| 61 | wrist_plant_->Update(); |
| 62 | } |
| 63 | my_wrist_loop_.position.FetchLatest(); |
| 64 | EXPECT_FLOAT_EQ(0.0, my_wrist_loop_.position->pos); |
| 65 | } |
| 66 | |
| 67 | } // namespace testing |
| 68 | } // namespace control_loops |
| 69 | } // namespace frc971 |