blob: a522e409a6c6bd8c4019bb692c32cea89caabafb [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;
Austin Schuh10c2d112016-02-14 13:42:28 -080016
17namespace {
18double UseUnlessZero(double target_value, double default_value) {
19 if (target_value != 0.0) {
20 return target_value;
21 } else {
22 return default_value;
23 }
24}
Austin Schuhcb60e292017-01-01 16:07:31 -080025
26enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
27
Austin Schuh10c2d112016-02-14 13:42:28 -080028} // namespace
29
Austin Schuh10c2d112016-02-14 13:42:28 -080030// Intake
31Intake::Intake()
Brian Silvermanab0b6772017-02-05 16:16:21 -080032 : ::frc971::control_loops::SingleDOFProfiledSubsystem<>(
Austin Schuhcb60e292017-01-01 16:07:31 -080033 ::std::unique_ptr<
34 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
35 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
36 3, 1, 1>(::y2016::control_loops::superstructure::
Austin Schuh473a5652017-02-05 01:30:42 -080037 MakeIntegralIntakeLoop())),
38 constants::GetValues().intake.zeroing,
39 constants::Values::kIntakeRange, 10.0, 10.0) {}
Austin Schuh10c2d112016-02-14 13:42:28 -080040
41Arm::Arm()
Austin Schuh473a5652017-02-05 01:30:42 -080042 : ProfiledSubsystem(
43 ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
44 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
Campbell Crowley36e93e92017-12-23 14:21:43 -080045 {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
46 constants::GetValues().shoulder.zeroing),
47 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
48 constants::GetValues().wrist.zeroing)}}),
Austin Schuh10c2d112016-02-14 13:42:28 -080049 shoulder_profile_(::aos::controls::kLoopFrequency),
Austin Schuh473a5652017-02-05 01:30:42 -080050 wrist_profile_(::aos::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080051 Y_.setZero();
52 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080053 AdjustProfile(0.0, 0.0, 0.0, 0.0);
54}
55
56void Arm::UpdateWristOffset(double offset) {
57 const double doffset = offset - offset_(1, 0);
58 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
59
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);
75 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
76
77 loop_->mutable_X_hat()(0, 0) += doffset;
78 loop_->mutable_X_hat()(2, 0) += doffset;
79 Y_(0, 0) += doffset;
80 loop_->mutable_R(0, 0) += doffset;
81 loop_->mutable_R(2, 0) += doffset;
82 loop_->mutable_next_R(0, 0) += doffset;
83 loop_->mutable_next_R(2, 0) += doffset;
84 unprofiled_goal_(0, 0) += doffset;
85 unprofiled_goal_(2, 0) += doffset;
86
87 shoulder_profile_.MoveGoal(doffset);
88 wrist_profile_.MoveGoal(doffset);
89 offset_(0, 0) = offset;
90
91 CapGoal("R", &loop_->mutable_R());
92 CapGoal("unprofiled R", &loop_->mutable_next_R());
93}
94
95// TODO(austin): Handle zeroing errors.
96
97void Arm::Correct(PotAndIndexPosition position_shoulder,
98 PotAndIndexPosition position_wrist) {
Austin Schuh473a5652017-02-05 01:30:42 -080099 estimators_[kShoulderIndex].UpdateEstimate(position_shoulder);
100 estimators_[kWristIndex].UpdateEstimate(position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -0800101
Diana Vandenberge2843c62016-02-13 17:44:20 -0800102 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800103 if (estimators_[kShoulderIndex].error()) {
Diana Vandenberge2843c62016-02-13 17:44:20 -0800104 LOG(ERROR, "zeroing error with shoulder_estimator\n");
105 return;
106 }
Austin Schuh473a5652017-02-05 01:30:42 -0800107 if (estimators_[kWristIndex].error()) {
Diana Vandenberge2843c62016-02-13 17:44:20 -0800108 LOG(ERROR, "zeroing error with wrist_estimator\n");
109 return;
110 }
111
Austin Schuh10c2d112016-02-14 13:42:28 -0800112 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800113 if (estimators_[kShoulderIndex].offset_ready() &&
114 estimators_[kWristIndex].offset_ready()) {
115 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
116 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800117 initialized_ = true;
118 }
119 }
120
Austin Schuh473a5652017-02-05 01:30:42 -0800121 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
122 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800123 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800124 }
Austin Schuh473a5652017-02-05 01:30:42 -0800125 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
126 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800127 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800128 }
129
130 {
131 Y_ << position_shoulder.encoder, position_wrist.encoder;
132 Y_ += offset_;
133 loop_->Correct(Y_);
134 }
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 Schuh10c2d112016-02-14 13:42:28 -0800141 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000142 constants::Values::kShoulderRange.upper);
143 (*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 Schuh10c2d112016-02-14 13:42:28 -0800146 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000147 constants::Values::kShoulderRange.lower);
148 (*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 Schuh10c2d112016-02-14 13:42:28 -0800154 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000155 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
156 (*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 Schuh10c2d112016-02-14 13:42:28 -0800159 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000160 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
161 (*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 Schuh10c2d112016-02-14 13:42:28 -0800198 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 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 Schuh10c2d112016-02-14 13:42:28 -0800208 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuhb1d682b2016-02-16 13:07:44 -0800209 wrist_angle() - shoulder_angle(),
210 constants::Values::kWristRange.lower_hard,
Comran Morshed225f0b92016-02-10 20:34:27 +0000211 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 Schuhd7e29942016-03-12 21:35:11 -0800242 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 Schuhd7e29942016-03-12 21:35:11 -0800249 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
Austin Schuh10c2d112016-02-14 13:42:28 -0800255} // namespace superstructure
256} // namespace control_loops
257} // namespace y2016