blob: e581faa4746a6b75cff4a765b60738e5a7ffeff0 [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;
17}
Austin Schuhd78ab542013-03-01 22:22:19 -080018
19class IndexMotor
20 : public aos::control_loops::ControlLoop<control_loops::IndexLoop> {
21 public:
22 explicit IndexMotor(
Austin Schuhf8c52252013-03-03 02:25:49 -080023 control_loops::IndexLoop *my_index = &control_loops::index_loop);
24
25 static const double kTransferStartPosition;
26 static const double kIndexStartPosition;
27 // The distance from where the disc first grabs on the indexer to where it
28 // just bairly clears the loader.
29 static const double kIndexFreeLength;
30 // The distance to where the disc just starts to enter the loader.
31 static const double kLoaderFreeStopPosition;
32
33 // Distance that the grabber pulls the disc in by.
34 static const double kGrabberLength;
35 // Distance to where the grabber takes over.
36 static const double kGrabberStartPosition;
37
38 // The distance to where the disc hits the back of the loader and is ready to
39 // lift.
40 static const double kReadyToLiftPosition;
41
42 static const double kGrabberMovementVelocity;
43 // TODO(aschuh): This depends on the shooter angle...
44 // Distance to where the shooter is up and ready to shoot.
45 static const double kLifterStopPosition;
46 static const double kLifterMovementVelocity;
47
48 // Distance to where the disc has been launched.
49 // TODO(aschuh): This depends on the shooter angle...
50 static const double kEjectorStopPosition;
51 static const double kEjectorMovementVelocity;
52
53 // Start and stop position of the bottom disc detect sensor in meters.
54 static const double kBottomDiscDetectStart;
55 static const double kBottomDiscDetectStop;
56
57 static const double kTopDiscDetectStart;
58 static const double kTopDiscDetectStop;
Austin Schuhd78ab542013-03-01 22:22:19 -080059
60 // Converts the angle of the indexer to the angle of the disc.
61 static double ConvertIndexToDiscAngle(const double angle);
62 // Converts the angle of the indexer to the position that the center of the
63 // disc has traveled.
64 static double ConvertIndexToDiscPosition(const double angle);
65
Austin Schuhf8c52252013-03-03 02:25:49 -080066 // Converts the angle of the transfer roller to the position that the center
67 // of the disc has traveled.
68 static double ConvertTransferToDiscPosition(const double angle);
69
70 // Converts the distance around the indexer to the position of
71 // the index roller.
72 static double ConvertDiscPositionToIndex(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -080073 // Converts the angle around the indexer to the position of the index roller.
74 static double ConvertDiscAngleToIndex(const double angle);
75 // Converts the angle around the indexer to the position of the disc in the
76 // indexer.
77 static double ConvertDiscAngleToDiscPosition(const double angle);
Austin Schuhf8c52252013-03-03 02:25:49 -080078 // Converts the distance around the indexer to the angle of the disc around
79 // the indexer.
80 static double ConvertDiscPositionToDiscAngle(const double position);
Austin Schuhd78ab542013-03-01 22:22:19 -080081
82 // Disc radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -080083 static const double kDiscRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -080084 // Roller radius in meters.
Austin Schuhf8c52252013-03-03 02:25:49 -080085 static const double kRollerRadius;
86 // Transfer roller radius in meters.
87 static const double kTransferRollerRadius;
Austin Schuhd78ab542013-03-01 22:22:19 -080088
Austin Schuhf8c52252013-03-03 02:25:49 -080089 // Time that it takes to grab the disc in cycles.
90 static const int kGrabbingDelay;
91 // Time that it takes to lift the loader in cycles.
92 static const int kLiftingDelay;
93 // Time that it takes to shoot the disc in cycles.
94 static const int kShootingDelay;
95 // Time that it takes to lower the loader in cycles.
96 static const int kLoweringDelay;
97
98 // Object representing a Frisbee tracked by the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -080099 class Frisbee {
100 public:
101 Frisbee()
102 : bottom_posedge_time_(0, 0),
Austin Schuhf8c52252013-03-03 02:25:49 -0800103 bottom_negedge_time_(0, 0) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800104 Reset();
105 }
106
Austin Schuhf8c52252013-03-03 02:25:49 -0800107 // Resets a Frisbee so it can be reused.
Austin Schuhd78ab542013-03-01 22:22:19 -0800108 void Reset() {
109 bottom_posedge_time_ = ::aos::time::Time(0, 0);
110 bottom_negedge_time_ = ::aos::time::Time(0, 0);
Austin Schuhd78ab542013-03-01 22:22:19 -0800111 has_been_indexed_ = false;
112 index_start_position_ = 0.0;
113 }
114
Austin Schuhf8c52252013-03-03 02:25:49 -0800115 // Returns true if the position is valid.
116 bool has_position() const {
117 return has_been_indexed_;
118 }
119
120 // Returns the most up to date and accurate position that we have for the
121 // disc. This is the indexer position that the disc grabbed at.
122 double position() const {
123 return index_start_position_;
124 }
125
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800126 // Shifts the disc down the indexer by the provided offset. This is to
127 // handle when the cRIO reboots.
128 void OffsetDisc(double offset) {
129 index_start_position_ += offset;
130 }
131
Austin Schuhf8c52252013-03-03 02:25:49 -0800132 // Posedge and negedge disc times.
Austin Schuhd78ab542013-03-01 22:22:19 -0800133 ::aos::time::Time bottom_posedge_time_;
134 ::aos::time::Time bottom_negedge_time_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800135
136 // True if the disc has a valid index position.
Austin Schuhd78ab542013-03-01 22:22:19 -0800137 bool has_been_indexed_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800138 // Location of the index when the disc first contacted it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800139 double index_start_position_;
140 };
141
142 protected:
143 virtual void RunIteration(
144 const control_loops::IndexLoop::Goal *goal,
145 const control_loops::IndexLoop::Position *position,
146 control_loops::IndexLoop::Output *output,
147 control_loops::IndexLoop::Status *status);
148
149 private:
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800150 friend class testing::IndexTest_InvalidStateTest_Test;
Austin Schuh93485832013-03-04 00:01:34 -0800151
152 // This class implements the CapU function correctly given all the extra
153 // information that we know about from the wrist motor.
154 class IndexStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
155 public:
156 IndexStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop)
157 : StateFeedbackLoop<2, 1, 1>(loop),
158 low_voltage_count_(0) {
159 }
160
161 // Voltage below which the indexer won't move with a disc in it.
162 static const double kMinMotionVoltage;
163 // Maximum number of cycles to apply a low voltage to the motor.
164 static const double kNoMotionCuttoffCount;
165
166 // Caps U, but disables the motor after a number of cycles of inactivity.
167 virtual void CapU();
168 private:
169 // Number of cycles that we have seen a small voltage being applied.
170 uint32_t low_voltage_count_;
171 };
172
Austin Schuhf8c52252013-03-03 02:25:49 -0800173 // Sets disc_position to the minimum or maximum disc position.
174 // Returns true if there were discs, and false if there weren't.
175 // On false, disc_position is left unmodified.
176 bool MinDiscPosition(double *disc_position);
177 bool MaxDiscPosition(double *disc_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800178
179 // The state feedback control loop to talk to for the index.
Austin Schuh93485832013-03-04 00:01:34 -0800180 ::std::unique_ptr<IndexStateFeedbackLoop> wrist_loop_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800181
Austin Schuhd78ab542013-03-01 22:22:19 -0800182 // Count of the number of discs that we have collected.
183 uint32_t hopper_disc_count_;
184 uint32_t total_disc_count_;
185
Austin Schuhf8c52252013-03-03 02:25:49 -0800186 enum class Goal {
Austin Schuhd78ab542013-03-01 22:22:19 -0800187 // Hold position, in a low power state.
188 HOLD = 0,
189 // Get ready to load discs by shifting the discs down.
190 READY_LOWER = 1,
191 // Ready the discs, spin up the transfer roller, and accept discs.
192 INTAKE = 2,
193 // Get ready to shoot, and place a disc in the loader.
194 READY_SHOOTER = 3,
195 // Shoot at will.
196 SHOOT = 4
197 };
198
Austin Schuhf8c52252013-03-03 02:25:49 -0800199 // These two enums command and track the loader loading discs into the
200 // shooter.
201 enum class LoaderState {
202 // Open and down, ready to accept a disc.
203 READY,
204 // Closing the grabber.
205 GRABBING,
206 // Grabber closed.
207 GRABBED,
208 // Lifting the disc.
209 LIFTING,
210 // Disc lifted.
211 LIFTED,
212 // Ejecting the disc into the shooter.
213 SHOOTING,
214 // The disc has been shot.
215 SHOOT,
216 // Lowering the loader back down.
217 LOWERING,
218 // The indexer is lowered.
219 LOWERED
220 };
221
222 // TODO(aschuh): If we are grabbed and asked to be ready, now what?
223 // LOG ?
224 enum class LoaderGoal {
225 // Get the loader ready to accept another disc.
226 READY,
227 // Grab a disc now.
228 GRAB,
229 // Lift it up, shoot, and reset.
230 // Waits to shoot until the shooter is stable.
231 // Resets the goal to READY once one disc has been shot.
232 SHOOT_AND_RESET
233 };
234
Austin Schuhd78ab542013-03-01 22:22:19 -0800235 // The current goal
236 Goal safe_goal_;
237
Austin Schuhf8c52252013-03-03 02:25:49 -0800238 // Loader goal, state, and counter.
239 LoaderGoal loader_goal_;
240 LoaderState loader_state_;
241 int loader_countdown_;
242
Austin Schuhd78ab542013-03-01 22:22:19 -0800243 // Current state of the pistons.
244 bool loader_up_;
245 bool disc_clamped_;
246 bool disc_ejected_;
247
Austin Schuhd78ab542013-03-01 22:22:19 -0800248 // The frisbee that is flying through the transfer rollers.
249 Frisbee transfer_frisbee_;
250
Austin Schuhf8c52252013-03-03 02:25:49 -0800251 // Bottom disc detect from the last valid packet for detecting edges.
Austin Schuhd78ab542013-03-01 22:22:19 -0800252 bool last_bottom_disc_detect_;
253
254 // Frisbees are in order such that the newest frisbee is on the front.
255 ::std::deque<Frisbee> frisbees_;
256 // std::array ?
257
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800258 // True if we haven't seen a position before.
259 bool no_prior_position_;
260 // Number of position messages that we have missed in a row.
261 uint32_t missing_position_count_;
262
Austin Schuhd78ab542013-03-01 22:22:19 -0800263 DISALLOW_COPY_AND_ASSIGN(IndexMotor);
264};
265
266} // namespace control_loops
267} // namespace frc971
268
269#endif // FRC971_CONTROL_LOOPS_WRIST_H_