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