blob: f76450ed75bb69e7ef4ae6b187127ab7d0e7d028 [file] [log] [blame]
Austin Schuhd5ccb862017-03-11 22:06:36 -08001#ifndef Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_
2#define Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_
3
4#include <array>
5#include <chrono>
6#include <memory>
7#include <utility>
8
9#include "Eigen/Dense"
10
11#include "frc971/constants.h"
12#include "frc971/control_loops/profiled_subsystem.h"
13#include "frc971/control_loops/state_feedback_loop.h"
14#include "y2017/constants.h"
15#include "y2017/control_loops/superstructure/column/column_zeroing.h"
16#include "y2017/control_loops/superstructure/intake/intake.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "y2017/control_loops/superstructure/superstructure_position_generated.h"
18#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuhac76bb32017-03-22 22:34:26 -070019#include "y2017/control_loops/superstructure/vision_time_adjuster.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "y2017/vision/vision_generated.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -080021
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080022namespace y2017::control_loops::superstructure::column {
Austin Schuhd5ccb862017-03-11 22:06:36 -080023
24class ColumnProfiledSubsystem
25 : public ::frc971::control_loops::ProfiledSubsystem<
26 6, 1, ColumnZeroingEstimator, 2, 2> {
27 public:
28 ColumnProfiledSubsystem(
29 ::std::unique_ptr<
30 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>>
31 loop,
32 const ::y2017::constants::Values::Column &zeroing_constants,
33 const ::frc971::constants::Range &range, double default_angular_velocity,
34 double default_angular_acceleration);
35
36 // Updates our estimator with the latest position.
37 void Correct(const ColumnPosition &position);
38 // Runs the controller and profile generator for a cycle.
39 void Update(bool disabled);
40
41 // Fills out the ProfiledJointStatus structure with the current state.
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 void PopulateTurretStatus(
43 TurretProfiledSubsystemStatus::Builder *status_builder,
44 flatbuffers::Offset<ColumnZeroingEstimator::State>
45 estimator_state_offset);
Austin Schuhd5ccb862017-03-11 22:06:36 -080046
47 // Forces the current goal to the provided goal, bypassing the profiler.
48 void ForceGoal(double goal_velocity, double goal);
49 // Sets the unprofiled goal. The profiler will generate a profile to go to
50 // this goal.
51 void set_indexer_unprofiled_goal(double goal_velocity);
52 void set_turret_unprofiled_goal(double unprofiled_goal);
53 void set_unprofiled_goal(double goal_velocity, double unprofiled_goal);
54 // Limits our profiles to a max velocity and acceleration for proper motion.
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters);
Austin Schuhd5ccb862017-03-11 22:06:36 -080056 void AdjustProfile(double max_angular_velocity,
57 double max_angular_acceleration);
58
59 // Returns true if we have exceeded any hard limits.
60 bool CheckHardLimits();
61
62 // Returns the requested voltage.
63 double indexer_voltage() const { return loop_->U(0, 0); }
64 double uncapped_indexer_voltage() const { return loop_->U_uncapped(0, 0); }
65 double turret_voltage() const { return loop_->U(1, 0); }
66
67 // Returns the current Y.
68 const ::Eigen::Matrix<double, 2, 1> Y() const { return Y_; }
69 double Y(int i, int j) const { return Y_(i, j); }
70
71 // Returns the current indexer position.
72 double indexer_position() const { return Y_(0, 0); }
73
74 bool saturated() const { return saturated_; }
75
76 // Returns the current turret position.
77 double turret_position() const { return Y_(1, 0) + Y_(0, 0); }
78
79 // For testing:
80 // Triggers an estimator error.
81 void TriggerEstimatorError() { estimators_[0].TriggerError(); }
82
83 const ::frc971::constants::Range &range() const { return range_; }
84
85 bool IsIndexerStuck() const;
86 double IndexerStuckVoltage() const;
87 void PartialIndexerReset();
88 void PartialTurretReset();
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 void PopulateIndexerStatus(IndexerStatus::Builder *status_builder);
Austin Schuhd5ccb862017-03-11 22:06:36 -080090
91 void AddOffset(double indexer_offset_delta, double turret_offset_delta);
92
93 protected:
94 // Limits the provided goal to the soft limits. Prints "name" when it fails
95 // to aid debugging.
96 virtual void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal);
97
98 private:
99 void UpdateOffset(double indexer_offset, double turret_offset);
100
101 ::std::unique_ptr<StateFeedbackLoop<6, 2, 2>> stuck_indexer_detector_;
102
103 // History array for calculating a filtered angular velocity.
104 static constexpr int kHistoryLength = 5;
105 ::std::array<double, kHistoryLength> indexer_history_;
106 ptrdiff_t indexer_history_position_ = 0;
107
108 double indexer_error_ = 0.0;
109 double indexer_dt_velocity_ = 0.0;
110 double indexer_last_position_ = 0.0;
111 double indexer_average_angular_velocity_ = 0.0;
112 double indexer_position_error_ = 0.0;
113 bool indexer_ready_ = false;
114
115 bool saturated_ = false;
116
117 Eigen::Matrix<double, 6, 1> X_hat_current_;
118 Eigen::Matrix<double, 6, 1> stuck_indexer_X_hat_current_;
119
120 aos::util::TrapezoidProfile profile_;
121
122 // Current measurement.
123 Eigen::Matrix<double, 2, 1> Y_;
124 // Current offset. Y_ = offset_ + raw_sensor;
125 Eigen::Matrix<double, 2, 1> offset_;
126
127 const ::frc971::constants::Range range_;
128
129 const double default_velocity_;
130 const double default_acceleration_;
131
132 double turret_last_position_ = 0;
133};
134
Austin Schuhd5ccb862017-03-11 22:06:36 -0800135class Column {
136 public:
Austin Schuhb6c5c852019-05-19 20:13:31 -0700137 Column(::aos::EventLoop *event_loop);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800138 double goal(int row, int col) const {
139 return profiled_subsystem_.goal(row, col);
140 }
141
142 double turret_position() const {
143 return profiled_subsystem_.turret_position();
144 }
145
146 void set_freeze(bool freeze) { freeze_ = freeze; }
147
148 // The zeroing and operating voltages.
149 static constexpr double kZeroingVoltage = 5.0;
150 static constexpr double kOperatingVoltage = 12.0;
151 static constexpr double kIntakeZeroingMinDistance = 0.08;
152 static constexpr double kIntakeTolerance = 0.005;
153 static constexpr double kStuckZeroingTrackingError = 0.02;
Austin Schuh3ae47432017-04-16 19:15:46 -0700154 static constexpr double kTurretMin = -0.1;
155 static constexpr double kTurretMax = M_PI / 2.0 + 0.1;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800156
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 std::pair<flatbuffers::Offset<IndexerStatus>,
158 flatbuffers::Offset<TurretProfiledSubsystemStatus>>
159 Iterate(::aos::monotonic_clock::time_point monotonic_now,
160 const IndexerGoalT *unsafe_indexer_goal,
161 const TurretGoal *unsafe_turret_goal, const ColumnPosition *position,
162 const vision::VisionStatus *vision_status, double *indexer_output,
163 double *turret_output, flatbuffers::FlatBufferBuilder *fbb,
164 // IndexerStatus *indexer_status,
165 // TurretProfiledSubsystemStatus *turret_status,
166 intake::Intake *intake);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800167
168 void Reset();
169
170 enum class State : int32_t {
171 UNINITIALIZED = 0,
172 ZEROING_UNINITIALIZED = 1,
173 ZEROING_POSITIVE = 2,
174 ZEROING_NEGATIVE = 3,
175 RUNNING = 4,
176 ESTOP = 5,
177 };
178
179 enum class IndexerState : int32_t {
180 // The system is running correctly, no stuck condition detected.
181 RUNNING = 0,
182 // The system is reversing to unjam.
183 REVERSING = 1
184 };
185
186 State state() const { return state_; }
187 IndexerState indexer_state() const { return indexer_state_; }
188
189 private:
190 State state_ = State::UNINITIALIZED;
191
192 IndexerState indexer_state_ = IndexerState::RUNNING;
193
194 bool freeze_ = false;
195
Austin Schuhac76bb32017-03-22 22:34:26 -0700196 VisionTimeAdjuster vision_time_adjuster_;
197
Austin Schuhd5ccb862017-03-11 22:06:36 -0800198 // The last time that we transitioned from RUNNING to REVERSING or the
199 // reverse. Used to implement the timeouts.
200 ::aos::monotonic_clock::time_point last_transition_time_ =
201 ::aos::monotonic_clock::min_time;
202
203 ColumnProfiledSubsystem profiled_subsystem_;
Austin Schuh25db1262017-04-05 19:39:55 -0700204
205 const double vision_error_;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800206};
207
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800208} // namespace y2017::control_loops::superstructure::column
Austin Schuhd5ccb862017-03-11 22:06:36 -0800209
210#endif // Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_