blob: 1faed363039129eaa0b2c122b2c24bb24cb36c0f [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"
5#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"
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000010#include "y2017/vision/vision.q.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 Schuh87c10632017-02-05 19:02:17 -080022Superstructure::Superstructure(
23 control_loops::SuperstructureQueue *superstructure_queue)
24 : aos::controls::ControlLoop<control_loops::SuperstructureQueue>(
25 superstructure_queue) {}
26
27void Superstructure::RunIteration(
28 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
29 const control_loops::SuperstructureQueue::Position *position,
30 control_loops::SuperstructureQueue::Output *output,
31 control_loops::SuperstructureQueue::Status *status) {
32 if (WasReset()) {
33 LOG(ERROR, "WPILib reset, restarting\n");
34 hood_.Reset();
Adam Snaider79900c22017-02-08 20:23:15 -080035 intake_.Reset();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080036 shooter_.Reset();
Austin Schuhd5ccb862017-03-11 22:06:36 -080037 column_.Reset();
Austin Schuh87c10632017-02-05 19:02:17 -080038 }
39
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +000040 const vision::VisionStatus *vision_status = nullptr;
41 if (vision::vision_status.FetchLatest()) {
42 vision_status = vision::vision_status.get();
43 }
44
Parker Schuh208a58d2017-04-12 20:51:38 -070045 // Create a copy of the goals so that we can modify them.
46 HoodGoal hood_goal;
47 ShooterGoal shooter_goal;
Parker Schuh94d56792017-04-13 20:32:50 -070048 IndexerGoal indexer_goal;
Parker Schuh208a58d2017-04-12 20:51:38 -070049 if (unsafe_goal != nullptr) {
50 hood_goal = unsafe_goal->hood;
51 shooter_goal = unsafe_goal->shooter;
Parker Schuh94d56792017-04-13 20:32:50 -070052 indexer_goal = unsafe_goal->indexer;
Parker Schuh208a58d2017-04-12 20:51:38 -070053
54 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
55 status->vision_distance = distance_average_.Get();
56 if (distance_average_.Valid()) {
57 LOG(DEBUG, "VisionDistance %f\n", status->vision_distance);
58 if (unsafe_goal->use_vision_for_shots) {
Parker Schuh94d56792017-04-13 20:32:50 -070059 y2017::constants::Values::ShotParams shot_params;
60 if (constants::GetValues().shot_interpolation_table.GetInRange(
61 distance_average_.Get(), &shot_params)) {
62 hood_goal.angle = shot_params.angle;
63 shooter_goal.angular_velocity = shot_params.power;
64 if (indexer_goal.angular_velocity != 0.0) {
65 indexer_goal.angular_velocity = shot_params.indexer_velocity;
66 }
67 }
Parker Schuh208a58d2017-04-12 20:51:38 -070068 }
69 } else {
70 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
71 }
72 }
73
74 hood_.Iterate(
75 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
76 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
77 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -080078 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -080079 output != nullptr ? &(output->voltage_shooter) : nullptr,
80 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -080081
82 // Implement collision avoidance by passing down a freeze or range restricting
83 // signal to the column and intake objects.
84
85 // Wait until the column is ready before doing collision avoidance.
86 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
87 if (!ignore_collisions_) {
88 // The turret is in a position (or wants to be in a position) where we
89 // need the intake out. Push it out.
90 if (::std::abs(unsafe_goal->turret.angle) >
91 column::Column::kTurretNearZero ||
92 ::std::abs(column_.turret_position()) >
93 column::Column::kTurretNearZero) {
94 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
95 } else {
96 intake_.clear_min_position();
97 }
98 // The intake is in a position where it could hit. Don't move the turret.
99 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
100 column::Column::kIntakeTolerance &&
101 ::std::abs(column_.turret_position()) >
102 column::Column::kTurretNearZero) {
103 column_.set_freeze(true);
104 } else {
105 column_.set_freeze(false);
106 }
107 } else {
108 // If we are ignoring collisions, unfreeze and un-limit the min.
109 column_.set_freeze(false);
110 intake_.clear_min_position();
111 }
112 } else {
113 column_.set_freeze(false);
114 }
115
116 // Make some noise if someone left this set...
117 if (ignore_collisions_) {
118 LOG(ERROR, "Collisions ignored\n");
119 }
120
121 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
122 &(position->intake),
123 output != nullptr ? &(output->voltage_intake) : nullptr,
124 &(status->intake));
125
Parker Schuh94d56792017-04-13 20:32:50 -0700126 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800127 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000128 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800129 output != nullptr ? &(output->voltage_indexer) : nullptr,
130 output != nullptr ? &(output->voltage_turret) : nullptr,
131 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800132
Austin Schuhc587c882017-03-29 21:33:10 -0700133 status->estopped =
134 status->intake.estopped | status->hood.estopped | status->turret.estopped;
135
136 status->zeroed =
137 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
138
Campbell Crowley651c4b42017-02-17 22:30:50 -0800139 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700140 output->gear_servo =
141 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
142
Campbell Crowley651c4b42017-02-17 22:30:50 -0800143 output->voltage_intake_rollers =
144 ::std::max(-kMaxIntakeRollerVoltage,
145 ::std::min(unsafe_goal->intake.voltage_rollers,
146 kMaxIntakeRollerVoltage));
147 output->voltage_indexer_rollers =
148 ::std::max(-kMaxIndexerRollerVoltage,
149 ::std::min(unsafe_goal->indexer.voltage_rollers,
150 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800151
152 // Set the lights on or off
153 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700154
155 if (status->estopped) {
156 output->red_light_on = true;
157 output->green_light_on = false;
158 output->blue_light_on = false;
159 } else if (status->turret.vision_tracking) {
160 output->red_light_on = false;
161 output->green_light_on = true;
162 output->blue_light_on = false;
163 } else if (!status->zeroed) {
164 output->red_light_on = false;
165 output->green_light_on = false;
166 output->blue_light_on = true;
167 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800168 }
Austin Schuh87c10632017-02-05 19:02:17 -0800169}
170
171} // namespace superstructure
172} // namespace control_loops
173} // namespace y2017