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 | |
| 6 | #include "aos/common/control_loop/ControlLoop.h" |
| 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 |
| 29 | // information that we know about from the wrist motor. |
| 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: |
| 34 | ZeroedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> loop) |
| 35 | : StateFeedbackLoop<3, 1, 1>(loop), |
| 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 | |
| 45 | // Caps U, but this time respects the state of the wrist as well. |
| 46 | virtual void CapU(); |
| 47 | |
| 48 | // Returns the accumulated voltage. |
| 49 | double voltage() const { return voltage_; } |
| 50 | |
| 51 | // Returns the uncapped voltage. |
| 52 | double uncapped_voltage() const { return uncapped_voltage_; } |
| 53 | |
| 54 | // Zeros the accumulator. |
| 55 | void ZeroPower() { voltage_ = 0.0; } |
| 56 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 57 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 58 | // encoder value. |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 59 | void SetCalibration(double encoder_val, double known_position); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 60 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 61 | double offset() const { return offset_; } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 62 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 63 | double absolute_position() const { return X_hat(0, 0) + kPositionOffset; } |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 64 | double absolute_velocity() const { return X_hat(1, 0); } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 65 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 66 | void CorrectPosition(double position) { |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 67 | Eigen::Matrix<double, 1, 1> Y; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 68 | Y << position + offset_ - kPositionOffset; |
Brian Silverman | 2508c08 | 2014-02-17 15:45:02 -0800 | [diff] [blame] | 69 | LOG(DEBUG, "Setting position to %f\n", position); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 70 | Correct(Y); |
| 71 | } |
| 72 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame^] | 73 | // Recomputes the power goal for the current controller and position/velocity. |
| 74 | void RecalculatePowerGoal(); |
| 75 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 76 | double goal_position() const { return R(0, 0) + kPositionOffset; } |
| 77 | double goal_velocity() const { return R(1, 0); } |
| 78 | void InitializeState(double position) { |
| 79 | X_hat(0, 0) = position - kPositionOffset; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 82 | void SetGoalPosition(double desired_position, double desired_velocity) { |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 83 | 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] | 84 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 85 | R << desired_position - kPositionOffset, desired_velocity, |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 86 | (-A(1, 0) / A(1, 2) * (desired_position - kPositionOffset) - |
| 87 | A(1, 1) / A(1, 2) * desired_velocity); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 88 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 89 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 90 | double position() const { return X_hat(0, 0) - offset_ + kPositionOffset; } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 91 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame^] | 92 | void set_max_voltage(const double max_voltage) { max_voltage_ = max_voltage; } |
| 93 | bool capped_goal() const { return capped_goal_; } |
| 94 | |
| 95 | void CapGoal(); |
| 96 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 97 | private: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 98 | // The offset between what is '0' (0 rate on the spring) and the 0 (all the |
| 99 | // way cocked). |
| 100 | constexpr static double kPositionOffset = 0.305054 + 0.0254; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 101 | // The accumulated voltage to apply to the motor. |
| 102 | double voltage_; |
| 103 | double last_voltage_; |
| 104 | double uncapped_voltage_; |
| 105 | double offset_; |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame^] | 106 | double max_voltage_; |
| 107 | bool capped_goal_; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 110 | class ShooterMotor |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 111 | : public aos::control_loops::ControlLoop<control_loops::ShooterGroup> { |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 112 | public: |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 113 | explicit ShooterMotor(control_loops::ShooterGroup *my_shooter = |
| 114 | &control_loops::shooter_queue_group); |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 115 | |
| 116 | // True if the goal was moved to avoid goal windup. |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame^] | 117 | bool capped_goal() const { return shooter_.capped_goal(); } |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 118 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 119 | double PowerToPosition(double power); |
Ben Fredrickson | 4413f3b | 2014-02-16 23:25:36 +0000 | [diff] [blame] | 120 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 121 | typedef enum { |
| 122 | STATE_INITIALIZE = 0, |
| 123 | STATE_REQUEST_LOAD = 1, |
| 124 | STATE_LOAD_BACKTRACK = 2, |
| 125 | STATE_LOAD = 3, |
| 126 | STATE_LOADING_PROBLEM = 4, |
| 127 | STATE_PREPARE_SHOT = 5, |
| 128 | STATE_READY = 6, |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 129 | STATE_PREPARE_FIRE = 7, |
| 130 | STATE_FIRE = 8, |
| 131 | STATE_UNLOAD = 9, |
| 132 | STATE_UNLOAD_MOVE = 10, |
| 133 | STATE_READY_UNLOAD = 11, |
| 134 | STATE_ESTOP = 12 |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 135 | } State; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 136 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 137 | State state() { return state_; } |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 138 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 139 | protected: |
| 140 | virtual void RunIteration( |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 141 | const ShooterGroup::Goal *goal, |
| 142 | const control_loops::ShooterGroup::Position *position, |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 143 | ShooterGroup::Output *output, ShooterGroup::Status *status); |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | // Friend the test classes for acces to the internal state. |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame^] | 147 | friend class testing::ShooterTest_UnloadWindupPositive_Test; |
| 148 | friend class testing::ShooterTest_UnloadWindupNegative_Test; |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 149 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 150 | // Enter state STATE_UNLOAD |
| 151 | void Unload() { |
| 152 | state_ = STATE_UNLOAD; |
| 153 | unload_timeout_ = Time::Now() + Time::InSeconds(1); |
| 154 | } |
| 155 | // Enter state STATE_LOAD |
| 156 | void Load() { |
| 157 | state_ = STATE_LOAD; |
| 158 | load_timeout_ = Time::Now() + Time::InSeconds(1); |
| 159 | } |
| 160 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 161 | control_loops::ShooterGroup::Position last_position_; |
| 162 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 163 | ZeroedStateFeedbackLoop shooter_; |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 164 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 165 | // state machine state |
| 166 | State state_; |
| 167 | |
| 168 | // time to giving up on loading problem |
| 169 | Time loading_problem_end_time_; |
| 170 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 171 | // The end time when loading for it to timeout. |
| 172 | Time load_timeout_; |
| 173 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 174 | // wait for brake to set |
| 175 | Time shooter_brake_set_time_; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 176 | |
| 177 | // The timeout for unloading. |
| 178 | Time unload_timeout_; |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 179 | |
| 180 | // we are attempting to take up some of the backlash |
| 181 | // in the gears before the plunger hits |
| 182 | Time prepare_fire_end_time_; |
| 183 | |
| 184 | // time that shot must have completed |
| 185 | Time shot_end_time_; |
| 186 | |
| 187 | // track cycles that we are stuck to detect errors |
| 188 | int cycles_not_moved_; |
| 189 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 190 | double firing_starting_position_; |
| 191 | |
| 192 | // True if the latch should be engaged and the brake should be engaged. |
| 193 | bool latch_piston_; |
| 194 | bool brake_piston_; |
| 195 | int32_t last_distal_posedge_count_; |
| 196 | int32_t last_proximal_posedge_count_; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 197 | |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(ShooterMotor); |
| 199 | }; |
| 200 | |
| 201 | } // namespace control_loops |
| 202 | } // namespace frc971 |
| 203 | |
| 204 | #endif // FRC971_CONTROL_LOOPS_shooter_shooter_H_ |