blob: ee7b80b01a930b61d53453adc821ef9a6a201560 [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;
21} // namespace
22
Austin Schuhe8b00752017-04-16 19:14:31 -070023typedef ::y2017::constants::Values::ShotParams ShotParams;
24using ::frc971::control_loops::drivetrain_queue;
25
Austin Schuh87c10632017-02-05 19:02:17 -080026Superstructure::Superstructure(
27 control_loops::SuperstructureQueue *superstructure_queue)
28 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(
Austin Schuhe8b00752017-04-16 19:14:31 -070029 superstructure_queue) {
30 shot_interpolation_table_ =
31 ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({
32 // { distance_to_target, { shot_angle, shot_power, indexer_velocity }},
33 {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry
34 {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry
35 {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry
36 {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry
37 {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry
38 {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry
39 {2.81, {0.41, 351.0, -1.90 * M_PI}}, // table entry
40 });
41}
Austin Schuh87c10632017-02-05 19:02:17 -080042
43void Superstructure::RunIteration(
44 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
45 const control_loops::SuperstructureQueue::Position *position,
46 control_loops::SuperstructureQueue::Output *output,
47 control_loops::SuperstructureQueue::Status *status) {
48 if (WasReset()) {
49 LOG(ERROR, "WPILib reset, restarting\n");
50 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080051 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080052 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080053 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080054 }
55
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000056 const vision::VisionStatus *vision_status = nullptr;
57 if (vision::vision_status.FetchLatest()) {
58 vision_status = vision::vision_status.get();
59 }
60
Parker Schuh208a58d2017-04-12 20:51:38 -070061 // Create a copy of the goals so that we can modify them.
62 HoodGoal hood_goal;
63 ShooterGoal shooter_goal;
Parker Schuh94d56792017-04-13 20:32:50 -070064 IndexerGoal indexer_goal;
Austin Schuhe8b00752017-04-16 19:14:31 -070065 bool in_range = true;
Parker Schuh208a58d2017-04-12 20:51:38 -070066 if (unsafe_goal != nullptr) {
67 hood_goal = unsafe_goal->hood;
68 shooter_goal = unsafe_goal->shooter;
Parker Schuh94d56792017-04-13 20:32:50 -070069 indexer_goal = unsafe_goal->indexer;
Parker Schuh208a58d2017-04-12 20:51:38 -070070
Austin Schuhd85c66e2017-04-16 10:50:33 -070071 if (!unsafe_goal->use_vision_for_shots) {
72 distance_average_.Reset();
73 }
74
Parker Schuh208a58d2017-04-12 20:51:38 -070075 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
76 status->vision_distance = distance_average_.Get();
Austin Schuhe8b00752017-04-16 19:14:31 -070077
78 // If we are moving too fast, disable shooting and clear the accumulator.
79 double robot_velocity = 0.0;
80 drivetrain_queue.status.FetchLatest();
81 if (drivetrain_queue.status.get()) {
82 robot_velocity = drivetrain_queue.status->robot_speed;
83 }
84
85 if (::std::abs(robot_velocity) > 0.2) {
Parker Schuh208a58d2017-04-12 20:51:38 -070086 if (unsafe_goal->use_vision_for_shots) {
Austin Schuhe8b00752017-04-16 19:14:31 -070087 LOG(INFO, "Moving too fast, resetting\n");
88 }
89 distance_average_.Reset();
90 }
91 if (distance_average_.Valid()) {
92 if (unsafe_goal->use_vision_for_shots) {
93 ShotParams shot_params;
94 if (shot_interpolation_table_.GetInRange(
Parker Schuh94d56792017-04-13 20:32:50 -070095 distance_average_.Get(), &shot_params)) {
96 hood_goal.angle = shot_params.angle;
97 shooter_goal.angular_velocity = shot_params.power;
98 if (indexer_goal.angular_velocity != 0.0) {
99 indexer_goal.angular_velocity = shot_params.indexer_velocity;
100 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700101 } else {
102 in_range = false;
Parker Schuh94d56792017-04-13 20:32:50 -0700103 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700104 }
Austin Schuhe8b00752017-04-16 19:14:31 -0700105 LOG(DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n",
106 status->vision_distance, hood_goal.angle,
107 shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI);
Parker Schuh208a58d2017-04-12 20:51:38 -0700108 } else {
109 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
Austin Schuhe8b00752017-04-16 19:14:31 -0700110 if (unsafe_goal->use_vision_for_shots) {
111 in_range = false;
112 indexer_goal.angular_velocity = 0.0;
113 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700114 }
115 }
116
117 hood_.Iterate(
118 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
119 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
120 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800121 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -0800122 output != nullptr ? &(output->voltage_shooter) : nullptr,
123 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -0800124
125 // Implement collision avoidance by passing down a freeze or range restricting
126 // signal to the column and intake objects.
127
128 // Wait until the column is ready before doing collision avoidance.
129 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
130 if (!ignore_collisions_) {
131 // The turret is in a position (or wants to be in a position) where we
132 // need the intake out. Push it out.
133 if (::std::abs(unsafe_goal->turret.angle) >
134 column::Column::kTurretNearZero ||
135 ::std::abs(column_.turret_position()) >
136 column::Column::kTurretNearZero) {
137 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
138 } else {
139 intake_.clear_min_position();
140 }
141 // The intake is in a position where it could hit. Don't move the turret.
142 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
143 column::Column::kIntakeTolerance &&
144 ::std::abs(column_.turret_position()) >
145 column::Column::kTurretNearZero) {
146 column_.set_freeze(true);
147 } else {
148 column_.set_freeze(false);
149 }
150 } else {
151 // If we are ignoring collisions, unfreeze and un-limit the min.
152 column_.set_freeze(false);
153 intake_.clear_min_position();
154 }
155 } else {
156 column_.set_freeze(false);
157 }
158
159 // Make some noise if someone left this set...
160 if (ignore_collisions_) {
161 LOG(ERROR, "Collisions ignored\n");
162 }
163
164 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
165 &(position->intake),
166 output != nullptr ? &(output->voltage_intake) : nullptr,
167 &(status->intake));
168
Parker Schuh94d56792017-04-13 20:32:50 -0700169 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800170 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000171 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800172 output != nullptr ? &(output->voltage_indexer) : nullptr,
173 output != nullptr ? &(output->voltage_turret) : nullptr,
174 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800175
Austin Schuhc587c882017-03-29 21:33:10 -0700176 status->estopped =
177 status->intake.estopped | status->hood.estopped | status->turret.estopped;
178
179 status->zeroed =
180 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
181
Campbell Crowley651c4b42017-02-17 22:30:50 -0800182 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700183 output->gear_servo =
184 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
185
Campbell Crowley651c4b42017-02-17 22:30:50 -0800186 output->voltage_intake_rollers =
187 ::std::max(-kMaxIntakeRollerVoltage,
188 ::std::min(unsafe_goal->intake.voltage_rollers,
189 kMaxIntakeRollerVoltage));
190 output->voltage_indexer_rollers =
191 ::std::max(-kMaxIndexerRollerVoltage,
192 ::std::min(unsafe_goal->indexer.voltage_rollers,
193 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800194
195 // Set the lights on or off
196 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700197
198 if (status->estopped) {
199 output->red_light_on = true;
200 output->green_light_on = false;
201 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700202 } else if (!status->zeroed) {
203 output->red_light_on = false;
204 output->green_light_on = false;
205 output->blue_light_on = true;
Austin Schuhe8b00752017-04-16 19:14:31 -0700206 } else if (status->turret.vision_tracking && in_range) {
207 output->red_light_on = false;
208 output->green_light_on = true;
209 output->blue_light_on = false;
Austin Schuhc587c882017-03-29 21:33:10 -0700210 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800211 }
Austin Schuh87c10632017-02-05 19:02:17 -0800212}
213
214} // namespace superstructure
215} // namespace control_loops
216} // namespace y2017