blob: 3dc44f7dd633b516b45c028607cc9b8e8b78c171 [file] [log] [blame]
Niko Sohmerse69ee2d2022-09-28 19:52:27 -07001#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
9namespace frc971::control_loops {
10
11template <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
16class 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 Schuhbdabc702023-02-24 16:21:07 -080075 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 Sohmerse69ee2d2022-09-28 19:52:27 -070080 PositionSensorSimulator *encoder() { return &encoder_; }
81
Niko Sohmersc4d2c502024-02-19 19:35:35 -080082 double position() const { return plant_->X(0, 0); }
83 double velocity() const { return plant_->X(1, 0); }
84
Niko Sohmerse69ee2d2022-09-28 19:52:27 -070085 private:
86 std::unique_ptr<CappedTestPlant> plant_;
87 PositionSensorSimulator encoder_;
88 const SubsystemConstants subsystem_constants_;
89 const frc971::constants::Range range_;
90
91 double encoder_offset_ = 0.0;
92
93 double peak_velocity_ = std::numeric_limits<double>::infinity();
94 double peak_acceleration_ = std::numeric_limits<double>::infinity();
95
96 const std::chrono::nanoseconds dt_;
97};
98
Austin Schuhbdabc702023-02-24 16:21:07 -080099} // namespace frc971::control_loops