blob: 95c771b61e585a7ea2557153afd53a918c3d6142 [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
Austin Schuhd85c66e2017-04-16 10:50:33 -070054 if (!unsafe_goal->use_vision_for_shots) {
55 distance_average_.Reset();
56 }
57
Parker Schuh208a58d2017-04-12 20:51:38 -070058 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
59 status->vision_distance = distance_average_.Get();
60 if (distance_average_.Valid()) {
61 LOG(DEBUG, "VisionDistance %f\n", status->vision_distance);
62 if (unsafe_goal->use_vision_for_shots) {
Parker Schuh94d56792017-04-13 20:32:50 -070063 y2017::constants::Values::ShotParams shot_params;
64 if (constants::GetValues().shot_interpolation_table.GetInRange(
65 distance_average_.Get(), &shot_params)) {
66 hood_goal.angle = shot_params.angle;
67 shooter_goal.angular_velocity = shot_params.power;
68 if (indexer_goal.angular_velocity != 0.0) {
69 indexer_goal.angular_velocity = shot_params.indexer_velocity;
70 }
71 }
Parker Schuh208a58d2017-04-12 20:51:38 -070072 }
73 } else {
74 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
75 }
76 }
77
78 hood_.Iterate(
79 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
80 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
81 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -080082 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -080083 output != nullptr ? &(output->voltage_shooter) : nullptr,
84 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -080085
86 // Implement collision avoidance by passing down a freeze or range restricting
87 // signal to the column and intake objects.
88
89 // Wait until the column is ready before doing collision avoidance.
90 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
91 if (!ignore_collisions_) {
92 // The turret is in a position (or wants to be in a position) where we
93 // need the intake out. Push it out.
94 if (::std::abs(unsafe_goal->turret.angle) >
95 column::Column::kTurretNearZero ||
96 ::std::abs(column_.turret_position()) >
97 column::Column::kTurretNearZero) {
98 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
99 } else {
100 intake_.clear_min_position();
101 }
102 // The intake is in a position where it could hit. Don't move the turret.
103 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
104 column::Column::kIntakeTolerance &&
105 ::std::abs(column_.turret_position()) >
106 column::Column::kTurretNearZero) {
107 column_.set_freeze(true);
108 } else {
109 column_.set_freeze(false);
110 }
111 } else {
112 // If we are ignoring collisions, unfreeze and un-limit the min.
113 column_.set_freeze(false);
114 intake_.clear_min_position();
115 }
116 } else {
117 column_.set_freeze(false);
118 }
119
120 // Make some noise if someone left this set...
121 if (ignore_collisions_) {
122 LOG(ERROR, "Collisions ignored\n");
123 }
124
125 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
126 &(position->intake),
127 output != nullptr ? &(output->voltage_intake) : nullptr,
128 &(status->intake));
129
Parker Schuh94d56792017-04-13 20:32:50 -0700130 column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800131 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000132 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800133 output != nullptr ? &(output->voltage_indexer) : nullptr,
134 output != nullptr ? &(output->voltage_turret) : nullptr,
135 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800136
Austin Schuhc587c882017-03-29 21:33:10 -0700137 status->estopped =
138 status->intake.estopped | status->hood.estopped | status->turret.estopped;
139
140 status->zeroed =
141 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
142
Campbell Crowley651c4b42017-02-17 22:30:50 -0800143 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700144 output->gear_servo =
145 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
146
Campbell Crowley651c4b42017-02-17 22:30:50 -0800147 output->voltage_intake_rollers =
148 ::std::max(-kMaxIntakeRollerVoltage,
149 ::std::min(unsafe_goal->intake.voltage_rollers,
150 kMaxIntakeRollerVoltage));
151 output->voltage_indexer_rollers =
152 ::std::max(-kMaxIndexerRollerVoltage,
153 ::std::min(unsafe_goal->indexer.voltage_rollers,
154 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800155
156 // Set the lights on or off
157 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700158
159 if (status->estopped) {
160 output->red_light_on = true;
161 output->green_light_on = false;
162 output->blue_light_on = false;
163 } else if (status->turret.vision_tracking) {
164 output->red_light_on = false;
165 output->green_light_on = true;
166 output->blue_light_on = false;
167 } else if (!status->zeroed) {
168 output->red_light_on = false;
169 output->green_light_on = false;
170 output->blue_light_on = true;
171 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800172 }
Austin Schuh87c10632017-02-05 19:02:17 -0800173}
174
175} // namespace superstructure
176} // namespace control_loops
177} // namespace y2017