blob: 0749e3f9fd48fc4b0f6e5c95c7a7c93eb9c5aa5b [file] [log] [blame]
Austin Schuhcb091712018-02-21 20:01:55 -08001#include "y2018/control_loops/superstructure/arm/arm.h"
2
3#include <chrono>
4#include <iostream>
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/logging/logging.h"
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -08007#include "frc971/control_loops/double_jointed_arm/demo_path.h"
8#include "frc971/control_loops/double_jointed_arm/dynamics.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "y2018/constants.h"
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080010#include "y2018/control_loops/superstructure/arm/arm_constants.h"
Austin Schuh7dfccf62018-03-03 21:28:14 -080011#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Austin Schuhcb091712018-02-21 20:01:55 -080012
13namespace y2018 {
14namespace control_loops {
15namespace superstructure {
16namespace arm {
17
Austin Schuh7afcc232018-09-16 16:33:47 -070018namespace {
19
Austin Schuhcb091712018-02-21 20:01:55 -080020namespace chrono = ::std::chrono;
21using ::aos::monotonic_clock;
22
Austin Schuh7afcc232018-09-16 16:33:47 -070023constexpr int kMaxBrownoutCount = 4;
24
25} // namespace
26
Austin Schuhcb091712018-02-21 20:01:55 -080027Arm::Arm()
28 : proximal_zeroing_estimator_(constants::GetValues().arm_proximal.zeroing),
29 distal_zeroing_estimator_(constants::GetValues().arm_distal.zeroing),
30 alpha_unitizer_((::Eigen::Matrix<double, 2, 2>() << 1.0 / kAlpha0Max(),
31 0.0, 0.0, 1.0 / kAlpha1Max())
32 .finished()),
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080033 dynamics_(kArmConstants),
34 search_graph_(MakeSearchGraph(&dynamics_, &trajectories_, alpha_unitizer_,
35 kVMax())),
36 arm_ekf_(&dynamics_),
Austin Schuhcb091712018-02-21 20:01:55 -080037 // Go to the start of the first trajectory.
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080038 follower_(&dynamics_, ReadyAboveBoxPoint()),
Austin Schuhb874fd32018-03-05 00:27:10 -080039 points_(PointList()) {
Austin Schuh7dfccf62018-03-03 21:28:14 -080040 int i = 0;
41 for (const auto &trajectory : trajectories_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070042 AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i,
43 trajectory.trajectory.path().length());
Austin Schuh7dfccf62018-03-03 21:28:14 -080044 ++i;
Austin Schuhcb091712018-02-21 20:01:55 -080045 }
46}
47
48void Arm::Reset() { state_ = State::UNINITIALIZED; }
49
Alex Perrycb7da4b2019-08-28 19:35:56 -070050flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate(
51 const ::aos::monotonic_clock::time_point monotonic_now,
52 const uint32_t *unsafe_goal, bool grab_box, bool open_claw, bool close_claw,
53 const superstructure::ArmPosition *position,
54 const bool claw_beambreak_triggered,
55 const bool box_back_beambreak_triggered, const bool intake_clear_of_box,
56 bool suicide, bool trajectory_override, double *proximal_output,
57 double *distal_output, bool *release_arm_brake, bool *claw_closed,
58 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuhcb091712018-02-21 20:01:55 -080059 ::Eigen::Matrix<double, 2, 1> Y;
Austin Schuh96341532018-03-09 21:17:24 -080060 const bool outputs_disabled =
61 ((proximal_output == nullptr) || (distal_output == nullptr) ||
62 (release_arm_brake == nullptr) || (claw_closed == nullptr));
Austin Schuh7afcc232018-09-16 16:33:47 -070063 if (outputs_disabled) {
64 ++brownout_count_;
65 } else {
66 brownout_count_ = 0;
67 }
Austin Schuh96341532018-03-09 21:17:24 -080068
69 uint32_t filtered_goal = 0;
70 if (unsafe_goal != nullptr) {
71 filtered_goal = *unsafe_goal;
72 }
73
74 if (open_claw) {
75 claw_closed_ = false;
76 }
Neil Balchba9cbba2018-04-06 22:26:38 -070077 if (close_claw) {
Austin Schuh96341532018-03-09 21:17:24 -080078 claw_closed_ = true;
79 }
Neil Balchba9cbba2018-04-06 22:26:38 -070080 if (outputs_disabled) {
81 if (claw_closed_count_ == 0) {
82 claw_closed_ = true;
83 } else {
84 --claw_closed_count_;
85 }
86 } else {
87 // Wait this many iterations before closing the claw. That prevents
88 // brownouts from closing the claw.
89 claw_closed_count_ = 50;
90 }
Austin Schuhcb091712018-02-21 20:01:55 -080091
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 Y << position->proximal()->encoder() + proximal_offset_,
93 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -080094
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 proximal_zeroing_estimator_.UpdateEstimate(*position->proximal());
96 distal_zeroing_estimator_.UpdateEstimate(*position->distal());
Austin Schuhcb091712018-02-21 20:01:55 -080097
98 if (proximal_output != nullptr) {
99 *proximal_output = 0.0;
100 }
101 if (distal_output != nullptr) {
102 *distal_output = 0.0;
103 }
104
105 arm_ekf_.Correct(Y, kDt());
106
107 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
108 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05) {
109 close_enough_for_full_power_ = true;
110 }
111 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
112 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10) {
113 close_enough_for_full_power_ = false;
114 }
115
116 switch (state_) {
117 case State::UNINITIALIZED:
118 // Wait in the uninitialized state until the intake is initialized.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800120 state_ = State::ZEROING;
121 proximal_zeroing_estimator_.Reset();
122 distal_zeroing_estimator_.Reset();
Austin Schuhcb091712018-02-21 20:01:55 -0800123 break;
124
125 case State::ZEROING:
126 // Zero by not moving.
127 if (proximal_zeroing_estimator_.zeroed() &&
128 distal_zeroing_estimator_.zeroed()) {
Austin Schuh96341532018-03-09 21:17:24 -0800129 state_ = State::DISABLED;
Austin Schuhcb091712018-02-21 20:01:55 -0800130
131 proximal_offset_ = proximal_zeroing_estimator_.offset();
132 distal_offset_ = distal_zeroing_estimator_.offset();
133
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 Y << position->proximal()->encoder() + proximal_offset_,
135 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -0800136
137 // TODO(austin): Offset ekf rather than reset it. Since we aren't
138 // moving at this point, it's pretty safe to do this.
139 ::Eigen::Matrix<double, 4, 1> X;
140 X << Y(0), 0.0, Y(1), 0.0;
141 arm_ekf_.Reset(X);
142 } else {
143 break;
144 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800145 [[fallthrough]];
Austin Schuhcb091712018-02-21 20:01:55 -0800146
Austin Schuhb874fd32018-03-05 00:27:10 -0800147 case State::DISABLED: {
148 follower_.SwitchTrajectory(nullptr);
149 close_enough_for_full_power_ = false;
Austin Schuh96341532018-03-09 21:17:24 -0800150
Austin Schuhb874fd32018-03-05 00:27:10 -0800151 const ::Eigen::Matrix<double, 2, 1> current_theta =
152 (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0),
153 arm_ekf_.X_hat(2))
154 .finished();
155 uint32_t best_index = 0;
156 double best_distance = (points_[0] - current_theta).norm();
157 uint32_t current_index = 0;
158 for (const ::Eigen::Matrix<double, 2, 1> &point : points_) {
159 const double new_distance = (point - current_theta).norm();
160 if (new_distance < best_distance) {
161 best_distance = new_distance;
162 best_index = current_index;
163 }
164 ++current_index;
165 }
166 follower_.set_theta(points_[best_index]);
167 current_node_ = best_index;
168
169 if (!outputs_disabled) {
Austin Schuh96341532018-03-09 21:17:24 -0800170 state_ = State::GOTO_PATH;
171 } else {
172 break;
173 }
Austin Schuhb874fd32018-03-05 00:27:10 -0800174 }
Philipp Schrader790cb542023-07-05 21:06:52 -0700175 [[fallthrough]];
Austin Schuh96341532018-03-09 21:17:24 -0800176
177 case State::GOTO_PATH:
178 if (outputs_disabled) {
179 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700180 } else if (trajectory_override) {
181 follower_.SwitchTrajectory(nullptr);
182 current_node_ = filtered_goal;
183 follower_.set_theta(points_[current_node_]);
184 state_ = State::GOTO_PATH;
Austin Schuh96341532018-03-09 21:17:24 -0800185 } else if (close_enough_for_full_power_) {
186 state_ = State::RUNNING;
187 grab_state_ = GrabState::NORMAL;
188 }
189 break;
190
Austin Schuhcb091712018-02-21 20:01:55 -0800191 case State::RUNNING:
192 // ESTOP if we hit the hard limits.
193 // TODO(austin): Pick some sane limits.
194 if (proximal_zeroing_estimator_.error() ||
195 distal_zeroing_estimator_.error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700196 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800197 state_ = State::ESTOP;
Austin Schuh7afcc232018-09-16 16:33:47 -0700198 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
Austin Schuh96341532018-03-09 21:17:24 -0800199 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700200 } else if (trajectory_override) {
201 follower_.SwitchTrajectory(nullptr);
202 current_node_ = filtered_goal;
203 follower_.set_theta(points_[current_node_]);
204 state_ = State::GOTO_PATH;
Austin Schuh17e484e2018-03-11 01:11:36 -0800205 } else if (suicide) {
206 state_ = State::PREP_CLIMB;
207 climb_count_ = 50;
208 }
209 break;
210
211 case State::PREP_CLIMB:
212 --climb_count_;
213 if (climb_count_ <= 0) {
214 state_ = State::ESTOP;
215 } else if (!suicide) {
216 state_ = State::RUNNING;
Austin Schuhcb091712018-02-21 20:01:55 -0800217 }
218 break;
219
220 case State::ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700221 AOS_LOG(ERROR, "Estop\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800222 break;
223 }
224
Austin Schuh17e484e2018-03-11 01:11:36 -0800225 const bool disable = outputs_disabled || (state_ != State::RUNNING &&
226 state_ != State::GOTO_PATH &&
227 state_ != State::PREP_CLIMB);
Austin Schuhcb091712018-02-21 20:01:55 -0800228 if (disable) {
229 close_enough_for_full_power_ = false;
230 }
231
Austin Schuhb874fd32018-03-05 00:27:10 -0800232 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
Austin Schuh96341532018-03-09 21:17:24 -0800233 if (claw_closed_) {
234 if ((filtered_goal == ReadyAboveBoxIndex()) ||
235 (filtered_goal == TallBoxGrabIndex()) ||
236 (filtered_goal == ShortBoxGrabIndex())) {
237 filtered_goal = NeutralIndex();
238 }
239 }
240
241 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
242 switch (grab_state_) {
243 case GrabState::NORMAL:
244 if (grab_box && !claw_closed_) {
245 grab_state_ = GrabState::WAIT_FOR_BOX;
246 } else {
247 break;
248 }
249 case GrabState::WAIT_FOR_BOX:
250 if (!grab_box) {
251 grab_state_ = GrabState::NORMAL;
252 } else {
253 if (AtState(ReadyAboveBoxIndex()) && NearEnd()) {
254 // We are being asked to grab the box, and the claw is near the box.
255 if (box_back_beambreak_triggered) {
256 // And we now see the box! Try for a tall box.
257 grab_state_ = GrabState::TALL_BOX;
258 }
259 }
260 }
261 break;
262 case GrabState::TALL_BOX:
263 if (!grab_box) {
264 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800265 } else if (AtState(TallBoxGrabIndex()) && NearEnd()) {
266 // We are being asked to grab the box, and the claw is near the box.
267 if (claw_beambreak_triggered) {
268 grab_state_ = GrabState::CLAW_CLOSE;
269 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700270 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800271 } else {
272 grab_state_ = GrabState::SHORT_BOX;
273 }
274 }
275 break;
276 case GrabState::SHORT_BOX:
277 if (!grab_box) {
278 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800279 } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) {
280 // We are being asked to grab the box, and the claw is near the box.
281 if (claw_beambreak_triggered) {
282 grab_state_ = GrabState::CLAW_CLOSE;
283 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700284 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800285 } else {
286 grab_state_ = GrabState::WAIT_FOR_BOX;
287 }
288 }
289 break;
290 case GrabState::CLAW_CLOSE:
Austin Schuh20177c92019-07-07 20:48:24 -0700291 if (monotonic_now >
Austin Schuhb874fd32018-03-05 00:27:10 -0800292 claw_close_start_time_ + ::std::chrono::milliseconds(300)) {
Austin Schuh96341532018-03-09 21:17:24 -0800293 grab_state_ = GrabState::OPEN_INTAKE;
294 }
295 break;
296 case GrabState::OPEN_INTAKE:
297 if (intake_clear_of_box) {
298 grab_state_ = GrabState::NORMAL;
299 }
300 break;
301 }
302
303 // Now, based out our current state, go to the right state.
304 switch (grab_state_) {
305 case GrabState::NORMAL:
306 // Don't let the intake close fully with the claw closed.
307 // TODO(austin): If we want to transfer the box from the claw to the
308 // intake, we'll need to change this.
309 if (claw_closed_) {
310 max_intake_override_ = -0.5;
311 } else {
312 max_intake_override_ = 1000.0;
313 }
314 break;
315 case GrabState::WAIT_FOR_BOX:
316 filtered_goal = ReadyAboveBoxIndex();
317 claw_closed_ = false;
318 max_intake_override_ = 1000.0;
319 break;
320 case GrabState::TALL_BOX:
321 filtered_goal = TallBoxGrabIndex();
322 claw_closed_ = false;
323 max_intake_override_ = 1000.0;
324 break;
325 case GrabState::SHORT_BOX:
326 filtered_goal = ShortBoxGrabIndex();
327 claw_closed_ = false;
328 max_intake_override_ = 1000.0;
329 break;
330 case GrabState::CLAW_CLOSE:
331 // Don't move.
332 filtered_goal = current_node_;
333 claw_closed_ = true;
334 max_intake_override_ = 1000.0;
335 break;
336 case GrabState::OPEN_INTAKE:
337 // Don't move.
338 filtered_goal = current_node_;
339 claw_closed_ = true;
340 max_intake_override_ = -0.5;
341 break;
342 }
343
344 if (state_ == State::RUNNING && unsafe_goal != nullptr) {
345 if (current_node_ != filtered_goal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700346 AOS_LOG(INFO, "Goal is different\n");
Austin Schuh96341532018-03-09 21:17:24 -0800347 if (filtered_goal >= search_graph_.num_vertexes()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700348 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
Austin Schuh96341532018-03-09 21:17:24 -0800349 state_ = State::ESTOP;
350 } else if (follower_.path_distance_to_go() > 1e-3) {
351 // Still on the old path segment. Can't change yet.
352 } else {
353 search_graph_.SetGoal(filtered_goal);
354
355 size_t min_edge = 0;
356 double min_cost = ::std::numeric_limits<double>::infinity();
357 for (const SearchGraph::HalfEdge &edge :
358 search_graph_.Neighbors(current_node_)) {
359 const double cost = search_graph_.GetCostToGoal(edge.dest);
360 if (cost < min_cost) {
361 min_edge = edge.edge_id;
362 min_cost = cost;
363 }
364 }
365 // Ok, now we know which edge we are on. Figure out the path and
366 // trajectory.
367 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
Austin Schuhf257f3c2019-10-27 21:00:43 -0700368 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
369 static_cast<int>(current_node_),
370 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
Austin Schuh41c71e42018-04-04 20:11:20 -0700371 vmax_ = trajectories_[min_edge].vmax;
372 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
Austin Schuh96341532018-03-09 21:17:24 -0800373 current_node_ = next_edge.end;
374 }
375 }
376 }
377
Austin Schuh345a3732018-03-21 20:49:32 -0700378 const double max_operating_voltage =
379 close_enough_for_full_power_
380 ? kOperatingVoltage()
381 : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax());
Austin Schuh41c71e42018-04-04 20:11:20 -0700382 follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_,
Austin Schuh345a3732018-03-21 20:49:32 -0700383 max_operating_voltage);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700384 AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
Austin Schuhcb091712018-02-21 20:01:55 -0800385
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
387 proximal_estimator_state_offset =
388 proximal_zeroing_estimator_.GetEstimatorState(fbb);
389 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
390 distal_estimator_state_offset =
391 distal_zeroing_estimator_.GetEstimatorState(fbb);
392
393 superstructure::ArmStatus::Builder status_builder(*fbb);
394 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
395 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
396
397 status_builder.add_goal_theta0(follower_.theta(0));
398 status_builder.add_goal_theta1(follower_.theta(1));
399 status_builder.add_goal_omega0(follower_.omega(0));
400 status_builder.add_goal_omega1(follower_.omega(1));
401
402 status_builder.add_theta0(arm_ekf_.X_hat(0));
403 status_builder.add_theta1(arm_ekf_.X_hat(2));
404 status_builder.add_omega0(arm_ekf_.X_hat(1));
405 status_builder.add_omega1(arm_ekf_.X_hat(3));
406 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
407 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
Austin Schuhcb091712018-02-21 20:01:55 -0800408
409 if (!disable) {
410 *proximal_output = ::std::max(
411 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0)));
412 *distal_output = ::std::max(
413 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800414 if (state_ != State::PREP_CLIMB) {
415 *release_arm_brake = true;
416 } else {
417 *release_arm_brake = false;
418 }
Austin Schuh96341532018-03-09 21:17:24 -0800419 *claw_closed = claw_closed_;
Austin Schuhcb091712018-02-21 20:01:55 -0800420 }
421
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
423 status_builder.add_current_node(current_node_);
Austin Schuhcb091712018-02-21 20:01:55 -0800424
Alex Perrycb7da4b2019-08-28 19:35:56 -0700425 status_builder.add_zeroed(zeroed());
426 status_builder.add_estopped(estopped());
427 status_builder.add_state(static_cast<int32_t>(state_));
428 status_builder.add_grab_state(static_cast<int32_t>(grab_state_));
429 status_builder.add_failed_solutions(follower_.failed_solutions());
Austin Schuhcb091712018-02-21 20:01:55 -0800430
431 arm_ekf_.Predict(follower_.U(), kDt());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 return status_builder.Finish();
Austin Schuhcb091712018-02-21 20:01:55 -0800433}
434
435} // namespace arm
436} // namespace superstructure
437} // namespace control_loops
438} // namespace y2018