Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/superstructure.h" |
| 2 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include "frc971/control_loops/control_loops_generated.h" |
| 7 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 8 | #include "y2018/constants.h" |
| 9 | #include "y2018/control_loops/superstructure/intake/intake.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "y2018/status_light_generated.h" |
| 11 | #include "y2018/vision/vision_generated.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 12 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 13 | namespace y2018::control_loops::superstructure { |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 14 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 15 | using ::aos::monotonic_clock; |
| 16 | |
| 17 | namespace chrono = ::std::chrono; |
| 18 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 19 | namespace { |
| 20 | // The maximum voltage the intake roller will be allowed to use. |
| 21 | constexpr double kMaxIntakeRollerVoltage = 12.0; |
| 22 | } // namespace |
| 23 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 24 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 25 | const ::std::string &name) |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 26 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 27 | name), |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 28 | status_light_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | event_loop->MakeSender<::y2018::StatusLight>("/superstructure")), |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 30 | vision_status_fetcher_( |
| 31 | event_loop->MakeFetcher<::y2018::vision::VisionStatus>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | "/superstructure")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 33 | drivetrain_output_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Output>( |
| 35 | "/drivetrain")), |
Stephan Massalt | a769ca2 | 2019-01-09 05:29:13 +0000 | [diff] [blame] | 36 | intake_left_(constants::GetValues().left_intake.zeroing, |
| 37 | constants::GetValues().left_intake.spring_offset), |
| 38 | intake_right_(constants::GetValues().right_intake.zeroing, |
| 39 | constants::GetValues().right_intake.spring_offset) {} |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 42 | const Position *position, |
| 43 | aos::Sender<Output>::Builder *output, |
| 44 | aos::Sender<Status>::Builder *status) { |
Austin Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 45 | const monotonic_clock::time_point monotonic_now = |
| 46 | event_loop()->monotonic_now(); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 47 | if (WasReset()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 48 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 49 | intake_left_.Reset(); |
| 50 | intake_right_.Reset(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 51 | arm_.Reset(); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 54 | const double clipped_box_distance = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | ::std::min(1.0, ::std::max(0.0, position->box_distance())); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 56 | |
| 57 | const double box_velocity = |
| 58 | (clipped_box_distance - last_box_distance_) / 0.005; |
| 59 | |
| 60 | constexpr double kFilteredBoxVelocityAlpha = 0.02; |
| 61 | filtered_box_velocity_ = |
| 62 | box_velocity * kFilteredBoxVelocityAlpha + |
| 63 | (1.0 - kFilteredBoxVelocityAlpha) * filtered_box_velocity_; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 64 | |
| 65 | constexpr double kCenteringAngleGain = 0.0; |
| 66 | const double left_intake_goal = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | ::std::min(arm_.max_intake_override(), |
| 68 | (unsafe_goal == nullptr || !unsafe_goal->has_intake() |
| 69 | ? 0.0 |
| 70 | : unsafe_goal->intake()->left_intake_angle())) + |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 71 | last_intake_center_error_ * kCenteringAngleGain; |
| 72 | const double right_intake_goal = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 73 | ::std::min(arm_.max_intake_override(), |
| 74 | (unsafe_goal == nullptr || !unsafe_goal->has_intake() |
| 75 | ? 0.0 |
| 76 | : unsafe_goal->intake()->right_intake_angle())) - |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 77 | last_intake_center_error_ * kCenteringAngleGain; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 78 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | IntakeVoltageT left_intake_output; |
| 80 | flatbuffers::Offset<superstructure::IntakeSideStatus> left_status_offset = |
| 81 | intake_left_.Iterate( |
| 82 | unsafe_goal != nullptr ? &(left_intake_goal) : nullptr, |
| 83 | position->left_intake(), |
| 84 | output != nullptr ? &(left_intake_output) : nullptr, status->fbb()); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 85 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 86 | IntakeVoltageT right_intake_output; |
| 87 | flatbuffers::Offset<superstructure::IntakeSideStatus> right_status_offset = |
| 88 | intake_right_.Iterate( |
| 89 | unsafe_goal != nullptr ? &(right_intake_goal) : nullptr, |
| 90 | position->right_intake(), |
| 91 | output != nullptr ? &(right_intake_output) : nullptr, status->fbb()); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 92 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 93 | const double intake_center_error = |
| 94 | intake_right_.output_position() - intake_left_.output_position(); |
| 95 | last_intake_center_error_ = intake_center_error; |
| 96 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 97 | const bool intake_clear_of_box = |
| 98 | intake_left_.clear_of_box() && intake_right_.clear_of_box(); |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 99 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 100 | bool open_claw = unsafe_goal != nullptr ? unsafe_goal->open_claw() : false; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 101 | if (unsafe_goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 102 | if (unsafe_goal->open_threshold() != 0.0) { |
| 103 | if (arm_.current_node() != unsafe_goal->arm_goal_position() || |
| 104 | arm_.path_distance_to_go() > unsafe_goal->open_threshold()) { |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 105 | open_claw = false; |
| 106 | } |
| 107 | } |
| 108 | } |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 109 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 110 | const uint32_t arm_goal_position = |
| 111 | unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u; |
| 112 | |
| 113 | double voltage_proximal_output = 0.0; |
| 114 | double voltage_distal_output = 0.0; |
| 115 | bool release_arm_brake_output = false; |
| 116 | bool claw_grabbed_output = false; |
| 117 | flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset = |
| 118 | arm_.Iterate( |
| 119 | monotonic_now, |
| 120 | unsafe_goal != nullptr ? &(arm_goal_position) : nullptr, |
| 121 | unsafe_goal != nullptr ? unsafe_goal->grab_box() : false, open_claw, |
| 122 | unsafe_goal != nullptr ? unsafe_goal->close_claw() : false, |
| 123 | position->arm(), position->claw_beambreak_triggered(), |
| 124 | position->box_back_beambreak_triggered(), intake_clear_of_box, |
| 125 | unsafe_goal != nullptr ? unsafe_goal->voltage_winch() > 1.0 : false, |
| 126 | unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false, |
| 127 | output != nullptr ? &voltage_proximal_output : nullptr, |
| 128 | output != nullptr ? &voltage_distal_output : nullptr, |
| 129 | output != nullptr ? &release_arm_brake_output : nullptr, |
| 130 | output != nullptr ? &claw_grabbed_output : nullptr, status->fbb()); |
| 131 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | bool hook_release_output = false; |
| 133 | bool forks_release_output = false; |
| 134 | double voltage_winch_output = 0.0; |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 135 | if (output) { |
| 136 | if (unsafe_goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 137 | hook_release_output = unsafe_goal->hook_release(); |
| 138 | voltage_winch_output = unsafe_goal->voltage_winch(); |
| 139 | forks_release_output = unsafe_goal->deploy_fork(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 142 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 144 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 145 | status_builder.add_left_intake(left_status_offset); |
| 146 | status_builder.add_right_intake(right_status_offset); |
| 147 | status_builder.add_arm(arm_status_offset); |
| 148 | |
| 149 | status_builder.add_filtered_box_velocity(filtered_box_velocity_); |
| 150 | const bool estopped = |
| 151 | intake_left_.estopped() || intake_right_.estopped() || arm_.estopped(); |
| 152 | status_builder.add_estopped(estopped); |
| 153 | |
| 154 | status_builder.add_zeroed(intake_left_.zeroed() && intake_right_.zeroed() && |
| 155 | arm_.zeroed()); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 156 | |
| 157 | if (output && unsafe_goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | double roller_voltage = |
| 159 | ::std::max(-kMaxIntakeRollerVoltage, |
| 160 | ::std::min(unsafe_goal->intake()->roller_voltage(), |
| 161 | kMaxIntakeRollerVoltage)); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 162 | constexpr int kReverseTime = 14; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | if (unsafe_goal->intake()->roller_voltage() < 0.0 || |
| 164 | unsafe_goal->disable_box_correct()) { |
| 165 | left_intake_output.voltage_rollers = roller_voltage; |
| 166 | right_intake_output.voltage_rollers = roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 167 | rotation_state_ = RotationState::NOT_ROTATING; |
| 168 | rotation_count_ = 0; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 169 | stuck_count_ = 0; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 170 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | const bool stuck = position->box_distance() < 0.20 && |
| 172 | filtered_box_velocity_ > -0.05 && |
| 173 | !position->box_back_beambreak_triggered(); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 174 | // Make sure we don't declare ourselves re-stuck too quickly. We want to |
| 175 | // wait 400 ms before triggering the stuck condition again. |
| 176 | if (!stuck) { |
| 177 | last_unstuck_time_ = monotonic_now; |
| 178 | } |
| 179 | if (monotonic_now < last_stuck_time_ + chrono::milliseconds(400)) { |
| 180 | last_unstuck_time_ = monotonic_now; |
| 181 | } |
| 182 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 183 | switch (rotation_state_) { |
| 184 | case RotationState::NOT_ROTATING: |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 185 | if (stuck && |
| 186 | monotonic_now > last_stuck_time_ + chrono::milliseconds(400) && |
| 187 | monotonic_now > last_unstuck_time_ + chrono::milliseconds(100)) { |
| 188 | rotation_state_ = RotationState::STUCK; |
| 189 | ++stuck_count_; |
| 190 | last_stuck_time_ = monotonic_now; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | } else if (position->left_intake()->beam_break()) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 192 | rotation_state_ = RotationState::ROTATING_RIGHT; |
| 193 | rotation_count_ = kReverseTime; |
| 194 | break; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 195 | } else if (position->right_intake()->beam_break()) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 196 | rotation_state_ = RotationState::ROTATING_LEFT; |
| 197 | rotation_count_ = kReverseTime; |
| 198 | break; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 199 | } |
Vinay Siva | 86f568e | 2021-07-11 11:20:02 -0700 | [diff] [blame] | 200 | [[fallthrough]]; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 201 | case RotationState::STUCK: { |
| 202 | // Latch being stuck for 80 ms so we kick the box out far enough. |
| 203 | if (last_stuck_time_ + chrono::milliseconds(80) < monotonic_now) { |
| 204 | rotation_state_ = RotationState::NOT_ROTATING; |
| 205 | last_unstuck_time_ = monotonic_now; |
| 206 | } |
| 207 | } break; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 208 | case RotationState::ROTATING_LEFT: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 209 | if (position->right_intake()->beam_break()) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 210 | rotation_count_ = kReverseTime; |
| 211 | } else { |
| 212 | --rotation_count_; |
| 213 | } |
| 214 | if (rotation_count_ == 0) { |
| 215 | rotation_state_ = RotationState::NOT_ROTATING; |
| 216 | } |
| 217 | break; |
| 218 | case RotationState::ROTATING_RIGHT: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | if (position->left_intake()->beam_break()) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 220 | rotation_count_ = kReverseTime; |
| 221 | } else { |
| 222 | --rotation_count_; |
| 223 | } |
| 224 | if (rotation_count_ == 0) { |
| 225 | rotation_state_ = RotationState::NOT_ROTATING; |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 230 | constexpr double kHoldVoltage = 1.0; |
| 231 | constexpr double kStuckVoltage = 10.0; |
| 232 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | if (position->box_back_beambreak_triggered() && |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 234 | roller_voltage > kHoldVoltage) { |
| 235 | roller_voltage = kHoldVoltage; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 236 | } |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 237 | switch (rotation_state_) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 238 | case RotationState::NOT_ROTATING: { |
| 239 | double centering_gain = 13.0; |
| 240 | if (stuck_count_ > 1) { |
| 241 | if ((stuck_count_ - 1) % 2 == 0) { |
| 242 | centering_gain = 0.0; |
| 243 | } |
| 244 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 245 | left_intake_output.voltage_rollers = |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 246 | roller_voltage - intake_center_error * centering_gain; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | right_intake_output.voltage_rollers = |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 248 | roller_voltage + intake_center_error * centering_gain; |
| 249 | } break; |
| 250 | case RotationState::STUCK: { |
| 251 | if (roller_voltage > kHoldVoltage) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 252 | left_intake_output.voltage_rollers = -kStuckVoltage; |
| 253 | right_intake_output.voltage_rollers = -kStuckVoltage; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 254 | } |
| 255 | } break; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 256 | case RotationState::ROTATING_LEFT: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | if (position->left_intake()->beam_break()) { |
| 258 | left_intake_output.voltage_rollers = -roller_voltage * 0.9; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 259 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 260 | left_intake_output.voltage_rollers = -roller_voltage * 0.6; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 261 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 262 | right_intake_output.voltage_rollers = roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 263 | break; |
| 264 | case RotationState::ROTATING_RIGHT: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 265 | left_intake_output.voltage_rollers = roller_voltage; |
| 266 | if (position->right_intake()->beam_break()) { |
| 267 | right_intake_output.voltage_rollers = -roller_voltage * 0.9; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 268 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 269 | right_intake_output.voltage_rollers = -roller_voltage * 0.6; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 270 | } |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | } |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 274 | } else { |
| 275 | rotation_state_ = RotationState::NOT_ROTATING; |
| 276 | rotation_count_ = 0; |
| 277 | stuck_count_ = 0; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 278 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | status_builder.add_rotation_state(static_cast<uint32_t>(rotation_state_)); |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 280 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 281 | drivetrain_output_fetcher_.Fetch(); |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 282 | |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 283 | vision_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | if (estopped) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 285 | SendColors(0.5, 0.0, 0.0); |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 286 | } else if (!vision_status_fetcher_.get() || |
| 287 | monotonic_now > |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 288 | vision_status_fetcher_.context().monotonic_event_time + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 289 | chrono::seconds(1)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 290 | SendColors(0.5, 0.5, 0.0); |
| 291 | } else if (rotation_state_ == RotationState::ROTATING_LEFT || |
| 292 | rotation_state_ == RotationState::ROTATING_RIGHT) { |
| 293 | SendColors(0.5, 0.20, 0.0); |
| 294 | } else if (rotation_state_ == RotationState::STUCK) { |
| 295 | SendColors(0.5, 0.0, 0.5); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 296 | } else if (position->box_back_beambreak_triggered()) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 297 | SendColors(0.0, 0.0, 0.5); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 298 | } else if (position->box_distance() < 0.2) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 299 | SendColors(0.0, 0.5, 0.0); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 300 | } else if (drivetrain_output_fetcher_.get() && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | ::std::max( |
| 302 | ::std::abs(drivetrain_output_fetcher_->left_voltage()), |
| 303 | ::std::abs(drivetrain_output_fetcher_->right_voltage())) > |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 304 | 11.5) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 305 | SendColors(0.5, 0.0, 0.5); |
| 306 | } else { |
| 307 | SendColors(0.0, 0.0, 0.0); |
| 308 | } |
| 309 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 310 | last_box_distance_ = clipped_box_distance; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 311 | |
| 312 | if (output) { |
| 313 | flatbuffers::Offset<IntakeVoltage> left_intake_offset = |
| 314 | IntakeVoltage::Pack(*output->fbb(), &left_intake_output); |
| 315 | flatbuffers::Offset<IntakeVoltage> right_intake_offset = |
| 316 | IntakeVoltage::Pack(*output->fbb(), &right_intake_output); |
| 317 | |
| 318 | Output::Builder output_builder = output->MakeBuilder<Output>(); |
| 319 | output_builder.add_left_intake(left_intake_offset); |
| 320 | output_builder.add_right_intake(right_intake_offset); |
| 321 | output_builder.add_voltage_proximal(voltage_proximal_output); |
| 322 | output_builder.add_voltage_distal(voltage_distal_output); |
| 323 | output_builder.add_release_arm_brake(release_arm_brake_output); |
| 324 | output_builder.add_claw_grabbed(claw_grabbed_output); |
| 325 | |
| 326 | output_builder.add_hook_release(hook_release_output); |
| 327 | output_builder.add_forks_release(forks_release_output); |
| 328 | output_builder.add_voltage_winch(voltage_winch_output); |
| 329 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 330 | output->CheckOk(output->Send(output_builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | } |
| 332 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 333 | (void)status->Send(status_builder.Finish()); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 336 | void Superstructure::SendColors(float red, float green, float blue) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | auto builder = status_light_sender_.MakeBuilder(); |
| 338 | StatusLight::Builder status_light_builder = |
| 339 | builder.MakeBuilder<StatusLight>(); |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 340 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 341 | status_light_builder.add_red(red); |
| 342 | status_light_builder.add_green(green); |
| 343 | status_light_builder.add_blue(blue); |
| 344 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 345 | if (builder.Send(status_light_builder.Finish()) != |
| 346 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 347 | AOS_LOG(ERROR, "Failed to send lights.\n"); |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 351 | } // namespace y2018::control_loops::superstructure |