blob: 7d4dea542aea61d74b598b0401b9183903f46031 [file] [log] [blame]
Austin Schuh10c2d112016-02-14 13:42:28 -08001#include "y2016/control_loops/superstructure/superstructure_controls.h"
2
3#include "aos/common/controls/control_loops.q.h"
4#include "aos/common/logging/logging.h"
5
6#include "y2016/control_loops/superstructure/integral_intake_plant.h"
7#include "y2016/control_loops/superstructure/integral_arm_plant.h"
8
9#include "y2016/constants.h"
10
11namespace y2016 {
12namespace control_loops {
13namespace superstructure {
14
15using ::frc971::PotAndIndexPosition;
16using ::frc971::EstimatorState;
17
18namespace {
19double UseUnlessZero(double target_value, double default_value) {
20 if (target_value != 0.0) {
21 return target_value;
22 } else {
23 return default_value;
24 }
25}
Austin Schuhcb60e292017-01-01 16:07:31 -080026
27enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
28
Austin Schuh10c2d112016-02-14 13:42:28 -080029} // namespace
30
Austin Schuh10c2d112016-02-14 13:42:28 -080031// Intake
32Intake::Intake()
Austin Schuhcb60e292017-01-01 16:07:31 -080033 : ProfiledSubsystem(
34 ::std::unique_ptr<
35 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
36 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
37 3, 1, 1>(::y2016::control_loops::superstructure::
38 MakeIntegralIntakeLoop()))),
Austin Schuh10c2d112016-02-14 13:42:28 -080039 estimator_(constants::GetValues().intake.zeroing),
40 profile_(::aos::controls::kLoopFrequency) {
41 Y_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080042 offset_.setZero();
43 AdjustProfile(0.0, 0.0);
44}
45
46void Intake::UpdateIntakeOffset(double offset) {
47 const double doffset = offset - offset_(0, 0);
48 LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset);
49
50 loop_->mutable_X_hat()(0, 0) += doffset;
51 Y_(0, 0) += doffset;
52 loop_->mutable_R(0, 0) += doffset;
53
54 profile_.MoveGoal(doffset);
55 offset_(0, 0) = offset;
56
57 CapGoal("R", &loop_->mutable_R());
58}
59
60void Intake::Correct(PotAndIndexPosition position) {
61 estimator_.UpdateEstimate(position);
62
Diana Vandenberge2843c62016-02-13 17:44:20 -080063 if (estimator_.error()) {
64 LOG(ERROR, "zeroing error with intake_estimator\n");
65 return;
66 }
67
Austin Schuh10c2d112016-02-14 13:42:28 -080068 if (!initialized_) {
69 if (estimator_.offset_ready()) {
70 UpdateIntakeOffset(estimator_.offset());
71 initialized_ = true;
72 }
73 }
74
Austin Schuhcb60e292017-01-01 16:07:31 -080075 if (!zeroed(0) && estimator_.zeroed()) {
Austin Schuh10c2d112016-02-14 13:42:28 -080076 UpdateIntakeOffset(estimator_.offset());
Austin Schuhcb60e292017-01-01 16:07:31 -080077 set_zeroed(0, true);
Austin Schuh10c2d112016-02-14 13:42:28 -080078 }
79
80 Y_ << position.encoder;
81 Y_ += offset_;
82 loop_->Correct(Y_);
83}
84
85void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh10c2d112016-02-14 13:42:28 -080086 // Limit the goal to min/max allowable angles.
Austin Schuh3b0f3642016-02-14 21:04:26 -080087 if ((*goal)(0, 0) > constants::Values::kIntakeRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -080088 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000089 constants::Values::kIntakeRange.upper);
90 (*goal)(0, 0) = constants::Values::kIntakeRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -080091 }
Austin Schuh3b0f3642016-02-14 21:04:26 -080092 if ((*goal)(0, 0) < constants::Values::kIntakeRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -080093 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000094 constants::Values::kIntakeRange.lower);
95 (*goal)(0, 0) = constants::Values::kIntakeRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -080096 }
97}
98
99void Intake::ForceGoal(double goal) {
100 set_unprofiled_goal(goal);
101 loop_->mutable_R() = unprofiled_goal_;
102 loop_->mutable_next_R() = loop_->R();
103
104 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
105}
106
107void Intake::set_unprofiled_goal(double unprofiled_goal) {
108 unprofiled_goal_(0, 0) = unprofiled_goal;
109 unprofiled_goal_(1, 0) = 0.0;
110 unprofiled_goal_(2, 0) = 0.0;
111 CapGoal("unprofiled R", &unprofiled_goal_);
112}
113
114void Intake::Update(bool disable) {
115 if (!disable) {
116 ::Eigen::Matrix<double, 2, 1> goal_state =
117 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
118
119 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
120 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
121 loop_->mutable_next_R(2, 0) = 0.0;
122 CapGoal("next R", &loop_->mutable_next_R());
123 }
124
125 loop_->Update(disable);
126
127 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
128 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
129 }
130}
131
132bool Intake::CheckHardLimits() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800133 // Returns whether hard limits have been exceeded.
134
Austin Schuh3b0f3642016-02-14 21:04:26 -0800135 if (angle() > constants::Values::kIntakeRange.upper_hard ||
136 angle() < constants::Values::kIntakeRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800137 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
Austin Schuhb1d682b2016-02-16 13:07:44 -0800138 constants::Values::kIntakeRange.lower_hard,
139 constants::Values::kIntakeRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800140 return true;
141 }
142
143 return false;
144}
145
146void Intake::set_max_voltage(double voltage) {
Austin Schuhdc710d22016-02-16 13:55:26 -0800147 loop_->set_max_voltage(0, voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800148}
149
150void Intake::AdjustProfile(double max_angular_velocity,
151 double max_angular_acceleration) {
152 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
153 profile_.set_maximum_acceleration(
154 UseUnlessZero(max_angular_acceleration, 10.0));
155}
156
Austin Schuhcb60e292017-01-01 16:07:31 -0800157void Intake::DoReset() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800158 estimator_.Reset();
Austin Schuh10c2d112016-02-14 13:42:28 -0800159}
160
161EstimatorState Intake::IntakeEstimatorState() {
162 EstimatorState estimator_state;
163 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
164
165 return estimator_state;
166}
167
168Arm::Arm()
Austin Schuhcb60e292017-01-01 16:07:31 -0800169 : ProfiledSubsystem(::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
170 ::y2016::control_loops::superstructure::MakeIntegralArmLoop()))),
Austin Schuh10c2d112016-02-14 13:42:28 -0800171 shoulder_profile_(::aos::controls::kLoopFrequency),
172 wrist_profile_(::aos::controls::kLoopFrequency),
173 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
174 wrist_estimator_(constants::GetValues().wrist.zeroing) {
175 Y_.setZero();
176 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -0800177 AdjustProfile(0.0, 0.0, 0.0, 0.0);
178}
179
180void Arm::UpdateWristOffset(double offset) {
181 const double doffset = offset - offset_(1, 0);
182 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
183
184 loop_->mutable_X_hat()(2, 0) += doffset;
185 Y_(1, 0) += doffset;
186 loop_->mutable_R(2, 0) += doffset;
187 loop_->mutable_next_R(2, 0) += doffset;
188 unprofiled_goal_(2, 0) += doffset;
189
190 wrist_profile_.MoveGoal(doffset);
191 offset_(1, 0) = offset;
192
193 CapGoal("R", &loop_->mutable_R());
194 CapGoal("unprofiled R", &loop_->mutable_next_R());
195}
196
197void Arm::UpdateShoulderOffset(double offset) {
198 const double doffset = offset - offset_(0, 0);
199 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
200
201 loop_->mutable_X_hat()(0, 0) += doffset;
202 loop_->mutable_X_hat()(2, 0) += doffset;
203 Y_(0, 0) += doffset;
204 loop_->mutable_R(0, 0) += doffset;
205 loop_->mutable_R(2, 0) += doffset;
206 loop_->mutable_next_R(0, 0) += doffset;
207 loop_->mutable_next_R(2, 0) += doffset;
208 unprofiled_goal_(0, 0) += doffset;
209 unprofiled_goal_(2, 0) += doffset;
210
211 shoulder_profile_.MoveGoal(doffset);
212 wrist_profile_.MoveGoal(doffset);
213 offset_(0, 0) = offset;
214
215 CapGoal("R", &loop_->mutable_R());
216 CapGoal("unprofiled R", &loop_->mutable_next_R());
217}
218
219// TODO(austin): Handle zeroing errors.
220
221void Arm::Correct(PotAndIndexPosition position_shoulder,
222 PotAndIndexPosition position_wrist) {
223 shoulder_estimator_.UpdateEstimate(position_shoulder);
224 wrist_estimator_.UpdateEstimate(position_wrist);
225
Diana Vandenberge2843c62016-02-13 17:44:20 -0800226 // Handle zeroing errors
227 if (shoulder_estimator_.error()) {
228 LOG(ERROR, "zeroing error with shoulder_estimator\n");
229 return;
230 }
231 if (wrist_estimator_.error()) {
232 LOG(ERROR, "zeroing error with wrist_estimator\n");
233 return;
234 }
235
Austin Schuh10c2d112016-02-14 13:42:28 -0800236 if (!initialized_) {
237 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
238 UpdateShoulderOffset(shoulder_estimator_.offset());
239 UpdateWristOffset(wrist_estimator_.offset());
240 initialized_ = true;
241 }
242 }
243
Austin Schuhcb60e292017-01-01 16:07:31 -0800244 if (!zeroed(kShoulderIndex) && shoulder_estimator_.zeroed()) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800245 UpdateShoulderOffset(shoulder_estimator_.offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800246 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800247 }
Austin Schuhcb60e292017-01-01 16:07:31 -0800248 if (!zeroed(kWristIndex) && wrist_estimator_.zeroed()) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800249 UpdateWristOffset(wrist_estimator_.offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800250 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800251 }
252
253 {
254 Y_ << position_shoulder.encoder, position_wrist.encoder;
255 Y_ += offset_;
256 loop_->Correct(Y_);
257 }
258}
259
260void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
261 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800262
Austin Schuh3b0f3642016-02-14 21:04:26 -0800263 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800264 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000265 constants::Values::kShoulderRange.upper);
266 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800267 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800268 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800269 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000270 constants::Values::kShoulderRange.lower);
271 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800272 }
273
274 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
275
Austin Schuh3b0f3642016-02-14 21:04:26 -0800276 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800277 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000278 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
279 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800280 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800281 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800282 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000283 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
284 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800285 }
286}
287
288void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
289 set_unprofiled_goal(goal_shoulder, goal_wrist);
290 loop_->mutable_R() = unprofiled_goal_;
291 loop_->mutable_next_R() = loop_->R();
292
293 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
294 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
295}
296
297void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
298 double unprofiled_goal_wrist) {
299 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
300 0.0, 0.0;
301 CapGoal("unprofiled R", &unprofiled_goal_);
302}
303
304void Arm::AdjustProfile(double max_angular_velocity_shoulder,
305 double max_angular_acceleration_shoulder,
306 double max_angular_velocity_wrist,
307 double max_angular_acceleration_wrist) {
308 shoulder_profile_.set_maximum_velocity(
309 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
310 shoulder_profile_.set_maximum_acceleration(
311 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
312 wrist_profile_.set_maximum_velocity(
313 UseUnlessZero(max_angular_velocity_wrist, 10.0));
314 wrist_profile_.set_maximum_acceleration(
315 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
316}
317
318bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800319 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
320 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800321 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000322 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
323 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800324 return true;
325 }
326
Austin Schuhb1d682b2016-02-16 13:07:44 -0800327 if (wrist_angle() - shoulder_angle() >
328 constants::Values::kWristRange.upper_hard ||
329 wrist_angle() - shoulder_angle() <
330 constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800331 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuhb1d682b2016-02-16 13:07:44 -0800332 wrist_angle() - shoulder_angle(),
333 constants::Values::kWristRange.lower_hard,
Comran Morshed225f0b92016-02-10 20:34:27 +0000334 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800335 return true;
336 }
337
338 return false;
339}
340
341void Arm::Update(bool disable) {
342 if (!disable) {
343 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800344 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
345 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800346
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800347 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800348 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800349
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800350 loop_->mutable_next_R().block<2, 1>(4, 0) =
351 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800352 CapGoal("next R", &loop_->mutable_next_R());
353 }
354
355 // Move loop
356 loop_->Update(disable);
357
358 // Shoulder saturated
359 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhd7e29942016-03-12 21:35:11 -0800360 LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
361 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800362 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
363 }
364
365 // Wrist saturated
366 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhd7e29942016-03-12 21:35:11 -0800367 LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
368 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800369 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
370 }
371}
372
373void Arm::set_max_voltage(double shoulder_max_voltage,
374 double wrist_max_voltage) {
Austin Schuhdc710d22016-02-16 13:55:26 -0800375 loop_->set_max_voltage(0, shoulder_max_voltage);
376 loop_->set_max_voltage(1, wrist_max_voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800377}
378
Austin Schuhcb60e292017-01-01 16:07:31 -0800379void Arm::DoReset() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800380 shoulder_estimator_.Reset();
381 wrist_estimator_.Reset();
Austin Schuh10c2d112016-02-14 13:42:28 -0800382}
383
384EstimatorState Arm::ShoulderEstimatorState() {
385 EstimatorState estimator_state;
386 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
387 &estimator_state);
388
389 return estimator_state;
390}
391
392EstimatorState Arm::WristEstimatorState() {
393 EstimatorState estimator_state;
394 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
395
396 return estimator_state;
397}
398
399} // namespace superstructure
400} // namespace control_loops
401} // namespace y2016