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 | |
| 6 | #include "aos/common/control_loop/ControlLoop.h" |
| 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; |
| 120 | // Angles where we see a positive edge from the hall effect sensors. |
| 121 | double hall_effect_start_angle[kNumZeroSensors]; |
| 122 | }; |
| 123 | |
| 124 | // Current position data for the encoder and hall effect information. |
| 125 | struct PositionData { |
| 126 | // Current encoder position. |
| 127 | double position; |
| 128 | // Array of hall effect values. |
| 129 | bool hall_effects[kNumZeroSensors]; |
| 130 | // Array of the last positive edge position for the sensors. |
| 131 | double hall_effect_positions[kNumZeroSensors]; |
| 132 | }; |
| 133 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 134 | ZeroedJoint(StateFeedbackLoop<3, 1, 1> loop) |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 135 | : loop_(new ZeroedStateFeedbackLoop<kNumZeroSensors>(loop, this)), |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 136 | last_good_time_(0, 0), |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 137 | state_(UNINITIALIZED), |
| 138 | error_count_(0), |
| 139 | zero_offset_(0.0), |
| 140 | capped_goal_(false) { |
| 141 | } |
| 142 | |
| 143 | // Copies the provided configuration data locally. |
| 144 | void set_config_data(const ConfigurationData &config_data) { |
| 145 | config_data_ = config_data; |
| 146 | } |
| 147 | |
| 148 | // Clips the goal to be inside the limits and returns the clipped goal. |
| 149 | // Requires the constants to have already been fetched. |
| 150 | double ClipGoal(double goal) const { |
| 151 | return ::std::min(config_data_.upper_limit, |
| 152 | std::max(config_data_.lower_limit, goal)); |
| 153 | } |
| 154 | |
| 155 | // Updates the loop and state machine. |
| 156 | // position is null if the position data is stale, output_enabled is true if |
| 157 | // the output will actually go to the motors, and goal_angle and goal_velocity |
| 158 | // are the goal position and velocities. |
| 159 | double Update(const ZeroedJoint<kNumZeroSensors>::PositionData *position, |
| 160 | bool output_enabled, |
| 161 | double goal_angle, double goal_velocity); |
| 162 | |
| 163 | // True if the code is zeroing. |
| 164 | bool is_zeroing() const { return state_ == ZEROING; } |
| 165 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 166 | // True if the code is moving off the hall effect. |
| 167 | bool is_moving_off() const { return state_ == MOVING_OFF; } |
| 168 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 169 | // True if the state machine is uninitialized. |
| 170 | bool is_uninitialized() const { return state_ == UNINITIALIZED; } |
| 171 | |
| 172 | // True if the state machine is ready. |
| 173 | bool is_ready() const { return state_ == READY; } |
| 174 | |
| 175 | // Returns the uncapped voltage. |
| 176 | double U_uncapped() const { return loop_->U_uncapped(0, 0); } |
| 177 | |
| 178 | // True if the goal was moved to avoid goal windup. |
| 179 | bool capped_goal() const { return capped_goal_; } |
| 180 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 181 | // Timestamp |
| 182 | static const double dt; |
| 183 | |
Brian Silverman | 893ba07 | 2013-03-16 14:03:50 -0700 | [diff] [blame] | 184 | double absolute_position() const { return loop_->X_hat(0, 0); } |
| 185 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 186 | private: |
| 187 | friend class ZeroedStateFeedbackLoop<kNumZeroSensors>; |
| 188 | // Friend the wrist test cases so that they can simulate windeup. |
| 189 | friend class testing::WristTest_NoWindupPositive_Test; |
| 190 | friend class testing::WristTest_NoWindupNegative_Test; |
| 191 | |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 192 | static const ::aos::time::Time kRezeroTime; |
| 193 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 194 | // The state feedback control loop to talk to. |
| 195 | ::std::unique_ptr<ZeroedStateFeedbackLoop<kNumZeroSensors>> loop_; |
| 196 | |
| 197 | ConfigurationData config_data_; |
| 198 | |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 199 | ::aos::time::Time last_good_time_; |
| 200 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 201 | // Returns the index of the first active sensor, or -1 if none are active. |
| 202 | int ActiveSensorIndex( |
| 203 | const ZeroedJoint<kNumZeroSensors>::PositionData *position) { |
| 204 | if (!position) { |
| 205 | return -1; |
| 206 | } |
| 207 | int active_index = -1; |
| 208 | for (int i = 0; i < kNumZeroSensors; ++i) { |
| 209 | if (position->hall_effects[i]) { |
| 210 | if (active_index != -1) { |
| 211 | LOG(ERROR, "More than one hall effect sensor is active\n"); |
| 212 | } else { |
| 213 | active_index = i; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return active_index; |
| 218 | } |
| 219 | // Returns true if any of the sensors are active. |
| 220 | bool AnySensorsActive( |
| 221 | const ZeroedJoint<kNumZeroSensors>::PositionData *position) { |
| 222 | return ActiveSensorIndex(position) != -1; |
| 223 | } |
| 224 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 225 | // Enum to store the state of the internal zeroing state machine. |
| 226 | enum State { |
| 227 | UNINITIALIZED, |
| 228 | MOVING_OFF, |
| 229 | ZEROING, |
| 230 | READY, |
| 231 | ESTOP |
| 232 | }; |
| 233 | |
| 234 | // Internal state for zeroing. |
| 235 | State state_; |
| 236 | |
| 237 | // Missed position packet count. |
| 238 | int error_count_; |
| 239 | // Offset from the raw encoder value to the absolute angle. |
| 240 | double zero_offset_; |
| 241 | // Position that gets incremented when zeroing the wrist to slowly move it to |
| 242 | // the hall effect sensor. |
| 243 | double zeroing_position_; |
| 244 | // Last position at which the hall effect sensor was off. |
| 245 | double last_off_position_; |
| 246 | |
| 247 | // True if the zeroing goal was capped during this cycle. |
| 248 | bool capped_goal_; |
| 249 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 250 | // Returns true if number is between first and second inclusive. |
| 251 | bool is_between(double first, double second, double number) { |
| 252 | if ((number >= first || number >= second) && |
| 253 | (number <= first || number <= second)) { |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 259 | DISALLOW_COPY_AND_ASSIGN(ZeroedJoint); |
| 260 | }; |
| 261 | |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 262 | template <int kNumZeroSensors> |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 263 | const ::aos::time::Time ZeroedJoint<kNumZeroSensors>::kRezeroTime = |
| 264 | ::aos::time::Time::InSeconds(10); |
| 265 | |
| 266 | template <int kNumZeroSensors> |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 267 | /*static*/ const double ZeroedJoint<kNumZeroSensors>::dt = 0.01; |
| 268 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 269 | // Updates the zeroed joint controller and state machine. |
| 270 | template <int kNumZeroSensors> |
| 271 | double ZeroedJoint<kNumZeroSensors>::Update( |
| 272 | const ZeroedJoint<kNumZeroSensors>::PositionData *position, |
| 273 | bool output_enabled, |
| 274 | double goal_angle, double goal_velocity) { |
| 275 | // Uninitialize the bot if too many cycles pass without an encoder. |
| 276 | if (position == NULL) { |
| 277 | LOG(WARNING, "no new pos given\n"); |
| 278 | error_count_++; |
| 279 | } else { |
| 280 | error_count_ = 0; |
| 281 | } |
| 282 | if (error_count_ >= 4) { |
Austin Schuh | 261c405 | 2013-03-19 03:29:54 +0000 | [diff] [blame] | 283 | output_enabled = false; |
| 284 | LOG(WARNING, "err_count is %d so disabling\n", error_count_); |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 285 | } else if ((::aos::time::Time::Now() - last_good_time_) > kRezeroTime) { |
| 286 | LOG(WARNING, "err_count is %d so forcing a re-zero (or 1st time)\n", |
| 287 | error_count_); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 288 | state_ = UNINITIALIZED; |
| 289 | } |
Brian Silverman | ede2d78 | 2013-03-22 18:11:34 -0700 | [diff] [blame] | 290 | if (position) { |
| 291 | last_good_time_ = ::aos::time::Time::Now(); |
| 292 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 293 | |
| 294 | // Compute the absolute position of the wrist. |
| 295 | double absolute_position; |
| 296 | if (position) { |
| 297 | absolute_position = position->position; |
| 298 | if (state_ == READY) { |
| 299 | absolute_position -= zero_offset_; |
| 300 | } |
| 301 | loop_->Y << absolute_position; |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 302 | if (!AnySensorsActive(position)) { |
| 303 | last_off_position_ = position->position; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 304 | } |
| 305 | } else { |
| 306 | // Dead recon for now. |
| 307 | absolute_position = loop_->X_hat(0, 0); |
| 308 | } |
| 309 | |
| 310 | switch (state_) { |
| 311 | case UNINITIALIZED: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 312 | LOG(DEBUG, "UNINITIALIZED\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 313 | if (position) { |
| 314 | // Reset the zeroing goal. |
| 315 | zeroing_position_ = absolute_position; |
| 316 | // Clear the observer state. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 317 | loop_->X_hat << absolute_position, 0.0, 0.0; |
| 318 | loop_->ZeroPower(); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 319 | // Set the goal to here to make it so it doesn't move when disabled. |
| 320 | loop_->R = loop_->X_hat; |
| 321 | // Only progress if we are enabled. |
| 322 | if (::aos::robot_state->enabled) { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 323 | if (AnySensorsActive(position)) { |
| 324 | state_ = MOVING_OFF; |
| 325 | } else { |
| 326 | state_ = ZEROING; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | break; |
| 331 | case MOVING_OFF: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 332 | LOG(DEBUG, "MOVING_OFF\n"); |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 333 | { |
| 334 | // Move off the hall effect sensor. |
| 335 | if (!::aos::robot_state->enabled) { |
| 336 | // Start over if disabled. |
| 337 | state_ = UNINITIALIZED; |
| 338 | } else if (position && !AnySensorsActive(position)) { |
| 339 | // We are now off the sensor. Time to zero now. |
| 340 | state_ = ZEROING; |
| 341 | } else { |
| 342 | // Slowly creep off the sensor. |
Austin Schuh | dd3bc41 | 2013-03-16 17:02:40 -0700 | [diff] [blame] | 343 | zeroing_position_ -= config_data_.zeroing_off_speed * dt; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 344 | loop_->R << zeroing_position_, -config_data_.zeroing_off_speed, 0.0; |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 345 | break; |
| 346 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 347 | } |
| 348 | case ZEROING: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 349 | LOG(DEBUG, "ZEROING\n"); |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 350 | { |
| 351 | int active_sensor_index = ActiveSensorIndex(position); |
| 352 | if (!::aos::robot_state->enabled) { |
| 353 | // Start over if disabled. |
| 354 | state_ = UNINITIALIZED; |
| 355 | } else if (position && active_sensor_index != -1) { |
| 356 | state_ = READY; |
| 357 | // Verify that the calibration number is between the last off position |
| 358 | // and the current on position. If this is not true, move off and try |
| 359 | // again. |
| 360 | const double calibration = |
| 361 | position->hall_effect_positions[active_sensor_index]; |
| 362 | if (!is_between(last_off_position_, position->position, |
| 363 | calibration)) { |
| 364 | LOG(ERROR, "Got a bogus calibration number. Trying again.\n"); |
| 365 | LOG(ERROR, |
| 366 | "Last off position was %f, current is %f, calibration is %f\n", |
| 367 | last_off_position_, position->position, |
| 368 | position->hall_effect_positions[active_sensor_index]); |
| 369 | state_ = MOVING_OFF; |
| 370 | } else { |
| 371 | // Save the zero, and then offset the observer to deal with the |
| 372 | // phantom step change. |
| 373 | const double old_zero_offset = zero_offset_; |
| 374 | zero_offset_ = |
| 375 | position->hall_effect_positions[active_sensor_index] - |
| 376 | config_data_.hall_effect_start_angle[active_sensor_index]; |
| 377 | loop_->X_hat(0, 0) += old_zero_offset - zero_offset_; |
| 378 | loop_->Y(0, 0) += old_zero_offset - zero_offset_; |
| 379 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 380 | } else { |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 381 | // Slowly creep towards the sensor. |
| 382 | zeroing_position_ += config_data_.zeroing_speed * dt; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 383 | loop_->R << zeroing_position_, config_data_.zeroing_speed, 0.0; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 384 | } |
Austin Schuh | e20e93c | 2013-03-09 19:54:16 -0800 | [diff] [blame] | 385 | break; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 386 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 387 | |
| 388 | case READY: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 389 | LOG(DEBUG, "READY\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 390 | { |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 391 | const double limited_goal = ClipGoal(goal_angle); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 392 | loop_->R << limited_goal, goal_velocity, 0.0; |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | |
| 396 | case ESTOP: |
Austin Schuh | b5191b9 | 2013-03-10 18:22:24 -0700 | [diff] [blame] | 397 | LOG(DEBUG, "ESTOP\n"); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 398 | LOG(WARNING, "have already given up\n"); |
| 399 | return 0.0; |
| 400 | } |
| 401 | |
| 402 | // Update the observer. |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 403 | loop_->Update(position != NULL, !output_enabled); |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 404 | |
Brian Silverman | ae9d4b7 | 2013-03-16 20:11:29 -0700 | [diff] [blame] | 405 | LOG(DEBUG, "X_hat={%f, %f, %f}\n", |
| 406 | loop_->X_hat(0, 0), loop_->X_hat(1, 0), loop_->X_hat(2, 0)); |
| 407 | |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 408 | capped_goal_ = false; |
| 409 | // Verify that the zeroing goal hasn't run away. |
| 410 | switch (state_) { |
| 411 | case UNINITIALIZED: |
| 412 | case READY: |
| 413 | case ESTOP: |
| 414 | // Not zeroing. No worries. |
| 415 | break; |
| 416 | case MOVING_OFF: |
| 417 | case ZEROING: |
| 418 | // Check if we have cliped and adjust the goal. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 419 | if (loop_->uncapped_voltage() > config_data_.max_zeroing_voltage) { |
| 420 | double dx = (loop_->uncapped_voltage() - |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 421 | config_data_.max_zeroing_voltage) / loop_->K(0, 0); |
| 422 | zeroing_position_ -= dx; |
| 423 | capped_goal_ = true; |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 424 | } else if(loop_->uncapped_voltage() < -config_data_.max_zeroing_voltage) { |
| 425 | double dx = (loop_->uncapped_voltage() + |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 426 | config_data_.max_zeroing_voltage) / loop_->K(0, 0); |
| 427 | zeroing_position_ -= dx; |
| 428 | capped_goal_ = true; |
| 429 | } |
| 430 | break; |
| 431 | } |
Austin Schuh | 261c405 | 2013-03-19 03:29:54 +0000 | [diff] [blame] | 432 | if (output_enabled) { |
| 433 | return loop_->voltage(); |
| 434 | } else { |
| 435 | return 0.0; |
| 436 | } |
Austin Schuh | 844960d | 2013-03-09 17:07:51 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | } // namespace control_loops |
| 440 | } // namespace frc971 |
| 441 | |
| 442 | #endif // FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_ |