blob: c70765327f491d0a791e6210b1af3b212481553a [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
12namespace y2017 {
13namespace control_loops {
14namespace superstructure {
15
Campbell Crowley651c4b42017-02-17 22:30:50 -080016namespace {
17// The maximum voltage the intake roller will be allowed to use.
18constexpr double kMaxIntakeRollerVoltage = 12.0;
19constexpr double kMaxIndexerRollerVoltage = 12.0;
20} // namespace
21
Austin Schuhe8b00752017-04-16 19:14:31 -070022typedef ::y2017::constants::Values::ShotParams ShotParams;
Austin Schuhe8b00752017-04-16 19:14:31 -070023
Austin Schuh55a13dc2019-01-27 22:39:03 -080024Superstructure::Superstructure(::aos::EventLoop *event_loop,
25 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070026 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
27 name),
Austin Schuhb6c5c852019-05-19 20:13:31 -070028 vision_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 event_loop->MakeFetcher<::y2017::vision::VisionStatus>("/vision")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070030 drivetrain_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
32 "/drivetrain")),
Austin Schuhb6c5c852019-05-19 20:13:31 -070033 column_(event_loop) {
Austin Schuhe8b00752017-04-16 19:14:31 -070034 shot_interpolation_table_ =
35 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 // { distance_to_target, { shot_angle, shot_power, indexer_velocity
37 // }},
Austin Schuhe8b00752017-04-16 19:14:31 -070038 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070039 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070040 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
41 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
42 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
43 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070044 {2.81, {0.41, 354.0, -1.90 * M_PI}}, // table entry
45 // The following entry is wrong, but will make it so we keep shooting
46 // in auto.
47 {3.20, {0.41, 354.0, -1.90 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070048 });
49}
Austin Schuh87c10632017-02-05 19:02:17 -080050
James Kuszmaul61750662021-06-21 21:32:33 -070051void Superstructure::RunIteration(const Goal *unsafe_goal,
52 const Position *position,
53 aos::Sender<Output>::Builder *output,
54 aos::Sender<Status>::Builder *status) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 OutputT output_struct;
Austin Schuh92715362019-07-07 20:47:45 -070056 const ::aos::monotonic_clock::time_point monotonic_now =
57 event_loop()->monotonic_now();
Austin Schuh87c10632017-02-05 19:02:17 -080058 if (WasReset()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070059 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Austin Schuh87c10632017-02-05 19:02:17 -080060 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080061 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080062 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080063 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080064 }
65
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000066 const vision::VisionStatus *vision_status = nullptr;
Austin Schuhb6c5c852019-05-19 20:13:31 -070067 if (vision_status_fetcher_.Fetch()) {
68 vision_status = vision_status_fetcher_.get();
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000069 }
70
Parker Schuh208a58d2017-04-12 20:51:38 -070071 // Create a copy of the goals so that we can modify them.
Alex Perrycb7da4b2019-08-28 19:35:56 -070072 double hood_goal_angle = 0.0;
73 ShooterGoalT shooter_goal;
74 IndexerGoalT indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070075 bool in_range = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 double vision_distance = 0.0;
Parker Schuh208a58d2017-04-12 20:51:38 -070077 if (unsafe_goal != nullptr) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 hood_goal_angle = unsafe_goal->hood()->angle();
79 shooter_goal.angular_velocity = unsafe_goal->shooter()->angular_velocity();
80 indexer_goal.angular_velocity = unsafe_goal->indexer()->angular_velocity();
81 indexer_goal.voltage_rollers = unsafe_goal->indexer()->voltage_rollers();
Parker Schuh208a58d2017-04-12 20:51:38 -070082
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 if (!unsafe_goal->use_vision_for_shots()) {
Austin Schuhd85c66e2017-04-16 10:50:33 -070084 distance_average_.Reset();
85 }
86
Austin Schuh92715362019-07-07 20:47:45 -070087 distance_average_.Tick(monotonic_now, vision_status);
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070089
90 // If we are moving too fast, disable shooting and clear the accumulator.
91 double robot_velocity = 0.0;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070092 drivetrain_status_fetcher_.Fetch();
93 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070094 robot_velocity = drivetrain_status_fetcher_->robot_speed();
Austin Schuhe8b00752017-04-16 19:14:31 -070095 }
96
97 if (::std::abs(robot_velocity) > 0.2) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070099 AOS_LOG(INFO, "Moving too fast, resetting\n");
Austin Schuhe8b00752017-04-16 19:14:31 -0700100 }
101 distance_average_.Reset();
102 }
103 if (distance_average_.Valid()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700105 ShotParams shot_params;
James Kuszmaul61750662021-06-21 21:32:33 -0700106 if (shot_interpolation_table_.GetInRange(distance_average_.Get(),
107 &shot_params)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108 hood_goal_angle = shot_params.angle;
Parker Schuh94d56792017-04-13 20:32:50 -0700109 shooter_goal.angular_velocity = shot_params.power;
110 if (indexer_goal.angular_velocity != 0.0) {
111 indexer_goal.angular_velocity = shot_params.indexer_velocity;
112 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700113 } else {
114 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700115 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700116 }
James Kuszmaul61750662021-06-21 21:32:33 -0700117 AOS_LOG(DEBUG,
118 "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
119 vision_distance, hood_goal_angle, shooter_goal.angular_velocity,
120 indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700121 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700122 AOS_LOG(DEBUG, "VisionNotValid %f\n", vision_distance);
123 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700124 in_range = false;
125 indexer_goal.angular_velocity = 0.0;
126 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700127 }
128 }
129
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 flatbuffers::Offset<frc971::control_loops::IndexProfiledJointStatus>
131 hood_offset = hood_.Iterate(
132 monotonic_now, unsafe_goal != nullptr ? &hood_goal_angle : nullptr,
133 unsafe_goal != nullptr ? unsafe_goal->hood()->profile_params()
134 : nullptr,
135 position->hood(),
136 output != nullptr ? &(output_struct.voltage_hood) : nullptr,
137 status->fbb());
138 flatbuffers::Offset<ShooterStatus> shooter_offset = shooter_.Iterate(
139 unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuhad154822019-12-27 15:45:13 -0800140 position->theta_shooter(), position_context().monotonic_event_time,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 output != nullptr ? &(output_struct.voltage_shooter) : nullptr,
142 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800143
144 // Implement collision avoidance by passing down a freeze or range restricting
145 // signal to the column and intake objects.
146
147 // Wait until the column is ready before doing collision avoidance.
148 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
149 if (!ignore_collisions_) {
150 // The turret is in a position (or wants to be in a position) where we
151 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700152 const bool column_goal_not_safe =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 unsafe_goal->turret()->angle() > column::Column::kTurretMax ||
154 unsafe_goal->turret()->angle() < column::Column::kTurretMin;
Austin Schuh3ae47432017-04-16 19:15:46 -0700155 const bool column_position_not_safe =
156 column_.turret_position() > column::Column::kTurretMax ||
157 column_.turret_position() < column::Column::kTurretMin;
158
159 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800160 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
161 } else {
162 intake_.clear_min_position();
163 }
164 // The intake is in a position where it could hit. Don't move the turret.
165 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
166 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700167 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800168 column_.set_freeze(true);
169 } else {
170 column_.set_freeze(false);
171 }
172 } else {
173 // If we are ignoring collisions, unfreeze and un-limit the min.
174 column_.set_freeze(false);
175 intake_.clear_min_position();
176 }
177 } else {
178 column_.set_freeze(false);
179 }
180
181 // Make some noise if someone left this set...
182 if (ignore_collisions_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700183 AOS_LOG(ERROR, "Collisions ignored\n");
Austin Schuhd5ccb862017-03-11 22:06:36 -0800184 }
185
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 flatbuffers::Offset<
187 ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>
188 intake_offset = intake_.Iterate(
189 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
190 position->intake(),
191 output != nullptr ? &(output_struct.voltage_intake) : nullptr,
192 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800193
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 std::pair<flatbuffers::Offset<IndexerStatus>,
195 flatbuffers::Offset<TurretProfiledSubsystemStatus>>
196 indexer_and_turret_offsets = column_.Iterate(
197 monotonic_now, unsafe_goal != nullptr ? &indexer_goal : nullptr,
198 unsafe_goal != nullptr ? unsafe_goal->turret() : nullptr,
199 position->column(), vision_status,
200 output != nullptr ? &(output_struct.voltage_indexer) : nullptr,
201 output != nullptr ? &(output_struct.voltage_turret) : nullptr,
202 status->fbb(), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800203
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 const frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus
205 *temp_intake_status = GetTemporaryPointer(*status->fbb(), intake_offset);
206 const frc971::control_loops::IndexProfiledJointStatus *temp_hood_status =
207 GetTemporaryPointer(*status->fbb(), hood_offset);
208 const TurretProfiledSubsystemStatus *temp_turret_status =
209 GetTemporaryPointer(*status->fbb(), indexer_and_turret_offsets.second);
Austin Schuhc587c882017-03-29 21:33:10 -0700210
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 const bool estopped = temp_intake_status->estopped() ||
212 temp_hood_status->estopped() ||
213 temp_turret_status->estopped();
214 const bool zeroed = temp_intake_status->zeroed() &&
215 temp_hood_status->zeroed() &&
216 temp_turret_status->zeroed();
217 const bool turret_vision_tracking = temp_turret_status->vision_tracking();
218
219 Status::Builder status_builder = status->MakeBuilder<Status>();
220 status_builder.add_intake(intake_offset);
221 status_builder.add_hood(hood_offset);
222 status_builder.add_shooter(shooter_offset);
223 status_builder.add_turret(indexer_and_turret_offsets.second);
224 status_builder.add_indexer(indexer_and_turret_offsets.first);
225
226 status_builder.add_estopped(estopped);
227 status_builder.add_zeroed(zeroed);
228
229 status_builder.add_vision_distance(vision_distance);
Austin Schuhc587c882017-03-29 21:33:10 -0700230
Campbell Crowley651c4b42017-02-17 22:30:50 -0800231 if (output && unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700232 output_struct.gear_servo =
233 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake()->gear_servo()));
Austin Schuh6a8131b2017-04-08 15:39:22 -0700234
Alex Perrycb7da4b2019-08-28 19:35:56 -0700235 output_struct.voltage_intake_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800236 ::std::max(-kMaxIntakeRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 ::std::min(unsafe_goal->intake()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800238 kMaxIntakeRollerVoltage));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 output_struct.voltage_indexer_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800240 ::std::max(-kMaxIndexerRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 ::std::min(unsafe_goal->indexer()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800242 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800243
244 // Set the lights on or off
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245 output_struct.lights_on = unsafe_goal->lights_on();
Austin Schuhc587c882017-03-29 21:33:10 -0700246
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 if (estopped) {
248 output_struct.red_light_on = true;
249 output_struct.green_light_on = false;
250 output_struct.blue_light_on = false;
251 } else if (!zeroed) {
252 output_struct.red_light_on = false;
253 output_struct.green_light_on = false;
254 output_struct.blue_light_on = true;
255 } else if (turret_vision_tracking && in_range) {
256 output_struct.red_light_on = false;
257 output_struct.green_light_on = true;
258 output_struct.blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700259 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800260 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261
262 if (output) {
263 output->Send(Output::Pack(*output->fbb(), &output_struct));
264 }
265
266 status->Send(status_builder.Finish());
Austin Schuh87c10632017-02-05 19:02:17 -0800267}
268
269} // namespace superstructure
270} // namespace control_loops
271} // namespace y2017