blob: b259c796db2b114dbda4d62bf6ecb64a7cdd2b8c [file] [log] [blame]
Austin Schuh87c10632017-02-05 19:02:17 -08001#include "y2017/control_loops/superstructure/superstructure.h"
2
3#include "aos/common/controls/control_loops.q.h"
4#include "aos/common/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;
Austin Schuh3ae47432017-04-16 19:15:46 -070021constexpr double kTurretTuckAngle = M_PI / 2.0;
Campbell Crowley651c4b42017-02-17 22:30:50 -080022} // namespace
23
Austin Schuhe8b00752017-04-16 19:14:31 -070024typedef ::y2017::constants::Values::ShotParams ShotParams;
25using ::frc971::control_loops::drivetrain_queue;
26
Austin Schuh87c10632017-02-05 19:02:17 -080027Superstructure::Superstructure(
28 control_loops::SuperstructureQueue *superstructure_queue)
29 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(
Austin Schuhe8b00752017-04-16 19:14:31 -070030 superstructure_queue) {
31 shot_interpolation_table_ =
32 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
33 // { distance_to_target, { shot_angle, shot_power, indexer_velocity }},
34 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070035 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070036 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
37 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
38 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
39 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
Austin Schuh535a0882017-06-24 13:33:45 -070040 {2.81, {0.41, 354.0, -1.90 * M_PI}}, // table entry
41 // The following entry is wrong, but will make it so we keep shooting
42 // in auto.
43 {3.20, {0.41, 354.0, -1.90 * M_PI}}, // table entry
Austin Schuhe8b00752017-04-16 19:14:31 -070044 });
45}
Austin Schuh87c10632017-02-05 19:02:17 -080046
47void Superstructure::RunIteration(
48 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
49 const control_loops::SuperstructureQueue::Position *position,
50 control_loops::SuperstructureQueue::Output *output,
51 control_loops::SuperstructureQueue::Status *status) {
52 if (WasReset()) {
53 LOG(ERROR, "WPILib reset, restarting\n");
54 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080055 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080056 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080057 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080058 }
59
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000060 const vision::VisionStatus *vision_status = nullptr;
61 if (vision::vision_status.FetchLatest()) {
62 vision_status = vision::vision_status.get();
63 }
64
Parker Schuh208a58d2017-04-12 20:51:38 -070065 // Create a copy of the goals so that we can modify them.
66 HoodGoal hood_goal;
67 ShooterGoal shooter_goal;
Parker Schuh94d56792017-04-13 20:32:50 -070068 IndexerGoal indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070069 bool in_range = true;
Parker Schuh208a58d2017-04-12 20:51:38 -070070 if (unsafe_goal != nullptr) {
71 hood_goal = unsafe_goal->hood;
72 shooter_goal = unsafe_goal->shooter;
Parker Schuh94d56792017-04-13 20:32:50 -070073 indexer_goal = unsafe_goal->indexer;
Parker Schuh208a58d2017-04-12 20:51:38 -070074
Austin Schuhd85c66e2017-04-16 10:50:33 -070075 if (!unsafe_goal->use_vision_for_shots) {
76 distance_average_.Reset();
77 }
78
Parker Schuh208a58d2017-04-12 20:51:38 -070079 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
80 status->vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070081
82 // If we are moving too fast, disable shooting and clear the accumulator.
83 double robot_velocity = 0.0;
84 drivetrain_queue.status.FetchLatest();
85 if (drivetrain_queue.status.get()) {
86 robot_velocity = drivetrain_queue.status->robot_speed;
87 }
88
89 if (::std::abs(robot_velocity) > 0.2) {
Parker Schuh208a58d2017-04-12 20:51:38 -070090 if (unsafe_goal->use_vision_for_shots) {
Austin Schuhe8b00752017-04-16 19:14:31 -070091 LOG(INFO, "Moving too fast, resetting\n");
92 }
93 distance_average_.Reset();
94 }
95 if (distance_average_.Valid()) {
96 if (unsafe_goal->use_vision_for_shots) {
97 ShotParams shot_params;
98 if (shot_interpolation_table_.GetInRange(
Parker Schuh94d56792017-04-13 20:32:50 -070099 distance_average_.Get(), &shot_params)) {
100 hood_goal.angle = shot_params.angle;
101 shooter_goal.angular_velocity = shot_params.power;
102 if (indexer_goal.angular_velocity != 0.0) {
103 indexer_goal.angular_velocity = shot_params.indexer_velocity;
104 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700105 } else {
106 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700107 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700108 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700109 LOG(DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
110 status->vision_distance, hood_goal.angle,
111 shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700112 } else {
113 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
Austin Schuhe8b00752017-04-16 19:14:31 -0700114 if (unsafe_goal->use_vision_for_shots) {
115 in_range = false;
116 indexer_goal.angular_velocity = 0.0;
117 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700118 }
119 }
120
121 hood_.Iterate(
122 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
123 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
124 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800125 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -0800126 output != nullptr ? &(output->voltage_shooter) : nullptr,
127 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -0800128
129 // Implement collision avoidance by passing down a freeze or range restricting
130 // signal to the column and intake objects.
131
132 // Wait until the column is ready before doing collision avoidance.
133 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
134 if (!ignore_collisions_) {
135 // The turret is in a position (or wants to be in a position) where we
136 // need the intake out. Push it out.
Austin Schuh3ae47432017-04-16 19:15:46 -0700137 const bool column_goal_not_safe =
138 unsafe_goal->turret.angle > column::Column::kTurretMax ||
139 unsafe_goal->turret.angle < column::Column::kTurretMin;
140 const bool column_position_not_safe =
141 column_.turret_position() > column::Column::kTurretMax ||
142 column_.turret_position() < column::Column::kTurretMin;
143
144 if (column_goal_not_safe || column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800145 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
146 } else {
147 intake_.clear_min_position();
148 }
149 // The intake is in a position where it could hit. Don't move the turret.
150 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
151 column::Column::kIntakeTolerance &&
Austin Schuh3ae47432017-04-16 19:15:46 -0700152 column_position_not_safe) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800153 column_.set_freeze(true);
154 } else {
155 column_.set_freeze(false);
156 }
157 } else {
158 // If we are ignoring collisions, unfreeze and un-limit the min.
159 column_.set_freeze(false);
160 intake_.clear_min_position();
161 }
162 } else {
163 column_.set_freeze(false);
164 }
165
166 // Make some noise if someone left this set...
167 if (ignore_collisions_) {
168 LOG(ERROR, "Collisions ignored\n");
169 }
170
171 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
172 &(position->intake),
173 output != nullptr ? &(output->voltage_intake) : nullptr,
174 &(status->intake));
175
Parker Schuh94d56792017-04-13 20:32:50 -0700176 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800177 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000178 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800179 output != nullptr ? &(output->voltage_indexer) : nullptr,
180 output != nullptr ? &(output->voltage_turret) : nullptr,
181 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800182
Austin Schuhc587c882017-03-29 21:33:10 -0700183 status->estopped =
184 status->intake.estopped | status->hood.estopped | status->turret.estopped;
185
186 status->zeroed =
187 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
188
Campbell Crowley651c4b42017-02-17 22:30:50 -0800189 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700190 output->gear_servo =
191 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
192
Campbell Crowley651c4b42017-02-17 22:30:50 -0800193 output->voltage_intake_rollers =
194 ::std::max(-kMaxIntakeRollerVoltage,
195 ::std::min(unsafe_goal->intake.voltage_rollers,
196 kMaxIntakeRollerVoltage));
197 output->voltage_indexer_rollers =
198 ::std::max(-kMaxIndexerRollerVoltage,
199 ::std::min(unsafe_goal->indexer.voltage_rollers,
200 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800201
202 // Set the lights on or off
203 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700204
205 if (status->estopped) {
206 output->red_light_on = true;
207 output->green_light_on = false;
208 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700209 } else if (!status->zeroed) {
210 output->red_light_on = false;
211 output->green_light_on = false;
212 output->blue_light_on = true;
Austin Schuhe8b00752017-04-16 19:14:31 -0700213 } else if (status->turret.vision_tracking && in_range) {
214 output->red_light_on = false;
215 output->green_light_on = true;
216 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700217 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800218 }
Austin Schuh87c10632017-02-05 19:02:17 -0800219}
220
221} // namespace superstructure
222} // namespace control_loops
223} // namespace y2017