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