joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_shooter_shooter_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_shooter_shooter_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 6 | #include "aos/common/controls/control_loop.h" |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/state_feedback_loop.h" |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 8 | #include "aos/common/time.h" |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 9 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 10 | #include "frc971/constants.h" |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/shooter/shooter_motor_plant.h" |
| 12 | #include "frc971/control_loops/shooter/shooter.q.h" |
| 13 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | namespace testing { |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 17 | class ShooterTest_UnloadWindupPositive_Test; |
| 18 | class ShooterTest_UnloadWindupNegative_Test; |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 19 | }; |
| 20 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 21 | using ::aos::time::Time; |
| 22 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 23 | // Note: Everything in this file assumes that there is a 1 cycle delay between |
| 24 | // power being requested and it showing up at the motor. It assumes that |
| 25 | // X_hat(2, 1) is the voltage being applied as well. It will go unstable if |
| 26 | // that isn't true. |
| 27 | |
| 28 | // This class implements the CapU function correctly given all the extra |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame^] | 29 | // information that we know about. |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 30 | // It does not have any zeroing logic in it, only logic to deal with a delta U |
| 31 | // controller. |
| 32 | class ZeroedStateFeedbackLoop : public StateFeedbackLoop<3, 1, 1> { |
| 33 | public: |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame^] | 34 | ZeroedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> &&loop) |
| 35 | : StateFeedbackLoop<3, 1, 1>(::std::move(loop)), |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 36 | voltage_(0.0), |
| 37 | last_voltage_(0.0), |
| 38 | uncapped_voltage_(0.0), |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 39 | offset_(0.0), |
| 40 | max_voltage_(12.0), |
| 41 | capped_goal_(false) {} |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 42 | |
| 43 | const static int kZeroingMaxVoltage = 5; |
| 44 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 45 | virtual void CapU(); |
| 46 | |
| 47 | // Returns the accumulated voltage. |
| 48 | double voltage() const { return voltage_; } |
| 49 | |
| 50 | // Returns the uncapped voltage. |
| 51 | double uncapped_voltage() const { return uncapped_voltage_; } |
| 52 | |
| 53 | // Zeros the accumulator. |
| 54 | void ZeroPower() { voltage_ = 0.0; } |
| 55 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 56 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 57 | // encoder value. |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 58 | void SetCalibration(double encoder_val, double known_position); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 59 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 60 | double offset() const { return offset_; } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 61 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 62 | double absolute_position() const { return X_hat(0, 0) + kPositionOffset; } |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 63 | double absolute_velocity() const { return X_hat(1, 0); } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 64 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 65 | void CorrectPosition(double position) { |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 66 | Eigen::Matrix<double, 1, 1> Y; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 67 | Y << position + offset_ - kPositionOffset; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 68 | Correct(Y); |
| 69 | } |
| 70 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 71 | // Recomputes the power goal for the current controller and position/velocity. |
| 72 | void RecalculatePowerGoal(); |
| 73 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 74 | double goal_position() const { return R(0, 0) + kPositionOffset; } |
| 75 | double goal_velocity() const { return R(1, 0); } |
| 76 | void InitializeState(double position) { |
| 77 | X_hat(0, 0) = position - kPositionOffset; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 80 | void SetGoalPosition(double desired_position, double desired_velocity) { |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 81 | LOG(DEBUG, "Goal position: %f Goal velocity: %f\n", desired_position, desired_velocity); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 82 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 83 | R << desired_position - kPositionOffset, desired_velocity, |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 84 | (-A(1, 0) / A(1, 2) * (desired_position - kPositionOffset) - |
| 85 | A(1, 1) / A(1, 2) * desired_velocity); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 86 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 87 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 88 | double position() const { return X_hat(0, 0) - offset_ + kPositionOffset; } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 89 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 90 | void set_max_voltage(const double max_voltage) { max_voltage_ = max_voltage; } |
| 91 | bool capped_goal() const { return capped_goal_; } |
| 92 | |
| 93 | void CapGoal(); |
| 94 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 95 | private: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 96 | // The offset between what is '0' (0 rate on the spring) and the 0 (all the |
| 97 | // way cocked). |
Austin Schuh | 2593385 | 2014-02-23 02:04:13 -0800 | [diff] [blame] | 98 | constexpr static double kPositionOffset = kMaxExtension; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 99 | // The accumulated voltage to apply to the motor. |
| 100 | double voltage_; |
| 101 | double last_voltage_; |
| 102 | double uncapped_voltage_; |
| 103 | double offset_; |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 104 | double max_voltage_; |
| 105 | bool capped_goal_; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 108 | const Time kUnloadTimeout = Time::InSeconds(10); |
Austin Schuh | 492331a | 2014-03-02 21:13:17 -0800 | [diff] [blame] | 109 | const Time kLoadTimeout = Time::InSeconds(2); |
Austin Schuh | 1a08bd8 | 2014-03-09 00:47:11 -0800 | [diff] [blame] | 110 | const Time kLoadProblemEndTimeout = Time::InSeconds(1.0); |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 111 | const Time kShooterBrakeSetTime = Time::InSeconds(0.05); |
Austin Schuh | ecdb025 | 2014-02-23 04:12:35 -0800 | [diff] [blame] | 112 | // Time to wait after releasing the latch piston before winching back again. |
| 113 | const Time kShotEndTimeout = Time::InSeconds(0.2); |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 114 | const Time kPrepareFireEndTime = Time::InMS(40); |
| 115 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 116 | class ShooterMotor |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 117 | : public aos::controls::ControlLoop<control_loops::ShooterGroup> { |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 118 | public: |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 119 | explicit ShooterMotor(control_loops::ShooterGroup *my_shooter = |
| 120 | &control_loops::shooter_queue_group); |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 121 | |
| 122 | // True if the goal was moved to avoid goal windup. |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 123 | bool capped_goal() const { return shooter_.capped_goal(); } |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 124 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 125 | double PowerToPosition(double power); |
James Kuszmaul | d29689f | 2014-03-02 13:06:54 -0800 | [diff] [blame] | 126 | double PositionToPower(double position); |
Ben Fredrickson | 4413f3b | 2014-02-16 23:25:36 +0000 | [diff] [blame] | 127 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 128 | typedef enum { |
| 129 | STATE_INITIALIZE = 0, |
| 130 | STATE_REQUEST_LOAD = 1, |
| 131 | STATE_LOAD_BACKTRACK = 2, |
| 132 | STATE_LOAD = 3, |
| 133 | STATE_LOADING_PROBLEM = 4, |
| 134 | STATE_PREPARE_SHOT = 5, |
| 135 | STATE_READY = 6, |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 136 | STATE_FIRE = 8, |
| 137 | STATE_UNLOAD = 9, |
| 138 | STATE_UNLOAD_MOVE = 10, |
| 139 | STATE_READY_UNLOAD = 11, |
| 140 | STATE_ESTOP = 12 |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 141 | } State; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 142 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 143 | State state() { return state_; } |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 144 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 145 | protected: |
| 146 | virtual void RunIteration( |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 147 | const ShooterGroup::Goal *goal, |
| 148 | const control_loops::ShooterGroup::Position *position, |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 149 | ShooterGroup::Output *output, ShooterGroup::Status *status); |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 150 | |
| 151 | private: |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 152 | // We have to override this to keep the pistons in the correct positions. |
| 153 | virtual void ZeroOutputs(); |
| 154 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 155 | // Friend the test classes for acces to the internal state. |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 156 | friend class testing::ShooterTest_UnloadWindupPositive_Test; |
| 157 | friend class testing::ShooterTest_UnloadWindupNegative_Test; |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 158 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 159 | // Enter state STATE_UNLOAD |
| 160 | void Unload() { |
| 161 | state_ = STATE_UNLOAD; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 162 | unload_timeout_ = Time::Now() + kUnloadTimeout; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 163 | } |
| 164 | // Enter state STATE_LOAD |
| 165 | void Load() { |
| 166 | state_ = STATE_LOAD; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 167 | load_timeout_ = Time::Now() + kLoadTimeout; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 170 | control_loops::ShooterGroup::Position last_position_; |
| 171 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 172 | ZeroedStateFeedbackLoop shooter_; |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 173 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 174 | // state machine state |
| 175 | State state_; |
| 176 | |
| 177 | // time to giving up on loading problem |
| 178 | Time loading_problem_end_time_; |
| 179 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 180 | // The end time when loading for it to timeout. |
| 181 | Time load_timeout_; |
| 182 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 183 | // wait for brake to set |
| 184 | Time shooter_brake_set_time_; |
James Kuszmaul | 7290a94 | 2014-02-26 20:04:13 -0800 | [diff] [blame] | 185 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 186 | // The timeout for unloading. |
| 187 | Time unload_timeout_; |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 188 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 189 | // time that shot must have completed |
| 190 | Time shot_end_time_; |
| 191 | |
| 192 | // track cycles that we are stuck to detect errors |
| 193 | int cycles_not_moved_; |
| 194 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 195 | double firing_starting_position_; |
| 196 | |
| 197 | // True if the latch should be engaged and the brake should be engaged. |
| 198 | bool latch_piston_; |
| 199 | bool brake_piston_; |
| 200 | int32_t last_distal_posedge_count_; |
| 201 | int32_t last_proximal_posedge_count_; |
Austin Schuh | 4edad87 | 2014-03-02 11:56:12 -0800 | [diff] [blame] | 202 | uint32_t shot_count_; |
Austin Schuh | 492331a | 2014-03-02 21:13:17 -0800 | [diff] [blame] | 203 | bool zeroed_; |
| 204 | int distal_posedge_validation_cycles_left_; |
| 205 | int proximal_posedge_validation_cycles_left_; |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 206 | bool last_distal_current_; |
| 207 | bool last_proximal_current_; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 208 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(ShooterMotor); |
| 210 | }; |
| 211 | |
| 212 | } // namespace control_loops |
| 213 | } // namespace frc971 |
| 214 | |
| 215 | #endif // FRC971_CONTROL_LOOPS_shooter_shooter_H_ |