blob: 07988240d84b1655ec7db0850bdaf5a7b86cb1aa [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}
26} // namespace
27
Austin Schuh10c2d112016-02-14 13:42:28 -080028// Intake
29Intake::Intake()
Brian Silverman2b1957a2016-02-14 20:29:57 -050030 : loop_(new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>(
31 ::y2016::control_loops::superstructure::MakeIntegralIntakeLoop())),
Austin Schuh10c2d112016-02-14 13:42:28 -080032 estimator_(constants::GetValues().intake.zeroing),
33 profile_(::aos::controls::kLoopFrequency) {
34 Y_.setZero();
35 unprofiled_goal_.setZero();
36 offset_.setZero();
37 AdjustProfile(0.0, 0.0);
38}
39
40void Intake::UpdateIntakeOffset(double offset) {
41 const double doffset = offset - offset_(0, 0);
42 LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset);
43
44 loop_->mutable_X_hat()(0, 0) += doffset;
45 Y_(0, 0) += doffset;
46 loop_->mutable_R(0, 0) += doffset;
47
48 profile_.MoveGoal(doffset);
49 offset_(0, 0) = offset;
50
51 CapGoal("R", &loop_->mutable_R());
52}
53
54void Intake::Correct(PotAndIndexPosition position) {
55 estimator_.UpdateEstimate(position);
56
Diana Vandenberge2843c62016-02-13 17:44:20 -080057 if (estimator_.error()) {
58 LOG(ERROR, "zeroing error with intake_estimator\n");
59 return;
60 }
61
Austin Schuh10c2d112016-02-14 13:42:28 -080062 if (!initialized_) {
63 if (estimator_.offset_ready()) {
64 UpdateIntakeOffset(estimator_.offset());
65 initialized_ = true;
66 }
67 }
68
69 if (!zeroed_ && estimator_.zeroed()) {
70 UpdateIntakeOffset(estimator_.offset());
71 zeroed_ = true;
72 }
73
74 Y_ << position.encoder;
75 Y_ += offset_;
76 loop_->Correct(Y_);
77}
78
79void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh10c2d112016-02-14 13:42:28 -080080 // Limit the goal to min/max allowable angles.
Austin Schuh3b0f3642016-02-14 21:04:26 -080081 if ((*goal)(0, 0) > constants::Values::kIntakeRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -080082 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000083 constants::Values::kIntakeRange.upper);
84 (*goal)(0, 0) = constants::Values::kIntakeRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -080085 }
Austin Schuh3b0f3642016-02-14 21:04:26 -080086 if ((*goal)(0, 0) < constants::Values::kIntakeRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -080087 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000088 constants::Values::kIntakeRange.lower);
89 (*goal)(0, 0) = constants::Values::kIntakeRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -080090 }
91}
92
93void Intake::ForceGoal(double goal) {
94 set_unprofiled_goal(goal);
95 loop_->mutable_R() = unprofiled_goal_;
96 loop_->mutable_next_R() = loop_->R();
97
98 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
99}
100
101void Intake::set_unprofiled_goal(double unprofiled_goal) {
102 unprofiled_goal_(0, 0) = unprofiled_goal;
103 unprofiled_goal_(1, 0) = 0.0;
104 unprofiled_goal_(2, 0) = 0.0;
105 CapGoal("unprofiled R", &unprofiled_goal_);
106}
107
108void Intake::Update(bool disable) {
109 if (!disable) {
110 ::Eigen::Matrix<double, 2, 1> goal_state =
111 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
112
113 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
114 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
115 loop_->mutable_next_R(2, 0) = 0.0;
116 CapGoal("next R", &loop_->mutable_next_R());
117 }
118
119 loop_->Update(disable);
120
121 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
122 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
123 }
124}
125
126bool Intake::CheckHardLimits() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800127 // Returns whether hard limits have been exceeded.
128
Austin Schuh3b0f3642016-02-14 21:04:26 -0800129 if (angle() > constants::Values::kIntakeRange.upper_hard ||
130 angle() < constants::Values::kIntakeRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800131 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
Comran Morshed225f0b92016-02-10 20:34:27 +0000132 constants::Values::kIntakeRange.lower_hard, constants::Values::kIntakeRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800133 return true;
134 }
135
136 return false;
137}
138
139void Intake::set_max_voltage(double voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500140 loop_->set_max_voltages(voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800141}
142
143void Intake::AdjustProfile(double max_angular_velocity,
144 double max_angular_acceleration) {
145 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
146 profile_.set_maximum_acceleration(
147 UseUnlessZero(max_angular_acceleration, 10.0));
148}
149
150void Intake::Reset() {
151 estimator_.Reset();
152 initialized_ = false;
153 zeroed_ = false;
154}
155
156EstimatorState Intake::IntakeEstimatorState() {
157 EstimatorState estimator_state;
158 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
159
160 return estimator_state;
161}
162
163Arm::Arm()
Brian Silverman2b1957a2016-02-14 20:29:57 -0500164 : loop_(new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>(
Austin Schuh10c2d112016-02-14 13:42:28 -0800165 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
166 shoulder_profile_(::aos::controls::kLoopFrequency),
167 wrist_profile_(::aos::controls::kLoopFrequency),
168 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
169 wrist_estimator_(constants::GetValues().wrist.zeroing) {
170 Y_.setZero();
171 offset_.setZero();
172 unprofiled_goal_.setZero();
173 AdjustProfile(0.0, 0.0, 0.0, 0.0);
174}
175
176void Arm::UpdateWristOffset(double offset) {
177 const double doffset = offset - offset_(1, 0);
178 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
179
180 loop_->mutable_X_hat()(2, 0) += doffset;
181 Y_(1, 0) += doffset;
182 loop_->mutable_R(2, 0) += doffset;
183 loop_->mutable_next_R(2, 0) += doffset;
184 unprofiled_goal_(2, 0) += doffset;
185
186 wrist_profile_.MoveGoal(doffset);
187 offset_(1, 0) = offset;
188
189 CapGoal("R", &loop_->mutable_R());
190 CapGoal("unprofiled R", &loop_->mutable_next_R());
191}
192
193void Arm::UpdateShoulderOffset(double offset) {
194 const double doffset = offset - offset_(0, 0);
195 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), 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 loop_->mutable_next_R(0, 0) += doffset;
203 loop_->mutable_next_R(2, 0) += doffset;
204 unprofiled_goal_(0, 0) += doffset;
205 unprofiled_goal_(2, 0) += doffset;
206
207 shoulder_profile_.MoveGoal(doffset);
208 wrist_profile_.MoveGoal(doffset);
209 offset_(0, 0) = offset;
210
211 CapGoal("R", &loop_->mutable_R());
212 CapGoal("unprofiled R", &loop_->mutable_next_R());
213}
214
215// TODO(austin): Handle zeroing errors.
216
217void Arm::Correct(PotAndIndexPosition position_shoulder,
218 PotAndIndexPosition position_wrist) {
219 shoulder_estimator_.UpdateEstimate(position_shoulder);
220 wrist_estimator_.UpdateEstimate(position_wrist);
221
Diana Vandenberge2843c62016-02-13 17:44:20 -0800222 // Handle zeroing errors
223 if (shoulder_estimator_.error()) {
224 LOG(ERROR, "zeroing error with shoulder_estimator\n");
225 return;
226 }
227 if (wrist_estimator_.error()) {
228 LOG(ERROR, "zeroing error with wrist_estimator\n");
229 return;
230 }
231
Austin Schuh10c2d112016-02-14 13:42:28 -0800232 if (!initialized_) {
233 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
234 UpdateShoulderOffset(shoulder_estimator_.offset());
235 UpdateWristOffset(wrist_estimator_.offset());
236 initialized_ = true;
237 }
238 }
239
240 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
241 UpdateShoulderOffset(shoulder_estimator_.offset());
242 shoulder_zeroed_ = true;
243 }
244 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
245 UpdateWristOffset(wrist_estimator_.offset());
246 wrist_zeroed_ = true;
247 }
248
249 {
250 Y_ << position_shoulder.encoder, position_wrist.encoder;
251 Y_ += offset_;
252 loop_->Correct(Y_);
253 }
254}
255
256void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
257 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800258
Austin Schuh3b0f3642016-02-14 21:04:26 -0800259 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800260 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000261 constants::Values::kShoulderRange.upper);
262 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800263 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800264 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800265 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000266 constants::Values::kShoulderRange.lower);
267 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800268 }
269
270 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
271
Austin Schuh3b0f3642016-02-14 21:04:26 -0800272 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800273 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000274 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
275 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800276 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800277 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800278 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000279 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
280 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800281 }
282}
283
284void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
285 set_unprofiled_goal(goal_shoulder, goal_wrist);
286 loop_->mutable_R() = unprofiled_goal_;
287 loop_->mutable_next_R() = loop_->R();
288
289 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
290 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
291}
292
293void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
294 double unprofiled_goal_wrist) {
295 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
296 0.0, 0.0;
297 CapGoal("unprofiled R", &unprofiled_goal_);
298}
299
300void Arm::AdjustProfile(double max_angular_velocity_shoulder,
301 double max_angular_acceleration_shoulder,
302 double max_angular_velocity_wrist,
303 double max_angular_acceleration_wrist) {
304 shoulder_profile_.set_maximum_velocity(
305 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
306 shoulder_profile_.set_maximum_acceleration(
307 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
308 wrist_profile_.set_maximum_velocity(
309 UseUnlessZero(max_angular_velocity_wrist, 10.0));
310 wrist_profile_.set_maximum_acceleration(
311 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
312}
313
314bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800315 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
316 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800317 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000318 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
319 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800320 return true;
321 }
322
Austin Schuh3b0f3642016-02-14 21:04:26 -0800323 if (wrist_angle() - shoulder_angle() > constants::Values::kWristRange.upper_hard ||
324 wrist_angle() - shoulder_angle() < constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800325 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000326 wrist_angle() - shoulder_angle(), constants::Values::kWristRange.lower_hard,
327 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800328 return true;
329 }
330
331 return false;
332}
333
334void Arm::Update(bool disable) {
335 if (!disable) {
336 // Compute next goal.
337 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
338 shoulder_profile_.Update(unprofiled_goal_(0, 0),
339 unprofiled_goal_(1, 0));
340 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
341 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
342
343 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
344 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
345 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
346 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
347
348 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
349 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
350 CapGoal("next R", &loop_->mutable_next_R());
351 }
352
353 // Move loop
354 loop_->Update(disable);
355
356 // Shoulder saturated
357 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
358 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
359 }
360
361 // Wrist saturated
362 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
363 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
364 }
365}
366
367void Arm::set_max_voltage(double shoulder_max_voltage,
368 double wrist_max_voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500369 loop_->set_max_voltages(shoulder_max_voltage, wrist_max_voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800370}
371
372void Arm::Reset() {
373 shoulder_estimator_.Reset();
374 wrist_estimator_.Reset();
375 initialized_ = false;
376 shoulder_zeroed_ = false;
377 wrist_zeroed_ = false;
378}
379
380EstimatorState Arm::ShoulderEstimatorState() {
381 EstimatorState estimator_state;
382 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
383 &estimator_state);
384
385 return estimator_state;
386}
387
388EstimatorState Arm::WristEstimatorState() {
389 EstimatorState estimator_state;
390 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
391
392 return estimator_state;
393}
394
395} // namespace superstructure
396} // namespace control_loops
397} // namespace y2016