blob: 7a9f6ac5f459a1292db53bfdf6e073a9e4d274e6 [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
4#include <memory>
5#include <deque>
6
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 Schuh1b864a12013-03-07 00:46:50 -080017class IndexTest_ShiftedDiscsAreRefound_Test;
Austin Schuhbcdb90c2013-03-03 23:24:58 -080018}
Austin Schuhd78ab542013-03-01 22:22:19 -080019
20class IndexMotor
21 : public aos::control_loops::ControlLoop<control_loops::IndexLoop> {
22 public:
23 explicit IndexMotor(
Austin Schuhf8c52252013-03-03 02:25:49 -080024 control_loops::IndexLoop *my_index = &control_loops::index_loop);
25
26 static const double kTransferStartPosition;
27 static const double kIndexStartPosition;
28 // The distance from where the disc first grabs on the indexer to where it
29 // just bairly clears the loader.
30 static const double kIndexFreeLength;
31 // The distance to where the disc just starts to enter the loader.
32 static const double kLoaderFreeStopPosition;
Austin Schuh1b864a12013-03-07 00:46:50 -080033 // The distance to where the next disc gets positioned while the current disc
34 // is shooting.
35 static const double kReadyToPreload;
Austin Schuhf8c52252013-03-03 02:25:49 -080036
37 // Distance that the grabber pulls the disc in by.
38 static const double kGrabberLength;
39 // Distance to where the grabber takes over.
40 static const double kGrabberStartPosition;
41
42 // The distance to where the disc hits the back of the loader and is ready to
43 // lift.
44 static const double kReadyToLiftPosition;
45
46 static const double kGrabberMovementVelocity;
47 // TODO(aschuh): This depends on the shooter angle...
48 // Distance to where the shooter is up and ready to shoot.
49 static const double kLifterStopPosition;
50 static const double kLifterMovementVelocity;
51
52 // Distance to where the disc has been launched.
53 // TODO(aschuh): This depends on the shooter angle...
54 static const double kEjectorStopPosition;
55 static const double kEjectorMovementVelocity;
56
57 // Start and stop position of the bottom disc detect sensor in meters.
58 static const double kBottomDiscDetectStart;
59 static const double kBottomDiscDetectStop;
Austin Schuh6328daf2013-03-05 00:53:15 -080060 // Delay between the negedge of the disc detect and when it engages on the
61 // indexer.
62 static const double kBottomDiscIndexDelay;
Austin Schuhf8c52252013-03-03 02:25:49 -080063
64 static const double kTopDiscDetectStart;
65 static const double kTopDiscDetectStop;
Austin Schuhd78ab542013-03-01 22:22:19 -080066
67 // Converts the angle of the indexer to the angle of the disc.
68 static double ConvertIndexToDiscAngle(const double angle);
69 // Converts the angle of the indexer to the position that the center of the
70 // disc has traveled.
71 static double ConvertIndexToDiscPosition(const double angle);
72
Austin Schuhf8c52252013-03-03 02:25:49 -080073 // Converts the angle of the transfer roller to the position that the center
74 // of the disc has traveled.
75 static double ConvertTransferToDiscPosition(const double angle);
76
77 // Converts the distance around the indexer to the position of
78 // the index roller.
79 static double ConvertDiscPositionToIndex(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -080080 // Converts the angle around the indexer to the position of the index roller.
81 static double ConvertDiscAngleToIndex(const double angle);
82 // Converts the angle around the indexer to the position of the disc in the
83 // indexer.
84 static double ConvertDiscAngleToDiscPosition(const double angle);
Austin Schuhf8c52252013-03-03 02:25:49 -080085 // Converts the distance around the indexer to the angle of the disc around
86 // the indexer.
87 static double ConvertDiscPositionToDiscAngle(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -080088
89 // Disc radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -080090 static const double kDiscRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -080091 // Roller radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -080092 static const double kRollerRadius;
93 // Transfer roller radius in meters.
94 static const double kTransferRollerRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -080095
Austin Schuhf8c52252013-03-03 02:25:49 -080096 // Time that it takes to grab the disc in cycles.
97 static const int kGrabbingDelay;
98 // Time that it takes to lift the loader in cycles.
99 static const int kLiftingDelay;
100 // Time that it takes to shoot the disc in cycles.
101 static const int kShootingDelay;
102 // Time that it takes to lower the loader in cycles.
103 static const int kLoweringDelay;
104
105 // Object representing a Frisbee tracked by the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800106 class Frisbee {
107 public:
108 Frisbee()
109 : bottom_posedge_time_(0, 0),
Austin Schuhf8c52252013-03-03 02:25:49 -0800110 bottom_negedge_time_(0, 0) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800111 Reset();
112 }
113
Austin Schuhf8c52252013-03-03 02:25:49 -0800114 // Resets a Frisbee so it can be reused.
Austin Schuhd78ab542013-03-01 22:22:19 -0800115 void Reset() {
116 bottom_posedge_time_ = ::aos::time::Time(0, 0);
117 bottom_negedge_time_ = ::aos::time::Time(0, 0);
Austin Schuhd78ab542013-03-01 22:22:19 -0800118 has_been_indexed_ = false;
119 index_start_position_ = 0.0;
120 }
121
Austin Schuhf8c52252013-03-03 02:25:49 -0800122 // Returns true if the position is valid.
123 bool has_position() const {
124 return has_been_indexed_;
125 }
126
127 // Returns the most up to date and accurate position that we have for the
128 // disc. This is the indexer position that the disc grabbed at.
129 double position() const {
130 return index_start_position_;
131 }
132
Austin Schuh1b864a12013-03-07 00:46:50 -0800133 // Returns the absolute position of the disc in meters in the hopper given
134 // that the indexer is at the provided position.
135 double absolute_position(const double index_position) const {
136 return IndexMotor::ConvertIndexToDiscPosition(
137 index_position - index_start_position_) +
138 IndexMotor::kIndexStartPosition;
139 }
140
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800141 // Shifts the disc down the indexer by the provided offset. This is to
142 // handle when the cRIO reboots.
143 void OffsetDisc(double offset) {
144 index_start_position_ += offset;
145 }
146
Austin Schuh825bde92013-03-06 00:16:46 -0800147 // Potentially offsets the position with the knowledge that no discs are
148 // currently blocking the top sensor. This knowledge can be used to move
149 // this disc if it is believed to be blocking the top sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800150 // Returns the amount that the disc moved due to this observation.
151 double ObserveNoTopDiscSensor(double index_position, double index_velocity);
Austin Schuh825bde92013-03-06 00:16:46 -0800152
Austin Schuhf8c52252013-03-03 02:25:49 -0800153 // Posedge and negedge disc times.
Austin Schuhd78ab542013-03-01 22:22:19 -0800154 ::aos::time::Time bottom_posedge_time_;
155 ::aos::time::Time bottom_negedge_time_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800156
157 // True if the disc has a valid index position.
Austin Schuhd78ab542013-03-01 22:22:19 -0800158 bool has_been_indexed_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800159 // Location of the index when the disc first contacted it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800160 double index_start_position_;
161 };
162
Austin Schuh1b864a12013-03-07 00:46:50 -0800163 const ::std::deque<Frisbee> &frisbees() const { return frisbees_; }
164
Austin Schuhd78ab542013-03-01 22:22:19 -0800165 protected:
166 virtual void RunIteration(
167 const control_loops::IndexLoop::Goal *goal,
168 const control_loops::IndexLoop::Position *position,
169 control_loops::IndexLoop::Output *output,
170 control_loops::IndexLoop::Status *status);
171
172 private:
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800173 friend class testing::IndexTest_InvalidStateTest_Test;
Austin Schuh1b864a12013-03-07 00:46:50 -0800174 friend class testing::IndexTest_ShiftedDiscsAreRefound_Test;
Austin Schuh93485832013-03-04 00:01:34 -0800175
176 // This class implements the CapU function correctly given all the extra
177 // information that we know about from the wrist motor.
178 class IndexStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
179 public:
180 IndexStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop)
181 : StateFeedbackLoop<2, 1, 1>(loop),
182 low_voltage_count_(0) {
183 }
184
185 // Voltage below which the indexer won't move with a disc in it.
186 static const double kMinMotionVoltage;
187 // Maximum number of cycles to apply a low voltage to the motor.
188 static const double kNoMotionCuttoffCount;
189
190 // Caps U, but disables the motor after a number of cycles of inactivity.
191 virtual void CapU();
192 private:
193 // Number of cycles that we have seen a small voltage being applied.
194 uint32_t low_voltage_count_;
195 };
196
Austin Schuhf8c52252013-03-03 02:25:49 -0800197 // Sets disc_position to the minimum or maximum disc position.
Austin Schuh1b864a12013-03-07 00:46:50 -0800198 // Sets found_disc to point to the frisbee that was found, and ignores it if
199 // found_disc is NULL.
Austin Schuhf8c52252013-03-03 02:25:49 -0800200 // Returns true if there were discs, and false if there weren't.
201 // On false, disc_position is left unmodified.
Austin Schuh1b864a12013-03-07 00:46:50 -0800202 bool MinDiscPosition(double *disc_position, Frisbee **found_disc);
203 bool MaxDiscPosition(double *disc_position, Frisbee **found_disc);
Austin Schuhd78ab542013-03-01 22:22:19 -0800204
205 // The state feedback control loop to talk to for the index.
Austin Schuh93485832013-03-04 00:01:34 -0800206 ::std::unique_ptr<IndexStateFeedbackLoop> wrist_loop_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800207
Austin Schuhd78ab542013-03-01 22:22:19 -0800208 // Count of the number of discs that we have collected.
209 uint32_t hopper_disc_count_;
210 uint32_t total_disc_count_;
211
Austin Schuhf8c52252013-03-03 02:25:49 -0800212 enum class Goal {
Austin Schuhd78ab542013-03-01 22:22:19 -0800213 // Hold position, in a low power state.
214 HOLD = 0,
215 // Get ready to load discs by shifting the discs down.
216 READY_LOWER = 1,
217 // Ready the discs, spin up the transfer roller, and accept discs.
218 INTAKE = 2,
219 // Get ready to shoot, and place a disc in the loader.
220 READY_SHOOTER = 3,
221 // Shoot at will.
222 SHOOT = 4
223 };
224
Austin Schuhf8c52252013-03-03 02:25:49 -0800225 // These two enums command and track the loader loading discs into the
226 // shooter.
227 enum class LoaderState {
228 // Open and down, ready to accept a disc.
229 READY,
230 // Closing the grabber.
231 GRABBING,
232 // Grabber closed.
233 GRABBED,
234 // Lifting the disc.
235 LIFTING,
236 // Disc lifted.
237 LIFTED,
238 // Ejecting the disc into the shooter.
239 SHOOTING,
240 // The disc has been shot.
241 SHOOT,
242 // Lowering the loader back down.
243 LOWERING,
244 // The indexer is lowered.
245 LOWERED
246 };
247
248 // TODO(aschuh): If we are grabbed and asked to be ready, now what?
249 // LOG ?
250 enum class LoaderGoal {
251 // Get the loader ready to accept another disc.
252 READY,
253 // Grab a disc now.
254 GRAB,
255 // Lift it up, shoot, and reset.
256 // Waits to shoot until the shooter is stable.
257 // Resets the goal to READY once one disc has been shot.
258 SHOOT_AND_RESET
259 };
260
Austin Schuhd78ab542013-03-01 22:22:19 -0800261 // The current goal
262 Goal safe_goal_;
263
Austin Schuhf8c52252013-03-03 02:25:49 -0800264 // Loader goal, state, and counter.
265 LoaderGoal loader_goal_;
266 LoaderState loader_state_;
267 int loader_countdown_;
268
Austin Schuhd78ab542013-03-01 22:22:19 -0800269 // Current state of the pistons.
270 bool loader_up_;
271 bool disc_clamped_;
272 bool disc_ejected_;
273
Austin Schuhd78ab542013-03-01 22:22:19 -0800274 // The frisbee that is flying through the transfer rollers.
275 Frisbee transfer_frisbee_;
276
Austin Schuhf8c52252013-03-03 02:25:49 -0800277 // Bottom disc detect from the last valid packet for detecting edges.
Austin Schuhd78ab542013-03-01 22:22:19 -0800278 bool last_bottom_disc_detect_;
Austin Schuh825bde92013-03-06 00:16:46 -0800279 bool last_top_disc_detect_;
Austin Schuh6328daf2013-03-05 00:53:15 -0800280 int32_t last_bottom_disc_posedge_count_;
281 int32_t last_bottom_disc_negedge_count_;
282 int32_t last_bottom_disc_negedge_wait_count_;
Austin Schuh825bde92013-03-06 00:16:46 -0800283 int32_t last_top_disc_posedge_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800284
285 // Frisbees are in order such that the newest frisbee is on the front.
286 ::std::deque<Frisbee> frisbees_;
287 // std::array ?
288
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800289 // True if we haven't seen a position before.
290 bool no_prior_position_;
291 // Number of position messages that we have missed in a row.
292 uint32_t missing_position_count_;
293
Austin Schuhd78ab542013-03-01 22:22:19 -0800294 DISALLOW_COPY_AND_ASSIGN(IndexMotor);
295};
296
297} // namespace control_loops
298} // namespace frc971
299
300#endif // FRC971_CONTROL_LOOPS_WRIST_H_