blob: fc082809ce473012604457034d8d74478e187def [file] [log] [blame]
Sabina Davis8d20ca82018-02-19 13:17:45 -08001#include "y2018/control_loops/superstructure/superstructure.h"
2
Neil Balchba9cbba2018-04-06 22:26:38 -07003#include <chrono>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "frc971/control_loops/control_loops_generated.h"
7#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -08008#include "y2018/constants.h"
9#include "y2018/control_loops/superstructure/intake/intake.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "y2018/status_light_generated.h"
11#include "y2018/vision/vision_generated.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080012
13namespace y2018 {
14namespace control_loops {
15namespace superstructure {
16
Neil Balchba9cbba2018-04-06 22:26:38 -070017using ::aos::monotonic_clock;
18
19namespace chrono = ::std::chrono;
20
Sabina Davis8d20ca82018-02-19 13:17:45 -080021namespace {
22// The maximum voltage the intake roller will be allowed to use.
23constexpr double kMaxIntakeRollerVoltage = 12.0;
24} // namespace
25
Austin Schuh55a13dc2019-01-27 22:39:03 -080026Superstructure::Superstructure(::aos::EventLoop *event_loop,
27 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070028 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
29 name),
Austin Schuh01a9f2a2019-05-27 13:36:30 -070030 status_light_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 event_loop->MakeSender<::y2018::StatusLight>("/superstructure")),
Austin Schuh300f2f62019-05-27 13:49:23 -070032 vision_status_fetcher_(
33 event_loop->MakeFetcher<::y2018::vision::VisionStatus>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070035 drivetrain_output_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
37 "/drivetrain")),
Stephan Massalta769ca22019-01-09 05:29:13 +000038 intake_left_(constants::GetValues().left_intake.zeroing,
39 constants::GetValues().left_intake.spring_offset),
40 intake_right_(constants::GetValues().right_intake.zeroing,
41 constants::GetValues().right_intake.spring_offset) {}
Sabina Davis8d20ca82018-02-19 13:17:45 -080042
Alex Perrycb7da4b2019-08-28 19:35:56 -070043void Superstructure::RunIteration(const Goal *unsafe_goal,
44 const Position *position,
45 aos::Sender<Output>::Builder *output,
46 aos::Sender<Status>::Builder *status) {
Austin Schuh20177c92019-07-07 20:48:24 -070047 const monotonic_clock::time_point monotonic_now =
48 event_loop()->monotonic_now();
Sabina Davis8d20ca82018-02-19 13:17:45 -080049 if (WasReset()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070050 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -080051 intake_left_.Reset();
52 intake_right_.Reset();
Austin Schuhcb091712018-02-21 20:01:55 -080053 arm_.Reset();
Sabina Davis8d20ca82018-02-19 13:17:45 -080054 }
55
Neil Balchba9cbba2018-04-06 22:26:38 -070056 const double clipped_box_distance =
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 ::std::min(1.0, ::std::max(0.0, position->box_distance()));
Neil Balchba9cbba2018-04-06 22:26:38 -070058
59 const double box_velocity =
60 (clipped_box_distance - last_box_distance_) / 0.005;
61
62 constexpr double kFilteredBoxVelocityAlpha = 0.02;
63 filtered_box_velocity_ =
64 box_velocity * kFilteredBoxVelocityAlpha +
65 (1.0 - kFilteredBoxVelocityAlpha) * filtered_box_velocity_;
Neil Balchba9cbba2018-04-06 22:26:38 -070066
67 constexpr double kCenteringAngleGain = 0.0;
68 const double left_intake_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -070069 ::std::min(arm_.max_intake_override(),
70 (unsafe_goal == nullptr || !unsafe_goal->has_intake()
71 ? 0.0
72 : unsafe_goal->intake()->left_intake_angle())) +
Neil Balchba9cbba2018-04-06 22:26:38 -070073 last_intake_center_error_ * kCenteringAngleGain;
74 const double right_intake_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 ::std::min(arm_.max_intake_override(),
76 (unsafe_goal == nullptr || !unsafe_goal->has_intake()
77 ? 0.0
78 : unsafe_goal->intake()->right_intake_angle())) -
Neil Balchba9cbba2018-04-06 22:26:38 -070079 last_intake_center_error_ * kCenteringAngleGain;
Austin Schuh83cdd8a2018-03-21 20:49:02 -070080
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 IntakeVoltageT left_intake_output;
82 flatbuffers::Offset<superstructure::IntakeSideStatus> left_status_offset =
83 intake_left_.Iterate(
84 unsafe_goal != nullptr ? &(left_intake_goal) : nullptr,
85 position->left_intake(),
86 output != nullptr ? &(left_intake_output) : nullptr, status->fbb());
Sabina Davis8d20ca82018-02-19 13:17:45 -080087
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 IntakeVoltageT right_intake_output;
89 flatbuffers::Offset<superstructure::IntakeSideStatus> right_status_offset =
90 intake_right_.Iterate(
91 unsafe_goal != nullptr ? &(right_intake_goal) : nullptr,
92 position->right_intake(),
93 output != nullptr ? &(right_intake_output) : nullptr, status->fbb());
Sabina Davis8d20ca82018-02-19 13:17:45 -080094
Neil Balchba9cbba2018-04-06 22:26:38 -070095 const double intake_center_error =
96 intake_right_.output_position() - intake_left_.output_position();
97 last_intake_center_error_ = intake_center_error;
98
Austin Schuh96341532018-03-09 21:17:24 -080099 const bool intake_clear_of_box =
100 intake_left_.clear_of_box() && intake_right_.clear_of_box();
Austin Schuhd76546a2018-07-08 16:05:14 -0700101
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 bool open_claw = unsafe_goal != nullptr ? unsafe_goal->open_claw() : false;
Austin Schuhd76546a2018-07-08 16:05:14 -0700103 if (unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 if (unsafe_goal->open_threshold() != 0.0) {
105 if (arm_.current_node() != unsafe_goal->arm_goal_position() ||
106 arm_.path_distance_to_go() > unsafe_goal->open_threshold()) {
Austin Schuhd76546a2018-07-08 16:05:14 -0700107 open_claw = false;
108 }
109 }
110 }
Austin Schuh17e484e2018-03-11 01:11:36 -0800111
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 const uint32_t arm_goal_position =
113 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
114
115 double voltage_proximal_output = 0.0;
116 double voltage_distal_output = 0.0;
117 bool release_arm_brake_output = false;
118 bool claw_grabbed_output = false;
119 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
120 arm_.Iterate(
121 monotonic_now,
122 unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
123 unsafe_goal != nullptr ? unsafe_goal->grab_box() : false, open_claw,
124 unsafe_goal != nullptr ? unsafe_goal->close_claw() : false,
125 position->arm(), position->claw_beambreak_triggered(),
126 position->box_back_beambreak_triggered(), intake_clear_of_box,
127 unsafe_goal != nullptr ? unsafe_goal->voltage_winch() > 1.0 : false,
128 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
129 output != nullptr ? &voltage_proximal_output : nullptr,
130 output != nullptr ? &voltage_distal_output : nullptr,
131 output != nullptr ? &release_arm_brake_output : nullptr,
132 output != nullptr ? &claw_grabbed_output : nullptr, status->fbb());
133
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 bool hook_release_output = false;
135 bool forks_release_output = false;
136 double voltage_winch_output = 0.0;
Austin Schuh17e484e2018-03-11 01:11:36 -0800137 if (output) {
138 if (unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 hook_release_output = unsafe_goal->hook_release();
140 voltage_winch_output = unsafe_goal->voltage_winch();
141 forks_release_output = unsafe_goal->deploy_fork();
Austin Schuh17e484e2018-03-11 01:11:36 -0800142 }
143 }
Austin Schuhcb091712018-02-21 20:01:55 -0800144
Alex Perrycb7da4b2019-08-28 19:35:56 -0700145 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhcb091712018-02-21 20:01:55 -0800146
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 status_builder.add_left_intake(left_status_offset);
148 status_builder.add_right_intake(right_status_offset);
149 status_builder.add_arm(arm_status_offset);
150
151 status_builder.add_filtered_box_velocity(filtered_box_velocity_);
152 const bool estopped =
153 intake_left_.estopped() || intake_right_.estopped() || arm_.estopped();
154 status_builder.add_estopped(estopped);
155
156 status_builder.add_zeroed(intake_left_.zeroed() && intake_right_.zeroed() &&
157 arm_.zeroed());
Sabina Davis8d20ca82018-02-19 13:17:45 -0800158
159 if (output && unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 double roller_voltage =
161 ::std::max(-kMaxIntakeRollerVoltage,
162 ::std::min(unsafe_goal->intake()->roller_voltage(),
163 kMaxIntakeRollerVoltage));
Neil Balchba9cbba2018-04-06 22:26:38 -0700164 constexpr int kReverseTime = 14;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 if (unsafe_goal->intake()->roller_voltage() < 0.0 ||
166 unsafe_goal->disable_box_correct()) {
167 left_intake_output.voltage_rollers = roller_voltage;
168 right_intake_output.voltage_rollers = roller_voltage;
Austin Schuh17dd0892018-03-02 20:06:31 -0800169 rotation_state_ = RotationState::NOT_ROTATING;
170 rotation_count_ = 0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700171 stuck_count_ = 0;
Austin Schuh17dd0892018-03-02 20:06:31 -0800172 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 const bool stuck = position->box_distance() < 0.20 &&
174 filtered_box_velocity_ > -0.05 &&
175 !position->box_back_beambreak_triggered();
Neil Balchba9cbba2018-04-06 22:26:38 -0700176 // Make sure we don't declare ourselves re-stuck too quickly. We want to
177 // wait 400 ms before triggering the stuck condition again.
178 if (!stuck) {
179 last_unstuck_time_ = monotonic_now;
180 }
181 if (monotonic_now < last_stuck_time_ + chrono::milliseconds(400)) {
182 last_unstuck_time_ = monotonic_now;
183 }
184
Austin Schuh17dd0892018-03-02 20:06:31 -0800185 switch (rotation_state_) {
186 case RotationState::NOT_ROTATING:
Neil Balchba9cbba2018-04-06 22:26:38 -0700187 if (stuck &&
188 monotonic_now > last_stuck_time_ + chrono::milliseconds(400) &&
189 monotonic_now > last_unstuck_time_ + chrono::milliseconds(100)) {
190 rotation_state_ = RotationState::STUCK;
191 ++stuck_count_;
192 last_stuck_time_ = monotonic_now;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 } else if (position->left_intake()->beam_break()) {
Austin Schuh17dd0892018-03-02 20:06:31 -0800194 rotation_state_ = RotationState::ROTATING_RIGHT;
195 rotation_count_ = kReverseTime;
196 break;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 } else if (position->right_intake()->beam_break()) {
Austin Schuh17dd0892018-03-02 20:06:31 -0800198 rotation_state_ = RotationState::ROTATING_LEFT;
199 rotation_count_ = kReverseTime;
200 break;
Austin Schuh17dd0892018-03-02 20:06:31 -0800201 }
Vinay Siva86f568e2021-07-11 11:20:02 -0700202 [[fallthrough]];
Neil Balchba9cbba2018-04-06 22:26:38 -0700203 case RotationState::STUCK: {
204 // Latch being stuck for 80 ms so we kick the box out far enough.
205 if (last_stuck_time_ + chrono::milliseconds(80) < monotonic_now) {
206 rotation_state_ = RotationState::NOT_ROTATING;
207 last_unstuck_time_ = monotonic_now;
208 }
209 } break;
Austin Schuh17dd0892018-03-02 20:06:31 -0800210 case RotationState::ROTATING_LEFT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 if (position->right_intake()->beam_break()) {
Austin Schuh17dd0892018-03-02 20:06:31 -0800212 rotation_count_ = kReverseTime;
213 } else {
214 --rotation_count_;
215 }
216 if (rotation_count_ == 0) {
217 rotation_state_ = RotationState::NOT_ROTATING;
218 }
219 break;
220 case RotationState::ROTATING_RIGHT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700221 if (position->left_intake()->beam_break()) {
Austin Schuh17dd0892018-03-02 20:06:31 -0800222 rotation_count_ = kReverseTime;
223 } else {
224 --rotation_count_;
225 }
226 if (rotation_count_ == 0) {
227 rotation_state_ = RotationState::NOT_ROTATING;
228 }
229 break;
230 }
231
Neil Balchba9cbba2018-04-06 22:26:38 -0700232 constexpr double kHoldVoltage = 1.0;
233 constexpr double kStuckVoltage = 10.0;
234
Alex Perrycb7da4b2019-08-28 19:35:56 -0700235 if (position->box_back_beambreak_triggered() &&
Neil Balchba9cbba2018-04-06 22:26:38 -0700236 roller_voltage > kHoldVoltage) {
237 roller_voltage = kHoldVoltage;
Austin Schuh96341532018-03-09 21:17:24 -0800238 }
Austin Schuh17dd0892018-03-02 20:06:31 -0800239 switch (rotation_state_) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700240 case RotationState::NOT_ROTATING: {
241 double centering_gain = 13.0;
242 if (stuck_count_ > 1) {
243 if ((stuck_count_ - 1) % 2 == 0) {
244 centering_gain = 0.0;
245 }
246 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 left_intake_output.voltage_rollers =
Neil Balchba9cbba2018-04-06 22:26:38 -0700248 roller_voltage - intake_center_error * centering_gain;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 right_intake_output.voltage_rollers =
Neil Balchba9cbba2018-04-06 22:26:38 -0700250 roller_voltage + intake_center_error * centering_gain;
251 } break;
252 case RotationState::STUCK: {
253 if (roller_voltage > kHoldVoltage) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254 left_intake_output.voltage_rollers = -kStuckVoltage;
255 right_intake_output.voltage_rollers = -kStuckVoltage;
Neil Balchba9cbba2018-04-06 22:26:38 -0700256 }
257 } break;
Austin Schuh17dd0892018-03-02 20:06:31 -0800258 case RotationState::ROTATING_LEFT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 if (position->left_intake()->beam_break()) {
260 left_intake_output.voltage_rollers = -roller_voltage * 0.9;
Neil Balchba9cbba2018-04-06 22:26:38 -0700261 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262 left_intake_output.voltage_rollers = -roller_voltage * 0.6;
Neil Balchba9cbba2018-04-06 22:26:38 -0700263 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700264 right_intake_output.voltage_rollers = roller_voltage;
Austin Schuh17dd0892018-03-02 20:06:31 -0800265 break;
266 case RotationState::ROTATING_RIGHT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700267 left_intake_output.voltage_rollers = roller_voltage;
268 if (position->right_intake()->beam_break()) {
269 right_intake_output.voltage_rollers = -roller_voltage * 0.9;
Neil Balchba9cbba2018-04-06 22:26:38 -0700270 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 right_intake_output.voltage_rollers = -roller_voltage * 0.6;
Neil Balchba9cbba2018-04-06 22:26:38 -0700272 }
Austin Schuh17dd0892018-03-02 20:06:31 -0800273 break;
274 }
275 }
Neil Balchba9cbba2018-04-06 22:26:38 -0700276 } else {
277 rotation_state_ = RotationState::NOT_ROTATING;
278 rotation_count_ = 0;
279 stuck_count_ = 0;
Sabina Davis8d20ca82018-02-19 13:17:45 -0800280 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 status_builder.add_rotation_state(static_cast<uint32_t>(rotation_state_));
Austin Schuh8d5fff42018-05-30 20:44:12 -0700282
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700283 drivetrain_output_fetcher_.Fetch();
Austin Schuh8d5fff42018-05-30 20:44:12 -0700284
Austin Schuh300f2f62019-05-27 13:49:23 -0700285 vision_status_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 if (estopped) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700287 SendColors(0.5, 0.0, 0.0);
Austin Schuh300f2f62019-05-27 13:49:23 -0700288 } else if (!vision_status_fetcher_.get() ||
289 monotonic_now >
Austin Schuhad154822019-12-27 15:45:13 -0800290 vision_status_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291 chrono::seconds(1)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700292 SendColors(0.5, 0.5, 0.0);
293 } else if (rotation_state_ == RotationState::ROTATING_LEFT ||
294 rotation_state_ == RotationState::ROTATING_RIGHT) {
295 SendColors(0.5, 0.20, 0.0);
296 } else if (rotation_state_ == RotationState::STUCK) {
297 SendColors(0.5, 0.0, 0.5);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 } else if (position->box_back_beambreak_triggered()) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700299 SendColors(0.0, 0.0, 0.5);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 } else if (position->box_distance() < 0.2) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700301 SendColors(0.0, 0.5, 0.0);
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700302 } else if (drivetrain_output_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 ::std::max(
304 ::std::abs(drivetrain_output_fetcher_->left_voltage()),
305 ::std::abs(drivetrain_output_fetcher_->right_voltage())) >
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700306 11.5) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700307 SendColors(0.5, 0.0, 0.5);
308 } else {
309 SendColors(0.0, 0.0, 0.0);
310 }
311
Neil Balchba9cbba2018-04-06 22:26:38 -0700312 last_box_distance_ = clipped_box_distance;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313
314 if (output) {
315 flatbuffers::Offset<IntakeVoltage> left_intake_offset =
316 IntakeVoltage::Pack(*output->fbb(), &left_intake_output);
317 flatbuffers::Offset<IntakeVoltage> right_intake_offset =
318 IntakeVoltage::Pack(*output->fbb(), &right_intake_output);
319
320 Output::Builder output_builder = output->MakeBuilder<Output>();
321 output_builder.add_left_intake(left_intake_offset);
322 output_builder.add_right_intake(right_intake_offset);
323 output_builder.add_voltage_proximal(voltage_proximal_output);
324 output_builder.add_voltage_distal(voltage_distal_output);
325 output_builder.add_release_arm_brake(release_arm_brake_output);
326 output_builder.add_claw_grabbed(claw_grabbed_output);
327
328 output_builder.add_hook_release(hook_release_output);
329 output_builder.add_forks_release(forks_release_output);
330 output_builder.add_voltage_winch(voltage_winch_output);
331
milind1f1dca32021-07-03 13:50:07 -0700332 output->CheckOk(output->Send(output_builder.Finish()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 }
334
milind1f1dca32021-07-03 13:50:07 -0700335 (void)status->Send(status_builder.Finish());
Sabina Davis8d20ca82018-02-19 13:17:45 -0800336}
337
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700338void Superstructure::SendColors(float red, float green, float blue) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 auto builder = status_light_sender_.MakeBuilder();
340 StatusLight::Builder status_light_builder =
341 builder.MakeBuilder<StatusLight>();
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700342
Alex Perrycb7da4b2019-08-28 19:35:56 -0700343 status_light_builder.add_red(red);
344 status_light_builder.add_green(green);
345 status_light_builder.add_blue(blue);
346
milind1f1dca32021-07-03 13:50:07 -0700347 if (builder.Send(status_light_builder.Finish()) !=
348 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700349 AOS_LOG(ERROR, "Failed to send lights.\n");
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700350 }
351}
352
Sabina Davis8d20ca82018-02-19 13:17:45 -0800353} // namespace superstructure
354} // namespace control_loops
355} // namespace y2018