blob: 7cd67e3d90599f2d0c059072b2e80dac6df9761a [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) {
87 const auto &values = constants::GetValues();
88
89 // Limit the goal to min/max allowable angles.
90 if ((*goal)(0, 0) >= values.intake.limits.upper) {
91 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
92 values.intake.limits.upper);
93 (*goal)(0, 0) = values.intake.limits.upper;
94 }
95 if ((*goal)(0, 0) <= values.intake.limits.lower) {
96 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
97 values.intake.limits.lower);
98 (*goal)(0, 0) = values.intake.limits.lower;
99 }
100}
101
102void Intake::ForceGoal(double goal) {
103 set_unprofiled_goal(goal);
104 loop_->mutable_R() = unprofiled_goal_;
105 loop_->mutable_next_R() = loop_->R();
106
107 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
108}
109
110void Intake::set_unprofiled_goal(double unprofiled_goal) {
111 unprofiled_goal_(0, 0) = unprofiled_goal;
112 unprofiled_goal_(1, 0) = 0.0;
113 unprofiled_goal_(2, 0) = 0.0;
114 CapGoal("unprofiled R", &unprofiled_goal_);
115}
116
117void Intake::Update(bool disable) {
118 if (!disable) {
119 ::Eigen::Matrix<double, 2, 1> goal_state =
120 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
121
122 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
123 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
124 loop_->mutable_next_R(2, 0) = 0.0;
125 CapGoal("next R", &loop_->mutable_next_R());
126 }
127
128 loop_->Update(disable);
129
130 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
131 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
132 }
133}
134
135bool Intake::CheckHardLimits() {
136 const auto &values = constants::GetValues();
137 // Returns whether hard limits have been exceeded.
138
139 if (angle() >= values.intake.limits.upper_hard ||
140 angle() <= values.intake.limits.lower_hard) {
141 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
142 values.intake.limits.lower_hard, values.intake.limits.upper_hard);
143 return true;
144 }
145
146 return false;
147}
148
149void Intake::set_max_voltage(double voltage) {
150 loop_->set_max_voltage(voltage);
151}
152
153void Intake::AdjustProfile(double max_angular_velocity,
154 double max_angular_acceleration) {
155 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
156 profile_.set_maximum_acceleration(
157 UseUnlessZero(max_angular_acceleration, 10.0));
158}
159
160void Intake::Reset() {
161 estimator_.Reset();
162 initialized_ = false;
163 zeroed_ = false;
164}
165
166EstimatorState Intake::IntakeEstimatorState() {
167 EstimatorState estimator_state;
168 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
169
170 return estimator_state;
171}
172
173Arm::Arm()
174 : loop_(new DoubleCappedStateFeedbackLoop(
175 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
176 shoulder_profile_(::aos::controls::kLoopFrequency),
177 wrist_profile_(::aos::controls::kLoopFrequency),
178 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
179 wrist_estimator_(constants::GetValues().wrist.zeroing) {
180 Y_.setZero();
181 offset_.setZero();
182 unprofiled_goal_.setZero();
183 AdjustProfile(0.0, 0.0, 0.0, 0.0);
184}
185
186void Arm::UpdateWristOffset(double offset) {
187 const double doffset = offset - offset_(1, 0);
188 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
189
190 loop_->mutable_X_hat()(2, 0) += doffset;
191 Y_(1, 0) += doffset;
192 loop_->mutable_R(2, 0) += doffset;
193 loop_->mutable_next_R(2, 0) += doffset;
194 unprofiled_goal_(2, 0) += doffset;
195
196 wrist_profile_.MoveGoal(doffset);
197 offset_(1, 0) = offset;
198
199 CapGoal("R", &loop_->mutable_R());
200 CapGoal("unprofiled R", &loop_->mutable_next_R());
201}
202
203void Arm::UpdateShoulderOffset(double offset) {
204 const double doffset = offset - offset_(0, 0);
205 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
206
207 loop_->mutable_X_hat()(0, 0) += doffset;
208 loop_->mutable_X_hat()(2, 0) += doffset;
209 Y_(0, 0) += doffset;
210 loop_->mutable_R(0, 0) += doffset;
211 loop_->mutable_R(2, 0) += doffset;
212 loop_->mutable_next_R(0, 0) += doffset;
213 loop_->mutable_next_R(2, 0) += doffset;
214 unprofiled_goal_(0, 0) += doffset;
215 unprofiled_goal_(2, 0) += doffset;
216
217 shoulder_profile_.MoveGoal(doffset);
218 wrist_profile_.MoveGoal(doffset);
219 offset_(0, 0) = offset;
220
221 CapGoal("R", &loop_->mutable_R());
222 CapGoal("unprofiled R", &loop_->mutable_next_R());
223}
224
225// TODO(austin): Handle zeroing errors.
226
227void Arm::Correct(PotAndIndexPosition position_shoulder,
228 PotAndIndexPosition position_wrist) {
229 shoulder_estimator_.UpdateEstimate(position_shoulder);
230 wrist_estimator_.UpdateEstimate(position_wrist);
231
232 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.
258 const auto &values = constants::GetValues();
259
260 if ((*goal)(0, 0) >= values.shoulder.limits.upper) {
261 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
262 values.shoulder.limits.upper);
263 (*goal)(0, 0) = values.shoulder.limits.upper;
264 }
265 if ((*goal)(0, 0) <= values.shoulder.limits.lower) {
266 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
267 values.shoulder.limits.lower);
268 (*goal)(0, 0) = values.shoulder.limits.lower;
269 }
270
271 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
272
273 if (wrist_goal_angle_ungrounded >= values.wrist.limits.upper) {
274 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
275 wrist_goal_angle_ungrounded, values.wrist.limits.upper);
276 (*goal)(2, 0) = values.wrist.limits.upper + (*goal)(0, 0);
277 }
278 if (wrist_goal_angle_ungrounded <= values.wrist.limits.lower) {
279 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
280 wrist_goal_angle_ungrounded, values.wrist.limits.lower);
281 (*goal)(2, 0) = values.wrist.limits.lower + (*goal)(0, 0);
282 }
283}
284
285void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
286 set_unprofiled_goal(goal_shoulder, goal_wrist);
287 loop_->mutable_R() = unprofiled_goal_;
288 loop_->mutable_next_R() = loop_->R();
289
290 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
291 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
292}
293
294void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
295 double unprofiled_goal_wrist) {
296 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
297 0.0, 0.0;
298 CapGoal("unprofiled R", &unprofiled_goal_);
299}
300
301void Arm::AdjustProfile(double max_angular_velocity_shoulder,
302 double max_angular_acceleration_shoulder,
303 double max_angular_velocity_wrist,
304 double max_angular_acceleration_wrist) {
305 shoulder_profile_.set_maximum_velocity(
306 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
307 shoulder_profile_.set_maximum_acceleration(
308 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
309 wrist_profile_.set_maximum_velocity(
310 UseUnlessZero(max_angular_velocity_wrist, 10.0));
311 wrist_profile_.set_maximum_acceleration(
312 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
313}
314
315bool Arm::CheckHardLimits() {
316 const auto &values = constants::GetValues();
317 if (shoulder_angle() >= values.shoulder.limits.upper_hard ||
318 shoulder_angle() <= values.shoulder.limits.lower_hard) {
319 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
320 shoulder_angle(), values.shoulder.limits.lower_hard,
321 values.shoulder.limits.upper_hard);
322 return true;
323 }
324
325 if (wrist_angle() - shoulder_angle() >= values.wrist.limits.upper_hard ||
326 wrist_angle() - shoulder_angle() <= values.wrist.limits.lower_hard) {
327 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
328 wrist_angle() - shoulder_angle(), values.wrist.limits.lower_hard,
329 values.wrist.limits.upper_hard);
330 return true;
331 }
332
333 return false;
334}
335
336void Arm::Update(bool disable) {
337 if (!disable) {
338 // Compute next goal.
339 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
340 shoulder_profile_.Update(unprofiled_goal_(0, 0),
341 unprofiled_goal_(1, 0));
342 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
343 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
344
345 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
346 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
347 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
348 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
349
350 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
351 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
352 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)) {
360 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
361 }
362
363 // Wrist saturated
364 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
365 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
366 }
367}
368
369void Arm::set_max_voltage(double shoulder_max_voltage,
370 double wrist_max_voltage) {
371 loop_->set_max_voltage(shoulder_max_voltage, wrist_max_voltage);
372}
373
374void Arm::Reset() {
375 shoulder_estimator_.Reset();
376 wrist_estimator_.Reset();
377 initialized_ = false;
378 shoulder_zeroed_ = false;
379 wrist_zeroed_ = false;
380}
381
382EstimatorState Arm::ShoulderEstimatorState() {
383 EstimatorState estimator_state;
384 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
385 &estimator_state);
386
387 return estimator_state;
388}
389
390EstimatorState Arm::WristEstimatorState() {
391 EstimatorState estimator_state;
392 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
393
394 return estimator_state;
395}
396
397} // namespace superstructure
398} // namespace control_loops
399} // namespace y2016