Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/arm/arm.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <iostream> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 7 | #include "y2018/constants.h" |
| 8 | #include "y2018/control_loops/superstructure/arm/demo_path.h" |
| 9 | #include "y2018/control_loops/superstructure/arm/dynamics.h" |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 10 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 11 | |
| 12 | namespace y2018 { |
| 13 | namespace control_loops { |
| 14 | namespace superstructure { |
| 15 | namespace arm { |
| 16 | |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 19 | namespace chrono = ::std::chrono; |
| 20 | using ::aos::monotonic_clock; |
| 21 | |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 22 | constexpr int kMaxBrownoutCount = 4; |
| 23 | |
| 24 | } // namespace |
| 25 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 26 | Arm::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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 32 | search_graph_(MakeSearchGraph(&trajectories_, alpha_unitizer_, kVMax())), |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 33 | // Go to the start of the first trajectory. |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 34 | follower_(ReadyAboveBoxPoint()), |
| 35 | points_(PointList()) { |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 36 | int i = 0; |
| 37 | for (const auto &trajectory : trajectories_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 38 | AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i, |
| 39 | trajectory.trajectory.path().length()); |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 40 | ++i; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | void Arm::Reset() { state_ = State::UNINITIALIZED; } |
| 45 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | flatbuffers::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 Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 55 | ::Eigen::Matrix<double, 2, 1> Y; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 56 | const bool outputs_disabled = |
| 57 | ((proximal_output == nullptr) || (distal_output == nullptr) || |
| 58 | (release_arm_brake == nullptr) || (claw_closed == nullptr)); |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 59 | if (outputs_disabled) { |
| 60 | ++brownout_count_; |
| 61 | } else { |
| 62 | brownout_count_ = 0; |
| 63 | } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 64 | |
| 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 Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 73 | if (close_claw) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 74 | claw_closed_ = true; |
| 75 | } |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 76 | 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 Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 87 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | Y << position->proximal()->encoder() + proximal_offset_, |
| 89 | position->distal()->encoder() + distal_offset_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 90 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | proximal_zeroing_estimator_.UpdateEstimate(*position->proximal()); |
| 92 | distal_zeroing_estimator_.UpdateEstimate(*position->distal()); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 93 | |
| 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 115 | AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 116 | state_ = State::ZEROING; |
| 117 | proximal_zeroing_estimator_.Reset(); |
| 118 | distal_zeroing_estimator_.Reset(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 119 | break; |
| 120 | |
| 121 | case State::ZEROING: |
| 122 | // Zero by not moving. |
| 123 | if (proximal_zeroing_estimator_.zeroed() && |
| 124 | distal_zeroing_estimator_.zeroed()) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 125 | state_ = State::DISABLED; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 126 | |
| 127 | proximal_offset_ = proximal_zeroing_estimator_.offset(); |
| 128 | distal_offset_ = distal_zeroing_estimator_.offset(); |
| 129 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | Y << position->proximal()->encoder() + proximal_offset_, |
| 131 | position->distal()->encoder() + distal_offset_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 132 | |
| 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 Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame^] | 141 | [[fallthrough]]; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 142 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 143 | case State::DISABLED: { |
| 144 | follower_.SwitchTrajectory(nullptr); |
| 145 | close_enough_for_full_power_ = false; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 146 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 147 | 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 Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 166 | state_ = State::GOTO_PATH; |
| 167 | } else { |
| 168 | break; |
| 169 | } |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 170 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame^] | 171 | [[fallthrough]]; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 172 | |
| 173 | case State::GOTO_PATH: |
| 174 | if (outputs_disabled) { |
| 175 | state_ = State::DISABLED; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 176 | } 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 Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 181 | } else if (close_enough_for_full_power_) { |
| 182 | state_ = State::RUNNING; |
| 183 | grab_state_ = GrabState::NORMAL; |
| 184 | } |
| 185 | break; |
| 186 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 187 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 192 | AOS_LOG(ERROR, "Zeroing error ESTOP\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 193 | state_ = State::ESTOP; |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 194 | } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 195 | state_ = State::DISABLED; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 196 | } 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 Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 201 | } 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 Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 213 | } |
| 214 | break; |
| 215 | |
| 216 | case State::ESTOP: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 217 | AOS_LOG(ERROR, "Estop\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 221 | const bool disable = outputs_disabled || (state_ != State::RUNNING && |
| 222 | state_ != State::GOTO_PATH && |
| 223 | state_ != State::PREP_CLIMB); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 224 | if (disable) { |
| 225 | close_enough_for_full_power_ = false; |
| 226 | } |
| 227 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 228 | // TODO(austin): Do we need to debounce box_back_beambreak_triggered ? |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 229 | 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 Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 261 | } 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 Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 266 | claw_close_start_time_ = monotonic_now; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 267 | } 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 Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 275 | } 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 Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 280 | claw_close_start_time_ = monotonic_now; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 281 | } else { |
| 282 | grab_state_ = GrabState::WAIT_FOR_BOX; |
| 283 | } |
| 284 | } |
| 285 | break; |
| 286 | case GrabState::CLAW_CLOSE: |
Austin Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 287 | if (monotonic_now > |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 288 | claw_close_start_time_ + ::std::chrono::milliseconds(300)) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 289 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 342 | AOS_LOG(INFO, "Goal is different\n"); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 343 | if (filtered_goal >= search_graph_.num_vertexes()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 344 | AOS_LOG(ERROR, "goal node out of range ESTOP\n"); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 345 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 364 | 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 Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 367 | vmax_ = trajectories_[min_edge].vmax; |
| 368 | follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 369 | current_node_ = next_edge.end; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Austin Schuh | 345a373 | 2018-03-21 20:49:32 -0700 | [diff] [blame] | 374 | const double max_operating_voltage = |
| 375 | close_enough_for_full_power_ |
| 376 | ? kOperatingVoltage() |
| 377 | : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax()); |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 378 | follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_, |
Austin Schuh | 345a373 | 2018-03-21 20:49:32 -0700 | [diff] [blame] | 379 | max_operating_voltage); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 380 | AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 381 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 382 | 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 Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 404 | |
| 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 Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 410 | if (state_ != State::PREP_CLIMB) { |
| 411 | *release_arm_brake = true; |
| 412 | } else { |
| 413 | *release_arm_brake = false; |
| 414 | } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 415 | *claw_closed = claw_closed_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | status_builder.add_path_distance_to_go(follower_.path_distance_to_go()); |
| 419 | status_builder.add_current_node(current_node_); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 420 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 421 | 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 Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 426 | |
| 427 | arm_ekf_.Predict(follower_.U(), kDt()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 428 | return status_builder.Finish(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | } // namespace arm |
| 432 | } // namespace superstructure |
| 433 | } // namespace control_loops |
| 434 | } // namespace y2018 |