blob: 4e700221b5ba5368193cc58bd715dec91516610f [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080013namespace y2018::control_loops::superstructure::arm {
Austin Schuhcb091712018-02-21 20:01:55 -080014
Austin Schuh7afcc232018-09-16 16:33:47 -070015namespace {
16
Austin Schuhcb091712018-02-21 20:01:55 -080017namespace chrono = ::std::chrono;
18using ::aos::monotonic_clock;
19
Austin Schuh7afcc232018-09-16 16:33:47 -070020constexpr int kMaxBrownoutCount = 4;
21
22} // namespace
23
Austin Schuhcb091712018-02-21 20:01:55 -080024Arm::Arm()
25 : proximal_zeroing_estimator_(constants::GetValues().arm_proximal.zeroing),
26 distal_zeroing_estimator_(constants::GetValues().arm_distal.zeroing),
27 alpha_unitizer_((::Eigen::Matrix<double, 2, 2>() << 1.0 / kAlpha0Max(),
28 0.0, 0.0, 1.0 / kAlpha1Max())
29 .finished()),
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080030 dynamics_(kArmConstants),
31 search_graph_(MakeSearchGraph(&dynamics_, &trajectories_, alpha_unitizer_,
32 kVMax())),
33 arm_ekf_(&dynamics_),
Austin Schuhcb091712018-02-21 20:01:55 -080034 // Go to the start of the first trajectory.
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080035 follower_(&dynamics_, ReadyAboveBoxPoint()),
Austin Schuhb874fd32018-03-05 00:27:10 -080036 points_(PointList()) {
Austin Schuh7dfccf62018-03-03 21:28:14 -080037 int i = 0;
38 for (const auto &trajectory : trajectories_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070039 AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i,
40 trajectory.trajectory.path().length());
Austin Schuh7dfccf62018-03-03 21:28:14 -080041 ++i;
Austin Schuhcb091712018-02-21 20:01:55 -080042 }
43}
44
45void Arm::Reset() { state_ = State::UNINITIALIZED; }
46
Alex Perrycb7da4b2019-08-28 19:35:56 -070047flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate(
48 const ::aos::monotonic_clock::time_point monotonic_now,
49 const uint32_t *unsafe_goal, bool grab_box, bool open_claw, bool close_claw,
50 const superstructure::ArmPosition *position,
51 const bool claw_beambreak_triggered,
52 const bool box_back_beambreak_triggered, const bool intake_clear_of_box,
53 bool suicide, bool trajectory_override, double *proximal_output,
54 double *distal_output, bool *release_arm_brake, bool *claw_closed,
55 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuhcb091712018-02-21 20:01:55 -080056 ::Eigen::Matrix<double, 2, 1> Y;
Austin Schuh96341532018-03-09 21:17:24 -080057 const bool outputs_disabled =
58 ((proximal_output == nullptr) || (distal_output == nullptr) ||
59 (release_arm_brake == nullptr) || (claw_closed == nullptr));
Austin Schuh7afcc232018-09-16 16:33:47 -070060 if (outputs_disabled) {
61 ++brownout_count_;
62 } else {
63 brownout_count_ = 0;
64 }
Austin Schuh96341532018-03-09 21:17:24 -080065
66 uint32_t filtered_goal = 0;
67 if (unsafe_goal != nullptr) {
68 filtered_goal = *unsafe_goal;
69 }
70
71 if (open_claw) {
72 claw_closed_ = false;
73 }
Neil Balchba9cbba2018-04-06 22:26:38 -070074 if (close_claw) {
Austin Schuh96341532018-03-09 21:17:24 -080075 claw_closed_ = true;
76 }
Neil Balchba9cbba2018-04-06 22:26:38 -070077 if (outputs_disabled) {
78 if (claw_closed_count_ == 0) {
79 claw_closed_ = true;
80 } else {
81 --claw_closed_count_;
82 }
83 } else {
84 // Wait this many iterations before closing the claw. That prevents
85 // brownouts from closing the claw.
86 claw_closed_count_ = 50;
87 }
Austin Schuhcb091712018-02-21 20:01:55 -080088
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 Y << position->proximal()->encoder() + proximal_offset_,
90 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -080091
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 proximal_zeroing_estimator_.UpdateEstimate(*position->proximal());
93 distal_zeroing_estimator_.UpdateEstimate(*position->distal());
Austin Schuhcb091712018-02-21 20:01:55 -080094
95 if (proximal_output != nullptr) {
96 *proximal_output = 0.0;
97 }
98 if (distal_output != nullptr) {
99 *distal_output = 0.0;
100 }
101
102 arm_ekf_.Correct(Y, kDt());
103
104 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
105 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05) {
106 close_enough_for_full_power_ = true;
107 }
108 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
109 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10) {
110 close_enough_for_full_power_ = false;
111 }
112
113 switch (state_) {
114 case State::UNINITIALIZED:
115 // Wait in the uninitialized state until the intake is initialized.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700116 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800117 state_ = State::ZEROING;
118 proximal_zeroing_estimator_.Reset();
119 distal_zeroing_estimator_.Reset();
Austin Schuhcb091712018-02-21 20:01:55 -0800120 break;
121
122 case State::ZEROING:
123 // Zero by not moving.
124 if (proximal_zeroing_estimator_.zeroed() &&
125 distal_zeroing_estimator_.zeroed()) {
Austin Schuh96341532018-03-09 21:17:24 -0800126 state_ = State::DISABLED;
Austin Schuhcb091712018-02-21 20:01:55 -0800127
128 proximal_offset_ = proximal_zeroing_estimator_.offset();
129 distal_offset_ = distal_zeroing_estimator_.offset();
130
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 Y << position->proximal()->encoder() + proximal_offset_,
132 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -0800133
134 // TODO(austin): Offset ekf rather than reset it. Since we aren't
135 // moving at this point, it's pretty safe to do this.
136 ::Eigen::Matrix<double, 4, 1> X;
137 X << Y(0), 0.0, Y(1), 0.0;
138 arm_ekf_.Reset(X);
139 } else {
140 break;
141 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800142 [[fallthrough]];
Austin Schuhcb091712018-02-21 20:01:55 -0800143
Austin Schuhb874fd32018-03-05 00:27:10 -0800144 case State::DISABLED: {
145 follower_.SwitchTrajectory(nullptr);
146 close_enough_for_full_power_ = false;
Austin Schuh96341532018-03-09 21:17:24 -0800147
Austin Schuhb874fd32018-03-05 00:27:10 -0800148 const ::Eigen::Matrix<double, 2, 1> current_theta =
149 (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0),
150 arm_ekf_.X_hat(2))
151 .finished();
152 uint32_t best_index = 0;
153 double best_distance = (points_[0] - current_theta).norm();
154 uint32_t current_index = 0;
155 for (const ::Eigen::Matrix<double, 2, 1> &point : points_) {
156 const double new_distance = (point - current_theta).norm();
157 if (new_distance < best_distance) {
158 best_distance = new_distance;
159 best_index = current_index;
160 }
161 ++current_index;
162 }
163 follower_.set_theta(points_[best_index]);
164 current_node_ = best_index;
165
166 if (!outputs_disabled) {
Austin Schuh96341532018-03-09 21:17:24 -0800167 state_ = State::GOTO_PATH;
168 } else {
169 break;
170 }
Austin Schuhb874fd32018-03-05 00:27:10 -0800171 }
Philipp Schrader790cb542023-07-05 21:06:52 -0700172 [[fallthrough]];
Austin Schuh96341532018-03-09 21:17:24 -0800173
174 case State::GOTO_PATH:
175 if (outputs_disabled) {
176 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700177 } else if (trajectory_override) {
178 follower_.SwitchTrajectory(nullptr);
179 current_node_ = filtered_goal;
180 follower_.set_theta(points_[current_node_]);
181 state_ = State::GOTO_PATH;
Austin Schuh96341532018-03-09 21:17:24 -0800182 } else if (close_enough_for_full_power_) {
183 state_ = State::RUNNING;
184 grab_state_ = GrabState::NORMAL;
185 }
186 break;
187
Austin Schuhcb091712018-02-21 20:01:55 -0800188 case State::RUNNING:
189 // ESTOP if we hit the hard limits.
190 // TODO(austin): Pick some sane limits.
191 if (proximal_zeroing_estimator_.error() ||
192 distal_zeroing_estimator_.error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700193 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800194 state_ = State::ESTOP;
Austin Schuh7afcc232018-09-16 16:33:47 -0700195 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
Austin Schuh96341532018-03-09 21:17:24 -0800196 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700197 } else if (trajectory_override) {
198 follower_.SwitchTrajectory(nullptr);
199 current_node_ = filtered_goal;
200 follower_.set_theta(points_[current_node_]);
201 state_ = State::GOTO_PATH;
Austin Schuh17e484e2018-03-11 01:11:36 -0800202 } else if (suicide) {
203 state_ = State::PREP_CLIMB;
204 climb_count_ = 50;
205 }
206 break;
207
208 case State::PREP_CLIMB:
209 --climb_count_;
210 if (climb_count_ <= 0) {
211 state_ = State::ESTOP;
212 } else if (!suicide) {
213 state_ = State::RUNNING;
Austin Schuhcb091712018-02-21 20:01:55 -0800214 }
215 break;
216
217 case State::ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700218 AOS_LOG(ERROR, "Estop\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800219 break;
220 }
221
Austin Schuh17e484e2018-03-11 01:11:36 -0800222 const bool disable = outputs_disabled || (state_ != State::RUNNING &&
223 state_ != State::GOTO_PATH &&
224 state_ != State::PREP_CLIMB);
Austin Schuhcb091712018-02-21 20:01:55 -0800225 if (disable) {
226 close_enough_for_full_power_ = false;
227 }
228
Austin Schuhb874fd32018-03-05 00:27:10 -0800229 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
Austin Schuh96341532018-03-09 21:17:24 -0800230 if (claw_closed_) {
231 if ((filtered_goal == ReadyAboveBoxIndex()) ||
232 (filtered_goal == TallBoxGrabIndex()) ||
233 (filtered_goal == ShortBoxGrabIndex())) {
234 filtered_goal = NeutralIndex();
235 }
236 }
237
238 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
239 switch (grab_state_) {
240 case GrabState::NORMAL:
241 if (grab_box && !claw_closed_) {
242 grab_state_ = GrabState::WAIT_FOR_BOX;
243 } else {
244 break;
245 }
246 case GrabState::WAIT_FOR_BOX:
247 if (!grab_box) {
248 grab_state_ = GrabState::NORMAL;
249 } else {
250 if (AtState(ReadyAboveBoxIndex()) && NearEnd()) {
251 // We are being asked to grab the box, and the claw is near the box.
252 if (box_back_beambreak_triggered) {
253 // And we now see the box! Try for a tall box.
254 grab_state_ = GrabState::TALL_BOX;
255 }
256 }
257 }
258 break;
259 case GrabState::TALL_BOX:
260 if (!grab_box) {
261 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800262 } else if (AtState(TallBoxGrabIndex()) && NearEnd()) {
263 // We are being asked to grab the box, and the claw is near the box.
264 if (claw_beambreak_triggered) {
265 grab_state_ = GrabState::CLAW_CLOSE;
266 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700267 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800268 } else {
269 grab_state_ = GrabState::SHORT_BOX;
270 }
271 }
272 break;
273 case GrabState::SHORT_BOX:
274 if (!grab_box) {
275 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800276 } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) {
277 // We are being asked to grab the box, and the claw is near the box.
278 if (claw_beambreak_triggered) {
279 grab_state_ = GrabState::CLAW_CLOSE;
280 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700281 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800282 } else {
283 grab_state_ = GrabState::WAIT_FOR_BOX;
284 }
285 }
286 break;
287 case GrabState::CLAW_CLOSE:
Austin Schuh20177c92019-07-07 20:48:24 -0700288 if (monotonic_now >
Austin Schuhb874fd32018-03-05 00:27:10 -0800289 claw_close_start_time_ + ::std::chrono::milliseconds(300)) {
Austin Schuh96341532018-03-09 21:17:24 -0800290 grab_state_ = GrabState::OPEN_INTAKE;
291 }
292 break;
293 case GrabState::OPEN_INTAKE:
294 if (intake_clear_of_box) {
295 grab_state_ = GrabState::NORMAL;
296 }
297 break;
298 }
299
300 // Now, based out our current state, go to the right state.
301 switch (grab_state_) {
302 case GrabState::NORMAL:
303 // Don't let the intake close fully with the claw closed.
304 // TODO(austin): If we want to transfer the box from the claw to the
305 // intake, we'll need to change this.
306 if (claw_closed_) {
307 max_intake_override_ = -0.5;
308 } else {
309 max_intake_override_ = 1000.0;
310 }
311 break;
312 case GrabState::WAIT_FOR_BOX:
313 filtered_goal = ReadyAboveBoxIndex();
314 claw_closed_ = false;
315 max_intake_override_ = 1000.0;
316 break;
317 case GrabState::TALL_BOX:
318 filtered_goal = TallBoxGrabIndex();
319 claw_closed_ = false;
320 max_intake_override_ = 1000.0;
321 break;
322 case GrabState::SHORT_BOX:
323 filtered_goal = ShortBoxGrabIndex();
324 claw_closed_ = false;
325 max_intake_override_ = 1000.0;
326 break;
327 case GrabState::CLAW_CLOSE:
328 // Don't move.
329 filtered_goal = current_node_;
330 claw_closed_ = true;
331 max_intake_override_ = 1000.0;
332 break;
333 case GrabState::OPEN_INTAKE:
334 // Don't move.
335 filtered_goal = current_node_;
336 claw_closed_ = true;
337 max_intake_override_ = -0.5;
338 break;
339 }
340
341 if (state_ == State::RUNNING && unsafe_goal != nullptr) {
342 if (current_node_ != filtered_goal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700343 AOS_LOG(INFO, "Goal is different\n");
Austin Schuh96341532018-03-09 21:17:24 -0800344 if (filtered_goal >= search_graph_.num_vertexes()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700345 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
Austin Schuh96341532018-03-09 21:17:24 -0800346 state_ = State::ESTOP;
347 } else if (follower_.path_distance_to_go() > 1e-3) {
348 // Still on the old path segment. Can't change yet.
349 } else {
350 search_graph_.SetGoal(filtered_goal);
351
352 size_t min_edge = 0;
353 double min_cost = ::std::numeric_limits<double>::infinity();
354 for (const SearchGraph::HalfEdge &edge :
355 search_graph_.Neighbors(current_node_)) {
356 const double cost = search_graph_.GetCostToGoal(edge.dest);
357 if (cost < min_cost) {
358 min_edge = edge.edge_id;
359 min_cost = cost;
360 }
361 }
362 // Ok, now we know which edge we are on. Figure out the path and
363 // trajectory.
364 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
Austin Schuhf257f3c2019-10-27 21:00:43 -0700365 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
366 static_cast<int>(current_node_),
367 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
Austin Schuh41c71e42018-04-04 20:11:20 -0700368 vmax_ = trajectories_[min_edge].vmax;
369 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
Austin Schuh96341532018-03-09 21:17:24 -0800370 current_node_ = next_edge.end;
371 }
372 }
373 }
374
Austin Schuh345a3732018-03-21 20:49:32 -0700375 const double max_operating_voltage =
376 close_enough_for_full_power_
377 ? kOperatingVoltage()
378 : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax());
Austin Schuh41c71e42018-04-04 20:11:20 -0700379 follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_,
Austin Schuh345a3732018-03-21 20:49:32 -0700380 max_operating_voltage);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700381 AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
Austin Schuhcb091712018-02-21 20:01:55 -0800382
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
384 proximal_estimator_state_offset =
385 proximal_zeroing_estimator_.GetEstimatorState(fbb);
386 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
387 distal_estimator_state_offset =
388 distal_zeroing_estimator_.GetEstimatorState(fbb);
389
390 superstructure::ArmStatus::Builder status_builder(*fbb);
391 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
392 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
393
394 status_builder.add_goal_theta0(follower_.theta(0));
395 status_builder.add_goal_theta1(follower_.theta(1));
396 status_builder.add_goal_omega0(follower_.omega(0));
397 status_builder.add_goal_omega1(follower_.omega(1));
398
399 status_builder.add_theta0(arm_ekf_.X_hat(0));
400 status_builder.add_theta1(arm_ekf_.X_hat(2));
401 status_builder.add_omega0(arm_ekf_.X_hat(1));
402 status_builder.add_omega1(arm_ekf_.X_hat(3));
403 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
404 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
Austin Schuhcb091712018-02-21 20:01:55 -0800405
406 if (!disable) {
407 *proximal_output = ::std::max(
408 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0)));
409 *distal_output = ::std::max(
410 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800411 if (state_ != State::PREP_CLIMB) {
412 *release_arm_brake = true;
413 } else {
414 *release_arm_brake = false;
415 }
Austin Schuh96341532018-03-09 21:17:24 -0800416 *claw_closed = claw_closed_;
Austin Schuhcb091712018-02-21 20:01:55 -0800417 }
418
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
420 status_builder.add_current_node(current_node_);
Austin Schuhcb091712018-02-21 20:01:55 -0800421
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 status_builder.add_zeroed(zeroed());
423 status_builder.add_estopped(estopped());
424 status_builder.add_state(static_cast<int32_t>(state_));
425 status_builder.add_grab_state(static_cast<int32_t>(grab_state_));
426 status_builder.add_failed_solutions(follower_.failed_solutions());
Austin Schuhcb091712018-02-21 20:01:55 -0800427
428 arm_ekf_.Predict(follower_.U(), kDt());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700429 return status_builder.Finish();
Austin Schuhcb091712018-02-21 20:01:55 -0800430}
431
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800432} // namespace y2018::control_loops::superstructure::arm