Niko Sohmers | e69ee2d | 2022-09-28 19:52:27 -0700 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <memory> |
| 3 | |
| 4 | #include "frc971/constants.h" |
| 5 | #include "frc971/control_loops/capped_test_plant.h" |
| 6 | #include "frc971/control_loops/control_loop_test.h" |
| 7 | #include "frc971/control_loops/position_sensor_sim.h" |
| 8 | |
| 9 | namespace frc971::control_loops { |
| 10 | |
| 11 | template <typename SubsystemStatus, typename SubsystemState, |
| 12 | typename SubsystemConstants> |
| 13 | |
| 14 | // Class used for simulating a single degree of freedom subsystem in test. |
| 15 | // Simulates the state of the subsystem as a voltage is applied |
| 16 | class SubsystemSimulator { |
| 17 | public: |
| 18 | SubsystemSimulator(CappedTestPlant *plant, PositionSensorSimulator encoder, |
| 19 | const SubsystemConstants subsystem_constants, |
| 20 | const frc971::constants::Range range, |
| 21 | double encoder_offset, const std::chrono::nanoseconds dt) |
| 22 | : plant_(plant), |
| 23 | encoder_(encoder), |
| 24 | subsystem_constants_(subsystem_constants), |
| 25 | range_(range), |
| 26 | encoder_offset_(encoder_offset), |
| 27 | dt_(dt) {} |
| 28 | |
| 29 | void InitializePosition(double start_pos) { |
| 30 | plant_->mutable_X(0, 0) = start_pos; |
| 31 | plant_->mutable_X(1, 0) = 0.0; |
| 32 | |
| 33 | encoder_.Initialize(start_pos, 0.0, encoder_offset_); |
| 34 | } |
| 35 | |
| 36 | // Simulates the superstructure for a single timestep. |
| 37 | void Simulate(double voltage, const SubsystemStatus *status) { |
| 38 | double last_velocity = plant_->X(1, 0); |
| 39 | |
| 40 | const double voltage_check = |
| 41 | (static_cast<SubsystemState>(status->state()) == |
| 42 | SubsystemState::RUNNING) |
| 43 | ? subsystem_constants_.subsystem_params.operating_voltage |
| 44 | : subsystem_constants_.subsystem_params.zeroing_voltage; |
| 45 | |
| 46 | EXPECT_NEAR(voltage, 0.0, voltage_check); |
| 47 | |
| 48 | ::Eigen::Matrix<double, 1, 1> U; |
| 49 | U << voltage + plant_->voltage_offset(); |
| 50 | plant_->Update(U); |
| 51 | |
| 52 | const double position = plant_->Y(0, 0); |
| 53 | |
| 54 | encoder_.MoveTo(position); |
| 55 | |
| 56 | EXPECT_GE(position, range_.lower_hard); |
| 57 | EXPECT_LE(position, range_.upper_hard); |
| 58 | |
| 59 | const double loop_time = ::aos::time::DurationInSeconds(dt_); |
| 60 | |
| 61 | const double velocity = plant_->X(1, 0); |
| 62 | const double acceleration = (velocity - last_velocity) / loop_time; |
| 63 | |
| 64 | EXPECT_GE(peak_acceleration_, acceleration); |
| 65 | EXPECT_LE(-peak_acceleration_, acceleration); |
| 66 | EXPECT_GE(peak_velocity_, velocity); |
| 67 | EXPECT_LE(-peak_velocity_, velocity); |
| 68 | } |
| 69 | |
| 70 | void set_peak_acceleration(double value) { peak_acceleration_ = value; } |
| 71 | void set_peak_velocity(double value) { peak_velocity_ = value; } |
| 72 | |
| 73 | void set_controller_index(size_t index) { plant_->set_index(index); } |
| 74 | |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame^] | 75 | double voltage_offset() const { return plant_->voltage_offset(); } |
| 76 | void set_voltage_offset(double voltage_offset) { |
| 77 | plant_->set_voltage_offset(voltage_offset); |
| 78 | } |
| 79 | |
Niko Sohmers | e69ee2d | 2022-09-28 19:52:27 -0700 | [diff] [blame] | 80 | PositionSensorSimulator *encoder() { return &encoder_; } |
| 81 | |
| 82 | private: |
| 83 | std::unique_ptr<CappedTestPlant> plant_; |
| 84 | PositionSensorSimulator encoder_; |
| 85 | const SubsystemConstants subsystem_constants_; |
| 86 | const frc971::constants::Range range_; |
| 87 | |
| 88 | double encoder_offset_ = 0.0; |
| 89 | |
| 90 | double peak_velocity_ = std::numeric_limits<double>::infinity(); |
| 91 | double peak_acceleration_ = std::numeric_limits<double>::infinity(); |
| 92 | |
| 93 | const std::chrono::nanoseconds dt_; |
| 94 | }; |
| 95 | |
Austin Schuh | bdabc70 | 2023-02-24 16:21:07 -0800 | [diff] [blame^] | 96 | } // namespace frc971::control_loops |