Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 1 | #ifndef Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_ |
| 2 | #define Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/common/controls/control_loop.h" |
| 7 | #include "frc971/control_loops/state_feedback_loop.h" |
Brian Silverman | 2b1957a | 2016-02-14 20:29:57 -0500 | [diff] [blame] | 8 | #include "frc971/control_loops/simple_capped_state_feedback_loop.h" |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 9 | #include "aos/common/util/trapezoid_profile.h" |
| 10 | |
| 11 | #include "frc971/zeroing/zeroing.h" |
| 12 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 13 | #include "y2016/control_loops/superstructure/integral_arm_plant.h" |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 14 | |
| 15 | namespace y2016 { |
| 16 | namespace control_loops { |
| 17 | namespace superstructure { |
| 18 | namespace testing { |
| 19 | class SuperstructureTest_DisabledGoalTest_Test; |
| 20 | } // namespace testing |
| 21 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 22 | class Intake { |
| 23 | public: |
| 24 | Intake(); |
| 25 | // Returns whether the estimators have been initialized and zeroed. |
| 26 | bool initialized() const { return initialized_; } |
| 27 | bool zeroed() const { return zeroed_; } |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 28 | // Returns whether an error has occured |
| 29 | bool error() const { return estimator_.error(); } |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 30 | |
| 31 | // Updates our estimator with the latest position. |
| 32 | void Correct(::frc971::PotAndIndexPosition position); |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 33 | // Runs the controller and profile generator for a cycle. |
| 34 | void Update(bool disabled); |
| 35 | // Sets the maximum voltage that will be commanded by the loop. |
| 36 | void set_max_voltage(double voltage); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 37 | |
| 38 | // Forces the current goal to the provided goal, bypassing the profiler. |
| 39 | void ForceGoal(double goal); |
| 40 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 41 | // this goal. |
| 42 | void set_unprofiled_goal(double unprofiled_goal); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 43 | // Limits our profiles to a max velocity and acceleration for proper motion. |
| 44 | void AdjustProfile(double max_angular_velocity, |
| 45 | double max_angular_acceleration); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 46 | |
| 47 | // Returns true if we have exceeded any hard limits. |
| 48 | bool CheckHardLimits(); |
| 49 | // Resets the internal state. |
| 50 | void Reset(); |
| 51 | |
| 52 | // Returns the current internal estimator state for logging. |
| 53 | ::frc971::EstimatorState IntakeEstimatorState(); |
| 54 | |
| 55 | // Returns the requested intake voltage. |
| 56 | double intake_voltage() const { return loop_->U(0, 0); } |
| 57 | |
| 58 | // Returns the current position. |
| 59 | double angle() const { return Y_(0, 0); } |
| 60 | |
Austin Schuh | be04115 | 2016-02-28 22:01:52 -0800 | [diff] [blame] | 61 | // Returns the controller error. |
| 62 | const StateFeedbackLoop<3, 1, 1> &controller() const { return *loop_; } |
| 63 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 64 | // Returns the filtered goal. |
| 65 | const Eigen::Matrix<double, 3, 1> &goal() const { return loop_->R(); } |
| 66 | double goal(int row, int col) const { return loop_->R(row, col); } |
| 67 | |
| 68 | // Returns the unprofiled goal. |
| 69 | const Eigen::Matrix<double, 3, 1> &unprofiled_goal() const { |
| 70 | return unprofiled_goal_; |
| 71 | } |
| 72 | double unprofiled_goal(int row, int col) const { |
| 73 | return unprofiled_goal_(row, col); |
| 74 | } |
| 75 | |
| 76 | // Returns the current state estimate. |
| 77 | const Eigen::Matrix<double, 3, 1> &X_hat() const { return loop_->X_hat(); } |
| 78 | double X_hat(int row, int col) const { return loop_->X_hat(row, col); } |
| 79 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 80 | // For testing: |
| 81 | // Triggers an estimator error. |
| 82 | void TriggerEstimatorError() { estimator_.TriggerError(); } |
| 83 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 84 | private: |
| 85 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 86 | // to aid debugging. |
| 87 | void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal); |
| 88 | |
| 89 | void UpdateIntakeOffset(double offset); |
| 90 | |
Brian Silverman | 2b1957a | 2016-02-14 20:29:57 -0500 | [diff] [blame] | 91 | ::std::unique_ptr< |
| 92 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>> loop_; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 93 | |
| 94 | ::frc971::zeroing::ZeroingEstimator estimator_; |
| 95 | aos::util::TrapezoidProfile profile_; |
| 96 | |
| 97 | // Current measurement. |
| 98 | Eigen::Matrix<double, 1, 1> Y_; |
| 99 | // Current offset. Y_ = offset_ + raw_sensor; |
| 100 | Eigen::Matrix<double, 1, 1> offset_; |
| 101 | |
| 102 | // The goal that the profile tries to reach. |
| 103 | Eigen::Matrix<double, 3, 1> unprofiled_goal_; |
| 104 | |
| 105 | bool initialized_ = false; |
| 106 | bool zeroed_ = false; |
| 107 | }; |
| 108 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 109 | class ArmControlLoop |
| 110 | : public ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2> { |
| 111 | public: |
| 112 | ArmControlLoop(SimpleCappedStateFeedbackLoop<6, 2, 2> &&loop) |
| 113 | : SimpleCappedStateFeedbackLoop<6, 2, 2>(::std::move(loop)) {} |
| 114 | |
| 115 | const Eigen::Matrix<double, 2, 1> ControllerOutput() override { |
| 116 | const Eigen::Matrix<double, 2, 1> accelerating_ff = |
| 117 | controller(0).Kff * (next_R() - controller(0).plant.A() * R()); |
| 118 | const Eigen::Matrix<double, 2, 1> accelerating_controller = |
| 119 | controller(0).K * error() + accelerating_ff; |
| 120 | |
| 121 | const Eigen::Matrix<double, 2, 1> decelerating_ff = |
| 122 | controller(1).Kff * (next_R() - controller(1).plant.A() * R()); |
| 123 | const Eigen::Matrix<double, 2, 1> decelerating_controller = |
| 124 | controller(1).K * error() + decelerating_ff; |
| 125 | |
| 126 | const double bemf_voltage = X_hat(1, 0) / kV_shoulder; |
| 127 | bool use_accelerating_controller = true; |
| 128 | LOG(DEBUG, "Accelerating at %f, decel %f, bemf %f\n", |
| 129 | accelerating_controller(0, 0), accelerating_controller(1, 0), |
| 130 | bemf_voltage); |
| 131 | if (IsAccelerating(bemf_voltage, accelerating_controller(0, 0))) { |
| 132 | use_accelerating_controller = true; |
| 133 | } else { |
| 134 | use_accelerating_controller = false; |
| 135 | } |
| 136 | if (use_accelerating_controller) { |
| 137 | ff_U_ = accelerating_ff; |
| 138 | set_controller_index(0); |
| 139 | return accelerating_controller; |
| 140 | } else { |
| 141 | set_controller_index(1); |
| 142 | ff_U_ = decelerating_ff; |
| 143 | return decelerating_controller; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
Austin Schuh | d7e2994 | 2016-03-12 21:35:11 -0800 | [diff] [blame^] | 148 | void CapU() override { |
| 149 | // U(0) |
| 150 | // U(1) = coupling * U(0) + ... |
| 151 | // So, when modifying U(0), remove the coupling. |
| 152 | if (U(0, 0) > max_voltage(0)) { |
| 153 | const double overage_amount = U(0, 0) - max_voltage(0); |
| 154 | mutable_U(0, 0) = max_voltage(0); |
| 155 | const double coupled_amount = |
| 156 | (Kff().block<1, 2>(1, 2) * B().block<2, 1>(2, 0))(0, 0) * overage_amount; |
| 157 | LOG(DEBUG, "Removing coupled amount %f\n", coupled_amount); |
| 158 | mutable_U(1, 0) += coupled_amount; |
| 159 | } |
| 160 | if (U(0, 0) < min_voltage(0)) { |
| 161 | const double under_amount = U(0, 0) - min_voltage(0); |
| 162 | mutable_U(0, 0) = min_voltage(0); |
| 163 | const double coupled_amount = |
| 164 | (Kff().block<1, 2>(1, 2) * B().block<2, 1>(2, 0))(0, 0) * |
| 165 | under_amount; |
| 166 | LOG(DEBUG, "Removing coupled amount %f\n", coupled_amount); |
| 167 | mutable_U(1, 0) += coupled_amount; |
| 168 | } |
| 169 | |
| 170 | // Uncapping U above isn't actually a problem with U for the shoulder. |
| 171 | // Reset any change. |
| 172 | mutable_U_uncapped(1, 0) = U(1, 0); |
| 173 | mutable_U(1, 0) = |
| 174 | ::std::min(max_voltage(1), ::std::max(min_voltage(1), U(1, 0))); |
| 175 | } |
| 176 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 177 | bool IsAccelerating(double bemf_voltage, double voltage) { |
| 178 | if (bemf_voltage > 0) { |
| 179 | return voltage > bemf_voltage; |
| 180 | } else { |
| 181 | return voltage < bemf_voltage; |
| 182 | } |
| 183 | } |
| 184 | }; |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 185 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 186 | class Arm { |
| 187 | public: |
| 188 | Arm(); |
| 189 | // Returns whether the estimators have been initialized and zeroed. |
| 190 | bool initialized() const { return initialized_; } |
| 191 | bool zeroed() const { return shoulder_zeroed_ && wrist_zeroed_; } |
| 192 | bool shoulder_zeroed() const { return shoulder_zeroed_; } |
| 193 | bool wrist_zeroed() const { return wrist_zeroed_; } |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 194 | // Returns whether an error has occured |
| 195 | bool error() const { |
| 196 | return shoulder_estimator_.error() || wrist_estimator_.error(); |
| 197 | } |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 198 | |
| 199 | // Updates our estimator with the latest position. |
| 200 | void Correct(::frc971::PotAndIndexPosition position_shoulder, |
| 201 | ::frc971::PotAndIndexPosition position_wrist); |
| 202 | |
| 203 | // Forces the goal to be the provided goal. |
| 204 | void ForceGoal(double unprofiled_goal_shoulder, double unprofiled_goal_wrist); |
| 205 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 206 | // this goal. |
| 207 | void set_unprofiled_goal(double unprofiled_goal_shoulder, |
| 208 | double unprofiled_goal_wrist); |
| 209 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 210 | int controller_index() const { return loop_->controller_index(); } |
| 211 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 212 | // Runs the controller and profile generator for a cycle. |
| 213 | void Update(bool disabled); |
| 214 | |
| 215 | // Limits our profiles to a max velocity and acceleration for proper motion. |
| 216 | void AdjustProfile(double max_angular_velocity_shoulder, |
| 217 | double max_angular_acceleration_shoulder, |
| 218 | double max_angular_velocity_wrist, |
| 219 | double max_angular_acceleration_wrist); |
| 220 | void set_max_voltage(double shoulder_max_voltage, double wrist_max_voltage); |
| 221 | |
Austin Schuh | 9f4e8a7 | 2016-02-16 15:28:47 -0800 | [diff] [blame] | 222 | void set_shoulder_asymetric_limits(double shoulder_min_voltage, |
| 223 | double shoulder_max_voltage) { |
| 224 | loop_->set_asymetric_voltage(0, shoulder_min_voltage, shoulder_max_voltage); |
| 225 | } |
| 226 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 227 | // Returns true if we have exceeded any hard limits. |
| 228 | bool CheckHardLimits(); |
| 229 | // Resets the internal state. |
| 230 | void Reset(); |
| 231 | |
| 232 | // Returns the current internal estimator state for logging. |
| 233 | ::frc971::EstimatorState ShoulderEstimatorState(); |
| 234 | ::frc971::EstimatorState WristEstimatorState(); |
| 235 | |
| 236 | // Returns the requested shoulder and wrist voltages. |
| 237 | double shoulder_voltage() const { return loop_->U(0, 0); } |
| 238 | double wrist_voltage() const { return loop_->U(1, 0); } |
| 239 | |
| 240 | // Returns the current positions. |
| 241 | double shoulder_angle() const { return Y_(0, 0); } |
| 242 | double wrist_angle() const { return Y_(1, 0) + Y_(0, 0); } |
| 243 | |
Austin Schuh | be04115 | 2016-02-28 22:01:52 -0800 | [diff] [blame] | 244 | // Returns the controller error. |
| 245 | const StateFeedbackLoop<6, 2, 2> &controller() const { return *loop_; } |
| 246 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 247 | // Returns the unprofiled goal. |
| 248 | const Eigen::Matrix<double, 6, 1> &unprofiled_goal() const { |
| 249 | return unprofiled_goal_; |
| 250 | } |
| 251 | double unprofiled_goal(int row, int col) const { |
| 252 | return unprofiled_goal_(row, col); |
| 253 | } |
| 254 | |
| 255 | // Returns the filtered goal. |
| 256 | const Eigen::Matrix<double, 6, 1> &goal() const { return loop_->R(); } |
| 257 | double goal(int row, int col) const { return loop_->R(row, col); } |
| 258 | |
| 259 | // Returns the current state estimate. |
| 260 | const Eigen::Matrix<double, 6, 1> &X_hat() const { return loop_->X_hat(); } |
| 261 | double X_hat(int row, int col) const { return loop_->X_hat(row, col); } |
| 262 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 263 | // For testing: |
| 264 | // Triggers an estimator error. |
| 265 | void TriggerEstimatorError() { shoulder_estimator_.TriggerError(); } |
| 266 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 267 | private: |
| 268 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 269 | // to aid debugging. |
| 270 | void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal); |
| 271 | |
| 272 | // Updates the offset |
| 273 | void UpdateWristOffset(double offset); |
| 274 | void UpdateShoulderOffset(double offset); |
| 275 | |
| 276 | friend class testing::SuperstructureTest_DisabledGoalTest_Test; |
Brian Silverman | 2b1957a | 2016-02-14 20:29:57 -0500 | [diff] [blame] | 277 | ::std::unique_ptr< |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 278 | ArmControlLoop> loop_; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 279 | |
| 280 | aos::util::TrapezoidProfile shoulder_profile_, wrist_profile_; |
| 281 | ::frc971::zeroing::ZeroingEstimator shoulder_estimator_, wrist_estimator_; |
| 282 | |
| 283 | // Current measurement. |
| 284 | Eigen::Matrix<double, 2, 1> Y_; |
| 285 | // Current offset. Y_ = offset_ + raw_sensor; |
| 286 | Eigen::Matrix<double, 2, 1> offset_; |
| 287 | |
| 288 | // The goal that the profile tries to reach. |
| 289 | Eigen::Matrix<double, 6, 1> unprofiled_goal_; |
| 290 | |
| 291 | bool initialized_ = false; |
| 292 | bool shoulder_zeroed_ = false; |
| 293 | bool wrist_zeroed_ = false; |
| 294 | }; |
| 295 | |
| 296 | } // namespace superstructure |
| 297 | } // namespace control_loops |
| 298 | } // namespace y2016 |
| 299 | |
| 300 | #endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_ |