blob: e0017c1c38ec831925bcc0468f53560737bfb037 [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
Austin Schuh87c10632017-02-05 19:02:17 -080045 hood_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->hood) : nullptr,
46 &(position->hood),
47 output != nullptr ? &(output->voltage_hood) : nullptr,
48 &(status->hood));
Tyler Chatow2737d2a2017-02-08 21:20:51 -080049 shooter_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->shooter) : nullptr,
Austin Schuh932a5ce2017-03-05 01:04:18 -080050 &(position->theta_shooter), position->sent_time,
Campbell Crowley651c4b42017-02-17 22:30:50 -080051 output != nullptr ? &(output->voltage_shooter) : nullptr,
52 &(status->shooter));
Austin Schuhd5ccb862017-03-11 22:06:36 -080053
54 // Implement collision avoidance by passing down a freeze or range restricting
55 // signal to the column and intake objects.
56
57 // Wait until the column is ready before doing collision avoidance.
58 if (unsafe_goal && column_.state() == column::Column::State::RUNNING) {
59 if (!ignore_collisions_) {
60 // The turret is in a position (or wants to be in a position) where we
61 // need the intake out. Push it out.
62 if (::std::abs(unsafe_goal->turret.angle) >
63 column::Column::kTurretNearZero ||
64 ::std::abs(column_.turret_position()) >
65 column::Column::kTurretNearZero) {
66 intake_.set_min_position(column::Column::kIntakeZeroingMinDistance);
67 } else {
68 intake_.clear_min_position();
69 }
70 // The intake is in a position where it could hit. Don't move the turret.
71 if (intake_.position() < column::Column::kIntakeZeroingMinDistance -
72 column::Column::kIntakeTolerance &&
73 ::std::abs(column_.turret_position()) >
74 column::Column::kTurretNearZero) {
75 column_.set_freeze(true);
76 } else {
77 column_.set_freeze(false);
78 }
79 } else {
80 // If we are ignoring collisions, unfreeze and un-limit the min.
81 column_.set_freeze(false);
82 intake_.clear_min_position();
83 }
84 } else {
85 column_.set_freeze(false);
86 }
87
88 // Make some noise if someone left this set...
89 if (ignore_collisions_) {
90 LOG(ERROR, "Collisions ignored\n");
91 }
92
93 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
94 &(position->intake),
95 output != nullptr ? &(output->voltage_intake) : nullptr,
96 &(status->intake));
97
98 column_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->indexer) : nullptr,
99 unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000100 &(position->column), vision_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800101 output != nullptr ? &(output->voltage_indexer) : nullptr,
102 output != nullptr ? &(output->voltage_turret) : nullptr,
103 &(status->indexer), &(status->turret), &intake_);
Campbell Crowley651c4b42017-02-17 22:30:50 -0800104
Austin Schuhc587c882017-03-29 21:33:10 -0700105 status->estopped =
106 status->intake.estopped | status->hood.estopped | status->turret.estopped;
107
108 status->zeroed =
109 status->intake.zeroed && status->hood.zeroed && status->turret.zeroed;
110
Campbell Crowley651c4b42017-02-17 22:30:50 -0800111 if (output && unsafe_goal) {
112 output->voltage_intake_rollers =
113 ::std::max(-kMaxIntakeRollerVoltage,
114 ::std::min(unsafe_goal->intake.voltage_rollers,
115 kMaxIntakeRollerVoltage));
116 output->voltage_indexer_rollers =
117 ::std::max(-kMaxIndexerRollerVoltage,
118 ::std::min(unsafe_goal->indexer.voltage_rollers,
119 kMaxIndexerRollerVoltage));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800120
121 // Set the lights on or off
122 output->lights_on = unsafe_goal->lights_on;
Austin Schuhc587c882017-03-29 21:33:10 -0700123
124 if (status->estopped) {
125 output->red_light_on = true;
126 output->green_light_on = false;
127 output->blue_light_on = false;
128 } else if (status->turret.vision_tracking) {
129 output->red_light_on = false;
130 output->green_light_on = true;
131 output->blue_light_on = false;
132 } else if (!status->zeroed) {
133 output->red_light_on = false;
134 output->green_light_on = false;
135 output->blue_light_on = true;
136 }
Campbell Crowley651c4b42017-02-17 22:30:50 -0800137 }
Austin Schuh87c10632017-02-05 19:02:17 -0800138}
139
140} // namespace superstructure
141} // namespace control_loops
142} // namespace y2017