blob: 269089b641b63a1b9367b8a75f34a6e05363ea7b [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
5#include "y2016/control_loops/superstructure/integral_intake_plant.h"
6#include "y2016/control_loops/superstructure/integral_arm_plant.h"
7
8#include "y2016/constants.h"
9
10namespace y2016 {
11namespace control_loops {
12namespace superstructure {
13
14using ::frc971::PotAndIndexPosition;
Austin Schuh10c2d112016-02-14 13:42:28 -080015
16namespace {
17double UseUnlessZero(double target_value, double default_value) {
18 if (target_value != 0.0) {
19 return target_value;
20 } else {
21 return default_value;
22 }
23}
Austin Schuhcb60e292017-01-01 16:07:31 -080024
25enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
26
Austin Schuh10c2d112016-02-14 13:42:28 -080027} // namespace
28
Austin Schuh10c2d112016-02-14 13:42:28 -080029// Intake
30Intake::Intake()
Brian Silvermanab0b6772017-02-05 16:16:21 -080031 : ::frc971::control_loops::SingleDOFProfiledSubsystem<>(
Austin Schuhcb60e292017-01-01 16:07:31 -080032 ::std::unique_ptr<
33 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
34 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
35 3, 1, 1>(::y2016::control_loops::superstructure::
Austin Schuh473a5652017-02-05 01:30:42 -080036 MakeIntegralIntakeLoop())),
37 constants::GetValues().intake.zeroing,
38 constants::Values::kIntakeRange, 10.0, 10.0) {}
Austin Schuh10c2d112016-02-14 13:42:28 -080039
40Arm::Arm()
Austin Schuh473a5652017-02-05 01:30:42 -080041 : ProfiledSubsystem(
42 ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
43 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
Campbell Crowley36e93e92017-12-23 14:21:43 -080044 {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
45 constants::GetValues().shoulder.zeroing),
46 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
47 constants::GetValues().wrist.zeroing)}}),
Austin Schuh10c2d112016-02-14 13:42:28 -080048 shoulder_profile_(::aos::controls::kLoopFrequency),
Austin Schuh473a5652017-02-05 01:30:42 -080049 wrist_profile_(::aos::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080050 Y_.setZero();
51 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080052 AdjustProfile(0.0, 0.0, 0.0, 0.0);
53}
54
55void Arm::UpdateWristOffset(double offset) {
56 const double doffset = offset - offset_(1, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0),
58 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080059
60 loop_->mutable_X_hat()(2, 0) += doffset;
61 Y_(1, 0) += doffset;
62 loop_->mutable_R(2, 0) += doffset;
63 loop_->mutable_next_R(2, 0) += doffset;
64 unprofiled_goal_(2, 0) += doffset;
65
66 wrist_profile_.MoveGoal(doffset);
67 offset_(1, 0) = offset;
68
69 CapGoal("R", &loop_->mutable_R());
70 CapGoal("unprofiled R", &loop_->mutable_next_R());
71}
72
73void Arm::UpdateShoulderOffset(double offset) {
74 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070075 AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0),
76 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080077
78 loop_->mutable_X_hat()(0, 0) += doffset;
79 loop_->mutable_X_hat()(2, 0) += doffset;
80 Y_(0, 0) += doffset;
81 loop_->mutable_R(0, 0) += doffset;
82 loop_->mutable_R(2, 0) += doffset;
83 loop_->mutable_next_R(0, 0) += doffset;
84 loop_->mutable_next_R(2, 0) += doffset;
85 unprofiled_goal_(0, 0) += doffset;
86 unprofiled_goal_(2, 0) += doffset;
87
88 shoulder_profile_.MoveGoal(doffset);
89 wrist_profile_.MoveGoal(doffset);
90 offset_(0, 0) = offset;
91
92 CapGoal("R", &loop_->mutable_R());
93 CapGoal("unprofiled R", &loop_->mutable_next_R());
94}
95
96// TODO(austin): Handle zeroing errors.
97
Alex Perrycb7da4b2019-08-28 19:35:56 -070098void Arm::Correct(const PotAndIndexPosition *position_shoulder,
99 const PotAndIndexPosition *position_wrist) {
100 estimators_[kShoulderIndex].UpdateEstimate(*position_shoulder);
101 estimators_[kWristIndex].UpdateEstimate(*position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -0800102
Diana Vandenberge2843c62016-02-13 17:44:20 -0800103 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800104 if (estimators_[kShoulderIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700105 AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800106 return;
107 }
Austin Schuh473a5652017-02-05 01:30:42 -0800108 if (estimators_[kWristIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700109 AOS_LOG(ERROR, "zeroing error with wrist_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800110 return;
111 }
112
Austin Schuh10c2d112016-02-14 13:42:28 -0800113 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800114 if (estimators_[kShoulderIndex].offset_ready() &&
115 estimators_[kWristIndex].offset_ready()) {
116 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
117 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800118 initialized_ = true;
119 }
120 }
121
Austin Schuh473a5652017-02-05 01:30:42 -0800122 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
123 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800124 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800125 }
Austin Schuh473a5652017-02-05 01:30:42 -0800126 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
127 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800128 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800129 }
130
131 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 Y_ << position_shoulder->encoder(), position_wrist->encoder();
Austin Schuh10c2d112016-02-14 13:42:28 -0800133 Y_ += offset_;
134 loop_->Correct(Y_);
135 }
136}
137
138void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
139 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800140
Austin Schuh3b0f3642016-02-14 21:04:26 -0800141 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700142 AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name,
143 (*goal)(0, 0), constants::Values::kShoulderRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000144 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800145 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800146 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700147 AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name,
148 (*goal)(0, 0), constants::Values::kShoulderRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000149 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800150 }
151
152 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
153
Austin Schuh3b0f3642016-02-14 21:04:26 -0800154 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700155 AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
156 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000157 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800158 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800159 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700160 AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
161 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000162 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800163 }
164}
165
166void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
167 set_unprofiled_goal(goal_shoulder, goal_wrist);
168 loop_->mutable_R() = unprofiled_goal_;
169 loop_->mutable_next_R() = loop_->R();
170
171 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
172 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
173}
174
175void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
176 double unprofiled_goal_wrist) {
177 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
178 0.0, 0.0;
179 CapGoal("unprofiled R", &unprofiled_goal_);
180}
181
182void Arm::AdjustProfile(double max_angular_velocity_shoulder,
183 double max_angular_acceleration_shoulder,
184 double max_angular_velocity_wrist,
185 double max_angular_acceleration_wrist) {
186 shoulder_profile_.set_maximum_velocity(
187 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
188 shoulder_profile_.set_maximum_acceleration(
189 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
190 wrist_profile_.set_maximum_velocity(
191 UseUnlessZero(max_angular_velocity_wrist, 10.0));
192 wrist_profile_.set_maximum_acceleration(
193 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
194}
195
196bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800197 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
198 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700199 AOS_LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
200 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
201 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800202 return true;
203 }
204
Austin Schuhb1d682b2016-02-16 13:07:44 -0800205 if (wrist_angle() - shoulder_angle() >
206 constants::Values::kWristRange.upper_hard ||
207 wrist_angle() - shoulder_angle() <
208 constants::Values::kWristRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700209 AOS_LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
210 wrist_angle() - shoulder_angle(),
211 constants::Values::kWristRange.lower_hard,
212 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800213 return true;
214 }
215
216 return false;
217}
218
219void Arm::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800220 // TODO(austin): Reset the state vectors and internal state here.
221 if (should_reset_) {
222 should_reset_ = false;
223 }
224
Austin Schuh10c2d112016-02-14 13:42:28 -0800225 if (!disable) {
226 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800227 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
228 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800229
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800230 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800231 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800232
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800233 loop_->mutable_next_R().block<2, 1>(4, 0) =
234 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800235 CapGoal("next R", &loop_->mutable_next_R());
236 }
237
238 // Move loop
239 loop_->Update(disable);
240
241 // Shoulder saturated
242 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700243 AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
244 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800245 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
246 }
247
248 // Wrist saturated
249 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700250 AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
251 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800252 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
253 }
254}
255
Austin Schuh10c2d112016-02-14 13:42:28 -0800256} // namespace superstructure
257} // namespace control_loops
258} // namespace y2016