blob: d286fa8fb18cc79a06cc1cc620cf0864ad4c878a [file] [log] [blame]
Comran Morshed25f81a02016-01-23 13:40:10 +00001#include "y2016/control_loops/superstructure/superstructure.h"
2
3#include "aos/common/controls/control_loops.q.h"
4#include "aos/common/logging/logging.h"
5
Austin Schuh2fc10fa2016-02-08 00:44:34 -08006#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
Comran Morshed25f81a02016-01-23 13:40:10 +000011namespace y2016 {
12namespace control_loops {
Austin Schuh2fc10fa2016-02-08 00:44:34 -080013namespace superstructure {
Comran Morshed25f81a02016-01-23 13:40:10 +000014
Austin Schuh2fc10fa2016-02-08 00:44:34 -080015namespace {
16constexpr double kZeroingVoltage = 4.0;
Comran Morshed25f81a02016-01-23 13:40:10 +000017
Austin Schuh2fc10fa2016-02-08 00:44:34 -080018double UseUnlessZero(double target_value, double default_value) {
19 if (target_value != 0.0) {
20 return target_value;
21 } else {
22 return default_value;
23 }
Comran Morshed25f81a02016-01-23 13:40:10 +000024}
25
Austin Schuh2fc10fa2016-02-08 00:44:34 -080026} // namespace
27
28void SimpleCappedStateFeedbackLoop::CapU() {
29 mutable_U(0, 0) = ::std::min(U(0, 0), max_voltage_);
30 mutable_U(0, 0) = ::std::max(U(0, 0), -max_voltage_);
31}
32
33void DoubleCappedStateFeedbackLoop::CapU() {
34 mutable_U(0, 0) = ::std::min(U(0, 0), shoulder_max_voltage_);
35 mutable_U(0, 0) = ::std::max(U(0, 0), -shoulder_max_voltage_);
36 mutable_U(1, 0) = ::std::min(U(1, 0), wrist_max_voltage_);
37 mutable_U(1, 0) = ::std::max(U(1, 0), -wrist_max_voltage_);
38}
39
40// Intake
41Intake::Intake()
42 : loop_(new SimpleCappedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1>(
43 ::y2016::control_loops::superstructure::MakeIntegralIntakeLoop()))),
44 estimator_(constants::GetValues().intake.zeroing),
45 profile_(::aos::controls::kLoopFrequency) {
46 Y_.setZero();
47 unprofiled_goal_.setZero();
48 offset_.setZero();
49 AdjustProfile(0.0, 0.0);
50}
51
52void Intake::UpdateIntakeOffset(double offset) {
53 const double doffset = offset - offset_(0, 0);
54 LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset);
55
56 loop_->mutable_X_hat()(0, 0) += doffset;
57 Y_(0, 0) += doffset;
58 loop_->mutable_R(0, 0) += doffset;
59
60 profile_.MoveGoal(doffset);
61 offset_(0, 0) = offset;
62
63 CapGoal("R", &loop_->mutable_R());
64}
65
66
67void Intake::Correct(PotAndIndexPosition position) {
68 estimator_.UpdateEstimate(position);
69
70 if (!initialized_) {
71 if (estimator_.offset_ready()) {
72 UpdateIntakeOffset(estimator_.offset());
73 initialized_ = true;
74 }
75 }
76
77 Y_ << position.encoder;
78 Y_ += offset_;
79 loop_->Correct(Y_);
80}
81
82void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
83 const auto &values = constants::GetValues();
84
85 // Limit the goal to min/max allowable angles.
86 if ((*goal)(0, 0) >= values.intake.limits.upper) {
87 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
88 values.intake.limits.upper);
89 (*goal)(0, 0) = values.intake.limits.upper;
90 }
91 if ((*goal)(0, 0) <= values.intake.limits.lower) {
92 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
93 values.intake.limits.lower);
94 (*goal)(0, 0) = values.intake.limits.lower;
95 }
96}
97
98void Intake::ForceGoal(double goal) {
99 set_unprofiled_goal(goal);
100 loop_->mutable_R() = unprofiled_goal_;
101}
102
103void Intake::set_unprofiled_goal(double unprofiled_goal) {
104 unprofiled_goal_(0, 0) = unprofiled_goal;
105 unprofiled_goal_(1, 0) = 0.0;
106 unprofiled_goal_(2, 0) = 0.0;
107 CapGoal("unprofiled R", &unprofiled_goal_);
108}
109
110void Intake::Update(bool disable) {
111 if (!disable) {
112 ::Eigen::Matrix<double, 2, 1> goal_state =
113 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
114
115 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
116 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
117 loop_->mutable_next_R(2, 0) = 0.0;
118 CapGoal("next R", &loop_->mutable_next_R());
119 }
120
121 loop_->Update(disable);
122
123 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
124 ::Eigen::Matrix<double, 2, 1> new_state;
125 new_state << loop_->R(0, 0), loop_->R(1, 0);
126 profile_.MoveCurrentState(new_state);
127 }
128}
129
130bool Intake::CheckHardLimits() {
131 const auto &values = constants::GetValues();
132 // Returns whether hard limits have been exceeded.
133
134 if (angle() >= values.intake.limits.upper_hard ||
135 angle() <= values.intake.limits.lower_hard) {
136 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
137 values.intake.limits.lower_hard, values.intake.limits.upper_hard);
138 return true;
139 }
140
141 return false;
142}
143
144void Intake::set_max_voltage(double voltage) { loop_->set_max_voltage(voltage); }
145
146void Intake::AdjustProfile(double max_angular_velocity,
147 double max_angular_acceleration) {
148 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
149 profile_.set_maximum_acceleration(UseUnlessZero(max_angular_acceleration, 10.0));
150}
151
152void Intake::Reset() {
153 estimator_.Reset();
154 initialized_ = false;
155 zeroed_ = false;
156}
157
158EstimatorState Intake::IntakeEstimatorState() {
159 EstimatorState estimator_state;
160 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
161
162 return estimator_state;
163}
164
165Arm::Arm()
166 : loop_(new DoubleCappedStateFeedbackLoop(
167 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
168 shoulder_profile_(::aos::controls::kLoopFrequency),
169 wrist_profile_(::aos::controls::kLoopFrequency),
170 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
171 wrist_estimator_(constants::GetValues().wrist.zeroing) {
172 Y_.setZero();
173 offset_.setZero();
174 unprofiled_goal_.setZero();
175 AdjustProfile(0.0, 0.0, 0.0, 0.0);
176}
177
178void Arm::UpdateWristOffset(double offset) {
179 const double doffset = offset - offset_(1, 0);
180 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
181
182 loop_->mutable_X_hat()(2, 0) += doffset;
183 Y_(1, 0) += doffset;
184 loop_->mutable_R(2, 0) += doffset;
185
186 wrist_profile_.MoveGoal(doffset);
187 offset_(1, 0) = offset;
188
189 CapGoal("R", &loop_->mutable_R());
190}
191
192void Arm::UpdateShoulderOffset(double offset) {
193 const double doffset = offset - offset_(0, 0);
194 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0),
195 offset);
196
197 loop_->mutable_X_hat()(0, 0) += doffset;
198 loop_->mutable_X_hat()(2, 0) += doffset;
199 Y_(0, 0) += doffset;
200 loop_->mutable_R(0, 0) += doffset;
201 loop_->mutable_R(2, 0) += doffset;
202
203 shoulder_profile_.MoveGoal(doffset);
204 offset_(0, 0) = offset;
205
206 CapGoal("R", &loop_->mutable_R());
207}
208
209// TODO(austin): Handle zeroing errors.
210
211void Arm::Correct(PotAndIndexPosition position_shoulder,
212 PotAndIndexPosition position_wrist) {
213 shoulder_estimator_.UpdateEstimate(position_shoulder);
214 wrist_estimator_.UpdateEstimate(position_wrist);
215
216 if (!initialized_) {
217 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
218 UpdateShoulderOffset(shoulder_estimator_.offset());
219 UpdateWristOffset(wrist_estimator_.offset());
220 initialized_ = true;
221 }
222 }
223
224 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
225 UpdateShoulderOffset(shoulder_estimator_.offset());
226 shoulder_zeroed_ = true;
227 }
228 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
229 UpdateWristOffset(wrist_estimator_.offset());
230 wrist_zeroed_ = true;
231 }
232
233 {
234 Y_ << position_shoulder.encoder, position_wrist.encoder;
235 Y_ += offset_;
236 loop_->Correct(Y_);
237 }
238}
239
240void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
241 // Limit the goals to min/max allowable angles.
242 const auto &values = constants::GetValues();
243
244 if ((*goal)(0, 0) >= values.shoulder.limits.upper) {
245 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
246 values.shoulder.limits.upper);
247 (*goal)(0, 0) = values.shoulder.limits.upper;
248 }
249 if ((*goal)(0, 0) <= values.shoulder.limits.lower) {
250 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
251 values.shoulder.limits.lower);
252 (*goal)(0, 0) = values.shoulder.limits.lower;
253 }
254
255 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
256
257 if (wrist_goal_angle_ungrounded >= values.wrist.limits.upper) {
258 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
259 wrist_goal_angle_ungrounded, values.wrist.limits.upper);
260 (*goal)(2, 0) = values.wrist.limits.upper + (*goal)(0, 0);
261 }
262 if (wrist_goal_angle_ungrounded <= values.wrist.limits.lower) {
263 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
264 wrist_goal_angle_ungrounded, values.wrist.limits.lower);
265 (*goal)(2, 0) = values.wrist.limits.lower + (*goal)(0, 0);
266 }
267}
268
269void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
270 set_unprofiled_goal(goal_shoulder, goal_wrist);
271 loop_->mutable_R() = unprofiled_goal_;
272}
273
274void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
275 double unprofiled_goal_wrist) {
276 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
277 0.0, 0.0;
278 CapGoal("unprofiled R", &unprofiled_goal_);
279}
280
281void Arm::AdjustProfile(double max_angular_velocity_shoulder,
282 double max_angular_acceleration_shoulder,
283 double max_angular_velocity_wrist,
284 double max_angular_acceleration_wrist) {
285 shoulder_profile_.set_maximum_velocity(
286 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
287 shoulder_profile_.set_maximum_acceleration(
288 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
289 wrist_profile_.set_maximum_velocity(
290 UseUnlessZero(max_angular_velocity_wrist, 10.0));
291 wrist_profile_.set_maximum_acceleration(
292 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
293}
294
295bool Arm::CheckHardLimits() {
296 const auto &values = constants::GetValues();
297 if (shoulder_angle() >= values.shoulder.limits.upper_hard ||
298 shoulder_angle() <= values.shoulder.limits.lower_hard) {
299 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
300 shoulder_angle(), values.shoulder.limits.lower_hard,
301 values.shoulder.limits.upper_hard);
302 return true;
303 }
304
305 if (wrist_angle() - shoulder_angle() >= values.wrist.limits.upper_hard ||
306 wrist_angle() - shoulder_angle() <= values.wrist.limits.lower_hard) {
307 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
308 wrist_angle() - shoulder_angle(), values.wrist.limits.lower_hard,
309 values.wrist.limits.upper_hard);
310 return true;
311 }
312
313 return false;
314}
315
316void Arm::Update(bool disable) {
317 if (!disable) {
318 // Compute next goal.
319 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
320 shoulder_profile_.Update(unprofiled_goal_(0, 0),
321 unprofiled_goal_(1, 0));
322 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
323 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
324
325 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
326 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
327 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
328 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
329
330 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
331 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
332 CapGoal("next R", &loop_->mutable_next_R());
333 }
334
335 // Move loop
336 loop_->Update(disable);
337
338 // Shoulder saturated
339 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
340 ::Eigen::Matrix<double, 2, 1> new_shoulder_state;
341 new_shoulder_state << loop_->R(0, 0), loop_->R(1, 0);
342 shoulder_profile_.MoveCurrentState(new_shoulder_state);
343 }
344
345 // Wrist saturated
346 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
347 ::Eigen::Matrix<double, 2, 1> new_wrist_state;
348 new_wrist_state << loop_->R(2, 0), loop_->R(3, 0);
349 wrist_profile_.MoveCurrentState(new_wrist_state);
350 }
351}
352
353void Arm::set_max_voltage(double shoulder_max_voltage,
354 double wrist_max_voltage) {
355 loop_->set_max_voltage(shoulder_max_voltage, wrist_max_voltage);
356}
357
358void Arm::Reset() {
359 shoulder_estimator_.Reset();
360 wrist_estimator_.Reset();
361 initialized_ = false;
362 shoulder_zeroed_ = false;
363 wrist_zeroed_ = false;
364}
365
366EstimatorState Arm::ShoulderEstimatorState() {
367 EstimatorState estimator_state;
368 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
369 &estimator_state);
370
371 return estimator_state;
372}
373
374EstimatorState Arm::WristEstimatorState() {
375 EstimatorState estimator_state;
376 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
377
378 return estimator_state;
379}
380
381// ///// Superstructure /////
382Superstructure::Superstructure(
383 control_loops::SuperstructureQueue *superstructure_queue)
384 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(
385 superstructure_queue) {}
386
387void Superstructure::UpdateZeroingState() {
388 // TODO(austin): Explicit state transitions instead of this.
389 // TODO(adam): Change this once we have zeroing written.
390 if (!arm_.initialized() || !intake_.initialized()) {
391 state_ = INITIALIZING;
392 } else if (!intake_.zeroed()) {
393 state_ = ZEROING_INTAKE;
394 } else if (!arm_.zeroed()) {
395 state_ = ZEROING_ARM;
396 } else {
397 state_ = RUNNING;
398 }
399}
400
401void Superstructure::RunIteration(
402 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
403 const control_loops::SuperstructureQueue::Position *position,
404 control_loops::SuperstructureQueue::Output *output,
405 control_loops::SuperstructureQueue::Status *status) {
406 if (WasReset()) {
407 LOG(ERROR, "WPILib reset, restarting\n");
408 arm_.Reset();
409 intake_.Reset();
410 state_ = UNINITIALIZED;
411 }
412
413 // Bool to track if we should turn the motors on or not.
414 bool disable = output == nullptr;
415
416 arm_.Correct(position->shoulder, position->wrist);
417 intake_.Correct(position->intake);
418
419 // Zeroing will work as follows:
420 // Start with the intake. Move it towards the center. Once zeroed, move it
421 // back to the bottom. Rotate the shoulder towards the center. Once zeroed,
422 // move it up enough to rotate the wrist towards the center.
423
424 // We'll then need code to do sanity checking on values.
425
426 switch (state_) {
427 case UNINITIALIZED:
428 LOG(DEBUG, "Uninitialized\n");
429 state_ = INITIALIZING;
430 disable = true;
431 break;
432
433 case INITIALIZING:
434 LOG(DEBUG, "Waiting for accurate initial position.\n");
435 disable = true;
436 // Update state_ to accurately represent the state of the zeroing
437 // estimators.
438 UpdateZeroingState();
439 if (state_ != INITIALIZING) {
440 // Set the goals to where we are now.
441 intake_.ForceGoal(intake_.angle());
442 arm_.ForceGoal(arm_.shoulder_angle(), arm_.wrist_angle());
443 }
444 break;
445
446 case ZEROING_INTAKE:
447 case ZEROING_ARM:
448 // TODO(adam): Add your magic here.
449 state_ = RUNNING;
450 break;
451
452 case RUNNING:
453 if (unsafe_goal) {
454 arm_.AdjustProfile(unsafe_goal->max_angular_velocity_shoulder,
455 unsafe_goal->max_angular_acceleration_shoulder,
456 unsafe_goal->max_angular_velocity_wrist,
457 unsafe_goal->max_angular_acceleration_wrist);
458 intake_.AdjustProfile(unsafe_goal->max_angular_velocity_wrist,
459 unsafe_goal->max_angular_acceleration_intake);
460
461 arm_.set_unprofiled_goal(unsafe_goal->angle_shoulder,
462 unsafe_goal->angle_wrist);
463 intake_.set_unprofiled_goal(unsafe_goal->angle_intake);
464 }
465
466 // Update state_ to accurately represent the state of the zeroing
467 // estimators.
468
469 if (state_ != RUNNING && state_ != ESTOP) {
470 state_ = UNINITIALIZED;
471 }
472 break;
473
474 case ESTOP:
475 LOG(ERROR, "Estop\n");
476 disable = true;
477 break;
478 }
479
480 // ESTOP if we hit any of the limits. It is safe(ish) to hit the limits while
481 // zeroing since we use such low power.
482 if (state_ == RUNNING) {
483 // ESTOP if we hit the hard limits.
484 if ((arm_.CheckHardLimits() || intake_.CheckHardLimits()) && output) {
485 state_ = ESTOP;
486 }
487 }
488
489 // Set the voltage limits.
490 const double max_voltage = state_ == RUNNING ? 12.0 : kZeroingVoltage;
491 arm_.set_max_voltage(max_voltage, max_voltage);
492 intake_.set_max_voltage(max_voltage);
493
494 // Calculate the loops for a cycle.
495 arm_.Update(disable);
496 intake_.Update(disable);
497
498 // Write out all the voltages.
499 if (output) {
500 output->voltage_intake = intake_.intake_voltage();
501 output->voltage_shoulder = arm_.shoulder_voltage();
502 output->voltage_wrist = arm_.wrist_voltage();
503 }
504
505 // Save debug/internal state.
506 // TODO(austin): Save the voltage errors.
507 status->zeroed = state_ == RUNNING;
508
509 status->shoulder.angle = arm_.X_hat(0, 0);
510 status->shoulder.angular_velocity = arm_.X_hat(1, 0);
511 status->shoulder.goal_angle = arm_.goal(0, 0);
512 status->shoulder.goal_angular_velocity = arm_.goal(1, 0);
513 status->shoulder.estimator_state = arm_.ShoulderEstimatorState();
514
515 status->wrist.angle = arm_.X_hat(2, 0);
516 status->wrist.angular_velocity = arm_.X_hat(3, 0);
517 status->wrist.goal_angle = arm_.goal(2, 0);
518 status->wrist.goal_angular_velocity = arm_.goal(3, 0);
519 status->wrist.estimator_state = arm_.WristEstimatorState();
520
521 status->intake.angle = intake_.X_hat(0, 0);
522 status->intake.angular_velocity = intake_.X_hat(1, 0);
523 status->intake.goal_angle = intake_.goal(0, 0);
524 status->intake.goal_angular_velocity = intake_.goal(1, 0);
525 status->intake.estimator_state = intake_.IntakeEstimatorState();
526
527 status->estopped = (state_ == ESTOP);
528
529 status->state = state_;
530
531 last_state_ = state_;
532}
533
534} // namespace superstructure
Comran Morshed25f81a02016-01-23 13:40:10 +0000535} // namespace control_loops
536} // namespace y2016