blob: 1f904e2f8f9f5c96a3b31e0c4152cb8926dee48c [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"
Austin Schuhcb091712018-02-21 20:01:55 -08007#include "y2018/constants.h"
8#include "y2018/control_loops/superstructure/arm/demo_path.h"
9#include "y2018/control_loops/superstructure/arm/dynamics.h"
Austin Schuh7dfccf62018-03-03 21:28:14 -080010#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Austin Schuhcb091712018-02-21 20:01:55 -080011
12namespace y2018 {
13namespace control_loops {
14namespace superstructure {
15namespace arm {
16
Austin Schuh7afcc232018-09-16 16:33:47 -070017namespace {
18
Austin Schuhcb091712018-02-21 20:01:55 -080019namespace chrono = ::std::chrono;
20using ::aos::monotonic_clock;
21
Austin Schuh7afcc232018-09-16 16:33:47 -070022constexpr int kMaxBrownoutCount = 4;
23
24} // namespace
25
Austin Schuhcb091712018-02-21 20:01:55 -080026Arm::Arm()
27 : proximal_zeroing_estimator_(constants::GetValues().arm_proximal.zeroing),
28 distal_zeroing_estimator_(constants::GetValues().arm_distal.zeroing),
29 alpha_unitizer_((::Eigen::Matrix<double, 2, 2>() << 1.0 / kAlpha0Max(),
30 0.0, 0.0, 1.0 / kAlpha1Max())
31 .finished()),
Austin Schuh7dfccf62018-03-03 21:28:14 -080032 search_graph_(MakeSearchGraph(&trajectories_, alpha_unitizer_, kVMax())),
Austin Schuhcb091712018-02-21 20:01:55 -080033 // Go to the start of the first trajectory.
Austin Schuhb874fd32018-03-05 00:27:10 -080034 follower_(ReadyAboveBoxPoint()),
35 points_(PointList()) {
Austin Schuh7dfccf62018-03-03 21:28:14 -080036 int i = 0;
37 for (const auto &trajectory : trajectories_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070038 AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i,
39 trajectory.trajectory.path().length());
Austin Schuh7dfccf62018-03-03 21:28:14 -080040 ++i;
Austin Schuhcb091712018-02-21 20:01:55 -080041 }
42}
43
44void Arm::Reset() { state_ = State::UNINITIALIZED; }
45
Alex Perrycb7da4b2019-08-28 19:35:56 -070046flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate(
47 const ::aos::monotonic_clock::time_point monotonic_now,
48 const uint32_t *unsafe_goal, bool grab_box, bool open_claw, bool close_claw,
49 const superstructure::ArmPosition *position,
50 const bool claw_beambreak_triggered,
51 const bool box_back_beambreak_triggered, const bool intake_clear_of_box,
52 bool suicide, bool trajectory_override, double *proximal_output,
53 double *distal_output, bool *release_arm_brake, bool *claw_closed,
54 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuhcb091712018-02-21 20:01:55 -080055 ::Eigen::Matrix<double, 2, 1> Y;
Austin Schuh96341532018-03-09 21:17:24 -080056 const bool outputs_disabled =
57 ((proximal_output == nullptr) || (distal_output == nullptr) ||
58 (release_arm_brake == nullptr) || (claw_closed == nullptr));
Austin Schuh7afcc232018-09-16 16:33:47 -070059 if (outputs_disabled) {
60 ++brownout_count_;
61 } else {
62 brownout_count_ = 0;
63 }
Austin Schuh96341532018-03-09 21:17:24 -080064
65 uint32_t filtered_goal = 0;
66 if (unsafe_goal != nullptr) {
67 filtered_goal = *unsafe_goal;
68 }
69
70 if (open_claw) {
71 claw_closed_ = false;
72 }
Neil Balchba9cbba2018-04-06 22:26:38 -070073 if (close_claw) {
Austin Schuh96341532018-03-09 21:17:24 -080074 claw_closed_ = true;
75 }
Neil Balchba9cbba2018-04-06 22:26:38 -070076 if (outputs_disabled) {
77 if (claw_closed_count_ == 0) {
78 claw_closed_ = true;
79 } else {
80 --claw_closed_count_;
81 }
82 } else {
83 // Wait this many iterations before closing the claw. That prevents
84 // brownouts from closing the claw.
85 claw_closed_count_ = 50;
86 }
Austin Schuhcb091712018-02-21 20:01:55 -080087
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 Y << position->proximal()->encoder() + proximal_offset_,
89 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -080090
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 proximal_zeroing_estimator_.UpdateEstimate(*position->proximal());
92 distal_zeroing_estimator_.UpdateEstimate(*position->distal());
Austin Schuhcb091712018-02-21 20:01:55 -080093
94 if (proximal_output != nullptr) {
95 *proximal_output = 0.0;
96 }
97 if (distal_output != nullptr) {
98 *distal_output = 0.0;
99 }
100
101 arm_ekf_.Correct(Y, kDt());
102
103 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
104 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05) {
105 close_enough_for_full_power_ = true;
106 }
107 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
108 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10) {
109 close_enough_for_full_power_ = false;
110 }
111
112 switch (state_) {
113 case State::UNINITIALIZED:
114 // Wait in the uninitialized state until the intake is initialized.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700115 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800116 state_ = State::ZEROING;
117 proximal_zeroing_estimator_.Reset();
118 distal_zeroing_estimator_.Reset();
Austin Schuhcb091712018-02-21 20:01:55 -0800119 break;
120
121 case State::ZEROING:
122 // Zero by not moving.
123 if (proximal_zeroing_estimator_.zeroed() &&
124 distal_zeroing_estimator_.zeroed()) {
Austin Schuh96341532018-03-09 21:17:24 -0800125 state_ = State::DISABLED;
Austin Schuhcb091712018-02-21 20:01:55 -0800126
127 proximal_offset_ = proximal_zeroing_estimator_.offset();
128 distal_offset_ = distal_zeroing_estimator_.offset();
129
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 Y << position->proximal()->encoder() + proximal_offset_,
131 position->distal()->encoder() + distal_offset_;
Austin Schuhcb091712018-02-21 20:01:55 -0800132
133 // TODO(austin): Offset ekf rather than reset it. Since we aren't
134 // moving at this point, it's pretty safe to do this.
135 ::Eigen::Matrix<double, 4, 1> X;
136 X << Y(0), 0.0, Y(1), 0.0;
137 arm_ekf_.Reset(X);
138 } else {
139 break;
140 }
141
Austin Schuhb874fd32018-03-05 00:27:10 -0800142 case State::DISABLED: {
143 follower_.SwitchTrajectory(nullptr);
144 close_enough_for_full_power_ = false;
Austin Schuh96341532018-03-09 21:17:24 -0800145
Austin Schuhb874fd32018-03-05 00:27:10 -0800146 const ::Eigen::Matrix<double, 2, 1> current_theta =
147 (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0),
148 arm_ekf_.X_hat(2))
149 .finished();
150 uint32_t best_index = 0;
151 double best_distance = (points_[0] - current_theta).norm();
152 uint32_t current_index = 0;
153 for (const ::Eigen::Matrix<double, 2, 1> &point : points_) {
154 const double new_distance = (point - current_theta).norm();
155 if (new_distance < best_distance) {
156 best_distance = new_distance;
157 best_index = current_index;
158 }
159 ++current_index;
160 }
161 follower_.set_theta(points_[best_index]);
162 current_node_ = best_index;
163
164 if (!outputs_disabled) {
Austin Schuh96341532018-03-09 21:17:24 -0800165 state_ = State::GOTO_PATH;
166 } else {
167 break;
168 }
Austin Schuhb874fd32018-03-05 00:27:10 -0800169 }
Austin Schuh96341532018-03-09 21:17:24 -0800170
171 case State::GOTO_PATH:
172 if (outputs_disabled) {
173 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700174 } else if (trajectory_override) {
175 follower_.SwitchTrajectory(nullptr);
176 current_node_ = filtered_goal;
177 follower_.set_theta(points_[current_node_]);
178 state_ = State::GOTO_PATH;
Austin Schuh96341532018-03-09 21:17:24 -0800179 } else if (close_enough_for_full_power_) {
180 state_ = State::RUNNING;
181 grab_state_ = GrabState::NORMAL;
182 }
183 break;
184
Austin Schuhcb091712018-02-21 20:01:55 -0800185 case State::RUNNING:
186 // ESTOP if we hit the hard limits.
187 // TODO(austin): Pick some sane limits.
188 if (proximal_zeroing_estimator_.error() ||
189 distal_zeroing_estimator_.error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700190 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800191 state_ = State::ESTOP;
Austin Schuh7afcc232018-09-16 16:33:47 -0700192 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
Austin Schuh96341532018-03-09 21:17:24 -0800193 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700194 } else if (trajectory_override) {
195 follower_.SwitchTrajectory(nullptr);
196 current_node_ = filtered_goal;
197 follower_.set_theta(points_[current_node_]);
198 state_ = State::GOTO_PATH;
Austin Schuh17e484e2018-03-11 01:11:36 -0800199 } else if (suicide) {
200 state_ = State::PREP_CLIMB;
201 climb_count_ = 50;
202 }
203 break;
204
205 case State::PREP_CLIMB:
206 --climb_count_;
207 if (climb_count_ <= 0) {
208 state_ = State::ESTOP;
209 } else if (!suicide) {
210 state_ = State::RUNNING;
Austin Schuhcb091712018-02-21 20:01:55 -0800211 }
212 break;
213
214 case State::ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700215 AOS_LOG(ERROR, "Estop\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800216 break;
217 }
218
Austin Schuh17e484e2018-03-11 01:11:36 -0800219 const bool disable = outputs_disabled || (state_ != State::RUNNING &&
220 state_ != State::GOTO_PATH &&
221 state_ != State::PREP_CLIMB);
Austin Schuhcb091712018-02-21 20:01:55 -0800222 if (disable) {
223 close_enough_for_full_power_ = false;
224 }
225
Austin Schuhb874fd32018-03-05 00:27:10 -0800226 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
Austin Schuh96341532018-03-09 21:17:24 -0800227 if (claw_closed_) {
228 if ((filtered_goal == ReadyAboveBoxIndex()) ||
229 (filtered_goal == TallBoxGrabIndex()) ||
230 (filtered_goal == ShortBoxGrabIndex())) {
231 filtered_goal = NeutralIndex();
232 }
233 }
234
235 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
236 switch (grab_state_) {
237 case GrabState::NORMAL:
238 if (grab_box && !claw_closed_) {
239 grab_state_ = GrabState::WAIT_FOR_BOX;
240 } else {
241 break;
242 }
243 case GrabState::WAIT_FOR_BOX:
244 if (!grab_box) {
245 grab_state_ = GrabState::NORMAL;
246 } else {
247 if (AtState(ReadyAboveBoxIndex()) && NearEnd()) {
248 // We are being asked to grab the box, and the claw is near the box.
249 if (box_back_beambreak_triggered) {
250 // And we now see the box! Try for a tall box.
251 grab_state_ = GrabState::TALL_BOX;
252 }
253 }
254 }
255 break;
256 case GrabState::TALL_BOX:
257 if (!grab_box) {
258 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800259 } else if (AtState(TallBoxGrabIndex()) && NearEnd()) {
260 // We are being asked to grab the box, and the claw is near the box.
261 if (claw_beambreak_triggered) {
262 grab_state_ = GrabState::CLAW_CLOSE;
263 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700264 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800265 } else {
266 grab_state_ = GrabState::SHORT_BOX;
267 }
268 }
269 break;
270 case GrabState::SHORT_BOX:
271 if (!grab_box) {
272 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800273 } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) {
274 // We are being asked to grab the box, and the claw is near the box.
275 if (claw_beambreak_triggered) {
276 grab_state_ = GrabState::CLAW_CLOSE;
277 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700278 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800279 } else {
280 grab_state_ = GrabState::WAIT_FOR_BOX;
281 }
282 }
283 break;
284 case GrabState::CLAW_CLOSE:
Austin Schuh20177c92019-07-07 20:48:24 -0700285 if (monotonic_now >
Austin Schuhb874fd32018-03-05 00:27:10 -0800286 claw_close_start_time_ + ::std::chrono::milliseconds(300)) {
Austin Schuh96341532018-03-09 21:17:24 -0800287 grab_state_ = GrabState::OPEN_INTAKE;
288 }
289 break;
290 case GrabState::OPEN_INTAKE:
291 if (intake_clear_of_box) {
292 grab_state_ = GrabState::NORMAL;
293 }
294 break;
295 }
296
297 // Now, based out our current state, go to the right state.
298 switch (grab_state_) {
299 case GrabState::NORMAL:
300 // Don't let the intake close fully with the claw closed.
301 // TODO(austin): If we want to transfer the box from the claw to the
302 // intake, we'll need to change this.
303 if (claw_closed_) {
304 max_intake_override_ = -0.5;
305 } else {
306 max_intake_override_ = 1000.0;
307 }
308 break;
309 case GrabState::WAIT_FOR_BOX:
310 filtered_goal = ReadyAboveBoxIndex();
311 claw_closed_ = false;
312 max_intake_override_ = 1000.0;
313 break;
314 case GrabState::TALL_BOX:
315 filtered_goal = TallBoxGrabIndex();
316 claw_closed_ = false;
317 max_intake_override_ = 1000.0;
318 break;
319 case GrabState::SHORT_BOX:
320 filtered_goal = ShortBoxGrabIndex();
321 claw_closed_ = false;
322 max_intake_override_ = 1000.0;
323 break;
324 case GrabState::CLAW_CLOSE:
325 // Don't move.
326 filtered_goal = current_node_;
327 claw_closed_ = true;
328 max_intake_override_ = 1000.0;
329 break;
330 case GrabState::OPEN_INTAKE:
331 // Don't move.
332 filtered_goal = current_node_;
333 claw_closed_ = true;
334 max_intake_override_ = -0.5;
335 break;
336 }
337
338 if (state_ == State::RUNNING && unsafe_goal != nullptr) {
339 if (current_node_ != filtered_goal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700340 AOS_LOG(INFO, "Goal is different\n");
Austin Schuh96341532018-03-09 21:17:24 -0800341 if (filtered_goal >= search_graph_.num_vertexes()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700342 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
Austin Schuh96341532018-03-09 21:17:24 -0800343 state_ = State::ESTOP;
344 } else if (follower_.path_distance_to_go() > 1e-3) {
345 // Still on the old path segment. Can't change yet.
346 } else {
347 search_graph_.SetGoal(filtered_goal);
348
349 size_t min_edge = 0;
350 double min_cost = ::std::numeric_limits<double>::infinity();
351 for (const SearchGraph::HalfEdge &edge :
352 search_graph_.Neighbors(current_node_)) {
353 const double cost = search_graph_.GetCostToGoal(edge.dest);
354 if (cost < min_cost) {
355 min_edge = edge.edge_id;
356 min_cost = cost;
357 }
358 }
359 // Ok, now we know which edge we are on. Figure out the path and
360 // trajectory.
361 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
Austin Schuhf257f3c2019-10-27 21:00:43 -0700362 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
363 static_cast<int>(current_node_),
364 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
Austin Schuh41c71e42018-04-04 20:11:20 -0700365 vmax_ = trajectories_[min_edge].vmax;
366 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
Austin Schuh96341532018-03-09 21:17:24 -0800367 current_node_ = next_edge.end;
368 }
369 }
370 }
371
Austin Schuh345a3732018-03-21 20:49:32 -0700372 const double max_operating_voltage =
373 close_enough_for_full_power_
374 ? kOperatingVoltage()
375 : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax());
Austin Schuh41c71e42018-04-04 20:11:20 -0700376 follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_,
Austin Schuh345a3732018-03-21 20:49:32 -0700377 max_operating_voltage);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700378 AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
Austin Schuhcb091712018-02-21 20:01:55 -0800379
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
381 proximal_estimator_state_offset =
382 proximal_zeroing_estimator_.GetEstimatorState(fbb);
383 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
384 distal_estimator_state_offset =
385 distal_zeroing_estimator_.GetEstimatorState(fbb);
386
387 superstructure::ArmStatus::Builder status_builder(*fbb);
388 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
389 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
390
391 status_builder.add_goal_theta0(follower_.theta(0));
392 status_builder.add_goal_theta1(follower_.theta(1));
393 status_builder.add_goal_omega0(follower_.omega(0));
394 status_builder.add_goal_omega1(follower_.omega(1));
395
396 status_builder.add_theta0(arm_ekf_.X_hat(0));
397 status_builder.add_theta1(arm_ekf_.X_hat(2));
398 status_builder.add_omega0(arm_ekf_.X_hat(1));
399 status_builder.add_omega1(arm_ekf_.X_hat(3));
400 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
401 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
Austin Schuhcb091712018-02-21 20:01:55 -0800402
403 if (!disable) {
404 *proximal_output = ::std::max(
405 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0)));
406 *distal_output = ::std::max(
407 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800408 if (state_ != State::PREP_CLIMB) {
409 *release_arm_brake = true;
410 } else {
411 *release_arm_brake = false;
412 }
Austin Schuh96341532018-03-09 21:17:24 -0800413 *claw_closed = claw_closed_;
Austin Schuhcb091712018-02-21 20:01:55 -0800414 }
415
Alex Perrycb7da4b2019-08-28 19:35:56 -0700416 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
417 status_builder.add_current_node(current_node_);
Austin Schuhcb091712018-02-21 20:01:55 -0800418
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 status_builder.add_zeroed(zeroed());
420 status_builder.add_estopped(estopped());
421 status_builder.add_state(static_cast<int32_t>(state_));
422 status_builder.add_grab_state(static_cast<int32_t>(grab_state_));
423 status_builder.add_failed_solutions(follower_.failed_solutions());
Austin Schuhcb091712018-02-21 20:01:55 -0800424
425 arm_ekf_.Predict(follower_.U(), kDt());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 return status_builder.Finish();
Austin Schuhcb091712018-02-21 20:01:55 -0800427}
428
429} // namespace arm
430} // namespace superstructure
431} // namespace control_loops
432} // namespace y2018