blob: 2de0ab8b55f3b14f7c15bb3a104b0001b5a6a37e [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;
48 if (unsafe_goal != nullptr) {
49 hood_goal = unsafe_goal->hood;
50 shooter_goal = unsafe_goal->shooter;
51
52 distance_average_.Tick(::aos::monotonic_clock::now(), vision_status);
53 status->vision_distance = distance_average_.Get();
54 if (distance_average_.Valid()) {
55 LOG(DEBUG, "VisionDistance %f\n", status->vision_distance);
56 if (unsafe_goal->use_vision_for_shots) {
57 auto shot_params =
58 constants::GetValues().shot_interpolation_table.GetShooterData(
59 distance_average_.Get());
60 hood_goal.angle = shot_params.angle;
61 shooter_goal.angular_velocity = shot_params.power;
62 }
63 } else {
64 LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance);
65 }
66 }
67
68 hood_.Iterate(
69 unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood),
70 output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood));
71 shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -080072 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -080073 output != nullptr ? &(output->voltage_shooter) : nullptr,
74 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -080075
76 // Implement collision avoidance by passing down a freeze or range restricting
77 // signal to the column and intake objects.
78
79 // Wait until the column is ready before doing collision avoidance.
80 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
81 if (!ignore_collisions_) {
82 // The turret is in a position (or wants to be in a position) where we
83 // need the intake out. Push it out.
84 if (::std::abs(unsafe_goal->turret.angle) >
85 column::Column::kTurretNearZero ||
86 ::std::abs(column_.turret_position()) >
87 column::Column::kTurretNearZero) {
88 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
89 } else {
90 intake_.clear_min_position();
91 }
92 // The intake is in a position where it could hit. Don't move the turret.
93 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
94 column::Column::kIntakeTolerance &&
95 ::std::abs(column_.turret_position()) >
96 column::Column::kTurretNearZero) {
97 column_.set_freeze(true);
98 } else {
99 column_.set_freeze(false);
100 }
101 } else {
102 // If we are ignoring collisions, unfreeze and un-limit the min.
103 column_.set_freeze(false);
104 intake_.clear_min_position();
105 }
106 } else {
107 column_.set_freeze(false);
108 }
109
110 // Make some noise if someone left this set...
111 if (ignore_collisions_) {
112 LOG(ERROR, "Collisions ignored\n");
113 }
114
115 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
116 &(position->intake),
117 output != nullptr ? &(output->voltage_intake) : nullptr,
118 &(status->intake));
119
120 column_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->indexer) : nullptr,
121 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000122 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800123 output != nullptr ? &(output->voltage_indexer) : nullptr,
124 output != nullptr ? &(output->voltage_turret) : nullptr,
125 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800126
Austin Schuhc587c882017-03-29 21:33:10 -0700127 status->estopped =
128 status->intake.estopped | status->hood.estopped | status->turret.estopped;
129
130 status->zeroed =
131 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
132
Campbell Crowley651c4b42017-02-17 22:30:50 -0800133 if (output && unsafe_goal) {
Austin Schuh6a8131b2017-04-08 15:39:22 -0700134 output->gear_servo =
135 ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo));
136
Campbell Crowley651c4b42017-02-17 22:30:50 -0800137 output->voltage_intake_rollers =
138 ::std::max(-kMaxIntakeRollerVoltage,
139 ::std::min(unsafe_goal->intake.voltage_rollers,
140 kMaxIntakeRollerVoltage));
141 output->voltage_indexer_rollers =
142 ::std::max(-kMaxIndexerRollerVoltage,
143 ::std::min(unsafe_goal->indexer.voltage_rollers,
144 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800145
146 // Set the lights on or off
147 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700148
149 if (status->estopped) {
150 output->red_light_on = true;
151 output->green_light_on = false;
152 output->blue_light_on = false;
153 } else if (status->turret.vision_tracking) {
154 output->red_light_on = false;
155 output->green_light_on = true;
156 output->blue_light_on = false;
157 } else if (!status->zeroed) {
158 output->red_light_on = false;
159 output->green_light_on = false;
160 output->blue_light_on = true;
161 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800162 }
Austin Schuh87c10632017-02-05 19:02:17 -0800163}
164
165} // namespace superstructure
166} // namespace control_loops
167} // namespace y2017