blob: e414f935046d61ee26067cadb4d3d333c10b45b5 [file] [log] [blame]
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08001#include "frc971/control_loops/fridge/fridge.h"
2
Austin Schuh703b8d42015-02-01 14:56:34 -08003#include <cmath>
4
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08005#include "aos/common/controls/control_loops.q.h"
6#include "aos/common/logging/logging.h"
7
8#include "frc971/control_loops/fridge/elevator_motor_plant.h"
Austin Schuh8de10c72015-02-27 23:33:40 -08009#include "frc971/control_loops/fridge/integral_arm_plant.h"
Austin Schuhb966c432015-02-16 15:47:18 -080010#include "frc971/control_loops/voltage_cap/voltage_cap.h"
Austin Schuh703b8d42015-02-01 14:56:34 -080011#include "frc971/zeroing/zeroing.h"
12
13#include "frc971/constants.h"
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080014
15namespace frc971 {
16namespace control_loops {
17
Austin Schuh703b8d42015-02-01 14:56:34 -080018namespace {
Austin Schuhb966c432015-02-16 15:47:18 -080019constexpr double kZeroingVoltage = 4.0;
20constexpr double kElevatorZeroingVelocity = 0.10;
Daniel Pettie1bb13e2015-02-17 13:59:15 -080021// What speed we move to our safe height at.
Austin Schuhb3ae2592015-03-15 00:33:38 -070022constexpr double kElevatorSafeHeightVelocity = 0.3;
Austin Schuhb966c432015-02-16 15:47:18 -080023constexpr double kArmZeroingVelocity = 0.20;
Austin Schuh703b8d42015-02-01 14:56:34 -080024} // namespace
25
Austin Schuh8de10c72015-02-27 23:33:40 -080026template <int S>
27void CappedStateFeedbackLoop<S>::CapU() {
28 VoltageCap(max_voltage_, this->U(0, 0), this->U(1, 0), &this->mutable_U(0, 0),
29 &this->mutable_U(1, 0));
Austin Schuh703b8d42015-02-01 14:56:34 -080030}
31
Austin Schuh8de10c72015-02-27 23:33:40 -080032template <int S>
Austin Schuh703b8d42015-02-01 14:56:34 -080033Eigen::Matrix<double, 2, 1>
Austin Schuh8de10c72015-02-27 23:33:40 -080034CappedStateFeedbackLoop<S>::UnsaturateOutputGoalChange() {
Austin Schuh703b8d42015-02-01 14:56:34 -080035 // Compute the K matrix used to compensate for position errors.
36 Eigen::Matrix<double, 2, 2> Kp;
37 Kp.setZero();
Austin Schuh8de10c72015-02-27 23:33:40 -080038 Kp.col(0) = this->K().col(0);
39 Kp.col(1) = this->K().col(2);
Austin Schuh703b8d42015-02-01 14:56:34 -080040
41 Eigen::Matrix<double, 2, 2> Kp_inv = Kp.inverse();
42
43 // Compute how much we need to change R in order to achieve the change in U
44 // that was observed.
Austin Schuh8de10c72015-02-27 23:33:40 -080045 Eigen::Matrix<double, 2, 1> deltaR =
46 -Kp_inv * (this->U_uncapped() - this->U());
Austin Schuh703b8d42015-02-01 14:56:34 -080047 return deltaR;
48}
49
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080050Fridge::Fridge(control_loops::FridgeQueue *fridge)
Brian Silverman089f5812015-02-15 01:58:19 -050051 : aos::controls::ControlLoop<control_loops::FridgeQueue>(fridge),
Austin Schuh8de10c72015-02-27 23:33:40 -080052 arm_loop_(new CappedStateFeedbackLoop<5>(
53 StateFeedbackLoop<5, 2, 2>(MakeIntegralArmLoop()))),
54 elevator_loop_(new CappedStateFeedbackLoop<4>(
Austin Schuh703b8d42015-02-01 14:56:34 -080055 StateFeedbackLoop<4, 2, 2>(MakeElevatorLoop()))),
Daniel Pettia7827412015-02-13 20:55:57 -080056 left_arm_estimator_(constants::GetValues().fridge.left_arm_zeroing),
57 right_arm_estimator_(constants::GetValues().fridge.right_arm_zeroing),
Austin Schuh482a4e12015-02-14 22:43:43 -080058 left_elevator_estimator_(constants::GetValues().fridge.left_elev_zeroing),
Austin Schuh703b8d42015-02-01 14:56:34 -080059 right_elevator_estimator_(
Philipp Schrader3e281412015-03-01 23:48:23 +000060 constants::GetValues().fridge.right_elev_zeroing),
Philipp Schrader085bf012015-03-15 01:43:11 +000061 last_profiling_type_(ProfilingType::ANGLE_HEIGHT_PROFILING),
62 kinematics_(constants::GetValues().fridge.arm_length,
63 constants::GetValues().fridge.elevator.upper_limit,
64 constants::GetValues().fridge.elevator.lower_limit,
65 constants::GetValues().fridge.arm.upper_limit,
66 constants::GetValues().fridge.arm.lower_limit),
Philipp Schrader3e281412015-03-01 23:48:23 +000067 arm_profile_(::aos::controls::kLoopFrequency),
Philipp Schrader085bf012015-03-15 01:43:11 +000068 elevator_profile_(::aos::controls::kLoopFrequency),
69 x_profile_(::aos::controls::kLoopFrequency),
70 y_profile_(::aos::controls::kLoopFrequency) {}
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080071
Austin Schuh703b8d42015-02-01 14:56:34 -080072void Fridge::UpdateZeroingState() {
Austin Schuh47bf6902015-02-14 22:46:22 -080073 if (left_elevator_estimator_.offset_ratio_ready() < 1.0 ||
74 right_elevator_estimator_.offset_ratio_ready() < 1.0 ||
75 left_arm_estimator_.offset_ratio_ready() < 1.0 ||
76 right_arm_estimator_.offset_ratio_ready() < 1.0) {
Austin Schuh703b8d42015-02-01 14:56:34 -080077 state_ = INITIALIZING;
78 } else if (!left_elevator_estimator_.zeroed() ||
Adam Snaider3cd11c52015-02-16 02:16:09 +000079 !right_elevator_estimator_.zeroed()) {
Austin Schuh703b8d42015-02-01 14:56:34 -080080 state_ = ZEROING_ELEVATOR;
Adam Snaider3cd11c52015-02-16 02:16:09 +000081 } else if (!left_arm_estimator_.zeroed() || !right_arm_estimator_.zeroed()) {
Austin Schuh703b8d42015-02-01 14:56:34 -080082 state_ = ZEROING_ARM;
83 } else {
84 state_ = RUNNING;
85 }
86}
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080087
Austin Schuh703b8d42015-02-01 14:56:34 -080088void Fridge::Correct() {
89 {
90 Eigen::Matrix<double, 2, 1> Y;
91 Y << left_elevator(), right_elevator();
92 elevator_loop_->Correct(Y);
93 }
94
95 {
96 Eigen::Matrix<double, 2, 1> Y;
97 Y << left_arm(), right_arm();
98 arm_loop_->Correct(Y);
99 }
100}
101
102void Fridge::SetElevatorOffset(double left_offset, double right_offset) {
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800103 LOG(INFO, "Changing Elevator offset from %f, %f to %f, %f\n",
104 left_elevator_offset_, right_elevator_offset_, left_offset, right_offset);
Austin Schuh703b8d42015-02-01 14:56:34 -0800105 double left_doffset = left_offset - left_elevator_offset_;
106 double right_doffset = right_offset - right_elevator_offset_;
107
108 // Adjust the average height and height difference between the two sides.
109 // The derivatives of both should not need to be updated since the speeds
110 // haven't changed.
111 // The height difference is calculated as left - right, not right - left.
112 elevator_loop_->mutable_X_hat(0, 0) += (left_doffset + right_doffset) / 2;
113 elevator_loop_->mutable_X_hat(2, 0) += (left_doffset - right_doffset) / 2;
114
115 // Modify the zeroing goal.
116 elevator_goal_ += (left_doffset + right_doffset) / 2;
117
118 // Update the cached offset values to the actual values.
119 left_elevator_offset_ = left_offset;
120 right_elevator_offset_ = right_offset;
121}
122
123void Fridge::SetArmOffset(double left_offset, double right_offset) {
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800124 LOG(INFO, "Changing Arm offset from %f, %f to %f, %f\n", left_arm_offset_,
125 right_arm_offset_, left_offset, right_offset);
Austin Schuh703b8d42015-02-01 14:56:34 -0800126 double left_doffset = left_offset - left_arm_offset_;
127 double right_doffset = right_offset - right_arm_offset_;
128
129 // Adjust the average angle and angle difference between the two sides.
130 // The derivatives of both should not need to be updated since the speeds
131 // haven't changed.
132 arm_loop_->mutable_X_hat(0, 0) += (left_doffset + right_doffset) / 2;
133 arm_loop_->mutable_X_hat(2, 0) += (left_doffset - right_doffset) / 2;
134
135 // Modify the zeroing goal.
136 arm_goal_ += (left_doffset + right_doffset) / 2;
137
138 // Update the cached offset values to the actual values.
139 left_arm_offset_ = left_offset;
140 right_arm_offset_ = right_offset;
141}
142
143double Fridge::estimated_left_elevator() {
144 return current_position_.elevator.left.encoder +
145 left_elevator_estimator_.offset();
146}
147double Fridge::estimated_right_elevator() {
148 return current_position_.elevator.right.encoder +
149 right_elevator_estimator_.offset();
150}
151
152double Fridge::estimated_elevator() {
153 return (estimated_left_elevator() + estimated_right_elevator()) / 2.0;
154}
155
156double Fridge::estimated_left_arm() {
Austin Schuhb3ae2592015-03-15 00:33:38 -0700157 return current_position_.arm.left.encoder + left_arm_estimator_.offset();
Austin Schuh703b8d42015-02-01 14:56:34 -0800158}
159double Fridge::estimated_right_arm() {
Austin Schuhb3ae2592015-03-15 00:33:38 -0700160 return current_position_.arm.right.encoder + right_arm_estimator_.offset();
Austin Schuh703b8d42015-02-01 14:56:34 -0800161}
162double Fridge::estimated_arm() {
163 return (estimated_left_arm() + estimated_right_arm()) / 2.0;
164}
165
166double Fridge::left_elevator() {
167 return current_position_.elevator.left.encoder + left_elevator_offset_;
168}
169double Fridge::right_elevator() {
170 return current_position_.elevator.right.encoder + right_elevator_offset_;
171}
172
173double Fridge::elevator() { return (left_elevator() + right_elevator()) / 2.0; }
174
175double Fridge::left_arm() {
176 return current_position_.arm.left.encoder + left_arm_offset_;
177}
178double Fridge::right_arm() {
179 return current_position_.arm.right.encoder + right_arm_offset_;
180}
181double Fridge::arm() { return (left_arm() + right_arm()) / 2.0; }
182
183double Fridge::elevator_zeroing_velocity() {
184 double average_elevator =
185 (constants::GetValues().fridge.elevator.lower_limit +
186 constants::GetValues().fridge.elevator.upper_limit) /
187 2.0;
188
189 const double pulse_width = ::std::max(
Daniel Pettia7827412015-02-13 20:55:57 -0800190 constants::GetValues().fridge.left_elev_zeroing.index_difference,
191 constants::GetValues().fridge.right_elev_zeroing.index_difference);
Austin Schuh703b8d42015-02-01 14:56:34 -0800192
193 if (elevator_zeroing_velocity_ == 0) {
194 if (estimated_elevator() > average_elevator) {
195 elevator_zeroing_velocity_ = -kElevatorZeroingVelocity;
196 } else {
197 elevator_zeroing_velocity_ = kElevatorZeroingVelocity;
198 }
199 } else if (elevator_zeroing_velocity_ > 0 &&
Austin Schuh9e37c322015-02-16 15:47:49 -0800200 estimated_elevator() > average_elevator + 1.1 * pulse_width) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800201 elevator_zeroing_velocity_ = -kElevatorZeroingVelocity;
202 } else if (elevator_zeroing_velocity_ < 0 &&
Austin Schuh9e37c322015-02-16 15:47:49 -0800203 estimated_elevator() < average_elevator - 1.1 * pulse_width) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800204 elevator_zeroing_velocity_ = kElevatorZeroingVelocity;
205 }
206 return elevator_zeroing_velocity_;
207}
208
Philipp Schrader3e281412015-03-01 23:48:23 +0000209double Fridge::UseUnlessZero(double target_value, double default_value) {
210 if (target_value != 0.0) {
211 return target_value;
212 } else {
213 return default_value;
214 }
215}
216
Austin Schuh703b8d42015-02-01 14:56:34 -0800217double Fridge::arm_zeroing_velocity() {
218 const double average_arm = (constants::GetValues().fridge.arm.lower_limit +
219 constants::GetValues().fridge.arm.upper_limit) /
220 2.0;
221 const double pulse_width = ::std::max(
Daniel Pettia7827412015-02-13 20:55:57 -0800222 constants::GetValues().fridge.right_arm_zeroing.index_difference,
223 constants::GetValues().fridge.left_arm_zeroing.index_difference);
Austin Schuh703b8d42015-02-01 14:56:34 -0800224
225 if (arm_zeroing_velocity_ == 0) {
226 if (estimated_arm() > average_arm) {
227 arm_zeroing_velocity_ = -kArmZeroingVelocity;
228 } else {
229 arm_zeroing_velocity_ = kArmZeroingVelocity;
230 }
231 } else if (arm_zeroing_velocity_ > 0.0 &&
Austin Schuh9e37c322015-02-16 15:47:49 -0800232 estimated_arm() > average_arm + 1.1 * pulse_width) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800233 arm_zeroing_velocity_ = -kArmZeroingVelocity;
Austin Schuhb3ae2592015-03-15 00:33:38 -0700234 } else if (arm_zeroing_velocity_ < 0.0 && estimated_arm() < average_arm) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800235 arm_zeroing_velocity_ = kArmZeroingVelocity;
236 }
237 return arm_zeroing_velocity_;
238}
239
Austin Schuh482a4e12015-02-14 22:43:43 -0800240void Fridge::RunIteration(const control_loops::FridgeQueue::Goal *unsafe_goal,
Austin Schuh703b8d42015-02-01 14:56:34 -0800241 const control_loops::FridgeQueue::Position *position,
242 control_loops::FridgeQueue::Output *output,
243 control_loops::FridgeQueue::Status *status) {
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800244 if (WasReset()) {
245 LOG(ERROR, "WPILib reset, restarting\n");
246 left_elevator_estimator_.Reset();
247 right_elevator_estimator_.Reset();
248 left_arm_estimator_.Reset();
249 right_arm_estimator_.Reset();
250 state_ = UNINITIALIZED;
251 }
252
Austin Schuh703b8d42015-02-01 14:56:34 -0800253 // Get a reference to the constants struct since we use it so often in this
254 // code.
Philipp Schrader82c65072015-02-16 00:47:09 +0000255 const auto &values = constants::GetValues();
Austin Schuh703b8d42015-02-01 14:56:34 -0800256
257 // Bool to track if we should turn the motors on or not.
258 bool disable = output == nullptr;
Austin Schuh703b8d42015-02-01 14:56:34 -0800259
260 // Save the current position so it can be used easily in the class.
261 current_position_ = *position;
262
263 left_elevator_estimator_.UpdateEstimate(position->elevator.left);
264 right_elevator_estimator_.UpdateEstimate(position->elevator.right);
265 left_arm_estimator_.UpdateEstimate(position->arm.left);
266 right_arm_estimator_.UpdateEstimate(position->arm.right);
267
268 if (state_ != UNINITIALIZED) {
269 Correct();
270 }
271
272 // Zeroing will work as follows:
273 // At startup, record the offset of the two halves of the two subsystems.
274 // Then, start moving the elevator towards the center until both halves are
275 // zeroed.
276 // Then, start moving the claw towards the center until both halves are
277 // zeroed.
278 // Then, done!
279
280 // We'll then need code to do sanity checking on values.
281
282 // Now, we need to figure out which way to go.
283
284 switch (state_) {
285 case UNINITIALIZED:
286 LOG(DEBUG, "Uninitialized\n");
287 // Startup. Assume that we are at the origin everywhere.
288 // This records the encoder offset between the two sides of the elevator.
289 left_elevator_offset_ = -position->elevator.left.encoder;
290 right_elevator_offset_ = -position->elevator.right.encoder;
291 left_arm_offset_ = -position->arm.left.encoder;
292 right_arm_offset_ = -position->arm.right.encoder;
Austin Schuhaab01572015-02-15 00:12:35 -0800293 elevator_loop_->mutable_X_hat().setZero();
294 arm_loop_->mutable_X_hat().setZero();
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800295 LOG(INFO, "Initializing arm offsets to %f, %f\n", left_arm_offset_,
296 right_arm_offset_);
297 LOG(INFO, "Initializing elevator offsets to %f, %f\n",
298 left_elevator_offset_, right_elevator_offset_);
Austin Schuh703b8d42015-02-01 14:56:34 -0800299 Correct();
300 state_ = INITIALIZING;
301 disable = true;
302 break;
303
304 case INITIALIZING:
305 LOG(DEBUG, "Waiting for accurate initial position.\n");
306 disable = true;
307 // Update state_ to accurately represent the state of the zeroing
308 // estimators.
309 UpdateZeroingState();
310 if (state_ != INITIALIZING) {
311 // Set the goals to where we are now.
312 elevator_goal_ = elevator();
313 arm_goal_ = arm();
314 }
315 break;
316
317 case ZEROING_ELEVATOR:
318 LOG(DEBUG, "Zeroing elevator\n");
Austin Schuh703b8d42015-02-01 14:56:34 -0800319
320 // Update state_ to accurately represent the state of the zeroing
321 // estimators.
322 UpdateZeroingState();
323 if (left_elevator_estimator_.zeroed() &&
324 right_elevator_estimator_.zeroed()) {
325 SetElevatorOffset(left_elevator_estimator_.offset(),
326 right_elevator_estimator_.offset());
327 LOG(DEBUG, "Zeroed the elevator!\n");
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800328
Austin Schuh5e872c82015-02-17 22:59:08 -0800329 if (elevator() < values.fridge.arm_zeroing_height &&
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800330 state_ != INITIALIZING) {
331 // Move the elevator to a safe height before we start zeroing the arm,
332 // so that we don't crash anything.
333 LOG(DEBUG, "Moving elevator to safe height.\n");
Brian Silverman283849f2015-03-28 01:25:16 -0400334 if (elevator_goal_ < values.fridge.arm_zeroing_height) {
335 elevator_goal_ += kElevatorSafeHeightVelocity *
336 ::aos::controls::kLoopFrequency.ToSeconds();
337 elevator_goal_velocity_ = kElevatorSafeHeightVelocity;
338 } else {
339 // We want it stopped at whatever height it's currently set to.
340 elevator_goal_velocity_ = 0;
341 }
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800342
343 state_ = ZEROING_ELEVATOR;
344 break;
345 }
346
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800347 } else if (!disable) {
Philipp Schrader085bf012015-03-15 01:43:11 +0000348 elevator_goal_velocity_ = elevator_zeroing_velocity();
349 elevator_goal_ += elevator_goal_velocity_ *
Adam Snaider3cd11c52015-02-16 02:16:09 +0000350 ::aos::controls::kLoopFrequency.ToSeconds();
Austin Schuh703b8d42015-02-01 14:56:34 -0800351 }
Philipp Schrader3e281412015-03-01 23:48:23 +0000352
353 // Bypass motion profiles while we are zeroing.
354 // This is also an important step right after the elevator is zeroed and
355 // we reach into the elevator's state matrix and change it based on the
356 // newly-obtained offset.
357 {
358 Eigen::Matrix<double, 2, 1> current;
359 current.setZero();
Philipp Schrader085bf012015-03-15 01:43:11 +0000360 current << elevator_goal_, elevator_goal_velocity_;
Philipp Schrader3e281412015-03-01 23:48:23 +0000361 elevator_profile_.MoveCurrentState(current);
362 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800363 break;
364
365 case ZEROING_ARM:
366 LOG(DEBUG, "Zeroing the arm\n");
Austin Schuh703b8d42015-02-01 14:56:34 -0800367
Brian Silverman283849f2015-03-28 01:25:16 -0400368 if (elevator() < values.fridge.arm_zeroing_height) {
369 LOG(INFO,
370 "Going back to ZEROING_ELEVATOR until it gets high enough to "
371 "safely zero the arm\n");
372 state_ = ZEROING_ELEVATOR;
373 break;
374 }
375
Austin Schuh703b8d42015-02-01 14:56:34 -0800376 // Update state_ to accurately represent the state of the zeroing
377 // estimators.
378 UpdateZeroingState();
379 if (left_arm_estimator_.zeroed() && right_arm_estimator_.zeroed()) {
380 SetArmOffset(left_arm_estimator_.offset(),
381 right_arm_estimator_.offset());
382 LOG(DEBUG, "Zeroed the arm!\n");
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800383 } else if (!disable) {
Philipp Schrader085bf012015-03-15 01:43:11 +0000384 arm_goal_velocity_ = arm_zeroing_velocity();
Adam Snaider3cd11c52015-02-16 02:16:09 +0000385 arm_goal_ +=
Philipp Schrader085bf012015-03-15 01:43:11 +0000386 arm_goal_velocity_ * ::aos::controls::kLoopFrequency.ToSeconds();
Austin Schuh703b8d42015-02-01 14:56:34 -0800387 }
Philipp Schrader3e281412015-03-01 23:48:23 +0000388
389 // Bypass motion profiles while we are zeroing.
390 // This is also an important step right after the arm is zeroed and
391 // we reach into the arm's state matrix and change it based on the
392 // newly-obtained offset.
393 {
394 Eigen::Matrix<double, 2, 1> current;
395 current.setZero();
Philipp Schrader085bf012015-03-15 01:43:11 +0000396 current << arm_goal_, arm_goal_velocity_;
Philipp Schrader3e281412015-03-01 23:48:23 +0000397 arm_profile_.MoveCurrentState(current);
398 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800399 break;
400
401 case RUNNING:
402 LOG(DEBUG, "Running!\n");
Austin Schuh482a4e12015-02-14 22:43:43 -0800403 if (unsafe_goal) {
Philipp Schrader085bf012015-03-15 01:43:11 +0000404 // Handle the case where we switch between the types of profiling.
405 ProfilingType new_profiling_type =
406 static_cast<ProfilingType>(unsafe_goal->profiling_type);
Philipp Schrader3e281412015-03-01 23:48:23 +0000407
Philipp Schrader085bf012015-03-15 01:43:11 +0000408 if (last_profiling_type_ != new_profiling_type) {
409 // Reset the height/angle profiles.
410 Eigen::Matrix<double, 2, 1> current;
411 current.setZero();
412 current << arm_goal_, arm_goal_velocity_;
413 arm_profile_.MoveCurrentState(current);
414 current << elevator_goal_, elevator_goal_velocity_;
415 elevator_profile_.MoveCurrentState(current);
Philipp Schrader3e281412015-03-01 23:48:23 +0000416
Philipp Schrader085bf012015-03-15 01:43:11 +0000417 // Reset the x/y profiles.
418 aos::util::ElevatorArmKinematics::KinematicResult x_y_result;
419 kinematics_.ForwardKinematic(elevator_goal_, arm_goal_,
420 elevator_goal_velocity_,
421 arm_goal_velocity_, &x_y_result);
422 current << x_y_result.fridge_x, x_y_result.fridge_x_velocity;
423 x_profile_.MoveCurrentState(current);
424 current << x_y_result.fridge_h, x_y_result.fridge_h_velocity;
425 y_profile_.MoveCurrentState(current);
426
427 last_profiling_type_ = new_profiling_type;
428 }
429
430 if (new_profiling_type == ProfilingType::ANGLE_HEIGHT_PROFILING) {
431 // Pick a set of sane arm defaults if none are specified.
432 arm_profile_.set_maximum_velocity(
433 UseUnlessZero(unsafe_goal->max_angular_velocity, 1.0));
434 arm_profile_.set_maximum_acceleration(
435 UseUnlessZero(unsafe_goal->max_angular_acceleration, 3.0));
436 elevator_profile_.set_maximum_velocity(
437 UseUnlessZero(unsafe_goal->max_velocity, 0.50));
438 elevator_profile_.set_maximum_acceleration(
439 UseUnlessZero(unsafe_goal->max_acceleration, 2.0));
440
441 // Use the profiles to limit the arm's movements.
442 const double unfiltered_arm_goal = ::std::max(
443 ::std::min(unsafe_goal->angle, values.fridge.arm.upper_limit),
444 values.fridge.arm.lower_limit);
445 ::Eigen::Matrix<double, 2, 1> arm_goal_state = arm_profile_.Update(
446 unfiltered_arm_goal, unsafe_goal->angular_velocity);
447 arm_goal_ = arm_goal_state(0, 0);
448 arm_goal_velocity_ = arm_goal_state(1, 0);
449
450 // Use the profiles to limit the elevator's movements.
451 const double unfiltered_elevator_goal =
452 ::std::max(::std::min(unsafe_goal->height,
453 values.fridge.elevator.upper_limit),
454 values.fridge.elevator.lower_limit);
455 ::Eigen::Matrix<double, 2, 1> elevator_goal_state =
456 elevator_profile_.Update(unfiltered_elevator_goal,
457 unsafe_goal->velocity);
458 elevator_goal_ = elevator_goal_state(0, 0);
459 elevator_goal_velocity_ = elevator_goal_state(1, 0);
460 } else if (new_profiling_type == ProfilingType::X_Y_PROFILING) {
461 // Use x/y profiling
462 aos::util::ElevatorArmKinematics::KinematicResult kinematic_result;
463
464 x_profile_.set_maximum_velocity(
465 UseUnlessZero(unsafe_goal->max_x_velocity, 0.5));
466 x_profile_.set_maximum_acceleration(
467 UseUnlessZero(unsafe_goal->max_x_acceleration, 2.0));
468 y_profile_.set_maximum_velocity(
469 UseUnlessZero(unsafe_goal->max_y_velocity, 0.50));
470 y_profile_.set_maximum_acceleration(
471 UseUnlessZero(unsafe_goal->max_y_acceleration, 2.0));
472
473 // Limit the goals before we update the profiles.
474 kinematics_.InverseKinematic(
475 unsafe_goal->x, unsafe_goal->y, unsafe_goal->x_velocity,
476 unsafe_goal->y_velocity, &kinematic_result);
477
478 // Use the profiles to limit the x movements.
479 ::Eigen::Matrix<double, 2, 1> x_goal_state = x_profile_.Update(
480 kinematic_result.fridge_x, kinematic_result.fridge_x_velocity);
481
482 // Use the profiles to limit the y movements.
483 ::Eigen::Matrix<double, 2, 1> y_goal_state = y_profile_.Update(
484 kinematic_result.fridge_h, kinematic_result.fridge_h_velocity);
485
486 // Convert x/y goal states into arm/elevator goals.
487 // The inverse kinematics functions automatically perform range
488 // checking and adjust the results so that they're always valid.
489 kinematics_.InverseKinematic(x_goal_state(0, 0), y_goal_state(0, 0),
490 x_goal_state(1, 0), y_goal_state(1, 0),
491 &kinematic_result);
492
493 // Store the appropriate inverse kinematic results in the
494 // arm/elevator goals.
495 arm_goal_ = kinematic_result.arm_angle;
496 arm_goal_velocity_ = kinematic_result.arm_velocity;
497
498 elevator_goal_ = kinematic_result.elevator_height;
499 elevator_goal_velocity_ = kinematic_result.arm_velocity;
500 } else {
501 LOG(ERROR, "Unknown profiling_type: %d\n",
502 unsafe_goal->profiling_type);
503 }
Austin Schuh482a4e12015-02-14 22:43:43 -0800504 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800505
506 // Update state_ to accurately represent the state of the zeroing
507 // estimators.
508 UpdateZeroingState();
Austin Schuh703b8d42015-02-01 14:56:34 -0800509
510 if (state_ != RUNNING && state_ != ESTOP) {
511 state_ = UNINITIALIZED;
512 }
513 break;
514
515 case ESTOP:
516 LOG(ERROR, "Estop\n");
517 disable = true;
518 break;
519 }
520
521 // Commence death if either left/right tracking error gets too big. This
522 // should run immediately after the SetArmOffset and SetElevatorOffset
523 // functions to double-check that the hardware is in a sane state.
524 if (::std::abs(left_arm() - right_arm()) >=
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800525 values.max_allowed_left_right_arm_difference) {
526 LOG(ERROR, "The arms are too far apart. |%f - %f| > %f\n", left_arm(),
527 right_arm(), values.max_allowed_left_right_arm_difference);
528
529 // Indicate an ESTOP condition and stop the motors.
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700530 if (output) {
531 state_ = ESTOP;
532 }
Austin Schuhdbd6bfa2015-02-14 21:25:16 -0800533 disable = true;
534 }
535
536 if (::std::abs(left_elevator() - right_elevator()) >=
537 values.max_allowed_left_right_elevator_difference) {
538 LOG(ERROR, "The elevators are too far apart. |%f - %f| > %f\n",
539 left_elevator(), right_elevator(),
540 values.max_allowed_left_right_elevator_difference);
Austin Schuh703b8d42015-02-01 14:56:34 -0800541
542 // Indicate an ESTOP condition and stop the motors.
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700543 if (output) {
544 state_ = ESTOP;
545 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800546 disable = true;
547 }
548
549 // Limit the goals so we can't exceed the hardware limits if we are RUNNING.
550 if (state_ == RUNNING) {
551 // Limit the arm goal to min/max allowable angles.
552 if (arm_goal_ >= values.fridge.arm.upper_limit) {
553 LOG(WARNING, "Arm goal above limit, %f > %f\n", arm_goal_,
554 values.fridge.arm.upper_limit);
555 arm_goal_ = values.fridge.arm.upper_limit;
556 }
557 if (arm_goal_ <= values.fridge.arm.lower_limit) {
558 LOG(WARNING, "Arm goal below limit, %f < %f\n", arm_goal_,
559 values.fridge.arm.lower_limit);
560 arm_goal_ = values.fridge.arm.lower_limit;
561 }
562
563 // Limit the elevator goal to min/max allowable heights.
564 if (elevator_goal_ >= values.fridge.elevator.upper_limit) {
565 LOG(WARNING, "Elevator goal above limit, %f > %f\n", elevator_goal_,
566 values.fridge.elevator.upper_limit);
567 elevator_goal_ = values.fridge.elevator.upper_limit;
568 }
569 if (elevator_goal_ <= values.fridge.elevator.lower_limit) {
570 LOG(WARNING, "Elevator goal below limit, %f < %f\n", elevator_goal_,
571 values.fridge.elevator.lower_limit);
572 elevator_goal_ = values.fridge.elevator.lower_limit;
573 }
574 }
575
576 // Check the lower level hardware limit as well.
577 if (state_ == RUNNING) {
578 if (left_arm() >= values.fridge.arm.upper_hard_limit ||
579 left_arm() <= values.fridge.arm.lower_hard_limit) {
580 LOG(ERROR, "Left arm at %f out of bounds [%f, %f], ESTOPing\n",
581 left_arm(), values.fridge.arm.lower_hard_limit,
582 values.fridge.arm.upper_hard_limit);
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700583 if (output) {
584 state_ = ESTOP;
585 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800586 }
587
588 if (right_arm() >= values.fridge.arm.upper_hard_limit ||
589 right_arm() <= values.fridge.arm.lower_hard_limit) {
590 LOG(ERROR, "Right arm at %f out of bounds [%f, %f], ESTOPing\n",
591 right_arm(), values.fridge.arm.lower_hard_limit,
592 values.fridge.arm.upper_hard_limit);
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700593 if (output) {
594 state_ = ESTOP;
595 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800596 }
597
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700598 if (left_elevator() >= values.fridge.elevator.upper_hard_limit) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800599 LOG(ERROR, "Left elevator at %f out of bounds [%f, %f], ESTOPing\n",
600 left_elevator(), values.fridge.elevator.lower_hard_limit,
601 values.fridge.elevator.upper_hard_limit);
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700602 if (output) {
603 state_ = ESTOP;
604 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800605 }
606
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700607 if (right_elevator() >= values.fridge.elevator.upper_hard_limit) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800608 LOG(ERROR, "Right elevator at %f out of bounds [%f, %f], ESTOPing\n",
609 right_elevator(), values.fridge.elevator.lower_hard_limit,
610 values.fridge.elevator.upper_hard_limit);
Brian Silverman10b8f4c2015-03-21 23:44:46 -0700611 if (output) {
612 state_ = ESTOP;
613 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800614 }
615 }
616
617 // Set the goals.
Philipp Schrader085bf012015-03-15 01:43:11 +0000618 arm_loop_->mutable_R() << arm_goal_, arm_goal_velocity_, 0.0, 0.0, 0.0;
619 elevator_loop_->mutable_R() << elevator_goal_, elevator_goal_velocity_, 0.0,
Austin Schuh703b8d42015-02-01 14:56:34 -0800620 0.0;
621
622 const double max_voltage = state_ == RUNNING ? 12.0 : kZeroingVoltage;
623 arm_loop_->set_max_voltage(max_voltage);
624 elevator_loop_->set_max_voltage(max_voltage);
625
626 if (state_ == ESTOP) {
627 disable = true;
628 }
629 arm_loop_->Update(disable);
630 elevator_loop_->Update(disable);
631
632 if (state_ == INITIALIZING || state_ == ZEROING_ELEVATOR ||
633 state_ == ZEROING_ARM) {
634 if (arm_loop_->U() != arm_loop_->U_uncapped()) {
635 Eigen::Matrix<double, 2, 1> deltaR =
636 arm_loop_->UnsaturateOutputGoalChange();
637
638 // Move the average arm goal by the amount observed.
639 LOG(WARNING, "Moving arm goal by %f to handle saturation\n",
640 deltaR(0, 0));
641 arm_goal_ += deltaR(0, 0);
642 }
643
644 if (elevator_loop_->U() != elevator_loop_->U_uncapped()) {
645 Eigen::Matrix<double, 2, 1> deltaR =
646 elevator_loop_->UnsaturateOutputGoalChange();
647
648 // Move the average elevator goal by the amount observed.
649 LOG(WARNING, "Moving elevator goal by %f to handle saturation\n",
650 deltaR(0, 0));
651 elevator_goal_ += deltaR(0, 0);
652 }
653 }
654
655 if (output) {
656 output->left_arm = arm_loop_->U(0, 0);
657 output->right_arm = arm_loop_->U(1, 0);
658 output->left_elevator = elevator_loop_->U(0, 0);
659 output->right_elevator = elevator_loop_->U(1, 0);
Austin Schuh482a4e12015-02-14 22:43:43 -0800660 if (unsafe_goal) {
661 output->grabbers = unsafe_goal->grabbers;
662 } else {
663 output->grabbers.top_front = false;
664 output->grabbers.top_back = false;
665 output->grabbers.bottom_front = false;
666 output->grabbers.bottom_back = false;
667 }
Austin Schuh703b8d42015-02-01 14:56:34 -0800668 }
669
670 // TODO(austin): Populate these fully.
Austin Schuh5ae4efd2015-02-15 23:34:22 -0800671 status->zeroed = state_ == RUNNING;
Philipp Schrader2a25b612015-03-28 23:49:06 +0000672
Austin Schuh703b8d42015-02-01 14:56:34 -0800673 status->angle = arm_loop_->X_hat(0, 0);
Philipp Schrader3e281412015-03-01 23:48:23 +0000674 status->angular_velocity = arm_loop_->X_hat(1, 0);
Austin Schuh703b8d42015-02-01 14:56:34 -0800675 status->height = elevator_loop_->X_hat(0, 0);
Philipp Schrader3e281412015-03-01 23:48:23 +0000676 status->velocity = elevator_loop_->X_hat(1, 0);
Philipp Schrader2a25b612015-03-28 23:49:06 +0000677
Austin Schuh5e872c82015-02-17 22:59:08 -0800678 status->goal_angle = arm_goal_;
Philipp Schrader085bf012015-03-15 01:43:11 +0000679 status->goal_angular_velocity = arm_goal_velocity_;
Austin Schuh5e872c82015-02-17 22:59:08 -0800680 status->goal_height = elevator_goal_;
Philipp Schrader085bf012015-03-15 01:43:11 +0000681 status->goal_velocity = elevator_goal_velocity_;
Philipp Schrader2a25b612015-03-28 23:49:06 +0000682
683 // Populate the same status, but in X/Y co-ordinates.
684 aos::util::ElevatorArmKinematics::KinematicResult x_y_status;
685 kinematics_.ForwardKinematic(status->height, status->angle,
686 status->velocity, status->angular_velocity,
687 &x_y_status);
688 status->x = x_y_status.fridge_x;
689 status->y = x_y_status.fridge_h;
690 status->x_velocity = x_y_status.fridge_x_velocity;
691 status->y_velocity = x_y_status.fridge_h_velocity;
692
693 kinematics_.ForwardKinematic(status->goal_height, status->goal_angle,
694 status->goal_velocity, status->goal_angular_velocity,
695 &x_y_status);
696 status->goal_x = x_y_status.fridge_x;
697 status->goal_y = x_y_status.fridge_h;
698 status->goal_x_velocity = x_y_status.fridge_x_velocity;
699 status->goal_y_velocity = x_y_status.fridge_h_velocity;
700
Austin Schuh482a4e12015-02-14 22:43:43 -0800701 if (unsafe_goal) {
702 status->grabbers = unsafe_goal->grabbers;
703 } else {
704 status->grabbers.top_front = false;
705 status->grabbers.top_back = false;
706 status->grabbers.bottom_front = false;
707 status->grabbers.bottom_back = false;
708 }
Adam Snaider3cd11c52015-02-16 02:16:09 +0000709 zeroing::PopulateEstimatorState(left_arm_estimator_, &status->left_arm_state);
Daniel Pettiab274232015-02-16 19:15:34 -0800710 zeroing::PopulateEstimatorState(right_arm_estimator_,
711 &status->right_arm_state);
712 zeroing::PopulateEstimatorState(left_elevator_estimator_,
Adam Snaider3cd11c52015-02-16 02:16:09 +0000713 &status->left_elevator_state);
Daniel Pettiab274232015-02-16 19:15:34 -0800714 zeroing::PopulateEstimatorState(right_elevator_estimator_,
Adam Snaider3cd11c52015-02-16 02:16:09 +0000715 &status->right_elevator_state);
Austin Schuh703b8d42015-02-01 14:56:34 -0800716 status->estopped = (state_ == ESTOP);
Austin Schuh482a4e12015-02-14 22:43:43 -0800717 status->state = state_;
Austin Schuh703b8d42015-02-01 14:56:34 -0800718 last_state_ = state_;
Ben Fredrickson6b5ba792015-01-25 17:14:40 -0800719}
720
721} // namespace control_loops
722} // namespace frc971