blob: 9b0297e9762cb92092d2d436b96e2cdd4aa80064 [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/controls/control_loops.q.h"
4#include "aos/logging/logging.h"
Austin Schuhe8b00752017-04-16 19:14:31 -07005#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh87c10632017-02-05 19:02:17 -08006#include "y2017/constants.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -08007#include "y2017/control_loops/superstructure/column/column.h"
Austin Schuh87c10632017-02-05 19:02:17 -08008#include "y2017/control_loops/superstructure/hood/hood.h"
Adam Snaider79900c22017-02-08 20:23:15 -08009#include "y2017/control_loops/superstructure/intake/intake.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -080010#include "y2017/control_loops/superstructure/shooter/shooter.h"
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000011#include "y2017/vision/vision.q.h"
Austin Schuh87c10632017-02-05 19:02:17 -080012
13namespace y2017 {
14namespace control_loops {
15namespace superstructure {
16
Campbell Crowley651c4b42017-02-17 22:30:50 -080017namespace {
18// The maximum voltage the intake roller will be allowed to use.
19constexpr double kMaxIntakeRollerVoltage = 12.0;
20constexpr double kMaxIndexerRollerVoltage = 12.0;
21} // namespace
22
Austin Schuhe8b00752017-04-16 19:14:31 -070023typedef ::y2017::constants::Values::ShotParams ShotParams;
24using ::frc971::control_loops::drivetrain_queue;
25
Austin Schuh55a13dc2019-01-27 22:39:03 -080026Superstructure::Superstructure(::aos::EventLoop *event_loop,
27 const ::std::string &name)
28 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(event_loop,
Austin Schuhb6c5c852019-05-19 20:13:31 -070029 name),
30 vision_status_fetcher_(
31 event_loop->MakeFetcher<::y2017::vision::VisionStatus>(
32 ".y2017.vision.vision_status")),
33 column_(event_loop) {
Austin Schuhe8b00752017-04-16 19:14:31 -070034 shot_interpolation_table_ =
35 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
36 // { distance_to_target, { shot_angle, shot_power, indexer_velocity }},
37 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070038 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070039 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
40 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
41 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
42 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070043 {2.81, {0.41, 354.0, -1.90 * M_PI}}, // table entry
44 // The following entry is wrong, but will make it so we keep shooting
45 // in auto.
46 {3.20, {0.41, 354.0, -1.90 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070047 });
48}
Austin Schuh87c10632017-02-05 19:02:17 -080049
50void Superstructure::RunIteration(
51 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
52 const control_loops::SuperstructureQueue::Position *position,
53 control_loops::SuperstructureQueue::Output *output,
54 control_loops::SuperstructureQueue::Status *status) {
55 if (WasReset()) {
56 LOG(ERROR, "WPILib reset, restarting\n");
57 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080058 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080059 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080060 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080061 }
62
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000063 const vision::VisionStatus *vision_status = nullptr;
Austin Schuhb6c5c852019-05-19 20:13:31 -070064 if (vision_status_fetcher_.Fetch()) {
65 vision_status = vision_status_fetcher_.get();
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000066 }
67
Parker Schuh208a58d2017-04-12 20:51:38 -070068 // Create a copy of the goals so that we can modify them.
69 HoodGoal hood_goal;
70 ShooterGoal shooter_goal;
Parker Schuh94d56792017-04-13 20:32:50 -070071 IndexerGoal indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070072 bool in_range = true;
Parker Schuh208a58d2017-04-12 20:51:38 -070073 if (unsafe_goal != nullptr) {
74 hood_goal = unsafe_goal->hood;
75 shooter_goal = unsafe_goal->shooter;
Parker Schuh94d56792017-04-13 20:32:50 -070076 indexer_goal = unsafe_goal->indexer;
Parker Schuh208a58d2017-04-12 20:51:38 -070077
Austin Schuhd85c66e2017-04-16 10:50:33 -070078 if (!unsafe_goal->use_vision_for_shots) {
79 distance_average_.Reset();
80 }
81
Parker Schuh208a58d2017-04-12 20:51:38 -070082 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
83 status->vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070084
85 // If we are moving too fast, disable shooting and clear the accumulator.
86 double robot_velocity = 0.0;
87 drivetrain_queue.status.FetchLatest();
88 if (drivetrain_queue.status.get()) {
89 robot_velocity = drivetrain_queue.status->robot_speed;
90 }
91
92 if (::std::abs(robot_velocity) > 0.2) {
Parker Schuh208a58d2017-04-12 20:51:38 -070093 if (unsafe_goal->use_vision_for_shots) {
Austin Schuhe8b00752017-04-16 19:14:31 -070094 LOG(INFO, "Moving too fast, resetting\n");
95 }
96 distance_average_.Reset();
97 }
98 if (distance_average_.Valid()) {
99 if (unsafe_goal->use_vision_for_shots) {
100 ShotParams shot_params;
101 if (shot_interpolation_table_.GetInRange(
Parker Schuh94d56792017-04-13 20:32:50 -0700102 distance_average_.Get(), &shot_params)) {
103 hood_goal.angle = shot_params.angle;
104 shooter_goal.angular_velocity = shot_params.power;
105 if (indexer_goal.angular_velocity != 0.0) {
106 indexer_goal.angular_velocity = shot_params.indexer_velocity;
107 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700108 } else {
109 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700110 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700111 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700112 LOG(DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
113 status->vision_distance, hood_goal.angle,
114 shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700115 } else {
116 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
Austin Schuhe8b00752017-04-16 19:14:31 -0700117 if (unsafe_goal->use_vision_for_shots) {
118 in_range = false;
119 indexer_goal.angular_velocity = 0.0;
120 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700121 }
122 }
123
124 hood_.Iterate(
125 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
126 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
127 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800128 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -0800129 output != nullptr ? &(output->voltage_shooter) : nullptr,
130 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -0800131
132 // Implement collision avoidance by passing down a freeze or range restricting
133 // signal to the column and intake objects.
134
135 // Wait until the column is ready before doing collision avoidance.
136 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
137 if (!ignore_collisions_) {
138 // The turret is in a position (or wants to be in a position) where we
139 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700140 const bool column_goal_not_safe =
141 unsafe_goal->turret.angle > column::Column::kTurretMax ||
142 unsafe_goal->turret.angle < column::Column::kTurretMin;
143 const bool column_position_not_safe =
144 column_.turret_position() > column::Column::kTurretMax ||
145 column_.turret_position() < column::Column::kTurretMin;
146
147 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800148 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
149 } else {
150 intake_.clear_min_position();
151 }
152 // The intake is in a position where it could hit. Don't move the turret.
153 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
154 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700155 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800156 column_.set_freeze(true);
157 } else {
158 column_.set_freeze(false);
159 }
160 } else {
161 // If we are ignoring collisions, unfreeze and un-limit the min.
162 column_.set_freeze(false);
163 intake_.clear_min_position();
164 }
165 } else {
166 column_.set_freeze(false);
167 }
168
169 // Make some noise if someone left this set...
170 if (ignore_collisions_) {
171 LOG(ERROR, "Collisions ignored\n");
172 }
173
174 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
175 &(position->intake),
176 output != nullptr ? &(output->voltage_intake) : nullptr,
177 &(status->intake));
178
Parker Schuh94d56792017-04-13 20:32:50 -0700179 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800180 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000181 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800182 output != nullptr ? &(output->voltage_indexer) : nullptr,
183 output != nullptr ? &(output->voltage_turret) : nullptr,
184 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800185
Austin Schuhc587c882017-03-29 21:33:10 -0700186 status->estopped =
187 status->intake.estopped | status->hood.estopped | status->turret.estopped;
188
189 status->zeroed =
190 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
191
Campbell Crowley651c4b42017-02-17 22:30:50 -0800192 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700193 output->gear_servo =
194 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
195
Campbell Crowley651c4b42017-02-17 22:30:50 -0800196 output->voltage_intake_rollers =
197 ::std::max(-kMaxIntakeRollerVoltage,
198 ::std::min(unsafe_goal->intake.voltage_rollers,
199 kMaxIntakeRollerVoltage));
200 output->voltage_indexer_rollers =
201 ::std::max(-kMaxIndexerRollerVoltage,
202 ::std::min(unsafe_goal->indexer.voltage_rollers,
203 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800204
205 // Set the lights on or off
206 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700207
208 if (status->estopped) {
209 output->red_light_on = true;
210 output->green_light_on = false;
211 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700212 } else if (!status->zeroed) {
213 output->red_light_on = false;
214 output->green_light_on = false;
215 output->blue_light_on = true;
Austin Schuhe8b00752017-04-16 19:14:31 -0700216 } else if (status->turret.vision_tracking && in_range) {
217 output->red_light_on = false;
218 output->green_light_on = true;
219 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700220 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800221 }
Austin Schuh87c10632017-02-05 19:02:17 -0800222}
223
224} // namespace superstructure
225} // namespace control_loops
226} // namespace y2017