blob: a18d4c96bfb649fd18ce7ceb653a9a2b02350644 [file] [log] [blame]
Brian Silverman43bb73e2013-03-17 13:39:47 -07001#ifndef FRC971_CONTROL_LOOPS_INDEX_INDEX_H_
2#define FRC971_CONTROL_LOOPS_INDEX_INDEX_H_
Austin Schuhd78ab542013-03-01 22:22:19 -08003
Austin Schuhd78ab542013-03-01 22:22:19 -08004#include <deque>
Brian Silverman26683e22013-03-16 14:12:16 -07005#include "aos/common/libstdc++/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
Austin Schuhd3d0fbf2013-03-14 00:37:00 -070098 // Time after seeing the fourth disc that we need to wait before turning the
99 // transfer roller off.
100 static const ::aos::time::Time kTransferOffDelay;
101
Austin Schuhf8c52252013-03-03 02:25:49 -0800102 static const double kTopDiscDetectStart;
103 static const double kTopDiscDetectStop;
Austin Schuhd78ab542013-03-01 22:22:19 -0800104
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800105 // Minimum distance between 2 frisbees as seen by the top disc detect sensor.
106 static const double kTopDiscDetectMinSeperation;
107
Austin Schuhd78ab542013-03-01 22:22:19 -0800108 // Converts the angle of the indexer to the angle of the disc.
109 static double ConvertIndexToDiscAngle(const double angle);
110 // Converts the angle of the indexer to the position that the center of the
111 // disc has traveled.
112 static double ConvertIndexToDiscPosition(const double angle);
113
Austin Schuhf8c52252013-03-03 02:25:49 -0800114 // Converts the angle of the transfer roller to the position that the center
115 // of the disc has traveled.
116 static double ConvertTransferToDiscPosition(const double angle);
117
118 // Converts the distance around the indexer to the position of
119 // the index roller.
120 static double ConvertDiscPositionToIndex(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800121 // Converts the angle around the indexer to the position of the index roller.
122 static double ConvertDiscAngleToIndex(const double angle);
123 // Converts the angle around the indexer to the position of the disc in the
124 // indexer.
125 static double ConvertDiscAngleToDiscPosition(const double angle);
Austin Schuhf8c52252013-03-03 02:25:49 -0800126 // Converts the distance around the indexer to the angle of the disc around
127 // the indexer.
128 static double ConvertDiscPositionToDiscAngle(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800129
130 // Disc radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -0800131 static const double kDiscRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -0800132 // Roller radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -0800133 static const double kRollerRadius;
134 // Transfer roller radius in meters.
135 static const double kTransferRollerRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -0800136
Austin Schuhf8c52252013-03-03 02:25:49 -0800137 // Time that it takes to grab the disc in cycles.
138 static const int kGrabbingDelay;
139 // Time that it takes to lift the loader in cycles.
140 static const int kLiftingDelay;
141 // Time that it takes to shoot the disc in cycles.
142 static const int kShootingDelay;
143 // Time that it takes to lower the loader in cycles.
144 static const int kLoweringDelay;
145
146 // Object representing a Frisbee tracked by the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800147 class Frisbee {
148 public:
149 Frisbee()
150 : bottom_posedge_time_(0, 0),
Austin Schuhf8c52252013-03-03 02:25:49 -0800151 bottom_negedge_time_(0, 0) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800152 Reset();
153 }
154
Austin Schuhf8c52252013-03-03 02:25:49 -0800155 // Resets a Frisbee so it can be reused.
Austin Schuhd78ab542013-03-01 22:22:19 -0800156 void Reset() {
157 bottom_posedge_time_ = ::aos::time::Time(0, 0);
158 bottom_negedge_time_ = ::aos::time::Time(0, 0);
Austin Schuhd78ab542013-03-01 22:22:19 -0800159 has_been_indexed_ = false;
160 index_start_position_ = 0.0;
161 }
162
Austin Schuhf8c52252013-03-03 02:25:49 -0800163 // Returns true if the position is valid.
164 bool has_position() const {
165 return has_been_indexed_;
166 }
167
168 // Returns the most up to date and accurate position that we have for the
169 // disc. This is the indexer position that the disc grabbed at.
170 double position() const {
171 return index_start_position_;
172 }
173
Austin Schuh1b864a12013-03-07 00:46:50 -0800174 // Returns the absolute position of the disc in meters in the hopper given
175 // that the indexer is at the provided position.
176 double absolute_position(const double index_position) const {
177 return IndexMotor::ConvertIndexToDiscPosition(
178 index_position - index_start_position_) +
179 IndexMotor::kIndexStartPosition;
180 }
181
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800182 // Shifts the disc down the indexer by the provided offset. This is to
183 // handle when the cRIO reboots.
184 void OffsetDisc(double offset) {
185 index_start_position_ += offset;
186 }
187
Austin Schuh825bde92013-03-06 00:16:46 -0800188 // Potentially offsets the position with the knowledge that no discs are
189 // currently blocking the top sensor. This knowledge can be used to move
190 // this disc if it is believed to be blocking the top sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800191 // Returns the amount that the disc moved due to this observation.
Austin Schuh6b1ad2f2013-03-11 23:23:00 -0700192 double ObserveNoTopDiscSensor(double index_position);
Austin Schuh825bde92013-03-06 00:16:46 -0800193
Austin Schuhf8c52252013-03-03 02:25:49 -0800194 // Posedge and negedge disc times.
Austin Schuhd78ab542013-03-01 22:22:19 -0800195 ::aos::time::Time bottom_posedge_time_;
196 ::aos::time::Time bottom_negedge_time_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800197
198 // True if the disc has a valid index position.
Austin Schuhd78ab542013-03-01 22:22:19 -0800199 bool has_been_indexed_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800200 // Location of the index when the disc first contacted it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800201 double index_start_position_;
202 };
203
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800204 // Returns where the indexer thinks the frisbees are.
Austin Schuh1b864a12013-03-07 00:46:50 -0800205 const ::std::deque<Frisbee> &frisbees() const { return frisbees_; }
206
Austin Schuhd78ab542013-03-01 22:22:19 -0800207 protected:
208 virtual void RunIteration(
209 const control_loops::IndexLoop::Goal *goal,
210 const control_loops::IndexLoop::Position *position,
211 control_loops::IndexLoop::Output *output,
212 control_loops::IndexLoop::Status *status);
213
214 private:
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800215 friend class testing::IndexTest_InvalidStateTest_Test;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800216 friend class testing::IndexTest_LostDisc_Test;
Austin Schuh93485832013-03-04 00:01:34 -0800217
218 // This class implements the CapU function correctly given all the extra
219 // information that we know about from the wrist motor.
220 class IndexStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
221 public:
222 IndexStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop)
223 : StateFeedbackLoop<2, 1, 1>(loop),
224 low_voltage_count_(0) {
225 }
226
227 // Voltage below which the indexer won't move with a disc in it.
228 static const double kMinMotionVoltage;
229 // Maximum number of cycles to apply a low voltage to the motor.
230 static const double kNoMotionCuttoffCount;
231
232 // Caps U, but disables the motor after a number of cycles of inactivity.
233 virtual void CapU();
234 private:
235 // Number of cycles that we have seen a small voltage being applied.
236 uint32_t low_voltage_count_;
237 };
238
Austin Schuhf8c52252013-03-03 02:25:49 -0800239 // Sets disc_position to the minimum or maximum disc position.
Austin Schuh1b864a12013-03-07 00:46:50 -0800240 // Sets found_disc to point to the frisbee that was found, and ignores it if
241 // found_disc is NULL.
Austin Schuhf8c52252013-03-03 02:25:49 -0800242 // Returns true if there were discs, and false if there weren't.
243 // On false, disc_position is left unmodified.
Austin Schuh1b864a12013-03-07 00:46:50 -0800244 bool MinDiscPosition(double *disc_position, Frisbee **found_disc);
245 bool MaxDiscPosition(double *disc_position, Frisbee **found_disc);
Austin Schuhd78ab542013-03-01 22:22:19 -0800246
247 // The state feedback control loop to talk to for the index.
Austin Schuh93485832013-03-04 00:01:34 -0800248 ::std::unique_ptr<IndexStateFeedbackLoop> wrist_loop_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800249
Austin Schuhd78ab542013-03-01 22:22:19 -0800250 // Count of the number of discs that we have collected.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800251 int32_t hopper_disc_count_;
252 int32_t total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700253 int32_t shot_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800254
Austin Schuhf8c52252013-03-03 02:25:49 -0800255 enum class Goal {
Austin Schuhd78ab542013-03-01 22:22:19 -0800256 // Hold position, in a low power state.
257 HOLD = 0,
258 // Get ready to load discs by shifting the discs down.
259 READY_LOWER = 1,
260 // Ready the discs, spin up the transfer roller, and accept discs.
261 INTAKE = 2,
262 // Get ready to shoot, and place a disc in the loader.
263 READY_SHOOTER = 3,
264 // Shoot at will.
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700265 SHOOT = 4,
266 // Reinitialize.
267 REINITIALIZE = 5
Austin Schuhd78ab542013-03-01 22:22:19 -0800268 };
269
Austin Schuhf8c52252013-03-03 02:25:49 -0800270 // These two enums command and track the loader loading discs into the
271 // shooter.
272 enum class LoaderState {
273 // Open and down, ready to accept a disc.
274 READY,
275 // Closing the grabber.
276 GRABBING,
277 // Grabber closed.
278 GRABBED,
279 // Lifting the disc.
280 LIFTING,
281 // Disc lifted.
282 LIFTED,
283 // Ejecting the disc into the shooter.
284 SHOOTING,
285 // The disc has been shot.
286 SHOOT,
287 // Lowering the loader back down.
288 LOWERING,
289 // The indexer is lowered.
290 LOWERED
291 };
292
293 // TODO(aschuh): If we are grabbed and asked to be ready, now what?
294 // LOG ?
295 enum class LoaderGoal {
296 // Get the loader ready to accept another disc.
297 READY,
298 // Grab a disc now.
299 GRAB,
300 // Lift it up, shoot, and reset.
301 // Waits to shoot until the shooter is stable.
302 // Resets the goal to READY once one disc has been shot.
303 SHOOT_AND_RESET
304 };
305
Austin Schuhd78ab542013-03-01 22:22:19 -0800306 // The current goal
307 Goal safe_goal_;
308
Austin Schuhf8c52252013-03-03 02:25:49 -0800309 // Loader goal, state, and counter.
310 LoaderGoal loader_goal_;
311 LoaderState loader_state_;
312 int loader_countdown_;
313
Austin Schuhd78ab542013-03-01 22:22:19 -0800314 // Current state of the pistons.
315 bool loader_up_;
316 bool disc_clamped_;
317 bool disc_ejected_;
318
Austin Schuhd78ab542013-03-01 22:22:19 -0800319 // The frisbee that is flying through the transfer rollers.
320 Frisbee transfer_frisbee_;
321
Austin Schuhf8c52252013-03-03 02:25:49 -0800322 // Bottom disc detect from the last valid packet for detecting edges.
Austin Schuhd78ab542013-03-01 22:22:19 -0800323 bool last_bottom_disc_detect_;
Austin Schuh825bde92013-03-06 00:16:46 -0800324 bool last_top_disc_detect_;
Austin Schuh6328daf2013-03-05 00:53:15 -0800325 int32_t last_bottom_disc_posedge_count_;
326 int32_t last_bottom_disc_negedge_count_;
327 int32_t last_bottom_disc_negedge_wait_count_;
Austin Schuh825bde92013-03-06 00:16:46 -0800328 int32_t last_top_disc_posedge_count_;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800329 int32_t last_top_disc_negedge_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800330
331 // Frisbees are in order such that the newest frisbee is on the front.
332 ::std::deque<Frisbee> frisbees_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800333
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800334 // True if we haven't seen a position before.
335 bool no_prior_position_;
336 // Number of position messages that we have missed in a row.
337 uint32_t missing_position_count_;
338
Austin Schuh723770b2013-03-10 13:26:20 -0700339 // The no-disc regions for both the bottom and top beam break sensors.
340 Region upper_open_region_;
341 Region lower_open_region_;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800342
Austin Schuhd78ab542013-03-01 22:22:19 -0800343 DISALLOW_COPY_AND_ASSIGN(IndexMotor);
344};
Austin Schuhd78ab542013-03-01 22:22:19 -0800345} // namespace control_loops
346} // namespace frc971
347
Brian Silverman43bb73e2013-03-17 13:39:47 -0700348#endif // FRC971_CONTROL_LOOPS_INDEX_INDEX_H_