blob: c76dd195aab7b71c203ad4efa2fb3244f8bf24b2 [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;
Austin Schuhe8b00752017-04-16 19:14:31 -070024
Austin Schuh55a13dc2019-01-27 22:39:03 -080025Superstructure::Superstructure(::aos::EventLoop *event_loop,
26 const ::std::string &name)
27 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(event_loop,
Austin Schuhb6c5c852019-05-19 20:13:31 -070028 name),
29 vision_status_fetcher_(
30 event_loop->MakeFetcher<::y2017::vision::VisionStatus>(
31 ".y2017.vision.vision_status")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070032 drivetrain_status_fetcher_(
33 event_loop
34 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Status>(
35 ".frc971.control_loops.drivetrain_queue.status")),
Austin Schuhb6c5c852019-05-19 20:13:31 -070036 column_(event_loop) {
Austin Schuhe8b00752017-04-16 19:14:31 -070037 shot_interpolation_table_ =
38 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
39 // { distance_to_target, { shot_angle, shot_power, indexer_velocity }},
40 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070041 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070042 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
43 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
44 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
45 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070046 {2.81, {0.41, 354.0, -1.90 * M_PI}}, // table entry
47 // The following entry is wrong, but will make it so we keep shooting
48 // in auto.
49 {3.20, {0.41, 354.0, -1.90 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070050 });
51}
Austin Schuh87c10632017-02-05 19:02:17 -080052
53void Superstructure::RunIteration(
54 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
55 const control_loops::SuperstructureQueue::Position *position,
56 control_loops::SuperstructureQueue::Output *output,
57 control_loops::SuperstructureQueue::Status *status) {
58 if (WasReset()) {
59 LOG(ERROR, "WPILib reset, restarting\n");
60 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.
72 HoodGoal hood_goal;
73 ShooterGoal shooter_goal;
Parker Schuh94d56792017-04-13 20:32:50 -070074 IndexerGoal indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070075 bool in_range = true;
Parker Schuh208a58d2017-04-12 20:51:38 -070076 if (unsafe_goal != nullptr) {
77 hood_goal = unsafe_goal->hood;
78 shooter_goal = unsafe_goal->shooter;
Parker Schuh94d56792017-04-13 20:32:50 -070079 indexer_goal = unsafe_goal->indexer;
Parker Schuh208a58d2017-04-12 20:51:38 -070080
Austin Schuhd85c66e2017-04-16 10:50:33 -070081 if (!unsafe_goal->use_vision_for_shots) {
82 distance_average_.Reset();
83 }
84
Parker Schuh208a58d2017-04-12 20:51:38 -070085 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
86 status->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()) {
92 robot_velocity = drivetrain_status_fetcher_->robot_speed;
Austin Schuhe8b00752017-04-16 19:14:31 -070093 }
94
95 if (::std::abs(robot_velocity) > 0.2) {
Parker Schuh208a58d2017-04-12 20:51:38 -070096 if (unsafe_goal->use_vision_for_shots) {
Austin Schuhe8b00752017-04-16 19:14:31 -070097 LOG(INFO, "Moving too fast, resetting\n");
98 }
99 distance_average_.Reset();
100 }
101 if (distance_average_.Valid()) {
102 if (unsafe_goal->use_vision_for_shots) {
103 ShotParams shot_params;
104 if (shot_interpolation_table_.GetInRange(
Parker Schuh94d56792017-04-13 20:32:50 -0700105 distance_average_.Get(), &shot_params)) {
106 hood_goal.angle = shot_params.angle;
107 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 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700115 LOG(DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
116 status->vision_distance, hood_goal.angle,
117 shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700118 } else {
119 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
Austin Schuhe8b00752017-04-16 19:14:31 -0700120 if (unsafe_goal->use_vision_for_shots) {
121 in_range = false;
122 indexer_goal.angular_velocity = 0.0;
123 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700124 }
125 }
126
127 hood_.Iterate(
128 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
129 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
130 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800131 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -0800132 output != nullptr ? &(output->voltage_shooter) : nullptr,
133 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -0800134
135 // Implement collision avoidance by passing down a freeze or range restricting
136 // signal to the column and intake objects.
137
138 // Wait until the column is ready before doing collision avoidance.
139 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
140 if (!ignore_collisions_) {
141 // The turret is in a position (or wants to be in a position) where we
142 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700143 const bool column_goal_not_safe =
144 unsafe_goal->turret.angle > column::Column::kTurretMax ||
145 unsafe_goal->turret.angle < column::Column::kTurretMin;
146 const bool column_position_not_safe =
147 column_.turret_position() > column::Column::kTurretMax ||
148 column_.turret_position() < column::Column::kTurretMin;
149
150 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800151 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
152 } else {
153 intake_.clear_min_position();
154 }
155 // The intake is in a position where it could hit. Don't move the turret.
156 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
157 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700158 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800159 column_.set_freeze(true);
160 } else {
161 column_.set_freeze(false);
162 }
163 } else {
164 // If we are ignoring collisions, unfreeze and un-limit the min.
165 column_.set_freeze(false);
166 intake_.clear_min_position();
167 }
168 } else {
169 column_.set_freeze(false);
170 }
171
172 // Make some noise if someone left this set...
173 if (ignore_collisions_) {
174 LOG(ERROR, "Collisions ignored\n");
175 }
176
177 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
178 &(position->intake),
179 output != nullptr ? &(output->voltage_intake) : nullptr,
180 &(status->intake));
181
Parker Schuh94d56792017-04-13 20:32:50 -0700182 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800183 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000184 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800185 output != nullptr ? &(output->voltage_indexer) : nullptr,
186 output != nullptr ? &(output->voltage_turret) : nullptr,
187 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800188
Austin Schuhc587c882017-03-29 21:33:10 -0700189 status->estopped =
190 status->intake.estopped | status->hood.estopped | status->turret.estopped;
191
192 status->zeroed =
193 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
194
Campbell Crowley651c4b42017-02-17 22:30:50 -0800195 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700196 output->gear_servo =
197 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
198
Campbell Crowley651c4b42017-02-17 22:30:50 -0800199 output->voltage_intake_rollers =
200 ::std::max(-kMaxIntakeRollerVoltage,
201 ::std::min(unsafe_goal->intake.voltage_rollers,
202 kMaxIntakeRollerVoltage));
203 output->voltage_indexer_rollers =
204 ::std::max(-kMaxIndexerRollerVoltage,
205 ::std::min(unsafe_goal->indexer.voltage_rollers,
206 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800207
208 // Set the lights on or off
209 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700210
211 if (status->estopped) {
212 output->red_light_on = true;
213 output->green_light_on = false;
214 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700215 } else if (!status->zeroed) {
216 output->red_light_on = false;
217 output->green_light_on = false;
218 output->blue_light_on = true;
Austin Schuhe8b00752017-04-16 19:14:31 -0700219 } else if (status->turret.vision_tracking && in_range) {
220 output->red_light_on = false;
221 output->green_light_on = true;
222 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700223 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800224 }
Austin Schuh87c10632017-02-05 19:02:17 -0800225}
226
227} // namespace superstructure
228} // namespace control_loops
229} // namespace y2017