blob: b952a78fcc73772a4b7f48b8adea50663581b584 [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"
17#include "y2017/control_loops/superstructure/superstructure.q.h"
Austin Schuhac76bb32017-03-22 22:34:26 -070018#include "y2017/control_loops/superstructure/vision_time_adjuster.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -080019
20namespace y2017 {
21namespace control_loops {
22namespace superstructure {
23namespace column {
24
25class ColumnProfiledSubsystem
26 : public ::frc971::control_loops::ProfiledSubsystem<
27 6, 1, ColumnZeroingEstimator, 2, 2> {
28 public:
29 ColumnProfiledSubsystem(
30 ::std::unique_ptr<
31 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>>
32 loop,
33 const ::y2017::constants::Values::Column &zeroing_constants,
34 const ::frc971::constants::Range &range, double default_angular_velocity,
35 double default_angular_acceleration);
36
37 // Updates our estimator with the latest position.
38 void Correct(const ColumnPosition &position);
39 // Runs the controller and profile generator for a cycle.
40 void Update(bool disabled);
41
42 // Fills out the ProfiledJointStatus structure with the current state.
43 template <class StatusType>
44 void PopulateTurretStatus(StatusType *status);
45
46 // Forces the current goal to the provided goal, bypassing the profiler.
47 void ForceGoal(double goal_velocity, double goal);
48 // Sets the unprofiled goal. The profiler will generate a profile to go to
49 // this goal.
50 void set_indexer_unprofiled_goal(double goal_velocity);
51 void set_turret_unprofiled_goal(double unprofiled_goal);
52 void set_unprofiled_goal(double goal_velocity, double unprofiled_goal);
53 // Limits our profiles to a max velocity and acceleration for proper motion.
54 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
55 void AdjustProfile(double max_angular_velocity,
56 double max_angular_acceleration);
57
58 // Returns true if we have exceeded any hard limits.
59 bool CheckHardLimits();
60
61 // Returns the requested voltage.
62 double indexer_voltage() const { return loop_->U(0, 0); }
63 double uncapped_indexer_voltage() const { return loop_->U_uncapped(0, 0); }
64 double turret_voltage() const { return loop_->U(1, 0); }
65
66 // Returns the current Y.
67 const ::Eigen::Matrix<double, 2, 1> Y() const { return Y_; }
68 double Y(int i, int j) const { return Y_(i, j); }
69
70 // Returns the current indexer position.
71 double indexer_position() const { return Y_(0, 0); }
72
73 bool saturated() const { return saturated_; }
74
75 // Returns the current turret position.
76 double turret_position() const { return Y_(1, 0) + Y_(0, 0); }
77
78 // For testing:
79 // Triggers an estimator error.
80 void TriggerEstimatorError() { estimators_[0].TriggerError(); }
81
82 const ::frc971::constants::Range &range() const { return range_; }
83
84 bool IsIndexerStuck() const;
85 double IndexerStuckVoltage() const;
86 void PartialIndexerReset();
87 void PartialTurretReset();
88 void PopulateIndexerStatus(IndexerStatus *status);
89
90 void AddOffset(double indexer_offset_delta, double turret_offset_delta);
91
92 protected:
93 // Limits the provided goal to the soft limits. Prints "name" when it fails
94 // to aid debugging.
95 virtual void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal);
96
97 private:
98 void UpdateOffset(double indexer_offset, double turret_offset);
99
100 ::std::unique_ptr<StateFeedbackLoop<6, 2, 2>> stuck_indexer_detector_;
101
102 // History array for calculating a filtered angular velocity.
103 static constexpr int kHistoryLength = 5;
104 ::std::array<double, kHistoryLength> indexer_history_;
105 ptrdiff_t indexer_history_position_ = 0;
106
107 double indexer_error_ = 0.0;
108 double indexer_dt_velocity_ = 0.0;
109 double indexer_last_position_ = 0.0;
110 double indexer_average_angular_velocity_ = 0.0;
111 double indexer_position_error_ = 0.0;
112 bool indexer_ready_ = false;
113
114 bool saturated_ = false;
115
116 Eigen::Matrix<double, 6, 1> X_hat_current_;
117 Eigen::Matrix<double, 6, 1> stuck_indexer_X_hat_current_;
118
119 aos::util::TrapezoidProfile profile_;
120
121 // Current measurement.
122 Eigen::Matrix<double, 2, 1> Y_;
123 // Current offset. Y_ = offset_ + raw_sensor;
124 Eigen::Matrix<double, 2, 1> offset_;
125
126 const ::frc971::constants::Range range_;
127
128 const double default_velocity_;
129 const double default_acceleration_;
130
131 double turret_last_position_ = 0;
132};
133
134template <typename StatusType>
135void ColumnProfiledSubsystem::PopulateTurretStatus(StatusType *status) {
136 status->zeroed = zeroed();
137 status->state = -1;
138 // We don't know, so default to the bad case.
139 status->estopped = true;
140
141 status->position = X_hat(2, 0);
142 status->velocity = X_hat(3, 0);
143 status->goal_position = goal(2, 0);
144 status->goal_velocity = goal(3, 0);
145 status->unprofiled_goal_position = unprofiled_goal(2, 0);
146 status->unprofiled_goal_velocity = unprofiled_goal(3, 0);
147 status->voltage_error = X_hat(5, 0);
148 status->calculated_velocity =
149 (turret_position() - turret_last_position_) /
150 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
151 ::aos::controls::kLoopFrequency)
152 .count();
153
154 status->estimator_state = EstimatorState(0);
155
156 Eigen::Matrix<double, 6, 1> error = controller().error();
157 status->position_power = controller().controller().K(0, 0) * error(0, 0);
158 status->velocity_power = controller().controller().K(0, 1) * error(1, 0);
159}
160
161class Column {
162 public:
163 Column();
164 double goal(int row, int col) const {
165 return profiled_subsystem_.goal(row, col);
166 }
167
168 double turret_position() const {
169 return profiled_subsystem_.turret_position();
170 }
171
172 void set_freeze(bool freeze) { freeze_ = freeze; }
173
174 // The zeroing and operating voltages.
175 static constexpr double kZeroingVoltage = 5.0;
176 static constexpr double kOperatingVoltage = 12.0;
177 static constexpr double kIntakeZeroingMinDistance = 0.08;
178 static constexpr double kIntakeTolerance = 0.005;
179 static constexpr double kStuckZeroingTrackingError = 0.02;
Austin Schuhac76bb32017-03-22 22:34:26 -0700180 static constexpr double kTurretNearZero = M_PI / 2.0;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800181
182 void Iterate(const control_loops::IndexerGoal *unsafe_indexer_goal,
183 const control_loops::TurretGoal *unsafe_turret_goal,
184 const ColumnPosition *position, double *indexer_output,
185 double *turret_output, IndexerStatus *indexer_status,
186 TurretProfiledSubsystemStatus *turret_status,
187 intake::Intake *intake);
188
189 void Reset();
190
191 enum class State : int32_t {
192 UNINITIALIZED = 0,
193 ZEROING_UNINITIALIZED = 1,
194 ZEROING_POSITIVE = 2,
195 ZEROING_NEGATIVE = 3,
196 RUNNING = 4,
197 ESTOP = 5,
198 };
199
200 enum class IndexerState : int32_t {
201 // The system is running correctly, no stuck condition detected.
202 RUNNING = 0,
203 // The system is reversing to unjam.
204 REVERSING = 1
205 };
206
207 State state() const { return state_; }
208 IndexerState indexer_state() const { return indexer_state_; }
209
210 private:
211 State state_ = State::UNINITIALIZED;
212
213 IndexerState indexer_state_ = IndexerState::RUNNING;
214
215 bool freeze_ = false;
216
Austin Schuhac76bb32017-03-22 22:34:26 -0700217 VisionTimeAdjuster vision_time_adjuster_;
218
Austin Schuhd5ccb862017-03-11 22:06:36 -0800219 // The last time that we transitioned from RUNNING to REVERSING or the
220 // reverse. Used to implement the timeouts.
221 ::aos::monotonic_clock::time_point last_transition_time_ =
222 ::aos::monotonic_clock::min_time;
223
224 ColumnProfiledSubsystem profiled_subsystem_;
225};
226
227} // namespace column
228} // namespace superstructure
229} // namespace control_loops
230} // namespace y2017
231
232#endif // Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_