blob: 1c29157552fd8bb078d9d74838fde7829c82787a [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 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800141 [[fallthrough]];
Austin Schuhcb091712018-02-21 20:01:55 -0800142
Austin Schuhb874fd32018-03-05 00:27:10 -0800143 case State::DISABLED: {
144 follower_.SwitchTrajectory(nullptr);
145 close_enough_for_full_power_ = false;
Austin Schuh96341532018-03-09 21:17:24 -0800146
Austin Schuhb874fd32018-03-05 00:27:10 -0800147 const ::Eigen::Matrix<double, 2, 1> current_theta =
148 (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0),
149 arm_ekf_.X_hat(2))
150 .finished();
151 uint32_t best_index = 0;
152 double best_distance = (points_[0] - current_theta).norm();
153 uint32_t current_index = 0;
154 for (const ::Eigen::Matrix<double, 2, 1> &point : points_) {
155 const double new_distance = (point - current_theta).norm();
156 if (new_distance < best_distance) {
157 best_distance = new_distance;
158 best_index = current_index;
159 }
160 ++current_index;
161 }
162 follower_.set_theta(points_[best_index]);
163 current_node_ = best_index;
164
165 if (!outputs_disabled) {
Austin Schuh96341532018-03-09 21:17:24 -0800166 state_ = State::GOTO_PATH;
167 } else {
168 break;
169 }
Austin Schuhb874fd32018-03-05 00:27:10 -0800170 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800171 [[fallthrough]];
Austin Schuh96341532018-03-09 21:17:24 -0800172
173 case State::GOTO_PATH:
174 if (outputs_disabled) {
175 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700176 } else if (trajectory_override) {
177 follower_.SwitchTrajectory(nullptr);
178 current_node_ = filtered_goal;
179 follower_.set_theta(points_[current_node_]);
180 state_ = State::GOTO_PATH;
Austin Schuh96341532018-03-09 21:17:24 -0800181 } else if (close_enough_for_full_power_) {
182 state_ = State::RUNNING;
183 grab_state_ = GrabState::NORMAL;
184 }
185 break;
186
Austin Schuhcb091712018-02-21 20:01:55 -0800187 case State::RUNNING:
188 // ESTOP if we hit the hard limits.
189 // TODO(austin): Pick some sane limits.
190 if (proximal_zeroing_estimator_.error() ||
191 distal_zeroing_estimator_.error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700192 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800193 state_ = State::ESTOP;
Austin Schuh7afcc232018-09-16 16:33:47 -0700194 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
Austin Schuh96341532018-03-09 21:17:24 -0800195 state_ = State::DISABLED;
Austin Schuhd76546a2018-07-08 16:05:14 -0700196 } else if (trajectory_override) {
197 follower_.SwitchTrajectory(nullptr);
198 current_node_ = filtered_goal;
199 follower_.set_theta(points_[current_node_]);
200 state_ = State::GOTO_PATH;
Austin Schuh17e484e2018-03-11 01:11:36 -0800201 } else if (suicide) {
202 state_ = State::PREP_CLIMB;
203 climb_count_ = 50;
204 }
205 break;
206
207 case State::PREP_CLIMB:
208 --climb_count_;
209 if (climb_count_ <= 0) {
210 state_ = State::ESTOP;
211 } else if (!suicide) {
212 state_ = State::RUNNING;
Austin Schuhcb091712018-02-21 20:01:55 -0800213 }
214 break;
215
216 case State::ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700217 AOS_LOG(ERROR, "Estop\n");
Austin Schuhcb091712018-02-21 20:01:55 -0800218 break;
219 }
220
Austin Schuh17e484e2018-03-11 01:11:36 -0800221 const bool disable = outputs_disabled || (state_ != State::RUNNING &&
222 state_ != State::GOTO_PATH &&
223 state_ != State::PREP_CLIMB);
Austin Schuhcb091712018-02-21 20:01:55 -0800224 if (disable) {
225 close_enough_for_full_power_ = false;
226 }
227
Austin Schuhb874fd32018-03-05 00:27:10 -0800228 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
Austin Schuh96341532018-03-09 21:17:24 -0800229 if (claw_closed_) {
230 if ((filtered_goal == ReadyAboveBoxIndex()) ||
231 (filtered_goal == TallBoxGrabIndex()) ||
232 (filtered_goal == ShortBoxGrabIndex())) {
233 filtered_goal = NeutralIndex();
234 }
235 }
236
237 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
238 switch (grab_state_) {
239 case GrabState::NORMAL:
240 if (grab_box && !claw_closed_) {
241 grab_state_ = GrabState::WAIT_FOR_BOX;
242 } else {
243 break;
244 }
245 case GrabState::WAIT_FOR_BOX:
246 if (!grab_box) {
247 grab_state_ = GrabState::NORMAL;
248 } else {
249 if (AtState(ReadyAboveBoxIndex()) && NearEnd()) {
250 // We are being asked to grab the box, and the claw is near the box.
251 if (box_back_beambreak_triggered) {
252 // And we now see the box! Try for a tall box.
253 grab_state_ = GrabState::TALL_BOX;
254 }
255 }
256 }
257 break;
258 case GrabState::TALL_BOX:
259 if (!grab_box) {
260 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800261 } else if (AtState(TallBoxGrabIndex()) && NearEnd()) {
262 // We are being asked to grab the box, and the claw is near the box.
263 if (claw_beambreak_triggered) {
264 grab_state_ = GrabState::CLAW_CLOSE;
265 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700266 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800267 } else {
268 grab_state_ = GrabState::SHORT_BOX;
269 }
270 }
271 break;
272 case GrabState::SHORT_BOX:
273 if (!grab_box) {
274 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800275 } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) {
276 // We are being asked to grab the box, and the claw is near the box.
277 if (claw_beambreak_triggered) {
278 grab_state_ = GrabState::CLAW_CLOSE;
279 // Snap time for the delay here.
Austin Schuh20177c92019-07-07 20:48:24 -0700280 claw_close_start_time_ = monotonic_now;
Austin Schuh96341532018-03-09 21:17:24 -0800281 } else {
282 grab_state_ = GrabState::WAIT_FOR_BOX;
283 }
284 }
285 break;
286 case GrabState::CLAW_CLOSE:
Austin Schuh20177c92019-07-07 20:48:24 -0700287 if (monotonic_now >
Austin Schuhb874fd32018-03-05 00:27:10 -0800288 claw_close_start_time_ + ::std::chrono::milliseconds(300)) {
Austin Schuh96341532018-03-09 21:17:24 -0800289 grab_state_ = GrabState::OPEN_INTAKE;
290 }
291 break;
292 case GrabState::OPEN_INTAKE:
293 if (intake_clear_of_box) {
294 grab_state_ = GrabState::NORMAL;
295 }
296 break;
297 }
298
299 // Now, based out our current state, go to the right state.
300 switch (grab_state_) {
301 case GrabState::NORMAL:
302 // Don't let the intake close fully with the claw closed.
303 // TODO(austin): If we want to transfer the box from the claw to the
304 // intake, we'll need to change this.
305 if (claw_closed_) {
306 max_intake_override_ = -0.5;
307 } else {
308 max_intake_override_ = 1000.0;
309 }
310 break;
311 case GrabState::WAIT_FOR_BOX:
312 filtered_goal = ReadyAboveBoxIndex();
313 claw_closed_ = false;
314 max_intake_override_ = 1000.0;
315 break;
316 case GrabState::TALL_BOX:
317 filtered_goal = TallBoxGrabIndex();
318 claw_closed_ = false;
319 max_intake_override_ = 1000.0;
320 break;
321 case GrabState::SHORT_BOX:
322 filtered_goal = ShortBoxGrabIndex();
323 claw_closed_ = false;
324 max_intake_override_ = 1000.0;
325 break;
326 case GrabState::CLAW_CLOSE:
327 // Don't move.
328 filtered_goal = current_node_;
329 claw_closed_ = true;
330 max_intake_override_ = 1000.0;
331 break;
332 case GrabState::OPEN_INTAKE:
333 // Don't move.
334 filtered_goal = current_node_;
335 claw_closed_ = true;
336 max_intake_override_ = -0.5;
337 break;
338 }
339
340 if (state_ == State::RUNNING && unsafe_goal != nullptr) {
341 if (current_node_ != filtered_goal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700342 AOS_LOG(INFO, "Goal is different\n");
Austin Schuh96341532018-03-09 21:17:24 -0800343 if (filtered_goal >= search_graph_.num_vertexes()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700344 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
Austin Schuh96341532018-03-09 21:17:24 -0800345 state_ = State::ESTOP;
346 } else if (follower_.path_distance_to_go() > 1e-3) {
347 // Still on the old path segment. Can't change yet.
348 } else {
349 search_graph_.SetGoal(filtered_goal);
350
351 size_t min_edge = 0;
352 double min_cost = ::std::numeric_limits<double>::infinity();
353 for (const SearchGraph::HalfEdge &edge :
354 search_graph_.Neighbors(current_node_)) {
355 const double cost = search_graph_.GetCostToGoal(edge.dest);
356 if (cost < min_cost) {
357 min_edge = edge.edge_id;
358 min_cost = cost;
359 }
360 }
361 // Ok, now we know which edge we are on. Figure out the path and
362 // trajectory.
363 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
Austin Schuhf257f3c2019-10-27 21:00:43 -0700364 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
365 static_cast<int>(current_node_),
366 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
Austin Schuh41c71e42018-04-04 20:11:20 -0700367 vmax_ = trajectories_[min_edge].vmax;
368 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
Austin Schuh96341532018-03-09 21:17:24 -0800369 current_node_ = next_edge.end;
370 }
371 }
372 }
373
Austin Schuh345a3732018-03-21 20:49:32 -0700374 const double max_operating_voltage =
375 close_enough_for_full_power_
376 ? kOperatingVoltage()
377 : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax());
Austin Schuh41c71e42018-04-04 20:11:20 -0700378 follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_,
Austin Schuh345a3732018-03-21 20:49:32 -0700379 max_operating_voltage);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700380 AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
Austin Schuhcb091712018-02-21 20:01:55 -0800381
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
383 proximal_estimator_state_offset =
384 proximal_zeroing_estimator_.GetEstimatorState(fbb);
385 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
386 distal_estimator_state_offset =
387 distal_zeroing_estimator_.GetEstimatorState(fbb);
388
389 superstructure::ArmStatus::Builder status_builder(*fbb);
390 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
391 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
392
393 status_builder.add_goal_theta0(follower_.theta(0));
394 status_builder.add_goal_theta1(follower_.theta(1));
395 status_builder.add_goal_omega0(follower_.omega(0));
396 status_builder.add_goal_omega1(follower_.omega(1));
397
398 status_builder.add_theta0(arm_ekf_.X_hat(0));
399 status_builder.add_theta1(arm_ekf_.X_hat(2));
400 status_builder.add_omega0(arm_ekf_.X_hat(1));
401 status_builder.add_omega1(arm_ekf_.X_hat(3));
402 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
403 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
Austin Schuhcb091712018-02-21 20:01:55 -0800404
405 if (!disable) {
406 *proximal_output = ::std::max(
407 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0)));
408 *distal_output = ::std::max(
409 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800410 if (state_ != State::PREP_CLIMB) {
411 *release_arm_brake = true;
412 } else {
413 *release_arm_brake = false;
414 }
Austin Schuh96341532018-03-09 21:17:24 -0800415 *claw_closed = claw_closed_;
Austin Schuhcb091712018-02-21 20:01:55 -0800416 }
417
Alex Perrycb7da4b2019-08-28 19:35:56 -0700418 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
419 status_builder.add_current_node(current_node_);
Austin Schuhcb091712018-02-21 20:01:55 -0800420
Alex Perrycb7da4b2019-08-28 19:35:56 -0700421 status_builder.add_zeroed(zeroed());
422 status_builder.add_estopped(estopped());
423 status_builder.add_state(static_cast<int32_t>(state_));
424 status_builder.add_grab_state(static_cast<int32_t>(grab_state_));
425 status_builder.add_failed_solutions(follower_.failed_solutions());
Austin Schuhcb091712018-02-21 20:01:55 -0800426
427 arm_ekf_.Predict(follower_.U(), kDt());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700428 return status_builder.Finish();
Austin Schuhcb091712018-02-21 20:01:55 -0800429}
430
431} // namespace arm
432} // namespace superstructure
433} // namespace control_loops
434} // namespace y2018