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