blob: 5cf3f9f170d071d1e1c462917e19b685a83d51b0 [file] [log] [blame]
Austin Schuh87c10632017-02-05 19:02:17 -08001#include "y2017/control_loops/superstructure/superstructure.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh87c10632017-02-05 19:02:17 -08005#include "y2017/constants.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -08006#include "y2017/control_loops/superstructure/column/column.h"
Austin Schuh87c10632017-02-05 19:02:17 -08007#include "y2017/control_loops/superstructure/hood/hood.h"
Adam Snaider79900c22017-02-08 20:23:15 -08008#include "y2017/control_loops/superstructure/intake/intake.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -08009#include "y2017/control_loops/superstructure/shooter/shooter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "y2017/vision/vision_generated.h"
Austin Schuh87c10632017-02-05 19:02:17 -080011
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace y2017::control_loops::superstructure {
Austin Schuh87c10632017-02-05 19:02:17 -080013
Campbell Crowley651c4b42017-02-17 22:30:50 -080014namespace {
15// The maximum voltage the intake roller will be allowed to use.
16constexpr double kMaxIntakeRollerVoltage = 12.0;
17constexpr double kMaxIndexerRollerVoltage = 12.0;
18} // namespace
19
Austin Schuhe8b00752017-04-16 19:14:31 -070020typedef ::y2017::constants::Values::ShotParams ShotParams;
Austin Schuhe8b00752017-04-16 19:14:31 -070021
Austin Schuh55a13dc2019-01-27 22:39:03 -080022Superstructure::Superstructure(::aos::EventLoop *event_loop,
23 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070024 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
25 name),
Austin Schuhb6c5c852019-05-19 20:13:31 -070026 vision_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 event_loop->MakeFetcher<::y2017::vision::VisionStatus>("/vision")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070028 drivetrain_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
30 "/drivetrain")),
Austin Schuhb6c5c852019-05-19 20:13:31 -070031 column_(event_loop) {
Austin Schuhe8b00752017-04-16 19:14:31 -070032 shot_interpolation_table_ =
33 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 // { distance_to_target, { shot_angle, shot_power, indexer_velocity
35 // }},
Austin Schuhe8b00752017-04-16 19:14:31 -070036 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070037 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070038 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
39 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
40 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
41 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070042 {2.81, {0.41, 354.0, -1.90 * M_PI}}, // table entry
43 // The following entry is wrong, but will make it so we keep shooting
44 // in auto.
45 {3.20, {0.41, 354.0, -1.90 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070046 });
47}
Austin Schuh87c10632017-02-05 19:02:17 -080048
James Kuszmaul61750662021-06-21 21:32:33 -070049void Superstructure::RunIteration(const Goal *unsafe_goal,
50 const Position *position,
51 aos::Sender<Output>::Builder *output,
52 aos::Sender<Status>::Builder *status) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 OutputT output_struct;
Austin Schuh92715362019-07-07 20:47:45 -070054 const ::aos::monotonic_clock::time_point monotonic_now =
55 event_loop()->monotonic_now();
Austin Schuh87c10632017-02-05 19:02:17 -080056 if (WasReset()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Austin Schuh87c10632017-02-05 19:02:17 -080058 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080059 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080060 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080061 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080062 }
63
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000064 const vision::VisionStatus *vision_status = nullptr;
Austin Schuhb6c5c852019-05-19 20:13:31 -070065 if (vision_status_fetcher_.Fetch()) {
66 vision_status = vision_status_fetcher_.get();
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000067 }
68
Parker Schuh208a58d2017-04-12 20:51:38 -070069 // Create a copy of the goals so that we can modify them.
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 double hood_goal_angle = 0.0;
71 ShooterGoalT shooter_goal;
72 IndexerGoalT indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070073 bool in_range = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070074 double vision_distance = 0.0;
Parker Schuh208a58d2017-04-12 20:51:38 -070075 if (unsafe_goal != nullptr) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 hood_goal_angle = unsafe_goal->hood()->angle();
77 shooter_goal.angular_velocity = unsafe_goal->shooter()->angular_velocity();
78 indexer_goal.angular_velocity = unsafe_goal->indexer()->angular_velocity();
79 indexer_goal.voltage_rollers = unsafe_goal->indexer()->voltage_rollers();
Parker Schuh208a58d2017-04-12 20:51:38 -070080
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 if (!unsafe_goal->use_vision_for_shots()) {
Austin Schuhd85c66e2017-04-16 10:50:33 -070082 distance_average_.Reset();
83 }
84
Austin Schuh92715362019-07-07 20:47:45 -070085 distance_average_.Tick(monotonic_now, vision_status);
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070087
88 // If we are moving too fast, disable shooting and clear the accumulator.
89 double robot_velocity = 0.0;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070090 drivetrain_status_fetcher_.Fetch();
91 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 robot_velocity = drivetrain_status_fetcher_->robot_speed();
Austin Schuhe8b00752017-04-16 19:14:31 -070093 }
94
95 if (::std::abs(robot_velocity) > 0.2) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_LOG(INFO, "Moving too fast, resetting\n");
Austin Schuhe8b00752017-04-16 19:14:31 -070098 }
99 distance_average_.Reset();
100 }
101 if (distance_average_.Valid()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700103 ShotParams shot_params;
James Kuszmaul61750662021-06-21 21:32:33 -0700104 if (shot_interpolation_table_.GetInRange(distance_average_.Get(),
105 &shot_params)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 hood_goal_angle = shot_params.angle;
Parker Schuh94d56792017-04-13 20:32:50 -0700107 shooter_goal.angular_velocity = shot_params.power;
108 if (indexer_goal.angular_velocity != 0.0) {
109 indexer_goal.angular_velocity = shot_params.indexer_velocity;
110 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700111 } else {
112 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700113 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700114 }
James Kuszmaul61750662021-06-21 21:32:33 -0700115 AOS_LOG(DEBUG,
116 "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
117 vision_distance, hood_goal_angle, shooter_goal.angular_velocity,
118 indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700119 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 AOS_LOG(DEBUG, "VisionNotValid %f\n", vision_distance);
121 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700122 in_range = false;
123 indexer_goal.angular_velocity = 0.0;
124 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700125 }
126 }
127
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 flatbuffers::Offset<frc971::control_loops::IndexProfiledJointStatus>
129 hood_offset = hood_.Iterate(
130 monotonic_now, unsafe_goal != nullptr ? &hood_goal_angle : nullptr,
131 unsafe_goal != nullptr ? unsafe_goal->hood()->profile_params()
132 : nullptr,
133 position->hood(),
134 output != nullptr ? &(output_struct.voltage_hood) : nullptr,
135 status->fbb());
136 flatbuffers::Offset<ShooterStatus> shooter_offset = shooter_.Iterate(
137 unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuhad154822019-12-27 15:45:13 -0800138 position->theta_shooter(), position_context().monotonic_event_time,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 output != nullptr ? &(output_struct.voltage_shooter) : nullptr,
140 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800141
142 // Implement collision avoidance by passing down a freeze or range restricting
143 // signal to the column and intake objects.
144
145 // Wait until the column is ready before doing collision avoidance.
146 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
147 if (!ignore_collisions_) {
148 // The turret is in a position (or wants to be in a position) where we
149 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700150 const bool column_goal_not_safe =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 unsafe_goal->turret()->angle() > column::Column::kTurretMax ||
152 unsafe_goal->turret()->angle() < column::Column::kTurretMin;
Austin Schuh3ae47432017-04-16 19:15:46 -0700153 const bool column_position_not_safe =
154 column_.turret_position() > column::Column::kTurretMax ||
155 column_.turret_position() < column::Column::kTurretMin;
156
157 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800158 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
159 } else {
160 intake_.clear_min_position();
161 }
162 // The intake is in a position where it could hit. Don't move the turret.
163 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
164 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700165 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800166 column_.set_freeze(true);
167 } else {
168 column_.set_freeze(false);
169 }
170 } else {
171 // If we are ignoring collisions, unfreeze and un-limit the min.
172 column_.set_freeze(false);
173 intake_.clear_min_position();
174 }
175 } else {
176 column_.set_freeze(false);
177 }
178
179 // Make some noise if someone left this set...
180 if (ignore_collisions_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700181 AOS_LOG(ERROR, "Collisions ignored\n");
Austin Schuhd5ccb862017-03-11 22:06:36 -0800182 }
183
Alex Perrycb7da4b2019-08-28 19:35:56 -0700184 flatbuffers::Offset<
185 ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>
186 intake_offset = intake_.Iterate(
187 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
188 position->intake(),
189 output != nullptr ? &(output_struct.voltage_intake) : nullptr,
190 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800191
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 std::pair<flatbuffers::Offset<IndexerStatus>,
193 flatbuffers::Offset<TurretProfiledSubsystemStatus>>
194 indexer_and_turret_offsets = column_.Iterate(
195 monotonic_now, unsafe_goal != nullptr ? &indexer_goal : nullptr,
196 unsafe_goal != nullptr ? unsafe_goal->turret() : nullptr,
197 position->column(), vision_status,
198 output != nullptr ? &(output_struct.voltage_indexer) : nullptr,
199 output != nullptr ? &(output_struct.voltage_turret) : nullptr,
200 status->fbb(), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800201
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 const frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus
203 *temp_intake_status = GetTemporaryPointer(*status->fbb(), intake_offset);
204 const frc971::control_loops::IndexProfiledJointStatus *temp_hood_status =
205 GetTemporaryPointer(*status->fbb(), hood_offset);
206 const TurretProfiledSubsystemStatus *temp_turret_status =
207 GetTemporaryPointer(*status->fbb(), indexer_and_turret_offsets.second);
Austin Schuhc587c882017-03-29 21:33:10 -0700208
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209 const bool estopped = temp_intake_status->estopped() ||
210 temp_hood_status->estopped() ||
211 temp_turret_status->estopped();
212 const bool zeroed = temp_intake_status->zeroed() &&
213 temp_hood_status->zeroed() &&
214 temp_turret_status->zeroed();
215 const bool turret_vision_tracking = temp_turret_status->vision_tracking();
216
217 Status::Builder status_builder = status->MakeBuilder<Status>();
218 status_builder.add_intake(intake_offset);
219 status_builder.add_hood(hood_offset);
220 status_builder.add_shooter(shooter_offset);
221 status_builder.add_turret(indexer_and_turret_offsets.second);
222 status_builder.add_indexer(indexer_and_turret_offsets.first);
223
224 status_builder.add_estopped(estopped);
225 status_builder.add_zeroed(zeroed);
226
227 status_builder.add_vision_distance(vision_distance);
Austin Schuhc587c882017-03-29 21:33:10 -0700228
Campbell Crowley651c4b42017-02-17 22:30:50 -0800229 if (output && unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700230 output_struct.gear_servo =
231 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake()->gear_servo()));
Austin Schuh6a8131b2017-04-08 15:39:22 -0700232
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233 output_struct.voltage_intake_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800234 ::std::max(-kMaxIntakeRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700235 ::std::min(unsafe_goal->intake()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800236 kMaxIntakeRollerVoltage));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 output_struct.voltage_indexer_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800238 ::std::max(-kMaxIndexerRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 ::std::min(unsafe_goal->indexer()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800240 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800241
242 // Set the lights on or off
Alex Perrycb7da4b2019-08-28 19:35:56 -0700243 output_struct.lights_on = unsafe_goal->lights_on();
Austin Schuhc587c882017-03-29 21:33:10 -0700244
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245 if (estopped) {
246 output_struct.red_light_on = true;
247 output_struct.green_light_on = false;
248 output_struct.blue_light_on = false;
249 } else if (!zeroed) {
250 output_struct.red_light_on = false;
251 output_struct.green_light_on = false;
252 output_struct.blue_light_on = true;
253 } else if (turret_vision_tracking && in_range) {
254 output_struct.red_light_on = false;
255 output_struct.green_light_on = true;
256 output_struct.blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700257 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800258 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259
260 if (output) {
milind1f1dca32021-07-03 13:50:07 -0700261 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262 }
263
milind1f1dca32021-07-03 13:50:07 -0700264 (void)status->Send(status_builder.Finish());
Austin Schuh87c10632017-02-05 19:02:17 -0800265}
266
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800267} // namespace y2017::control_loops::superstructure