Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1 | #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 Schuh | 0055822 | 2013-03-03 14:16:16 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/index/index_motor.q.h" |
| 11 | #include "frc971/control_loops/index/index_motor_plant.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace control_loops { |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 15 | namespace testing { |
| 16 | class IndexTest_InvalidStateTest_Test; |
| 17 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 18 | |
| 19 | class IndexMotor |
| 20 | : public aos::control_loops::ControlLoop<control_loops::IndexLoop> { |
| 21 | public: |
| 22 | explicit IndexMotor( |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 23 | 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; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 56 | // Delay between the negedge of the disc detect and when it engages on the |
| 57 | // indexer. |
| 58 | static const double kBottomDiscIndexDelay; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 59 | |
| 60 | static const double kTopDiscDetectStart; |
| 61 | static const double kTopDiscDetectStop; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 62 | |
| 63 | // Converts the angle of the indexer to the angle of the disc. |
| 64 | static double ConvertIndexToDiscAngle(const double angle); |
| 65 | // Converts the angle of the indexer to the position that the center of the |
| 66 | // disc has traveled. |
| 67 | static double ConvertIndexToDiscPosition(const double angle); |
| 68 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 69 | // Converts the angle of the transfer roller to the position that the center |
| 70 | // of the disc has traveled. |
| 71 | static double ConvertTransferToDiscPosition(const double angle); |
| 72 | |
| 73 | // Converts the distance around the indexer to the position of |
| 74 | // the index roller. |
| 75 | static double ConvertDiscPositionToIndex(const double position); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 76 | // Converts the angle around the indexer to the position of the index roller. |
| 77 | static double ConvertDiscAngleToIndex(const double angle); |
| 78 | // Converts the angle around the indexer to the position of the disc in the |
| 79 | // indexer. |
| 80 | static double ConvertDiscAngleToDiscPosition(const double angle); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 81 | // Converts the distance around the indexer to the angle of the disc around |
| 82 | // the indexer. |
| 83 | static double ConvertDiscPositionToDiscAngle(const double position); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 84 | |
| 85 | // Disc radius in meters. |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 86 | static const double kDiscRadius; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 87 | // Roller radius in meters. |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 88 | static const double kRollerRadius; |
| 89 | // Transfer roller radius in meters. |
| 90 | static const double kTransferRollerRadius; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 91 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 92 | // Time that it takes to grab the disc in cycles. |
| 93 | static const int kGrabbingDelay; |
| 94 | // Time that it takes to lift the loader in cycles. |
| 95 | static const int kLiftingDelay; |
| 96 | // Time that it takes to shoot the disc in cycles. |
| 97 | static const int kShootingDelay; |
| 98 | // Time that it takes to lower the loader in cycles. |
| 99 | static const int kLoweringDelay; |
| 100 | |
| 101 | // Object representing a Frisbee tracked by the indexer. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 102 | class Frisbee { |
| 103 | public: |
| 104 | Frisbee() |
| 105 | : bottom_posedge_time_(0, 0), |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 106 | bottom_negedge_time_(0, 0) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 107 | Reset(); |
| 108 | } |
| 109 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 110 | // Resets a Frisbee so it can be reused. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 111 | void Reset() { |
| 112 | bottom_posedge_time_ = ::aos::time::Time(0, 0); |
| 113 | bottom_negedge_time_ = ::aos::time::Time(0, 0); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 114 | has_been_indexed_ = false; |
| 115 | index_start_position_ = 0.0; |
| 116 | } |
| 117 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 118 | // Returns true if the position is valid. |
| 119 | bool has_position() const { |
| 120 | return has_been_indexed_; |
| 121 | } |
| 122 | |
| 123 | // Returns the most up to date and accurate position that we have for the |
| 124 | // disc. This is the indexer position that the disc grabbed at. |
| 125 | double position() const { |
| 126 | return index_start_position_; |
| 127 | } |
| 128 | |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 129 | // Shifts the disc down the indexer by the provided offset. This is to |
| 130 | // handle when the cRIO reboots. |
| 131 | void OffsetDisc(double offset) { |
| 132 | index_start_position_ += offset; |
| 133 | } |
| 134 | |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame^] | 135 | // Potentially offsets the position with the knowledge that no discs are |
| 136 | // currently blocking the top sensor. This knowledge can be used to move |
| 137 | // this disc if it is believed to be blocking the top sensor. |
| 138 | void ObserveNoTopDiscSensor(double index_position, double index_velocity); |
| 139 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 140 | // Posedge and negedge disc times. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 141 | ::aos::time::Time bottom_posedge_time_; |
| 142 | ::aos::time::Time bottom_negedge_time_; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 143 | |
| 144 | // True if the disc has a valid index position. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 145 | bool has_been_indexed_; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 146 | // Location of the index when the disc first contacted it. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 147 | double index_start_position_; |
| 148 | }; |
| 149 | |
| 150 | protected: |
| 151 | virtual void RunIteration( |
| 152 | const control_loops::IndexLoop::Goal *goal, |
| 153 | const control_loops::IndexLoop::Position *position, |
| 154 | control_loops::IndexLoop::Output *output, |
| 155 | control_loops::IndexLoop::Status *status); |
| 156 | |
| 157 | private: |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 158 | friend class testing::IndexTest_InvalidStateTest_Test; |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 159 | |
| 160 | // This class implements the CapU function correctly given all the extra |
| 161 | // information that we know about from the wrist motor. |
| 162 | class IndexStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> { |
| 163 | public: |
| 164 | IndexStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop) |
| 165 | : StateFeedbackLoop<2, 1, 1>(loop), |
| 166 | low_voltage_count_(0) { |
| 167 | } |
| 168 | |
| 169 | // Voltage below which the indexer won't move with a disc in it. |
| 170 | static const double kMinMotionVoltage; |
| 171 | // Maximum number of cycles to apply a low voltage to the motor. |
| 172 | static const double kNoMotionCuttoffCount; |
| 173 | |
| 174 | // Caps U, but disables the motor after a number of cycles of inactivity. |
| 175 | virtual void CapU(); |
| 176 | private: |
| 177 | // Number of cycles that we have seen a small voltage being applied. |
| 178 | uint32_t low_voltage_count_; |
| 179 | }; |
| 180 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 181 | // Sets disc_position to the minimum or maximum disc position. |
| 182 | // Returns true if there were discs, and false if there weren't. |
| 183 | // On false, disc_position is left unmodified. |
| 184 | bool MinDiscPosition(double *disc_position); |
| 185 | bool MaxDiscPosition(double *disc_position); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 186 | |
| 187 | // The state feedback control loop to talk to for the index. |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 188 | ::std::unique_ptr<IndexStateFeedbackLoop> wrist_loop_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 189 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 190 | // Count of the number of discs that we have collected. |
| 191 | uint32_t hopper_disc_count_; |
| 192 | uint32_t total_disc_count_; |
| 193 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 194 | enum class Goal { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 195 | // Hold position, in a low power state. |
| 196 | HOLD = 0, |
| 197 | // Get ready to load discs by shifting the discs down. |
| 198 | READY_LOWER = 1, |
| 199 | // Ready the discs, spin up the transfer roller, and accept discs. |
| 200 | INTAKE = 2, |
| 201 | // Get ready to shoot, and place a disc in the loader. |
| 202 | READY_SHOOTER = 3, |
| 203 | // Shoot at will. |
| 204 | SHOOT = 4 |
| 205 | }; |
| 206 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 207 | // These two enums command and track the loader loading discs into the |
| 208 | // shooter. |
| 209 | enum class LoaderState { |
| 210 | // Open and down, ready to accept a disc. |
| 211 | READY, |
| 212 | // Closing the grabber. |
| 213 | GRABBING, |
| 214 | // Grabber closed. |
| 215 | GRABBED, |
| 216 | // Lifting the disc. |
| 217 | LIFTING, |
| 218 | // Disc lifted. |
| 219 | LIFTED, |
| 220 | // Ejecting the disc into the shooter. |
| 221 | SHOOTING, |
| 222 | // The disc has been shot. |
| 223 | SHOOT, |
| 224 | // Lowering the loader back down. |
| 225 | LOWERING, |
| 226 | // The indexer is lowered. |
| 227 | LOWERED |
| 228 | }; |
| 229 | |
| 230 | // TODO(aschuh): If we are grabbed and asked to be ready, now what? |
| 231 | // LOG ? |
| 232 | enum class LoaderGoal { |
| 233 | // Get the loader ready to accept another disc. |
| 234 | READY, |
| 235 | // Grab a disc now. |
| 236 | GRAB, |
| 237 | // Lift it up, shoot, and reset. |
| 238 | // Waits to shoot until the shooter is stable. |
| 239 | // Resets the goal to READY once one disc has been shot. |
| 240 | SHOOT_AND_RESET |
| 241 | }; |
| 242 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 243 | // The current goal |
| 244 | Goal safe_goal_; |
| 245 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 246 | // Loader goal, state, and counter. |
| 247 | LoaderGoal loader_goal_; |
| 248 | LoaderState loader_state_; |
| 249 | int loader_countdown_; |
| 250 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 251 | // Current state of the pistons. |
| 252 | bool loader_up_; |
| 253 | bool disc_clamped_; |
| 254 | bool disc_ejected_; |
| 255 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 256 | // The frisbee that is flying through the transfer rollers. |
| 257 | Frisbee transfer_frisbee_; |
| 258 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 259 | // Bottom disc detect from the last valid packet for detecting edges. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 260 | bool last_bottom_disc_detect_; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame^] | 261 | bool last_top_disc_detect_; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 262 | int32_t last_bottom_disc_posedge_count_; |
| 263 | int32_t last_bottom_disc_negedge_count_; |
| 264 | int32_t last_bottom_disc_negedge_wait_count_; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame^] | 265 | int32_t last_top_disc_posedge_count_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 266 | |
| 267 | // Frisbees are in order such that the newest frisbee is on the front. |
| 268 | ::std::deque<Frisbee> frisbees_; |
| 269 | // std::array ? |
| 270 | |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 271 | // True if we haven't seen a position before. |
| 272 | bool no_prior_position_; |
| 273 | // Number of position messages that we have missed in a row. |
| 274 | uint32_t missing_position_count_; |
| 275 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 276 | DISALLOW_COPY_AND_ASSIGN(IndexMotor); |
| 277 | }; |
| 278 | |
| 279 | } // namespace control_loops |
| 280 | } // namespace frc971 |
| 281 | |
| 282 | #endif // FRC971_CONTROL_LOOPS_WRIST_H_ |