blob: e43c245865535dcc792f9842f70bc26360033652 [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
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
66void Intake::Correct(PotAndIndexPosition position) {
67 estimator_.UpdateEstimate(position);
68
69 if (!initialized_) {
70 if (estimator_.offset_ready()) {
71 UpdateIntakeOffset(estimator_.offset());
72 initialized_ = true;
73 }
74 }
75
76 if (!zeroed_ && estimator_.zeroed()) {
77 UpdateIntakeOffset(estimator_.offset());
78 zeroed_ = true;
79 }
80
81 Y_ << position.encoder;
82 Y_ += offset_;
83 loop_->Correct(Y_);
84}
85
86void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh10c2d112016-02-14 13:42:28 -080087 // Limit the goal to min/max allowable angles.
Comran Morshed225f0b92016-02-10 20:34:27 +000088 if ((*goal)(0, 0) >= constants::Values::kIntakeRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -080089 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000090 constants::Values::kIntakeRange.upper);
91 (*goal)(0, 0) = constants::Values::kIntakeRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -080092 }
Comran Morshed225f0b92016-02-10 20:34:27 +000093 if ((*goal)(0, 0) <= constants::Values::kIntakeRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -080094 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000095 constants::Values::kIntakeRange.lower);
96 (*goal)(0, 0) = constants::Values::kIntakeRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -080097 }
98}
99
100void Intake::ForceGoal(double goal) {
101 set_unprofiled_goal(goal);
102 loop_->mutable_R() = unprofiled_goal_;
103 loop_->mutable_next_R() = loop_->R();
104
105 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
106}
107
108void Intake::set_unprofiled_goal(double unprofiled_goal) {
109 unprofiled_goal_(0, 0) = unprofiled_goal;
110 unprofiled_goal_(1, 0) = 0.0;
111 unprofiled_goal_(2, 0) = 0.0;
112 CapGoal("unprofiled R", &unprofiled_goal_);
113}
114
115void Intake::Update(bool disable) {
116 if (!disable) {
117 ::Eigen::Matrix<double, 2, 1> goal_state =
118 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
119
120 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
121 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
122 loop_->mutable_next_R(2, 0) = 0.0;
123 CapGoal("next R", &loop_->mutable_next_R());
124 }
125
126 loop_->Update(disable);
127
128 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
129 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
130 }
131}
132
133bool Intake::CheckHardLimits() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800134 // Returns whether hard limits have been exceeded.
135
Comran Morshed225f0b92016-02-10 20:34:27 +0000136 if (angle() >= constants::Values::kIntakeRange.upper_hard ||
137 angle() <= constants::Values::kIntakeRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800138 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
Comran Morshed225f0b92016-02-10 20:34:27 +0000139 constants::Values::kIntakeRange.lower_hard, 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) {
147 loop_->set_max_voltage(voltage);
148}
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
157void Intake::Reset() {
158 estimator_.Reset();
159 initialized_ = false;
160 zeroed_ = false;
161}
162
163EstimatorState Intake::IntakeEstimatorState() {
164 EstimatorState estimator_state;
165 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
166
167 return estimator_state;
168}
169
170Arm::Arm()
171 : loop_(new DoubleCappedStateFeedbackLoop(
172 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
173 shoulder_profile_(::aos::controls::kLoopFrequency),
174 wrist_profile_(::aos::controls::kLoopFrequency),
175 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
176 wrist_estimator_(constants::GetValues().wrist.zeroing) {
177 Y_.setZero();
178 offset_.setZero();
179 unprofiled_goal_.setZero();
180 AdjustProfile(0.0, 0.0, 0.0, 0.0);
181}
182
183void Arm::UpdateWristOffset(double offset) {
184 const double doffset = offset - offset_(1, 0);
185 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
186
187 loop_->mutable_X_hat()(2, 0) += doffset;
188 Y_(1, 0) += doffset;
189 loop_->mutable_R(2, 0) += doffset;
190 loop_->mutable_next_R(2, 0) += doffset;
191 unprofiled_goal_(2, 0) += doffset;
192
193 wrist_profile_.MoveGoal(doffset);
194 offset_(1, 0) = offset;
195
196 CapGoal("R", &loop_->mutable_R());
197 CapGoal("unprofiled R", &loop_->mutable_next_R());
198}
199
200void Arm::UpdateShoulderOffset(double offset) {
201 const double doffset = offset - offset_(0, 0);
202 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
203
204 loop_->mutable_X_hat()(0, 0) += doffset;
205 loop_->mutable_X_hat()(2, 0) += doffset;
206 Y_(0, 0) += doffset;
207 loop_->mutable_R(0, 0) += doffset;
208 loop_->mutable_R(2, 0) += doffset;
209 loop_->mutable_next_R(0, 0) += doffset;
210 loop_->mutable_next_R(2, 0) += doffset;
211 unprofiled_goal_(0, 0) += doffset;
212 unprofiled_goal_(2, 0) += doffset;
213
214 shoulder_profile_.MoveGoal(doffset);
215 wrist_profile_.MoveGoal(doffset);
216 offset_(0, 0) = offset;
217
218 CapGoal("R", &loop_->mutable_R());
219 CapGoal("unprofiled R", &loop_->mutable_next_R());
220}
221
222// TODO(austin): Handle zeroing errors.
223
224void Arm::Correct(PotAndIndexPosition position_shoulder,
225 PotAndIndexPosition position_wrist) {
226 shoulder_estimator_.UpdateEstimate(position_shoulder);
227 wrist_estimator_.UpdateEstimate(position_wrist);
228
229 if (!initialized_) {
230 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
231 UpdateShoulderOffset(shoulder_estimator_.offset());
232 UpdateWristOffset(wrist_estimator_.offset());
233 initialized_ = true;
234 }
235 }
236
237 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
238 UpdateShoulderOffset(shoulder_estimator_.offset());
239 shoulder_zeroed_ = true;
240 }
241 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
242 UpdateWristOffset(wrist_estimator_.offset());
243 wrist_zeroed_ = true;
244 }
245
246 {
247 Y_ << position_shoulder.encoder, position_wrist.encoder;
248 Y_ += offset_;
249 loop_->Correct(Y_);
250 }
251}
252
253void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
254 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800255
Comran Morshed225f0b92016-02-10 20:34:27 +0000256 if ((*goal)(0, 0) >= constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800257 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000258 constants::Values::kShoulderRange.upper);
259 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800260 }
Comran Morshed225f0b92016-02-10 20:34:27 +0000261 if ((*goal)(0, 0) <= constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800262 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000263 constants::Values::kShoulderRange.lower);
264 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800265 }
266
267 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
268
Comran Morshed225f0b92016-02-10 20:34:27 +0000269 if (wrist_goal_angle_ungrounded >= constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800270 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000271 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
272 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800273 }
Comran Morshed225f0b92016-02-10 20:34:27 +0000274 if (wrist_goal_angle_ungrounded <= constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800275 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000276 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
277 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800278 }
279}
280
281void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
282 set_unprofiled_goal(goal_shoulder, goal_wrist);
283 loop_->mutable_R() = unprofiled_goal_;
284 loop_->mutable_next_R() = loop_->R();
285
286 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
287 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
288}
289
290void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
291 double unprofiled_goal_wrist) {
292 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
293 0.0, 0.0;
294 CapGoal("unprofiled R", &unprofiled_goal_);
295}
296
297void Arm::AdjustProfile(double max_angular_velocity_shoulder,
298 double max_angular_acceleration_shoulder,
299 double max_angular_velocity_wrist,
300 double max_angular_acceleration_wrist) {
301 shoulder_profile_.set_maximum_velocity(
302 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
303 shoulder_profile_.set_maximum_acceleration(
304 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
305 wrist_profile_.set_maximum_velocity(
306 UseUnlessZero(max_angular_velocity_wrist, 10.0));
307 wrist_profile_.set_maximum_acceleration(
308 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
309}
310
311bool Arm::CheckHardLimits() {
Comran Morshed225f0b92016-02-10 20:34:27 +0000312 if (shoulder_angle() >= constants::Values::kShoulderRange.upper_hard ||
313 shoulder_angle() <= constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800314 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000315 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
316 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800317 return true;
318 }
319
Comran Morshed225f0b92016-02-10 20:34:27 +0000320 if (wrist_angle() - shoulder_angle() >= constants::Values::kWristRange.upper_hard ||
321 wrist_angle() - shoulder_angle() <= constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800322 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000323 wrist_angle() - shoulder_angle(), constants::Values::kWristRange.lower_hard,
324 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800325 return true;
326 }
327
328 return false;
329}
330
331void Arm::Update(bool disable) {
332 if (!disable) {
333 // Compute next goal.
334 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
335 shoulder_profile_.Update(unprofiled_goal_(0, 0),
336 unprofiled_goal_(1, 0));
337 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
338 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
339
340 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
341 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
342 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
343 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
344
345 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
346 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
347 CapGoal("next R", &loop_->mutable_next_R());
348 }
349
350 // Move loop
351 loop_->Update(disable);
352
353 // Shoulder saturated
354 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
355 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
356 }
357
358 // Wrist saturated
359 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
360 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
361 }
362}
363
364void Arm::set_max_voltage(double shoulder_max_voltage,
365 double wrist_max_voltage) {
366 loop_->set_max_voltage(shoulder_max_voltage, wrist_max_voltage);
367}
368
369void Arm::Reset() {
370 shoulder_estimator_.Reset();
371 wrist_estimator_.Reset();
372 initialized_ = false;
373 shoulder_zeroed_ = false;
374 wrist_zeroed_ = false;
375}
376
377EstimatorState Arm::ShoulderEstimatorState() {
378 EstimatorState estimator_state;
379 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
380 &estimator_state);
381
382 return estimator_state;
383}
384
385EstimatorState Arm::WristEstimatorState() {
386 EstimatorState estimator_state;
387 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
388
389 return estimator_state;
390}
391
392} // namespace superstructure
393} // namespace control_loops
394} // namespace y2016