Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_ZEROED_JOINT_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" |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 8 | |
| 9 | namespace frc971 { |
| 10 | namespace control_loops { |
| 11 | namespace testing { |
| 12 | class WristTest_NoWindupPositive_Test; |
| 13 | class WristTest_NoWindupNegative_Test; |
| 14 | }; |
| 15 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 16 | // Note: Everything in this file assumes that there is a 1 cycle delay between |
| 17 | // power being requested and it showing up at the motor. It assumes that |
| 18 | // X_hat(2, 1) is the voltage being applied as well. It will go unstable if |
| 19 | // that isn't true. |
| 20 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 21 | template<int kNumZeroSensors> |
| 22 | class ZeroedJoint; |
| 23 | |
| 24 | // This class implements the CapU function correctly given all the extra |
| 25 | // information that we know about from the wrist motor. |
| 26 | template<int kNumZeroSensors> |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 27 | class ZeroedStateFeedbackLoop : public StateFeedbackLoop<3, 1, 1> { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 28 | public: |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 29 | ZeroedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> loop, |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 30 | ZeroedJoint<kNumZeroSensors> *zeroed_joint) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 31 | : StateFeedbackLoop<3, 1, 1>(loop), |
| 32 | zeroed_joint_(zeroed_joint), |
| 33 | voltage_(0.0), |
| 34 | last_voltage_(0.0) { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Caps U, but this time respects the state of the wrist as well. |
| 38 | virtual void CapU(); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 39 | |
| 40 | // Returns the accumulated voltage. |
| 41 | double voltage() const { return voltage_; } |
| 42 | |
| 43 | // Returns the uncapped voltage. |
| 44 | double uncapped_voltage() const { return uncapped_voltage_; } |
| 45 | |
| 46 | // Zeros the accumulator. |
| 47 | void ZeroPower() { voltage_ = 0.0; } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 48 | private: |
| 49 | ZeroedJoint<kNumZeroSensors> *zeroed_joint_; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 50 | |
| 51 | // The accumulated voltage to apply to the motor. |
| 52 | double voltage_; |
| 53 | double last_voltage_; |
| 54 | double uncapped_voltage_; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | template<int kNumZeroSensors> |
| 58 | void ZeroedStateFeedbackLoop<kNumZeroSensors>::CapU() { |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 59 | const double old_voltage = voltage_; |
| 60 | voltage_ += U(0, 0); |
| 61 | |
| 62 | uncapped_voltage_ = voltage_; |
| 63 | |
| 64 | // Do all our computations with the voltage, and then compute what the delta |
| 65 | // is to make that happen. |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 66 | if (zeroed_joint_->state_ == ZeroedJoint<kNumZeroSensors>::READY) { |
| 67 | if (Y(0, 0) >= zeroed_joint_->config_data_.upper_limit) { |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 68 | voltage_ = std::min(0.0, voltage_); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 69 | } |
| 70 | if (Y(0, 0) <= zeroed_joint_->config_data_.lower_limit) { |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 71 | voltage_ = std::max(0.0, voltage_); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | const bool is_ready = |
| 76 | zeroed_joint_->state_ == ZeroedJoint<kNumZeroSensors>::READY; |
| 77 | double limit = is_ready ? |
| 78 | 12.0 : zeroed_joint_->config_data_.max_zeroing_voltage; |
| 79 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 80 | // Make sure that reality and the observer can't get too far off. There is a |
| 81 | // delay by one cycle between the applied voltage and X_hat(2, 0), so compare |
| 82 | // against last cycle's voltage. |
| 83 | if (X_hat(2, 0) > last_voltage_ + 2.0) { |
Brian Silverman | ae9d4b7 | 2013-03-16 20:11:29 -0700 | [diff] [blame] | 84 | //X_hat(2, 0) = last_voltage_ + 2.0; |
| 85 | voltage_ -= X_hat(2, 0) - (last_voltage_ + 2.0); |
| 86 | LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0)); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 87 | } else if (X_hat(2, 0) < last_voltage_ -2.0) { |
Brian Silverman | ae9d4b7 | 2013-03-16 20:11:29 -0700 | [diff] [blame] | 88 | //X_hat(2, 0) = last_voltage_ - 2.0; |
| 89 | voltage_ += X_hat(2, 0) - (last_voltage_ - 2.0); |
| 90 | LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0)); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Brian Silverman | ae9d4b7 | 2013-03-16 20:11:29 -0700 | [diff] [blame] | 93 | voltage_ = std::min(limit, voltage_); |
| 94 | voltage_ = std::max(-limit, voltage_); |
| 95 | U(0, 0) = voltage_ - old_voltage; |
| 96 | LOG(DEBUG, "abc %f\n", X_hat(2, 0) - voltage_); |
| 97 | LOG(DEBUG, "error %f\n", X_hat(0, 0) - R(0, 0)); |
| 98 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 99 | last_voltage_ = voltage_; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | // Class to zero and control a joint with any number of zeroing sensors with a |
| 104 | // state feedback controller. |
| 105 | template<int kNumZeroSensors> |
| 106 | class ZeroedJoint { |
| 107 | public: |
| 108 | // Sturcture to hold the hardware configuration information. |
| 109 | struct ConfigurationData { |
| 110 | // Angle at the lower hardware limit. |
| 111 | double lower_limit; |
| 112 | // Angle at the upper hardware limit. |
| 113 | double upper_limit; |
| 114 | // Speed (and direction) to move while zeroing. |
| 115 | double zeroing_speed; |
Austin Schuh | dd3bc41 | 2013-03-16 17:02:40 -0700 | [diff] [blame] | 116 | // Speed (and direction) to move while moving off the sensor. |
| 117 | double zeroing_off_speed; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 118 | // Maximum voltage to apply when zeroing. |
| 119 | double max_zeroing_voltage; |
Austin Schuh | 039d4f9 | 2013-04-04 05:52:03 +0000 | [diff] [blame] | 120 | // Deadband voltage. |
| 121 | double deadband_voltage; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 122 | // Angles where we see a positive edge from the hall effect sensors. |
| 123 | double hall_effect_start_angle[kNumZeroSensors]; |
| 124 | }; |
| 125 | |
| 126 | // Current position data for the encoder and hall effect information. |
| 127 | struct PositionData { |
| 128 | // Current encoder position. |
| 129 | double position; |
| 130 | // Array of hall effect values. |
| 131 | bool hall_effects[kNumZeroSensors]; |
| 132 | // Array of the last positive edge position for the sensors. |
| 133 | double hall_effect_positions[kNumZeroSensors]; |
| 134 | }; |
| 135 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 136 | ZeroedJoint(StateFeedbackLoop<3, 1, 1> loop) |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 137 | : loop_(new ZeroedStateFeedbackLoop<kNumZeroSensors>(loop, this)), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 138 | last_good_time_(::aos::monotonic_clock::min_time()), |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 139 | state_(UNINITIALIZED), |
| 140 | error_count_(0), |
| 141 | zero_offset_(0.0), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 142 | capped_goal_(false) {} |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 143 | |
| 144 | // Copies the provided configuration data locally. |
| 145 | void set_config_data(const ConfigurationData &config_data) { |
| 146 | config_data_ = config_data; |
| 147 | } |
| 148 | |
| 149 | // Clips the goal to be inside the limits and returns the clipped goal. |
| 150 | // Requires the constants to have already been fetched. |
| 151 | double ClipGoal(double goal) const { |
| 152 | return ::std::min(config_data_.upper_limit, |
| 153 | std::max(config_data_.lower_limit, goal)); |
| 154 | } |
| 155 | |
| 156 | // Updates the loop and state machine. |
| 157 | // position is null if the position data is stale, output_enabled is true if |
| 158 | // the output will actually go to the motors, and goal_angle and goal_velocity |
| 159 | // are the goal position and velocities. |
| 160 | double Update(const ZeroedJoint<kNumZeroSensors>::PositionData *position, |
| 161 | bool output_enabled, |
| 162 | double goal_angle, double goal_velocity); |
| 163 | |
| 164 | // True if the code is zeroing. |
| 165 | bool is_zeroing() const { return state_ == ZEROING; } |
| 166 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 167 | // True if the code is moving off the hall effect. |
| 168 | bool is_moving_off() const { return state_ == MOVING_OFF; } |
| 169 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 170 | // True if the state machine is uninitialized. |
| 171 | bool is_uninitialized() const { return state_ == UNINITIALIZED; } |
| 172 | |
| 173 | // True if the state machine is ready. |
| 174 | bool is_ready() const { return state_ == READY; } |
| 175 | |
| 176 | // Returns the uncapped voltage. |
| 177 | double U_uncapped() const { return loop_->U_uncapped(0, 0); } |
| 178 | |
| 179 | // True if the goal was moved to avoid goal windup. |
| 180 | bool capped_goal() const { return capped_goal_; } |
| 181 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 182 | // Timestamp |
| 183 | static const double dt; |
| 184 | |
Brian Silverman | 893ba07 | 2013-03-16 14:03:50 -0700 | [diff] [blame] | 185 | double absolute_position() const { return loop_->X_hat(0, 0); } |
| 186 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 187 | private: |
| 188 | friend class ZeroedStateFeedbackLoop<kNumZeroSensors>; |
| 189 | // Friend the wrist test cases so that they can simulate windeup. |
| 190 | friend class testing::WristTest_NoWindupPositive_Test; |
| 191 | friend class testing::WristTest_NoWindupNegative_Test; |
| 192 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 193 | static constexpr ::aos::monotonic_clock::duration kRezeroTime; |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 194 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 195 | // The state feedback control loop to talk to. |
| 196 | ::std::unique_ptr<ZeroedStateFeedbackLoop<kNumZeroSensors>> loop_; |
| 197 | |
| 198 | ConfigurationData config_data_; |
| 199 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 200 | ::aos::monotonic_clock::time_point last_good_time_; |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 201 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 202 | // Returns the index of the first active sensor, or -1 if none are active. |
| 203 | int ActiveSensorIndex( |
| 204 | const ZeroedJoint<kNumZeroSensors>::PositionData *position) { |
| 205 | if (!position) { |
| 206 | return -1; |
| 207 | } |
| 208 | int active_index = -1; |
| 209 | for (int i = 0; i < kNumZeroSensors; ++i) { |
| 210 | if (position->hall_effects[i]) { |
| 211 | if (active_index != -1) { |
| 212 | LOG(ERROR, "More than one hall effect sensor is active\n"); |
| 213 | } else { |
| 214 | active_index = i; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | return active_index; |
| 219 | } |
| 220 | // Returns true if any of the sensors are active. |
| 221 | bool AnySensorsActive( |
| 222 | const ZeroedJoint<kNumZeroSensors>::PositionData *position) { |
| 223 | return ActiveSensorIndex(position) != -1; |
| 224 | } |
| 225 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 226 | // Enum to store the state of the internal zeroing state machine. |
| 227 | enum State { |
| 228 | UNINITIALIZED, |
| 229 | MOVING_OFF, |
| 230 | ZEROING, |
| 231 | READY, |
| 232 | ESTOP |
| 233 | }; |
| 234 | |
| 235 | // Internal state for zeroing. |
| 236 | State state_; |
| 237 | |
| 238 | // Missed position packet count. |
| 239 | int error_count_; |
| 240 | // Offset from the raw encoder value to the absolute angle. |
| 241 | double zero_offset_; |
| 242 | // Position that gets incremented when zeroing the wrist to slowly move it to |
| 243 | // the hall effect sensor. |
| 244 | double zeroing_position_; |
| 245 | // Last position at which the hall effect sensor was off. |
| 246 | double last_off_position_; |
| 247 | |
| 248 | // True if the zeroing goal was capped during this cycle. |
| 249 | bool capped_goal_; |
| 250 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 251 | // Returns true if number is between first and second inclusive. |
| 252 | bool is_between(double first, double second, double number) { |
| 253 | if ((number >= first || number >= second) && |
| 254 | (number <= first || number <= second)) { |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 260 | DISALLOW_COPY_AND_ASSIGN(ZeroedJoint); |
| 261 | }; |
| 262 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 263 | template <int kNumZeroSensors> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 264 | constexpr ::aos::monotonic_clock::duration |
| 265 | ZeroedJoint<kNumZeroSensors>::kRezeroTime = ::std::chrono::seconds(2); |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 266 | |
| 267 | template <int kNumZeroSensors> |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 268 | /*static*/ const double ZeroedJoint<kNumZeroSensors>::dt = 0.01; |
| 269 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 270 | // Updates the zeroed joint controller and state machine. |
| 271 | template <int kNumZeroSensors> |
| 272 | double ZeroedJoint<kNumZeroSensors>::Update( |
| 273 | const ZeroedJoint<kNumZeroSensors>::PositionData *position, |
| 274 | bool output_enabled, |
| 275 | double goal_angle, double goal_velocity) { |
| 276 | // Uninitialize the bot if too many cycles pass without an encoder. |
| 277 | if (position == NULL) { |
| 278 | LOG(WARNING, "no new pos given\n"); |
| 279 | error_count_++; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 280 | } |
| 281 | if (error_count_ >= 4) { |
Austin Schuh | 261c405 | 2013-03-19 03:29:54 +0000 | [diff] [blame] | 282 | output_enabled = false; |
| 283 | LOG(WARNING, "err_count is %d so disabling\n", error_count_); |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 284 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 285 | if (::aos::monotonic_clock::now() > kRezeroTime + last_good_time_) { |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 286 | LOG(WARNING, "err_count is %d (or 1st time) so forcing a re-zero\n", |
| 287 | error_count_); |
| 288 | state_ = UNINITIALIZED; |
| 289 | loop_->Reset(); |
| 290 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 291 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 292 | if (position != NULL) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 293 | last_good_time_ = ::aos::monotonic_clock::now(); |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 294 | error_count_ = 0; |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 295 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 296 | |
| 297 | // Compute the absolute position of the wrist. |
| 298 | double absolute_position; |
| 299 | if (position) { |
| 300 | absolute_position = position->position; |
| 301 | if (state_ == READY) { |
| 302 | absolute_position -= zero_offset_; |
| 303 | } |
| 304 | loop_->Y << absolute_position; |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 305 | if (!AnySensorsActive(position)) { |
| 306 | last_off_position_ = position->position; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 307 | } |
| 308 | } else { |
| 309 | // Dead recon for now. |
| 310 | absolute_position = loop_->X_hat(0, 0); |
| 311 | } |
| 312 | |
| 313 | switch (state_) { |
| 314 | case UNINITIALIZED: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 315 | LOG(DEBUG, "UNINITIALIZED\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 316 | if (position) { |
| 317 | // Reset the zeroing goal. |
| 318 | zeroing_position_ = absolute_position; |
| 319 | // Clear the observer state. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 320 | loop_->X_hat << absolute_position, 0.0, 0.0; |
| 321 | loop_->ZeroPower(); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 322 | // Set the goal to here to make it so it doesn't move when disabled. |
| 323 | loop_->R = loop_->X_hat; |
| 324 | // Only progress if we are enabled. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 325 | if (::aos::joystick_state->enabled) { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 326 | if (AnySensorsActive(position)) { |
| 327 | state_ = MOVING_OFF; |
| 328 | } else { |
| 329 | state_ = ZEROING; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | break; |
| 334 | case MOVING_OFF: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 335 | LOG(DEBUG, "MOVING_OFF\n"); |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 336 | { |
| 337 | // Move off the hall effect sensor. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 338 | if (!::aos::joystick_state->enabled) { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 339 | // Start over if disabled. |
| 340 | state_ = UNINITIALIZED; |
| 341 | } else if (position && !AnySensorsActive(position)) { |
| 342 | // We are now off the sensor. Time to zero now. |
| 343 | state_ = ZEROING; |
| 344 | } else { |
| 345 | // Slowly creep off the sensor. |
Austin Schuh | dd3bc41 | 2013-03-16 17:02:40 -0700 | [diff] [blame] | 346 | zeroing_position_ -= config_data_.zeroing_off_speed * dt; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 347 | loop_->R << zeroing_position_, -config_data_.zeroing_off_speed, 0.0; |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 348 | break; |
| 349 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 350 | } |
| 351 | case ZEROING: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 352 | LOG(DEBUG, "ZEROING\n"); |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 353 | { |
| 354 | int active_sensor_index = ActiveSensorIndex(position); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 355 | if (!::aos::joystick_state->enabled) { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 356 | // Start over if disabled. |
| 357 | state_ = UNINITIALIZED; |
| 358 | } else if (position && active_sensor_index != -1) { |
| 359 | state_ = READY; |
| 360 | // Verify that the calibration number is between the last off position |
| 361 | // and the current on position. If this is not true, move off and try |
| 362 | // again. |
| 363 | const double calibration = |
| 364 | position->hall_effect_positions[active_sensor_index]; |
| 365 | if (!is_between(last_off_position_, position->position, |
| 366 | calibration)) { |
| 367 | LOG(ERROR, "Got a bogus calibration number. Trying again.\n"); |
| 368 | LOG(ERROR, |
| 369 | "Last off position was %f, current is %f, calibration is %f\n", |
| 370 | last_off_position_, position->position, |
| 371 | position->hall_effect_positions[active_sensor_index]); |
| 372 | state_ = MOVING_OFF; |
| 373 | } else { |
| 374 | // Save the zero, and then offset the observer to deal with the |
| 375 | // phantom step change. |
| 376 | const double old_zero_offset = zero_offset_; |
| 377 | zero_offset_ = |
| 378 | position->hall_effect_positions[active_sensor_index] - |
| 379 | config_data_.hall_effect_start_angle[active_sensor_index]; |
| 380 | loop_->X_hat(0, 0) += old_zero_offset - zero_offset_; |
| 381 | loop_->Y(0, 0) += old_zero_offset - zero_offset_; |
| 382 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 383 | } else { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 384 | // Slowly creep towards the sensor. |
| 385 | zeroing_position_ += config_data_.zeroing_speed * dt; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 386 | loop_->R << zeroing_position_, config_data_.zeroing_speed, 0.0; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 387 | } |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 388 | break; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 389 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 390 | |
| 391 | case READY: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 392 | LOG(DEBUG, "READY\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 393 | { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 394 | const double limited_goal = ClipGoal(goal_angle); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 395 | loop_->R << limited_goal, goal_velocity, 0.0; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | |
| 399 | case ESTOP: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 400 | LOG(DEBUG, "ESTOP\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 401 | LOG(WARNING, "have already given up\n"); |
| 402 | return 0.0; |
| 403 | } |
| 404 | |
| 405 | // Update the observer. |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 406 | loop_->Update(position != NULL, !output_enabled); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 407 | |
Brian Silverman | ae9d4b7 | 2013-03-16 20:11:29 -0700 | [diff] [blame] | 408 | LOG(DEBUG, "X_hat={%f, %f, %f}\n", |
| 409 | loop_->X_hat(0, 0), loop_->X_hat(1, 0), loop_->X_hat(2, 0)); |
| 410 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 411 | capped_goal_ = false; |
| 412 | // Verify that the zeroing goal hasn't run away. |
| 413 | switch (state_) { |
| 414 | case UNINITIALIZED: |
| 415 | case READY: |
| 416 | case ESTOP: |
| 417 | // Not zeroing. No worries. |
| 418 | break; |
| 419 | case MOVING_OFF: |
| 420 | case ZEROING: |
| 421 | // Check if we have cliped and adjust the goal. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 422 | if (loop_->uncapped_voltage() > config_data_.max_zeroing_voltage) { |
| 423 | double dx = (loop_->uncapped_voltage() - |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 424 | config_data_.max_zeroing_voltage) / loop_->K(0, 0); |
| 425 | zeroing_position_ -= dx; |
| 426 | capped_goal_ = true; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 427 | } else if(loop_->uncapped_voltage() < -config_data_.max_zeroing_voltage) { |
| 428 | double dx = (loop_->uncapped_voltage() + |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 429 | config_data_.max_zeroing_voltage) / loop_->K(0, 0); |
| 430 | zeroing_position_ -= dx; |
| 431 | capped_goal_ = true; |
| 432 | } |
| 433 | break; |
| 434 | } |
Austin Schuh | 261c405 | 2013-03-19 03:29:54 +0000 | [diff] [blame] | 435 | if (output_enabled) { |
Austin Schuh | 039d4f9 | 2013-04-04 05:52:03 +0000 | [diff] [blame] | 436 | double voltage = loop_->voltage(); |
| 437 | if (voltage > 0) { |
| 438 | voltage += config_data_.deadband_voltage; |
| 439 | } else if (voltage < 0) { |
| 440 | voltage -= config_data_.deadband_voltage; |
| 441 | } |
| 442 | if (voltage > 12.0) { |
| 443 | voltage = 12.0; |
| 444 | } else if (voltage < -12.0) { |
| 445 | voltage = -12.0; |
| 446 | } |
| 447 | return voltage; |
Austin Schuh | 261c405 | 2013-03-19 03:29:54 +0000 | [diff] [blame] | 448 | } else { |
| 449 | return 0.0; |
| 450 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | } // namespace control_loops |
| 454 | } // namespace frc971 |
| 455 | |
| 456 | #endif // FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_ |