blob: 29886e2055e44f003f118c81b3b58830394b4e47 [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)
Alex Perrycb7da4b2019-08-28 19:35:56 -070026 : aos::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
51void Superstructure::RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070052 const Goal *unsafe_goal,
53 const Position *position,
54 aos::Sender<Output>::Builder *output,
55 aos::Sender<Status>::Builder *status) {
56 OutputT output_struct;
Austin Schuh92715362019-07-07 20:47:45 -070057 const ::aos::monotonic_clock::time_point monotonic_now =
58 event_loop()->monotonic_now();
Austin Schuh87c10632017-02-05 19:02:17 -080059 if (WasReset()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070060 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Austin Schuh87c10632017-02-05 19:02:17 -080061 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080062 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080063 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080064 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080065 }
66
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000067 const vision::VisionStatus *vision_status = nullptr;
Austin Schuhb6c5c852019-05-19 20:13:31 -070068 if (vision_status_fetcher_.Fetch()) {
69 vision_status = vision_status_fetcher_.get();
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000070 }
71
Parker Schuh208a58d2017-04-12 20:51:38 -070072 // Create a copy of the goals so that we can modify them.
Alex Perrycb7da4b2019-08-28 19:35:56 -070073 double hood_goal_angle = 0.0;
74 ShooterGoalT shooter_goal;
75 IndexerGoalT indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070076 bool in_range = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 double vision_distance = 0.0;
Parker Schuh208a58d2017-04-12 20:51:38 -070078 if (unsafe_goal != nullptr) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 hood_goal_angle = unsafe_goal->hood()->angle();
80 shooter_goal.angular_velocity = unsafe_goal->shooter()->angular_velocity();
81 indexer_goal.angular_velocity = unsafe_goal->indexer()->angular_velocity();
82 indexer_goal.voltage_rollers = unsafe_goal->indexer()->voltage_rollers();
Parker Schuh208a58d2017-04-12 20:51:38 -070083
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 if (!unsafe_goal->use_vision_for_shots()) {
Austin Schuhd85c66e2017-04-16 10:50:33 -070085 distance_average_.Reset();
86 }
87
Austin Schuh92715362019-07-07 20:47:45 -070088 distance_average_.Tick(monotonic_now, vision_status);
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070090
91 // If we are moving too fast, disable shooting and clear the accumulator.
92 double robot_velocity = 0.0;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070093 drivetrain_status_fetcher_.Fetch();
94 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 robot_velocity = drivetrain_status_fetcher_->robot_speed();
Austin Schuhe8b00752017-04-16 19:14:31 -070096 }
97
98 if (::std::abs(robot_velocity) > 0.2) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070099 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700100 AOS_LOG(INFO, "Moving too fast, resetting\n");
Austin Schuhe8b00752017-04-16 19:14:31 -0700101 }
102 distance_average_.Reset();
103 }
104 if (distance_average_.Valid()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700106 ShotParams shot_params;
107 if (shot_interpolation_table_.GetInRange(
Parker Schuh94d56792017-04-13 20:32:50 -0700108 distance_average_.Get(), &shot_params)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109 hood_goal_angle = shot_params.angle;
Parker Schuh94d56792017-04-13 20:32:50 -0700110 shooter_goal.angular_velocity = shot_params.power;
111 if (indexer_goal.angular_velocity != 0.0) {
112 indexer_goal.angular_velocity = shot_params.indexer_velocity;
113 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700114 } else {
115 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700116 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700117 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700118 AOS_LOG(
119 DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 vision_distance, hood_goal_angle,
Austin Schuhe8b00752017-04-16 19:14:31 -0700121 shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700122 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 AOS_LOG(DEBUG, "VisionNotValid %f\n", vision_distance);
124 if (unsafe_goal->use_vision_for_shots()) {
Austin Schuhe8b00752017-04-16 19:14:31 -0700125 in_range = false;
126 indexer_goal.angular_velocity = 0.0;
127 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700128 }
129 }
130
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 flatbuffers::Offset<frc971::control_loops::IndexProfiledJointStatus>
132 hood_offset = hood_.Iterate(
133 monotonic_now, unsafe_goal != nullptr ? &hood_goal_angle : nullptr,
134 unsafe_goal != nullptr ? unsafe_goal->hood()->profile_params()
135 : nullptr,
136 position->hood(),
137 output != nullptr ? &(output_struct.voltage_hood) : nullptr,
138 status->fbb());
139 flatbuffers::Offset<ShooterStatus> shooter_offset = shooter_.Iterate(
140 unsafe_goal != nullptr ? &shooter_goal : nullptr,
141 position->theta_shooter(), position_context().monotonic_sent_time,
142 output != nullptr ? &(output_struct.voltage_shooter) : nullptr,
143 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800144
145 // Implement collision avoidance by passing down a freeze or range restricting
146 // signal to the column and intake objects.
147
148 // Wait until the column is ready before doing collision avoidance.
149 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
150 if (!ignore_collisions_) {
151 // The turret is in a position (or wants to be in a position) where we
152 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700153 const bool column_goal_not_safe =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 unsafe_goal->turret()->angle() > column::Column::kTurretMax ||
155 unsafe_goal->turret()->angle() < column::Column::kTurretMin;
Austin Schuh3ae47432017-04-16 19:15:46 -0700156 const bool column_position_not_safe =
157 column_.turret_position() > column::Column::kTurretMax ||
158 column_.turret_position() < column::Column::kTurretMin;
159
160 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800161 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
162 } else {
163 intake_.clear_min_position();
164 }
165 // The intake is in a position where it could hit. Don't move the turret.
166 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
167 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700168 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800169 column_.set_freeze(true);
170 } else {
171 column_.set_freeze(false);
172 }
173 } else {
174 // If we are ignoring collisions, unfreeze and un-limit the min.
175 column_.set_freeze(false);
176 intake_.clear_min_position();
177 }
178 } else {
179 column_.set_freeze(false);
180 }
181
182 // Make some noise if someone left this set...
183 if (ignore_collisions_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700184 AOS_LOG(ERROR, "Collisions ignored\n");
Austin Schuhd5ccb862017-03-11 22:06:36 -0800185 }
186
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 flatbuffers::Offset<
188 ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>
189 intake_offset = intake_.Iterate(
190 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
191 position->intake(),
192 output != nullptr ? &(output_struct.voltage_intake) : nullptr,
193 status->fbb());
Austin Schuhd5ccb862017-03-11 22:06:36 -0800194
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 std::pair<flatbuffers::Offset<IndexerStatus>,
196 flatbuffers::Offset<TurretProfiledSubsystemStatus>>
197 indexer_and_turret_offsets = column_.Iterate(
198 monotonic_now, unsafe_goal != nullptr ? &indexer_goal : nullptr,
199 unsafe_goal != nullptr ? unsafe_goal->turret() : nullptr,
200 position->column(), vision_status,
201 output != nullptr ? &(output_struct.voltage_indexer) : nullptr,
202 output != nullptr ? &(output_struct.voltage_turret) : nullptr,
203 status->fbb(), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800204
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 const frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus
206 *temp_intake_status = GetTemporaryPointer(*status->fbb(), intake_offset);
207 const frc971::control_loops::IndexProfiledJointStatus *temp_hood_status =
208 GetTemporaryPointer(*status->fbb(), hood_offset);
209 const TurretProfiledSubsystemStatus *temp_turret_status =
210 GetTemporaryPointer(*status->fbb(), indexer_and_turret_offsets.second);
Austin Schuhc587c882017-03-29 21:33:10 -0700211
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212 const bool estopped = temp_intake_status->estopped() ||
213 temp_hood_status->estopped() ||
214 temp_turret_status->estopped();
215 const bool zeroed = temp_intake_status->zeroed() &&
216 temp_hood_status->zeroed() &&
217 temp_turret_status->zeroed();
218 const bool turret_vision_tracking = temp_turret_status->vision_tracking();
219
220 Status::Builder status_builder = status->MakeBuilder<Status>();
221 status_builder.add_intake(intake_offset);
222 status_builder.add_hood(hood_offset);
223 status_builder.add_shooter(shooter_offset);
224 status_builder.add_turret(indexer_and_turret_offsets.second);
225 status_builder.add_indexer(indexer_and_turret_offsets.first);
226
227 status_builder.add_estopped(estopped);
228 status_builder.add_zeroed(zeroed);
229
230 status_builder.add_vision_distance(vision_distance);
Austin Schuhc587c882017-03-29 21:33:10 -0700231
Campbell Crowley651c4b42017-02-17 22:30:50 -0800232 if (output && unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233 output_struct.gear_servo =
234 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake()->gear_servo()));
Austin Schuh6a8131b2017-04-08 15:39:22 -0700235
Alex Perrycb7da4b2019-08-28 19:35:56 -0700236 output_struct.voltage_intake_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800237 ::std::max(-kMaxIntakeRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700238 ::std::min(unsafe_goal->intake()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800239 kMaxIntakeRollerVoltage));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700240 output_struct.voltage_indexer_rollers =
Campbell Crowley651c4b42017-02-17 22:30:50 -0800241 ::std::max(-kMaxIndexerRollerVoltage,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700242 ::std::min(unsafe_goal->indexer()->voltage_rollers(),
Campbell Crowley651c4b42017-02-17 22:30:50 -0800243 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800244
245 // Set the lights on or off
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246 output_struct.lights_on = unsafe_goal->lights_on();
Austin Schuhc587c882017-03-29 21:33:10 -0700247
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 if (estopped) {
249 output_struct.red_light_on = true;
250 output_struct.green_light_on = false;
251 output_struct.blue_light_on = false;
252 } else if (!zeroed) {
253 output_struct.red_light_on = false;
254 output_struct.green_light_on = false;
255 output_struct.blue_light_on = true;
256 } else if (turret_vision_tracking && in_range) {
257 output_struct.red_light_on = false;
258 output_struct.green_light_on = true;
259 output_struct.blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700260 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800261 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262
263 if (output) {
264 output->Send(Output::Pack(*output->fbb(), &output_struct));
265 }
266
267 status->Send(status_builder.Finish());
Austin Schuh87c10632017-02-05 19:02:17 -0800268}
269
270} // namespace superstructure
271} // namespace control_loops
272} // namespace y2017