blob: 32c8b1761aacfab9cae59de4699b6b3b0daa6658 [file] [log] [blame]
Austin Schuh10c2d112016-02-14 13:42:28 -08001#include "y2016/control_loops/superstructure/superstructure_controls.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/logging/logging.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08004#include "y2016/constants.h"
James Kuszmaul61750662021-06-21 21:32:33 -07005#include "y2016/control_loops/superstructure/integral_arm_plant.h"
6#include "y2016/control_loops/superstructure/integral_intake_plant.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08007
Stephan Pleinesf63bde82024-01-13 15:59:33 -08008namespace y2016::control_loops::superstructure {
Austin Schuh10c2d112016-02-14 13:42:28 -08009
10using ::frc971::PotAndIndexPosition;
Austin Schuh10c2d112016-02-14 13:42:28 -080011
12namespace {
13double UseUnlessZero(double target_value, double default_value) {
14 if (target_value != 0.0) {
15 return target_value;
16 } else {
17 return default_value;
18 }
19}
Austin Schuhcb60e292017-01-01 16:07:31 -080020
21enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
22
Austin Schuh10c2d112016-02-14 13:42:28 -080023} // namespace
24
Austin Schuh10c2d112016-02-14 13:42:28 -080025// Intake
26Intake::Intake()
Brian Silvermanab0b6772017-02-05 16:16:21 -080027 : ::frc971::control_loops::SingleDOFProfiledSubsystem<>(
Austin Schuhcb60e292017-01-01 16:07:31 -080028 ::std::unique_ptr<
29 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
30 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
31 3, 1, 1>(::y2016::control_loops::superstructure::
Austin Schuh473a5652017-02-05 01:30:42 -080032 MakeIntegralIntakeLoop())),
33 constants::GetValues().intake.zeroing,
34 constants::Values::kIntakeRange, 10.0, 10.0) {}
Austin Schuh10c2d112016-02-14 13:42:28 -080035
36Arm::Arm()
Austin Schuh473a5652017-02-05 01:30:42 -080037 : ProfiledSubsystem(
38 ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
39 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
Campbell Crowley36e93e92017-12-23 14:21:43 -080040 {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
41 constants::GetValues().shoulder.zeroing),
42 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
43 constants::GetValues().wrist.zeroing)}}),
James Kuszmaul61750662021-06-21 21:32:33 -070044 shoulder_profile_(::frc971::controls::kLoopFrequency),
45 wrist_profile_(::frc971::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080046 Y_.setZero();
47 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080048 AdjustProfile(0.0, 0.0, 0.0, 0.0);
49}
50
51void Arm::UpdateWristOffset(double offset) {
52 const double doffset = offset - offset_(1, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070053 AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0),
54 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080055
56 loop_->mutable_X_hat()(2, 0) += doffset;
57 Y_(1, 0) += doffset;
58 loop_->mutable_R(2, 0) += doffset;
59 loop_->mutable_next_R(2, 0) += doffset;
60 unprofiled_goal_(2, 0) += doffset;
61
62 wrist_profile_.MoveGoal(doffset);
63 offset_(1, 0) = offset;
64
65 CapGoal("R", &loop_->mutable_R());
66 CapGoal("unprofiled R", &loop_->mutable_next_R());
67}
68
69void Arm::UpdateShoulderOffset(double offset) {
70 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070071 AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0),
72 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080073
74 loop_->mutable_X_hat()(0, 0) += doffset;
75 loop_->mutable_X_hat()(2, 0) += doffset;
76 Y_(0, 0) += doffset;
77 loop_->mutable_R(0, 0) += doffset;
78 loop_->mutable_R(2, 0) += doffset;
79 loop_->mutable_next_R(0, 0) += doffset;
80 loop_->mutable_next_R(2, 0) += doffset;
81 unprofiled_goal_(0, 0) += doffset;
82 unprofiled_goal_(2, 0) += doffset;
83
84 shoulder_profile_.MoveGoal(doffset);
85 wrist_profile_.MoveGoal(doffset);
86 offset_(0, 0) = offset;
87
88 CapGoal("R", &loop_->mutable_R());
89 CapGoal("unprofiled R", &loop_->mutable_next_R());
90}
91
92// TODO(austin): Handle zeroing errors.
93
Alex Perrycb7da4b2019-08-28 19:35:56 -070094void Arm::Correct(const PotAndIndexPosition *position_shoulder,
95 const PotAndIndexPosition *position_wrist) {
96 estimators_[kShoulderIndex].UpdateEstimate(*position_shoulder);
97 estimators_[kWristIndex].UpdateEstimate(*position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -080098
Diana Vandenberge2843c62016-02-13 17:44:20 -080099 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800100 if (estimators_[kShoulderIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700101 AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n");
Austin Schuh2669ece2022-03-11 18:30:57 -0800102 X_hat_ = loop_->X_hat();
Diana Vandenberge2843c62016-02-13 17:44:20 -0800103 return;
104 }
Austin Schuh473a5652017-02-05 01:30:42 -0800105 if (estimators_[kWristIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700106 AOS_LOG(ERROR, "zeroing error with wrist_estimator\n");
Austin Schuh2669ece2022-03-11 18:30:57 -0800107 X_hat_ = loop_->X_hat();
Diana Vandenberge2843c62016-02-13 17:44:20 -0800108 return;
109 }
110
Austin Schuh10c2d112016-02-14 13:42:28 -0800111 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800112 if (estimators_[kShoulderIndex].offset_ready() &&
113 estimators_[kWristIndex].offset_ready()) {
114 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
115 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800116 initialized_ = true;
117 }
118 }
119
Austin Schuh473a5652017-02-05 01:30:42 -0800120 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
121 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800122 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800123 }
Austin Schuh473a5652017-02-05 01:30:42 -0800124 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
125 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800126 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800127 }
128
129 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 Y_ << position_shoulder->encoder(), position_wrist->encoder();
Austin Schuh10c2d112016-02-14 13:42:28 -0800131 Y_ += offset_;
132 loop_->Correct(Y_);
Austin Schuh2669ece2022-03-11 18:30:57 -0800133 X_hat_ = loop_->X_hat();
Austin Schuh10c2d112016-02-14 13:42:28 -0800134 }
135}
136
137void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
138 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800139
Austin Schuh3b0f3642016-02-14 21:04:26 -0800140 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700141 AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name,
142 (*goal)(0, 0), constants::Values::kShoulderRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000143 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800144 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800145 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700146 AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name,
147 (*goal)(0, 0), constants::Values::kShoulderRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000148 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800149 }
150
151 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
152
Austin Schuh3b0f3642016-02-14 21:04:26 -0800153 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700154 AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
155 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000156 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800157 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800158 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700159 AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
160 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000161 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800162 }
163}
164
165void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
166 set_unprofiled_goal(goal_shoulder, goal_wrist);
167 loop_->mutable_R() = unprofiled_goal_;
168 loop_->mutable_next_R() = loop_->R();
169
170 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
171 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
172}
173
174void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
175 double unprofiled_goal_wrist) {
176 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
177 0.0, 0.0;
178 CapGoal("unprofiled R", &unprofiled_goal_);
179}
180
181void Arm::AdjustProfile(double max_angular_velocity_shoulder,
182 double max_angular_acceleration_shoulder,
183 double max_angular_velocity_wrist,
184 double max_angular_acceleration_wrist) {
185 shoulder_profile_.set_maximum_velocity(
186 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
187 shoulder_profile_.set_maximum_acceleration(
188 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
189 wrist_profile_.set_maximum_velocity(
190 UseUnlessZero(max_angular_velocity_wrist, 10.0));
191 wrist_profile_.set_maximum_acceleration(
192 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
193}
194
195bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800196 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
197 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700198 AOS_LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
199 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
200 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800201 return true;
202 }
203
Austin Schuhb1d682b2016-02-16 13:07:44 -0800204 if (wrist_angle() - shoulder_angle() >
205 constants::Values::kWristRange.upper_hard ||
206 wrist_angle() - shoulder_angle() <
207 constants::Values::kWristRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700208 AOS_LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
209 wrist_angle() - shoulder_angle(),
210 constants::Values::kWristRange.lower_hard,
211 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800212 return true;
213 }
214
215 return false;
216}
217
218void Arm::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800219 // TODO(austin): Reset the state vectors and internal state here.
220 if (should_reset_) {
221 should_reset_ = false;
222 }
223
Austin Schuh10c2d112016-02-14 13:42:28 -0800224 if (!disable) {
225 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800226 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
227 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800228
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800229 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800230 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800231
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800232 loop_->mutable_next_R().block<2, 1>(4, 0) =
233 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800234 CapGoal("next R", &loop_->mutable_next_R());
235 }
236
237 // Move loop
238 loop_->Update(disable);
239
240 // Shoulder saturated
241 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700242 AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
243 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800244 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
245 }
246
247 // Wrist saturated
248 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700249 AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
250 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800251 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
252 }
253}
254
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800255} // namespace y2016::control_loops::superstructure