blob: 5320cd5cfd9bdb31c754443bd547e0f69c641936 [file] [log] [blame]
Adam Snaider18f44172016-10-22 15:30:21 -07001#include "y2016_bot3/control_loops/intake/intake.h"
2#include "y2016_bot3/control_loops/intake/intake_controls.h"
3
4#include "aos/common/commonmath.h"
5#include "aos/common/controls/control_loops.q.h"
6#include "aos/common/logging/logging.h"
7
8#include "y2016_bot3/control_loops/intake/integral_intake_plant.h"
9#include "y2016_bot3/queues/ball_detector.q.h"
10
11namespace y2016_bot3 {
12namespace control_loops {
13namespace intake {
14
15namespace {
16// The maximum voltage the intake roller will be allowed to use.
17constexpr float kMaxIntakeTopVoltage = 12.0;
18constexpr float kMaxIntakeBottomVoltage = 12.0;
19
20}
21// namespace
22
23void LimitChecker::UpdateGoal(double intake_angle_goal) {
24 intake_->set_unprofiled_goal(intake_angle_goal);
25}
26
27Intake::Intake(control_loops::IntakeQueue *intake_queue)
28 : aos::controls::ControlLoop<control_loops::IntakeQueue>(intake_queue),
29 limit_checker_(&intake_) {}
30bool Intake::IsIntakeNear(double tolerance) {
31 return ((intake_.unprofiled_goal() - intake_.X_hat())
32 .block<2, 1>(0, 0)
33 .lpNorm<Eigen::Infinity>() < tolerance);
34}
35
36double Intake::MoveButKeepAbove(double reference_angle, double current_angle,
37 double move_distance) {
38 return -MoveButKeepBelow(-reference_angle, -current_angle, -move_distance);
39}
40
41double Intake::MoveButKeepBelow(double reference_angle, double current_angle,
42 double move_distance) {
43 // There are 3 interesting places to move to.
44 const double small_negative_move = current_angle - move_distance;
45 const double small_positive_move = current_angle + move_distance;
46 // And the reference angle.
47
48 // Move the the highest one that is below reference_angle.
49 if (small_negative_move > reference_angle) {
50 return reference_angle;
51 } else if (small_positive_move > reference_angle) {
52 return small_negative_move;
53 } else {
54 return small_positive_move;
55 }
56}
57
58void Intake::RunIteration(const control_loops::IntakeQueue::Goal *unsafe_goal,
59 const control_loops::IntakeQueue::Position *position,
60 control_loops::IntakeQueue::Output *output,
61 control_loops::IntakeQueue::Status *status) {
62 const State state_before_switch = state_;
63 if (WasReset()) {
64 LOG(ERROR, "WPILib reset, restarting\n");
65 intake_.Reset();
66 state_ = UNINITIALIZED;
67 }
68
69 // Bool to track if we should turn the motors on or not.
70 bool disable = output == nullptr;
71
72 intake_.Correct(position->intake);
73
74 // There are 2 main zeroing paths, HIGH_ARM_ZERO and LOW_ARM_ZERO.
75 //
76 // HIGH_ARM_ZERO works by lifting the arm all the way up so it is clear,
77 // moving the shooter to be horizontal, moving the intake out, and then moving
78 // the arm back down.
79 //
80 // LOW_ARM_ZERO works by moving the intake out of the way, lifting the arm up,
81 // leveling the shooter, and then moving back down.
82
83 if (intake_.error()) {
84 state_ = ESTOP;
85 }
86
87 switch (state_) {
88 case UNINITIALIZED:
89 // Wait in the uninitialized state until intake is initialized.
90 LOG(DEBUG, "Uninitialized, waiting for intake\n");
91 if (intake_.initialized()) {
92 state_ = DISABLED_INITIALIZED;
93 }
94 disable = true;
95 break;
96
97 case DISABLED_INITIALIZED:
98 // Wait here until we are either fully zeroed while disabled, or we become
99 // enabled.
100 if (disable) {
101 if (intake_.zeroed()) {
102 state_ = SLOW_RUNNING;
103 }
104 } else {
105 if (intake_.angle() <= kIntakeMiddleAngle) {
106 state_ = ZERO_LIFT_INTAKE;
107 } else {
108 state_ = ZERO_LOWER_INTAKE;
109 }
110 }
111
112 // Set the goals to where we are now so when we start back up, we don't
113 // jump.
114 intake_.ForceGoal(intake_.angle());
115 // Set up the profile to be the zeroing profile.
116 intake_.AdjustProfile(0.5, 10);
117
118 // We are not ready to start doing anything yet.
119 disable = true;
120 break;
121
122 case ZERO_LOWER_INTAKE:
123 if (disable) {
124 state_ = DISABLED_INITIALIZED;
125 } else {
126 intake_.set_unprofiled_goal(kIntakeDownAngle);
127
128 if (IsIntakeNear(kLooseTolerance)) {
129 // Close enough, start the next move.
130 state_ = RUNNING;
131 }
132 }
133 break;
134
135 case ZERO_LIFT_INTAKE:
136 if (disable) {
137 state_ = DISABLED_INITIALIZED;
138 } else {
139 intake_.set_unprofiled_goal(kIntakeUpAngle);
140
141 if (IsIntakeNear(kLooseTolerance)) {
142 // Close enough, start the next move.
143 state_ = RUNNING;
144 }
145 }
146 break;
147
148 // These 4 cases are very similar.
149 case SLOW_RUNNING:
150 case RUNNING: {
151 if (disable) {
152 // If we are disabled, go to slow running if we are collided.
153 // Reset the profile to the current position so it moves well from here.
154 intake_.ForceGoal(intake_.angle());
155 }
156
157 double requested_intake = M_PI / 2.0;
158
159 if (unsafe_goal) {
160 intake_.AdjustProfile(unsafe_goal->max_angular_velocity_intake,
161 unsafe_goal->max_angular_acceleration_intake);
162
163 requested_intake = unsafe_goal->angle_intake;
164 }
165 //Push the request out to the hardware.
166 limit_checker_.UpdateGoal(requested_intake);
167
168 // ESTOP if we hit the hard limits.
169 if (intake_.CheckHardLimits() && output) {
170 state_ = ESTOP;
171 }
172 } break;
173
174 case ESTOP:
175 LOG(ERROR, "Estop\n");
176 disable = true;
177 break;
178 }
179
180 // Set the voltage limits.
181 const double max_voltage =
182 (state_ == RUNNING) ? kOperatingVoltage : kZeroingVoltage;
183
184 intake_.set_max_voltage(max_voltage);
185
186 // Calculate the loops for a cycle.
187 {
188 Eigen::Matrix<double, 3, 1> error = intake_.controller().error();
189 status->intake.position_power = intake_.controller().K(0, 0) * error(0, 0);
190 status->intake.velocity_power = intake_.controller().K(0, 1) * error(1, 0);
191 }
192
193 intake_.Update(disable);
194
195 // Write out all the voltages.
196 if (output) {
197 output->voltage_intake = intake_.intake_voltage();
198
199 output->voltage_top_rollers = 0.0;
200 output->voltage_bottom_rollers = 0.0;
201
202 if (unsafe_goal) {
203 // Ball detector lights.
204 ::y2016_bot3::sensors::ball_detector.FetchLatest();
205 bool ball_detected = false;
206 if (::y2016_bot3::sensors::ball_detector.get()) {
207 ball_detected = ::y2016_bot3::sensors::ball_detector->voltage > 2.5;
208 }
209
210 // Intake.
211 if (unsafe_goal->force_intake || !ball_detected) {
212 output->voltage_top_rollers = ::std::max(
213 -kMaxIntakeTopVoltage,
214 ::std::min(unsafe_goal->voltage_top_rollers, kMaxIntakeTopVoltage));
215 output->voltage_bottom_rollers =
216 ::std::max(-kMaxIntakeBottomVoltage,
217 ::std::min(unsafe_goal->voltage_bottom_rollers,
218 kMaxIntakeBottomVoltage));
219 } else {
220 output->voltage_top_rollers = 0.0;
221 output->voltage_bottom_rollers = 0.0;
222 }
223
224 // Traverse.
225 output->traverse_unlatched = unsafe_goal->traverse_unlatched;
226 output->traverse_down = unsafe_goal->traverse_down;
227 }
228 }
229
230 // Save debug/internal state.
231 status->zeroed = intake_.zeroed();
232
233 status->intake.angle = intake_.X_hat(0, 0);
234 status->intake.angular_velocity = intake_.X_hat(1, 0);
235 status->intake.goal_angle = intake_.goal(0, 0);
236 status->intake.goal_angular_velocity = intake_.goal(1, 0);
237 status->intake.unprofiled_goal_angle = intake_.unprofiled_goal(0, 0);
238 status->intake.unprofiled_goal_angular_velocity =
239 intake_.unprofiled_goal(1, 0);
240 status->intake.calculated_velocity =
241 (intake_.angle() - last_intake_angle_) / 0.005;
242 status->intake.voltage_error = intake_.X_hat(2, 0);
243 status->intake.estimator_state = intake_.IntakeEstimatorState();
244 status->intake.feedforwards_power = intake_.controller().ff_U(0, 0);
245
246 last_intake_angle_ = intake_.angle();
247
248 status->estopped = (state_ == ESTOP);
249
250 status->state = state_;
251
252 last_state_ = state_before_switch;
253}
254
255constexpr double Intake::kZeroingVoltage;
256constexpr double Intake::kOperatingVoltage;
257constexpr double Intake::kLooseTolerance;
258constexpr double Intake::kTightTolerance;
259constexpr double Intake::kIntakeUpAngle;
260constexpr double Intake::kIntakeMiddleAngle;
261constexpr double Intake::kIntakeDownAngle;
262
263} // namespace intake
264} // namespace control_loops
265} // namespace y2016_bot3