blob: d79a293fc7e1198bd20516a38f96cdbd7468d1e2 [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
6#include "aos/common/logging/logging.h"
7#include "aos/common/logging/queue_logging.h"
8#include "y2018/constants.h"
9#include "y2018/control_loops/superstructure/arm/demo_path.h"
10#include "y2018/control_loops/superstructure/arm/dynamics.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
18namespace chrono = ::std::chrono;
19using ::aos::monotonic_clock;
20
Austin Schuhcb091712018-02-21 20:01:55 -080021Arm::Arm()
22 : proximal_zeroing_estimator_(constants::GetValues().arm_proximal.zeroing),
23 distal_zeroing_estimator_(constants::GetValues().arm_distal.zeroing),
24 alpha_unitizer_((::Eigen::Matrix<double, 2, 2>() << 1.0 / kAlpha0Max(),
25 0.0, 0.0, 1.0 / kAlpha1Max())
26 .finished()),
Austin Schuh7dfccf62018-03-03 21:28:14 -080027 search_graph_(MakeSearchGraph(&trajectories_, alpha_unitizer_, kVMax())),
Austin Schuhcb091712018-02-21 20:01:55 -080028 // Go to the start of the first trajectory.
Austin Schuhb874fd32018-03-05 00:27:10 -080029 follower_(ReadyAboveBoxPoint()),
30 points_(PointList()) {
Austin Schuh7dfccf62018-03-03 21:28:14 -080031 int i = 0;
32 for (const auto &trajectory : trajectories_) {
33 LOG(INFO, "trajectory length for edge node %d: %f\n", i,
Austin Schuh41c71e42018-04-04 20:11:20 -070034 trajectory.trajectory.path().length());
Austin Schuh7dfccf62018-03-03 21:28:14 -080035 ++i;
Austin Schuhcb091712018-02-21 20:01:55 -080036 }
37}
38
39void Arm::Reset() { state_ = State::UNINITIALIZED; }
40
Austin Schuh96341532018-03-09 21:17:24 -080041void Arm::Iterate(const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
Neil Balchba9cbba2018-04-06 22:26:38 -070042 bool close_claw, const control_loops::ArmPosition *position,
Austin Schuh96341532018-03-09 21:17:24 -080043 const bool claw_beambreak_triggered,
44 const bool box_back_beambreak_triggered,
45 const bool intake_clear_of_box, double *proximal_output,
46 double *distal_output, bool *release_arm_brake,
Austin Schuh17e484e2018-03-11 01:11:36 -080047 bool *claw_closed, control_loops::ArmStatus *status,
48 bool suicide) {
Austin Schuhcb091712018-02-21 20:01:55 -080049 ::Eigen::Matrix<double, 2, 1> Y;
Austin Schuh96341532018-03-09 21:17:24 -080050 const bool outputs_disabled =
51 ((proximal_output == nullptr) || (distal_output == nullptr) ||
52 (release_arm_brake == nullptr) || (claw_closed == nullptr));
53
54 uint32_t filtered_goal = 0;
55 if (unsafe_goal != nullptr) {
56 filtered_goal = *unsafe_goal;
57 }
58
59 if (open_claw) {
60 claw_closed_ = false;
61 }
Neil Balchba9cbba2018-04-06 22:26:38 -070062 if (close_claw) {
Austin Schuh96341532018-03-09 21:17:24 -080063 claw_closed_ = true;
64 }
Neil Balchba9cbba2018-04-06 22:26:38 -070065 if (outputs_disabled) {
66 if (claw_closed_count_ == 0) {
67 claw_closed_ = true;
68 } else {
69 --claw_closed_count_;
70 }
71 } else {
72 // Wait this many iterations before closing the claw. That prevents
73 // brownouts from closing the claw.
74 claw_closed_count_ = 50;
75 }
Austin Schuhcb091712018-02-21 20:01:55 -080076
77 Y << position->proximal.encoder + proximal_offset_,
78 position->distal.encoder + distal_offset_;
79
80 proximal_zeroing_estimator_.UpdateEstimate(position->proximal);
81 distal_zeroing_estimator_.UpdateEstimate(position->distal);
82
83 if (proximal_output != nullptr) {
84 *proximal_output = 0.0;
85 }
86 if (distal_output != nullptr) {
87 *distal_output = 0.0;
88 }
89
90 arm_ekf_.Correct(Y, kDt());
91
92 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
93 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05) {
94 close_enough_for_full_power_ = true;
95 }
96 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
97 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10) {
98 close_enough_for_full_power_ = false;
99 }
100
101 switch (state_) {
102 case State::UNINITIALIZED:
103 // Wait in the uninitialized state until the intake is initialized.
104 LOG(DEBUG, "Uninitialized, waiting for intake\n");
105 state_ = State::ZEROING;
106 proximal_zeroing_estimator_.Reset();
107 distal_zeroing_estimator_.Reset();
Austin Schuhcb091712018-02-21 20:01:55 -0800108 break;
109
110 case State::ZEROING:
111 // Zero by not moving.
112 if (proximal_zeroing_estimator_.zeroed() &&
113 distal_zeroing_estimator_.zeroed()) {
Austin Schuh96341532018-03-09 21:17:24 -0800114 state_ = State::DISABLED;
Austin Schuhcb091712018-02-21 20:01:55 -0800115
116 proximal_offset_ = proximal_zeroing_estimator_.offset();
117 distal_offset_ = distal_zeroing_estimator_.offset();
118
119 Y << position->proximal.encoder + proximal_offset_,
120 position->distal.encoder + distal_offset_;
121
122 // TODO(austin): Offset ekf rather than reset it. Since we aren't
123 // moving at this point, it's pretty safe to do this.
124 ::Eigen::Matrix<double, 4, 1> X;
125 X << Y(0), 0.0, Y(1), 0.0;
126 arm_ekf_.Reset(X);
127 } else {
128 break;
129 }
130
Austin Schuhb874fd32018-03-05 00:27:10 -0800131 case State::DISABLED: {
132 follower_.SwitchTrajectory(nullptr);
133 close_enough_for_full_power_ = false;
Austin Schuh96341532018-03-09 21:17:24 -0800134
Austin Schuhb874fd32018-03-05 00:27:10 -0800135 const ::Eigen::Matrix<double, 2, 1> current_theta =
136 (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0),
137 arm_ekf_.X_hat(2))
138 .finished();
139 uint32_t best_index = 0;
140 double best_distance = (points_[0] - current_theta).norm();
141 uint32_t current_index = 0;
142 for (const ::Eigen::Matrix<double, 2, 1> &point : points_) {
143 const double new_distance = (point - current_theta).norm();
144 if (new_distance < best_distance) {
145 best_distance = new_distance;
146 best_index = current_index;
147 }
148 ++current_index;
149 }
150 follower_.set_theta(points_[best_index]);
151 current_node_ = best_index;
152
153 if (!outputs_disabled) {
Austin Schuh96341532018-03-09 21:17:24 -0800154 state_ = State::GOTO_PATH;
155 } else {
156 break;
157 }
Austin Schuhb874fd32018-03-05 00:27:10 -0800158 }
Austin Schuh96341532018-03-09 21:17:24 -0800159
160 case State::GOTO_PATH:
161 if (outputs_disabled) {
162 state_ = State::DISABLED;
163 } else if (close_enough_for_full_power_) {
164 state_ = State::RUNNING;
165 grab_state_ = GrabState::NORMAL;
166 }
167 break;
168
Austin Schuhcb091712018-02-21 20:01:55 -0800169 case State::RUNNING:
170 // ESTOP if we hit the hard limits.
171 // TODO(austin): Pick some sane limits.
172 if (proximal_zeroing_estimator_.error() ||
173 distal_zeroing_estimator_.error()) {
174 LOG(ERROR, "Zeroing error ESTOP\n");
175 state_ = State::ESTOP;
Austin Schuh96341532018-03-09 21:17:24 -0800176 } else if (outputs_disabled) {
177 state_ = State::DISABLED;
Austin Schuh17e484e2018-03-11 01:11:36 -0800178 } else if (suicide) {
179 state_ = State::PREP_CLIMB;
180 climb_count_ = 50;
181 }
182 break;
183
184 case State::PREP_CLIMB:
185 --climb_count_;
186 if (climb_count_ <= 0) {
187 state_ = State::ESTOP;
188 } else if (!suicide) {
189 state_ = State::RUNNING;
Austin Schuhcb091712018-02-21 20:01:55 -0800190 }
191 break;
192
193 case State::ESTOP:
194 LOG(ERROR, "Estop\n");
195 break;
196 }
197
Austin Schuh17e484e2018-03-11 01:11:36 -0800198 const bool disable = outputs_disabled || (state_ != State::RUNNING &&
199 state_ != State::GOTO_PATH &&
200 state_ != State::PREP_CLIMB);
Austin Schuhcb091712018-02-21 20:01:55 -0800201 if (disable) {
202 close_enough_for_full_power_ = false;
203 }
204
Austin Schuhb874fd32018-03-05 00:27:10 -0800205 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
Austin Schuh96341532018-03-09 21:17:24 -0800206 if (claw_closed_) {
207 if ((filtered_goal == ReadyAboveBoxIndex()) ||
208 (filtered_goal == TallBoxGrabIndex()) ||
209 (filtered_goal == ShortBoxGrabIndex())) {
210 filtered_goal = NeutralIndex();
211 }
212 }
213
214 // TODO(austin): Do we need to debounce box_back_beambreak_triggered ?
215 switch (grab_state_) {
216 case GrabState::NORMAL:
217 if (grab_box && !claw_closed_) {
218 grab_state_ = GrabState::WAIT_FOR_BOX;
219 } else {
220 break;
221 }
222 case GrabState::WAIT_FOR_BOX:
223 if (!grab_box) {
224 grab_state_ = GrabState::NORMAL;
225 } else {
226 if (AtState(ReadyAboveBoxIndex()) && NearEnd()) {
227 // We are being asked to grab the box, and the claw is near the box.
228 if (box_back_beambreak_triggered) {
229 // And we now see the box! Try for a tall box.
230 grab_state_ = GrabState::TALL_BOX;
231 }
232 }
233 }
234 break;
235 case GrabState::TALL_BOX:
236 if (!grab_box) {
237 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800238 } else if (AtState(TallBoxGrabIndex()) && NearEnd()) {
239 // We are being asked to grab the box, and the claw is near the box.
240 if (claw_beambreak_triggered) {
241 grab_state_ = GrabState::CLAW_CLOSE;
242 // Snap time for the delay here.
243 claw_close_start_time_ = ::aos::monotonic_clock::now();
244 } else {
245 grab_state_ = GrabState::SHORT_BOX;
246 }
247 }
248 break;
249 case GrabState::SHORT_BOX:
250 if (!grab_box) {
251 grab_state_ = GrabState::NORMAL;
Austin Schuh96341532018-03-09 21:17:24 -0800252 } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) {
253 // We are being asked to grab the box, and the claw is near the box.
254 if (claw_beambreak_triggered) {
255 grab_state_ = GrabState::CLAW_CLOSE;
256 // Snap time for the delay here.
257 claw_close_start_time_ = ::aos::monotonic_clock::now();
258 } else {
259 grab_state_ = GrabState::WAIT_FOR_BOX;
260 }
261 }
262 break;
263 case GrabState::CLAW_CLOSE:
264 if (::aos::monotonic_clock::now() >
Austin Schuhb874fd32018-03-05 00:27:10 -0800265 claw_close_start_time_ + ::std::chrono::milliseconds(300)) {
Austin Schuh96341532018-03-09 21:17:24 -0800266 grab_state_ = GrabState::OPEN_INTAKE;
267 }
268 break;
269 case GrabState::OPEN_INTAKE:
270 if (intake_clear_of_box) {
271 grab_state_ = GrabState::NORMAL;
272 }
273 break;
274 }
275
276 // Now, based out our current state, go to the right state.
277 switch (grab_state_) {
278 case GrabState::NORMAL:
279 // Don't let the intake close fully with the claw closed.
280 // TODO(austin): If we want to transfer the box from the claw to the
281 // intake, we'll need to change this.
282 if (claw_closed_) {
283 max_intake_override_ = -0.5;
284 } else {
285 max_intake_override_ = 1000.0;
286 }
287 break;
288 case GrabState::WAIT_FOR_BOX:
289 filtered_goal = ReadyAboveBoxIndex();
290 claw_closed_ = false;
291 max_intake_override_ = 1000.0;
292 break;
293 case GrabState::TALL_BOX:
294 filtered_goal = TallBoxGrabIndex();
295 claw_closed_ = false;
296 max_intake_override_ = 1000.0;
297 break;
298 case GrabState::SHORT_BOX:
299 filtered_goal = ShortBoxGrabIndex();
300 claw_closed_ = false;
301 max_intake_override_ = 1000.0;
302 break;
303 case GrabState::CLAW_CLOSE:
304 // Don't move.
305 filtered_goal = current_node_;
306 claw_closed_ = true;
307 max_intake_override_ = 1000.0;
308 break;
309 case GrabState::OPEN_INTAKE:
310 // Don't move.
311 filtered_goal = current_node_;
312 claw_closed_ = true;
313 max_intake_override_ = -0.5;
314 break;
315 }
316
317 if (state_ == State::RUNNING && unsafe_goal != nullptr) {
318 if (current_node_ != filtered_goal) {
319 LOG(INFO, "Goal is different\n");
320 if (filtered_goal >= search_graph_.num_vertexes()) {
321 LOG(ERROR, "goal node out of range ESTOP\n");
322 state_ = State::ESTOP;
323 } else if (follower_.path_distance_to_go() > 1e-3) {
324 // Still on the old path segment. Can't change yet.
325 } else {
326 search_graph_.SetGoal(filtered_goal);
327
328 size_t min_edge = 0;
329 double min_cost = ::std::numeric_limits<double>::infinity();
330 for (const SearchGraph::HalfEdge &edge :
331 search_graph_.Neighbors(current_node_)) {
332 const double cost = search_graph_.GetCostToGoal(edge.dest);
333 if (cost < min_cost) {
334 min_edge = edge.edge_id;
335 min_cost = cost;
336 }
337 }
338 // Ok, now we know which edge we are on. Figure out the path and
339 // trajectory.
340 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
341 LOG(INFO, "Switching from node %d to %d along edge %d\n",
342 static_cast<int>(current_node_), static_cast<int>(next_edge.end),
343 static_cast<int>(min_edge));
Austin Schuh41c71e42018-04-04 20:11:20 -0700344 vmax_ = trajectories_[min_edge].vmax;
345 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
Austin Schuh96341532018-03-09 21:17:24 -0800346 current_node_ = next_edge.end;
347 }
348 }
349 }
350
Austin Schuh345a3732018-03-21 20:49:32 -0700351 const double max_operating_voltage =
352 close_enough_for_full_power_
353 ? kOperatingVoltage()
354 : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax());
Austin Schuh41c71e42018-04-04 20:11:20 -0700355 follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_,
Austin Schuh345a3732018-03-21 20:49:32 -0700356 max_operating_voltage);
357 LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
Austin Schuhcb091712018-02-21 20:01:55 -0800358 status->goal_theta0 = follower_.theta(0);
359 status->goal_theta1 = follower_.theta(1);
360 status->goal_omega0 = follower_.omega(0);
361 status->goal_omega1 = follower_.omega(1);
362
363 status->theta0 = arm_ekf_.X_hat(0);
364 status->theta1 = arm_ekf_.X_hat(2);
365 status->omega0 = arm_ekf_.X_hat(1);
366 status->omega1 = arm_ekf_.X_hat(3);
367 status->voltage_error0 = arm_ekf_.X_hat(4);
368 status->voltage_error1 = arm_ekf_.X_hat(5);
369
370 if (!disable) {
371 *proximal_output = ::std::max(
372 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0)));
373 *distal_output = ::std::max(
374 -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800375 if (state_ != State::PREP_CLIMB) {
376 *release_arm_brake = true;
377 } else {
378 *release_arm_brake = false;
379 }
Austin Schuh96341532018-03-09 21:17:24 -0800380 *claw_closed = claw_closed_;
Austin Schuhcb091712018-02-21 20:01:55 -0800381 }
382
383 status->proximal_estimator_state =
384 proximal_zeroing_estimator_.GetEstimatorState();
385 status->distal_estimator_state =
386 distal_zeroing_estimator_.GetEstimatorState();
387
388 status->path_distance_to_go = follower_.path_distance_to_go();
389 status->current_node = current_node_;
390
391 status->zeroed = (proximal_zeroing_estimator_.zeroed() &&
392 distal_zeroing_estimator_.zeroed());
393 status->estopped = (state_ == State::ESTOP);
394 status->state = static_cast<int32_t>(state_);
Austin Schuh96341532018-03-09 21:17:24 -0800395 status->grab_state = static_cast<int32_t>(grab_state_);
Austin Schuhcb091712018-02-21 20:01:55 -0800396 status->failed_solutions = follower_.failed_solutions();
397
398 arm_ekf_.Predict(follower_.U(), kDt());
399}
400
401} // namespace arm
402} // namespace superstructure
403} // namespace control_loops
404} // namespace y2018