blob: f76ea97ffadacc11c1f9d65afc6e3d6d957a5a4a [file] [log] [blame]
Austin Schuhd78ab542013-03-01 22:22:19 -08001#ifndef FRC971_CONTROL_LOOPS_WRIST_H_
2#define FRC971_CONTROL_LOOPS_WRIST_H_
3
Austin Schuhd78ab542013-03-01 22:22:19 -08004#include <deque>
Austin Schuh166b9482013-03-09 14:38:51 -08005#include <memory>
Austin Schuhd78ab542013-03-01 22:22:19 -08006
7#include "aos/common/control_loop/ControlLoop.h"
8#include "aos/common/time.h"
9#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh00558222013-03-03 14:16:16 -080010#include "frc971/control_loops/index/index_motor.q.h"
11#include "frc971/control_loops/index/index_motor_plant.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080012
13namespace frc971 {
14namespace control_loops {
Austin Schuhbcdb90c2013-03-03 23:24:58 -080015namespace testing {
16class IndexTest_InvalidStateTest_Test;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080017class IndexTest_LostDisc_Test;
Austin Schuhbcdb90c2013-03-03 23:24:58 -080018}
Austin Schuhd78ab542013-03-01 22:22:19 -080019
Austin Schuh723770b2013-03-10 13:26:20 -070020// This class represents a region of space.
21class Region {
22 public:
23 Region () : upper_bound_(0.0), lower_bound_(0.0) {}
24
25 // Restarts the region tracking by starting over with a 0 width region with
26 // the bounds at [edge, edge].
27 void Restart(double edge) {
28 upper_bound_ = edge;
29 lower_bound_ = edge;
30 }
31
32 // Expands the region to include the new point.
33 void Expand(double new_point) {
34 if (new_point > upper_bound_) {
35 upper_bound_ = new_point;
36 } else if (new_point < lower_bound_) {
37 lower_bound_ = new_point;
38 }
39 }
40
41 // Returns the width of the region.
42 double width() const { return upper_bound_ - lower_bound_; }
43 // Returns the upper and lower bounds.
44 double upper_bound() const { return upper_bound_; }
45 double lower_bound() const { return lower_bound_; }
46
47 private:
48 // Upper bound of the region.
49 double upper_bound_;
50 // Lower bound of the region.
51 double lower_bound_;
52};
53
Austin Schuhd78ab542013-03-01 22:22:19 -080054class IndexMotor
55 : public aos::control_loops::ControlLoop<control_loops::IndexLoop> {
56 public:
57 explicit IndexMotor(
Austin Schuhf8c52252013-03-03 02:25:49 -080058 control_loops::IndexLoop *my_index = &control_loops::index_loop);
59
60 static const double kTransferStartPosition;
61 static const double kIndexStartPosition;
62 // The distance from where the disc first grabs on the indexer to where it
63 // just bairly clears the loader.
64 static const double kIndexFreeLength;
65 // The distance to where the disc just starts to enter the loader.
66 static const double kLoaderFreeStopPosition;
Austin Schuh1b864a12013-03-07 00:46:50 -080067 // The distance to where the next disc gets positioned while the current disc
68 // is shooting.
69 static const double kReadyToPreload;
Austin Schuhf8c52252013-03-03 02:25:49 -080070
71 // Distance that the grabber pulls the disc in by.
72 static const double kGrabberLength;
73 // Distance to where the grabber takes over.
74 static const double kGrabberStartPosition;
75
76 // The distance to where the disc hits the back of the loader and is ready to
77 // lift.
78 static const double kReadyToLiftPosition;
79
80 static const double kGrabberMovementVelocity;
81 // TODO(aschuh): This depends on the shooter angle...
82 // Distance to where the shooter is up and ready to shoot.
83 static const double kLifterStopPosition;
84 static const double kLifterMovementVelocity;
85
86 // Distance to where the disc has been launched.
87 // TODO(aschuh): This depends on the shooter angle...
88 static const double kEjectorStopPosition;
89 static const double kEjectorMovementVelocity;
90
91 // Start and stop position of the bottom disc detect sensor in meters.
92 static const double kBottomDiscDetectStart;
93 static const double kBottomDiscDetectStop;
Austin Schuh6328daf2013-03-05 00:53:15 -080094 // Delay between the negedge of the disc detect and when it engages on the
95 // indexer.
96 static const double kBottomDiscIndexDelay;
Austin Schuhf8c52252013-03-03 02:25:49 -080097
98 static const double kTopDiscDetectStart;
99 static const double kTopDiscDetectStop;
Austin Schuhd78ab542013-03-01 22:22:19 -0800100
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800101 // Minimum distance between 2 frisbees as seen by the top disc detect sensor.
102 static const double kTopDiscDetectMinSeperation;
103
Austin Schuhd78ab542013-03-01 22:22:19 -0800104 // Converts the angle of the indexer to the angle of the disc.
105 static double ConvertIndexToDiscAngle(const double angle);
106 // Converts the angle of the indexer to the position that the center of the
107 // disc has traveled.
108 static double ConvertIndexToDiscPosition(const double angle);
109
Austin Schuhf8c52252013-03-03 02:25:49 -0800110 // Converts the angle of the transfer roller to the position that the center
111 // of the disc has traveled.
112 static double ConvertTransferToDiscPosition(const double angle);
113
114 // Converts the distance around the indexer to the position of
115 // the index roller.
116 static double ConvertDiscPositionToIndex(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800117 // Converts the angle around the indexer to the position of the index roller.
118 static double ConvertDiscAngleToIndex(const double angle);
119 // Converts the angle around the indexer to the position of the disc in the
120 // indexer.
121 static double ConvertDiscAngleToDiscPosition(const double angle);
Austin Schuhf8c52252013-03-03 02:25:49 -0800122 // Converts the distance around the indexer to the angle of the disc around
123 // the indexer.
124 static double ConvertDiscPositionToDiscAngle(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800125
126 // Disc radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -0800127 static const double kDiscRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -0800128 // Roller radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -0800129 static const double kRollerRadius;
130 // Transfer roller radius in meters.
131 static const double kTransferRollerRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -0800132
Austin Schuhf8c52252013-03-03 02:25:49 -0800133 // Time that it takes to grab the disc in cycles.
134 static const int kGrabbingDelay;
135 // Time that it takes to lift the loader in cycles.
136 static const int kLiftingDelay;
137 // Time that it takes to shoot the disc in cycles.
138 static const int kShootingDelay;
139 // Time that it takes to lower the loader in cycles.
140 static const int kLoweringDelay;
141
142 // Object representing a Frisbee tracked by the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800143 class Frisbee {
144 public:
145 Frisbee()
146 : bottom_posedge_time_(0, 0),
Austin Schuhf8c52252013-03-03 02:25:49 -0800147 bottom_negedge_time_(0, 0) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800148 Reset();
149 }
150
Austin Schuhf8c52252013-03-03 02:25:49 -0800151 // Resets a Frisbee so it can be reused.
Austin Schuhd78ab542013-03-01 22:22:19 -0800152 void Reset() {
153 bottom_posedge_time_ = ::aos::time::Time(0, 0);
154 bottom_negedge_time_ = ::aos::time::Time(0, 0);
Austin Schuhd78ab542013-03-01 22:22:19 -0800155 has_been_indexed_ = false;
156 index_start_position_ = 0.0;
157 }
158
Austin Schuhf8c52252013-03-03 02:25:49 -0800159 // Returns true if the position is valid.
160 bool has_position() const {
161 return has_been_indexed_;
162 }
163
164 // Returns the most up to date and accurate position that we have for the
165 // disc. This is the indexer position that the disc grabbed at.
166 double position() const {
167 return index_start_position_;
168 }
169
Austin Schuh1b864a12013-03-07 00:46:50 -0800170 // Returns the absolute position of the disc in meters in the hopper given
171 // that the indexer is at the provided position.
172 double absolute_position(const double index_position) const {
173 return IndexMotor::ConvertIndexToDiscPosition(
174 index_position - index_start_position_) +
175 IndexMotor::kIndexStartPosition;
176 }
177
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800178 // Shifts the disc down the indexer by the provided offset. This is to
179 // handle when the cRIO reboots.
180 void OffsetDisc(double offset) {
181 index_start_position_ += offset;
182 }
183
Austin Schuh825bde92013-03-06 00:16:46 -0800184 // Potentially offsets the position with the knowledge that no discs are
185 // currently blocking the top sensor. This knowledge can be used to move
186 // this disc if it is believed to be blocking the top sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800187 // Returns the amount that the disc moved due to this observation.
Austin Schuh6b1ad2f2013-03-11 23:23:00 -0700188 double ObserveNoTopDiscSensor(double index_position);
Austin Schuh825bde92013-03-06 00:16:46 -0800189
Austin Schuhf8c52252013-03-03 02:25:49 -0800190 // Posedge and negedge disc times.
Austin Schuhd78ab542013-03-01 22:22:19 -0800191 ::aos::time::Time bottom_posedge_time_;
192 ::aos::time::Time bottom_negedge_time_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800193
194 // True if the disc has a valid index position.
Austin Schuhd78ab542013-03-01 22:22:19 -0800195 bool has_been_indexed_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800196 // Location of the index when the disc first contacted it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800197 double index_start_position_;
198 };
199
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800200 // Returns where the indexer thinks the frisbees are.
Austin Schuh1b864a12013-03-07 00:46:50 -0800201 const ::std::deque<Frisbee> &frisbees() const { return frisbees_; }
202
Austin Schuhd78ab542013-03-01 22:22:19 -0800203 protected:
204 virtual void RunIteration(
205 const control_loops::IndexLoop::Goal *goal,
206 const control_loops::IndexLoop::Position *position,
207 control_loops::IndexLoop::Output *output,
208 control_loops::IndexLoop::Status *status);
209
210 private:
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800211 friend class testing::IndexTest_InvalidStateTest_Test;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800212 friend class testing::IndexTest_LostDisc_Test;
Austin Schuh93485832013-03-04 00:01:34 -0800213
214 // This class implements the CapU function correctly given all the extra
215 // information that we know about from the wrist motor.
216 class IndexStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
217 public:
218 IndexStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop)
219 : StateFeedbackLoop<2, 1, 1>(loop),
220 low_voltage_count_(0) {
221 }
222
223 // Voltage below which the indexer won't move with a disc in it.
224 static const double kMinMotionVoltage;
225 // Maximum number of cycles to apply a low voltage to the motor.
226 static const double kNoMotionCuttoffCount;
227
228 // Caps U, but disables the motor after a number of cycles of inactivity.
229 virtual void CapU();
230 private:
231 // Number of cycles that we have seen a small voltage being applied.
232 uint32_t low_voltage_count_;
233 };
234
Austin Schuhf8c52252013-03-03 02:25:49 -0800235 // Sets disc_position to the minimum or maximum disc position.
Austin Schuh1b864a12013-03-07 00:46:50 -0800236 // Sets found_disc to point to the frisbee that was found, and ignores it if
237 // found_disc is NULL.
Austin Schuhf8c52252013-03-03 02:25:49 -0800238 // Returns true if there were discs, and false if there weren't.
239 // On false, disc_position is left unmodified.
Austin Schuh1b864a12013-03-07 00:46:50 -0800240 bool MinDiscPosition(double *disc_position, Frisbee **found_disc);
241 bool MaxDiscPosition(double *disc_position, Frisbee **found_disc);
Austin Schuhd78ab542013-03-01 22:22:19 -0800242
243 // The state feedback control loop to talk to for the index.
Austin Schuh93485832013-03-04 00:01:34 -0800244 ::std::unique_ptr<IndexStateFeedbackLoop> wrist_loop_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800245
Austin Schuhd78ab542013-03-01 22:22:19 -0800246 // Count of the number of discs that we have collected.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800247 int32_t hopper_disc_count_;
248 int32_t total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700249 int32_t shot_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800250
Austin Schuhf8c52252013-03-03 02:25:49 -0800251 enum class Goal {
Austin Schuhd78ab542013-03-01 22:22:19 -0800252 // Hold position, in a low power state.
253 HOLD = 0,
254 // Get ready to load discs by shifting the discs down.
255 READY_LOWER = 1,
256 // Ready the discs, spin up the transfer roller, and accept discs.
257 INTAKE = 2,
258 // Get ready to shoot, and place a disc in the loader.
259 READY_SHOOTER = 3,
260 // Shoot at will.
261 SHOOT = 4
262 };
263
Austin Schuhf8c52252013-03-03 02:25:49 -0800264 // These two enums command and track the loader loading discs into the
265 // shooter.
266 enum class LoaderState {
267 // Open and down, ready to accept a disc.
268 READY,
269 // Closing the grabber.
270 GRABBING,
271 // Grabber closed.
272 GRABBED,
273 // Lifting the disc.
274 LIFTING,
275 // Disc lifted.
276 LIFTED,
277 // Ejecting the disc into the shooter.
278 SHOOTING,
279 // The disc has been shot.
280 SHOOT,
281 // Lowering the loader back down.
282 LOWERING,
283 // The indexer is lowered.
284 LOWERED
285 };
286
287 // TODO(aschuh): If we are grabbed and asked to be ready, now what?
288 // LOG ?
289 enum class LoaderGoal {
290 // Get the loader ready to accept another disc.
291 READY,
292 // Grab a disc now.
293 GRAB,
294 // Lift it up, shoot, and reset.
295 // Waits to shoot until the shooter is stable.
296 // Resets the goal to READY once one disc has been shot.
297 SHOOT_AND_RESET
298 };
299
Austin Schuhd78ab542013-03-01 22:22:19 -0800300 // The current goal
301 Goal safe_goal_;
302
Austin Schuhf8c52252013-03-03 02:25:49 -0800303 // Loader goal, state, and counter.
304 LoaderGoal loader_goal_;
305 LoaderState loader_state_;
306 int loader_countdown_;
307
Austin Schuhd78ab542013-03-01 22:22:19 -0800308 // Current state of the pistons.
309 bool loader_up_;
310 bool disc_clamped_;
311 bool disc_ejected_;
312
Austin Schuhd78ab542013-03-01 22:22:19 -0800313 // The frisbee that is flying through the transfer rollers.
314 Frisbee transfer_frisbee_;
315
Austin Schuhf8c52252013-03-03 02:25:49 -0800316 // Bottom disc detect from the last valid packet for detecting edges.
Austin Schuhd78ab542013-03-01 22:22:19 -0800317 bool last_bottom_disc_detect_;
Austin Schuh825bde92013-03-06 00:16:46 -0800318 bool last_top_disc_detect_;
Austin Schuh6328daf2013-03-05 00:53:15 -0800319 int32_t last_bottom_disc_posedge_count_;
320 int32_t last_bottom_disc_negedge_count_;
321 int32_t last_bottom_disc_negedge_wait_count_;
Austin Schuh825bde92013-03-06 00:16:46 -0800322 int32_t last_top_disc_posedge_count_;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800323 int32_t last_top_disc_negedge_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800324
325 // Frisbees are in order such that the newest frisbee is on the front.
326 ::std::deque<Frisbee> frisbees_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800327
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800328 // True if we haven't seen a position before.
329 bool no_prior_position_;
330 // Number of position messages that we have missed in a row.
331 uint32_t missing_position_count_;
332
Austin Schuh723770b2013-03-10 13:26:20 -0700333 // The no-disc regions for both the bottom and top beam break sensors.
334 Region upper_open_region_;
335 Region lower_open_region_;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800336
Austin Schuhd78ab542013-03-01 22:22:19 -0800337 DISALLOW_COPY_AND_ASSIGN(IndexMotor);
338};
Austin Schuhd78ab542013-03-01 22:22:19 -0800339} // namespace control_loops
340} // namespace frc971
341
342#endif // FRC971_CONTROL_LOOPS_WRIST_H_