Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/claw/claw.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | |
| 7 | #include "aos/common/control_loop/control_loops.q.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | |
| 10 | #include "frc971/constants.h" |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/claw/claw_motor_plant.h" |
| 12 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 13 | // Zeroing plan. |
| 14 | // There are 2 types of zeros. Enabled and disabled ones. |
| 15 | // Disabled ones are only valid during auto mode, and can be used to speed up |
| 16 | // the enabled zero process. We need to re-zero during teleop in case the auto |
| 17 | // zero was poor and causes us to miss all our shots. |
| 18 | // |
| 19 | // We need to be able to zero manually while disabled by moving the joint over |
| 20 | // the zeros. |
| 21 | // Zero on the down edge when disabled (gravity in the direction of motion) |
| 22 | // |
| 23 | // When enabled, zero on the up edge (gravity opposing the direction of motion) |
| 24 | // The enabled sequence needs to work as follows. We can crash the claw if we |
| 25 | // bring them too close to each other or too far from each other. The only safe |
| 26 | // thing to do is to move them in unison. |
| 27 | // |
| 28 | // Start by moving them both towards the front of the bot to either find either |
| 29 | // the middle hall effect on either jaw, or the front hall effect on the bottom |
| 30 | // jaw. Any edge that isn't the desired edge will provide an approximate edge |
| 31 | // location that can be used for the fine tuning step. |
| 32 | // Once an edge is found on the front claw, move back the other way with both |
| 33 | // claws until an edge is found for the other claw. |
| 34 | // Now that we have an approximate zero, we can robustify the limits to keep |
| 35 | // both claws safe. Then, we can move both claws to a position that is the |
| 36 | // correct side of the zero and go zero. |
| 37 | |
| 38 | // Valid region plan. |
| 39 | // Difference between the arms has a range, and the values of each arm has a range. |
| 40 | // If a claw runs up against a static limit, don't let the goal change outside |
| 41 | // the limit. |
| 42 | // If a claw runs up against a movable limit, move both claws outwards to get |
| 43 | // out of the condition. |
| 44 | |
| 45 | namespace frc971 { |
| 46 | namespace control_loops { |
| 47 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 48 | static const double kZeroingVoltage = 4.0; |
| 49 | static const double kMaxVoltage = 12.0; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 50 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 51 | ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop) |
| 52 | : StateFeedbackLoop<4, 2, 2>(loop), |
| 53 | uncapped_average_voltage_(0.0), |
| 54 | is_zeroing_(true) {} |
| 55 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 56 | void ClawLimitedLoop::CapU() { |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 57 | uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0; |
| 58 | if (is_zeroing_) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 59 | LOG(DEBUG, "zeroing\n"); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 60 | const frc971::constants::Values &values = constants::GetValues(); |
| 61 | if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) { |
| 62 | const double difference = |
| 63 | uncapped_average_voltage_ - values.claw.max_zeroing_voltage; |
| 64 | U(0, 0) -= difference; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 65 | } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) { |
| 66 | const double difference = |
| 67 | -uncapped_average_voltage_ - values.claw.max_zeroing_voltage; |
| 68 | U(0, 0) += difference; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | double max_value = |
| 73 | ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0))); |
| 74 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 75 | const double k_max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage; |
| 76 | if (max_value > k_max_voltage) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 77 | LOG(DEBUG, "Capping U because max is %f\n", max_value); |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 78 | U = U * k_max_voltage / max_value; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 79 | LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0)); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 80 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 83 | ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name, |
| 84 | ClawMotor *motor) |
| 85 | : offset_(0.0), |
| 86 | name_(name), |
| 87 | motor_(motor), |
| 88 | zeroing_state_(UNKNOWN_POSITION), |
| 89 | posedge_value_(0.0), |
| 90 | negedge_value_(0.0), |
| 91 | encoder_(0.0), |
| 92 | last_encoder_(0.0) {} |
| 93 | |
| 94 | void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) { |
| 95 | front_.Update(claw.front); |
| 96 | calibration_.Update(claw.calibration); |
| 97 | back_.Update(claw.back); |
| 98 | |
| 99 | bool any_sensor_triggered = any_triggered(); |
| 100 | if (any_sensor_triggered && any_triggered_last_) { |
| 101 | // We are still on the hall effect and nothing has changed. |
| 102 | min_hall_effect_on_angle_ = |
| 103 | ::std::min(min_hall_effect_on_angle_, claw.position); |
| 104 | max_hall_effect_on_angle_ = |
| 105 | ::std::max(max_hall_effect_on_angle_, claw.position); |
| 106 | } else if (!any_sensor_triggered && !any_triggered_last_) { |
| 107 | // We are still off the hall effect and nothing has changed. |
| 108 | min_hall_effect_off_angle_ = |
| 109 | ::std::min(min_hall_effect_off_angle_, claw.position); |
| 110 | max_hall_effect_off_angle_ = |
| 111 | ::std::max(max_hall_effect_off_angle_, claw.position); |
| 112 | } else if (any_sensor_triggered && !any_triggered_last_) { |
| 113 | // Saw a posedge on the hall effect. Reset the limits. |
| 114 | min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position); |
| 115 | max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position); |
| 116 | } else if (!any_sensor_triggered && any_triggered_last_) { |
| 117 | // Saw a negedge on the hall effect. Reset the limits. |
| 118 | min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position); |
| 119 | max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position); |
| 120 | } |
| 121 | |
| 122 | posedge_value_ = claw.posedge_value; |
| 123 | negedge_value_ = claw.negedge_value; |
| 124 | last_encoder_ = encoder_; |
| 125 | if (front().value() || calibration().value() || back().value()) { |
| 126 | last_on_encoder_ = encoder_; |
| 127 | } else { |
| 128 | last_off_encoder_ = encoder_; |
| 129 | } |
| 130 | encoder_ = claw.position; |
| 131 | any_triggered_last_ = any_sensor_triggered; |
| 132 | } |
| 133 | |
| 134 | void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) { |
| 135 | set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION); |
| 136 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 137 | front_.Reset(claw.front); |
| 138 | calibration_.Reset(claw.calibration); |
| 139 | back_.Reset(claw.back); |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 140 | // close up the min and max edge positions as they are no longer valid and |
| 141 | // will be expanded in future iterations |
| 142 | min_hall_effect_on_angle_ = claw.position; |
| 143 | max_hall_effect_on_angle_ = claw.position; |
| 144 | min_hall_effect_off_angle_ = claw.position; |
| 145 | max_hall_effect_off_angle_ = claw.position; |
| 146 | any_triggered_last_ = any_triggered(); |
| 147 | } |
| 148 | |
| 149 | bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge( |
| 150 | const constants::Values::Claws::Claw &claw_values, |
| 151 | JointZeroingState zeroing_state) { |
| 152 | double edge_encoder; |
| 153 | double edge_angle; |
| 154 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 155 | LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle); |
| 156 | SetCalibration(edge_encoder, edge_angle); |
| 157 | set_zeroing_state(zeroing_state); |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge( |
| 164 | const constants::Values::Claws::Claw &claw_values, |
| 165 | JointZeroingState zeroing_state) { |
| 166 | double edge_encoder; |
| 167 | double edge_angle; |
| 168 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 169 | LOG(INFO, "Calibration edge.\n"); |
| 170 | SetCalibration(edge_encoder, edge_angle); |
| 171 | set_zeroing_state(zeroing_state); |
| 172 | return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 177 | ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw) |
| 178 | : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 179 | has_top_claw_goal_(false), |
| 180 | top_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 181 | top_claw_(this), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 182 | has_bottom_claw_goal_(false), |
| 183 | bottom_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 184 | bottom_claw_(this), |
| 185 | claw_(MakeClawLoop()), |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 186 | was_enabled_(false), |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 187 | doing_calibration_fine_tune_(false), |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 188 | capped_goal_(false), |
| 189 | mode_(UNKNOWN_LOCATION) {} |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 190 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 191 | const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 192 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 193 | bool ZeroedStateFeedbackLoop::SawFilteredPosedge( |
| 194 | const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA, |
| 195 | const HallEffectTracker &sensorB) { |
| 196 | if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() && |
| 197 | !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() && |
| 198 | this_sensor.value() && !this_sensor.last_value()) { |
| 199 | posedge_filter_ = &this_sensor; |
| 200 | } else if (posedge_filter_ == &this_sensor && |
| 201 | !this_sensor.posedge_count_changed() && |
| 202 | !sensorA.posedge_count_changed() && |
| 203 | !sensorB.posedge_count_changed() && this_sensor.value()) { |
| 204 | posedge_filter_ = nullptr; |
| 205 | return true; |
| 206 | } else if (posedge_filter_ == &this_sensor) { |
| 207 | posedge_filter_ = nullptr; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | bool ZeroedStateFeedbackLoop::SawFilteredNegedge( |
| 213 | const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA, |
| 214 | const HallEffectTracker &sensorB) { |
| 215 | if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() && |
| 216 | !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() && |
| 217 | !this_sensor.value() && this_sensor.last_value()) { |
| 218 | negedge_filter_ = &this_sensor; |
| 219 | } else if (negedge_filter_ == &this_sensor && |
| 220 | !this_sensor.negedge_count_changed() && |
| 221 | !sensorA.negedge_count_changed() && |
| 222 | !sensorB.negedge_count_changed() && !this_sensor.value()) { |
| 223 | negedge_filter_ = nullptr; |
| 224 | return true; |
| 225 | } else if (negedge_filter_ == &this_sensor) { |
| 226 | negedge_filter_ = nullptr; |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 231 | bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge( |
| 232 | const constants::Values::Claws::AnglePair &angles, double *edge_encoder, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 233 | double *edge_angle, const HallEffectTracker &this_sensor, |
| 234 | const HallEffectTracker &sensorA, const HallEffectTracker &sensorB, |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 235 | const char *hall_effect_name) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 236 | bool found_edge = false; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 237 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 238 | if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 239 | if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) { |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 240 | LOG(INFO, "%s: Uncertain which side, rejecting posedge\n", name_); |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 241 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 242 | const double average_last_encoder = |
| 243 | (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0; |
| 244 | if (posedge_value_ < average_last_encoder) { |
| 245 | *edge_angle = angles.upper_decreasing_angle; |
| 246 | LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n", |
| 247 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 248 | average_last_encoder); |
| 249 | } else { |
| 250 | *edge_angle = angles.lower_angle; |
| 251 | LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n", |
| 252 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 253 | average_last_encoder); |
| 254 | } |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 255 | *edge_encoder = posedge_value_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 256 | found_edge = true; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) { |
| 261 | if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) { |
| 262 | LOG(INFO, "%s: Uncertain which side, rejecting negedge\n", name_); |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 263 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 264 | const double average_last_encoder = |
| 265 | (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0; |
| 266 | if (negedge_value_ > average_last_encoder) { |
| 267 | *edge_angle = angles.upper_angle; |
| 268 | LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n", |
| 269 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 270 | average_last_encoder); |
| 271 | } else { |
| 272 | *edge_angle = angles.lower_decreasing_angle; |
| 273 | LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n", |
| 274 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 275 | average_last_encoder); |
| 276 | } |
| 277 | *edge_encoder = negedge_value_; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 278 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 279 | } |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 282 | return found_edge; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 285 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 286 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 287 | double *edge_angle) { |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 288 | if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 289 | front_, calibration_, back_, "front")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 290 | return true; |
| 291 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 292 | if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 293 | calibration_, front_, back_, "calibration")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 294 | return true; |
| 295 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 296 | if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 297 | back_, calibration_, front_, "back")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 303 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 304 | double edge_angle) { |
| 305 | double old_offset = offset_; |
| 306 | offset_ = edge_angle - edge_encoder; |
| 307 | const double doffset = offset_ - old_offset; |
| 308 | motor_->ChangeTopOffset(doffset); |
| 309 | } |
| 310 | |
| 311 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 312 | double edge_angle) { |
| 313 | double old_offset = offset_; |
| 314 | offset_ = edge_angle - edge_encoder; |
| 315 | const double doffset = offset_ - old_offset; |
| 316 | motor_->ChangeBottomOffset(doffset); |
| 317 | } |
| 318 | |
| 319 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 320 | claw_.ChangeTopOffset(doffset); |
| 321 | if (has_top_claw_goal_) { |
| 322 | top_claw_goal_ += doffset; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 327 | claw_.ChangeBottomOffset(doffset); |
| 328 | if (has_bottom_claw_goal_) { |
| 329 | bottom_claw_goal_ += doffset; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 334 | Y_(1, 0) += doffset; |
| 335 | X_hat(1, 0) += doffset; |
| 336 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 337 | } |
| 338 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 339 | Y_(0, 0) += doffset; |
| 340 | X_hat(0, 0) += doffset; |
| 341 | X_hat(1, 0) -= doffset; |
| 342 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 343 | } |
joe | 7376ff5 | 2014-02-16 18:28:42 -0800 | [diff] [blame] | 344 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 345 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 346 | const frc971::constants::Values &values) { |
| 347 | // first update position based on angle limit |
| 348 | |
| 349 | const double separation = *top_goal - *bottom_goal; |
| 350 | if (separation > values.claw.claw_max_separation) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 351 | const double dsep = (separation - values.claw.claw_max_separation) / 2.0; |
| 352 | *bottom_goal += dsep; |
| 353 | *top_goal -= dsep; |
| 354 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 355 | } |
| 356 | if (separation < values.claw.claw_min_separation) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 357 | const double dsep = (separation - values.claw.claw_min_separation) / 2.0; |
| 358 | *bottom_goal += dsep; |
| 359 | *top_goal -= dsep; |
| 360 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 361 | } |
| 362 | |
| 363 | // now move both goals in unison |
| 364 | if (*bottom_goal < values.claw.lower_claw.lower_limit) { |
| 365 | *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal; |
| 366 | *bottom_goal = values.claw.lower_claw.lower_limit; |
| 367 | } |
| 368 | if (*bottom_goal > values.claw.lower_claw.upper_limit) { |
| 369 | *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit; |
| 370 | *bottom_goal = values.claw.lower_claw.upper_limit; |
| 371 | } |
| 372 | |
| 373 | if (*top_goal < values.claw.upper_claw.lower_limit) { |
| 374 | *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal; |
| 375 | *top_goal = values.claw.upper_claw.lower_limit; |
| 376 | } |
| 377 | if (*top_goal > values.claw.upper_claw.upper_limit) { |
| 378 | *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit; |
| 379 | *top_goal = values.claw.upper_claw.upper_limit; |
| 380 | } |
| 381 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 382 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 383 | bool ClawMotor::is_ready() const { |
| 384 | return ( |
| 385 | (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 386 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 387 | (::aos::robot_state->autonomous && |
| 388 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 389 | top_claw_.zeroing_state() == |
| 390 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 391 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 392 | bottom_claw_.zeroing_state() == |
| 393 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))); |
| 394 | } |
| 395 | |
| 396 | bool ClawMotor::is_zeroing() const { return !is_ready(); } |
| 397 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 398 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 399 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 400 | const control_loops::ClawGroup::Position *position, |
| 401 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 402 | ::aos::control_loops::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 403 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 404 | |
| 405 | // Disable the motors now so that all early returns will return with the |
| 406 | // motors disabled. |
| 407 | if (output) { |
| 408 | output->top_claw_voltage = 0; |
| 409 | output->bottom_claw_voltage = 0; |
| 410 | output->intake_voltage = 0; |
| 411 | } |
| 412 | |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 413 | if (reset()) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 414 | top_claw_.Reset(position->top); |
| 415 | bottom_claw_.Reset(position->bottom); |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 418 | if (::aos::robot_state.get() == nullptr) { |
| 419 | return; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 422 | const frc971::constants::Values &values = constants::GetValues(); |
| 423 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 424 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 425 | Eigen::Matrix<double, 2, 1> Y; |
| 426 | Y << position->bottom.position + bottom_claw_.offset(), |
| 427 | position->top.position + top_claw_.offset(); |
| 428 | claw_.Correct(Y); |
| 429 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 430 | top_claw_.SetPositionValues(position->top); |
| 431 | bottom_claw_.SetPositionValues(position->bottom); |
| 432 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 433 | if (!has_top_claw_goal_) { |
| 434 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 435 | top_claw_goal_ = top_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 436 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 437 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 438 | } |
| 439 | if (!has_bottom_claw_goal_) { |
| 440 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 441 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 442 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 443 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 444 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 445 | LOG(DEBUG, "Claw position is (top: %f bottom: %f\n", |
| 446 | top_claw_.absolute_position(), bottom_claw_.absolute_position()); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 449 | const bool autonomous = ::aos::robot_state->autonomous; |
| 450 | const bool enabled = ::aos::robot_state->enabled; |
| 451 | |
| 452 | double bottom_claw_velocity_ = 0.0; |
| 453 | double top_claw_velocity_ = 0.0; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 454 | |
| 455 | if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 456 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 457 | (autonomous && |
| 458 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 459 | top_claw_.zeroing_state() == |
| 460 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 461 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 462 | bottom_claw_.zeroing_state() == |
| 463 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) { |
| 464 | // Ready to use the claw. |
| 465 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 466 | bottom_claw_goal_ = goal->bottom_angle; |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 467 | top_claw_goal_ = goal->bottom_angle + goal->separation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 468 | has_bottom_claw_goal_ = true; |
| 469 | has_top_claw_goal_ = true; |
| 470 | doing_calibration_fine_tune_ = false; |
| 471 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 472 | mode_ = READY; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 473 | } else if (top_claw_.zeroing_state() != |
| 474 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
| 475 | bottom_claw_.zeroing_state() != |
| 476 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
| 477 | // Time to fine tune the zero. |
| 478 | // Limit the goals here. |
Austin Schuh | 0c73342 | 2014-02-17 01:17:12 -0800 | [diff] [blame] | 479 | if (!enabled) { |
| 480 | // If we are disabled, start the fine tune process over again. |
| 481 | doing_calibration_fine_tune_ = false; |
| 482 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 483 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 484 | // always get the bottom claw to calibrated first |
| 485 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 486 | if (!doing_calibration_fine_tune_) { |
| 487 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 488 | values.claw.start_fine_tune_pos) < |
| 489 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 490 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 491 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 492 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 493 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 494 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 495 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 496 | } else { |
| 497 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 498 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 499 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 500 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 501 | } |
| 502 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 503 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 504 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 505 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 506 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 507 | if (top_claw_.front_or_back_triggered() || |
| 508 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 509 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 510 | // point and try again. |
| 511 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 512 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 513 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 514 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 515 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 516 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 517 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 518 | if (position && |
| 519 | bottom_claw_.SawFilteredPosedge( |
| 520 | bottom_claw_.calibration(), |
| 521 | bottom_claw_.front(), bottom_claw_.back())) { |
| 522 | // do calibration |
| 523 | bottom_claw_.SetCalibration( |
| 524 | position->bottom.posedge_value, |
| 525 | values.claw.lower_claw.calibration.lower_angle); |
| 526 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 527 | // calibrated so we are done fine tuning bottom |
| 528 | doing_calibration_fine_tune_ = false; |
| 529 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 530 | } else if (bottom_claw_.calibration().last_value()) { |
| 531 | doing_calibration_fine_tune_ = false; |
| 532 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
| 533 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
| 534 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 535 | } else { |
| 536 | LOG(DEBUG, "Fine tuning\n"); |
| 537 | } |
| 538 | } |
| 539 | // now set the top claw to track |
| 540 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 541 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 542 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 543 | // bottom claw must be calibrated, start on the top |
| 544 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 545 | if (::std::abs(top_absolute_position() - |
| 546 | values.claw.start_fine_tune_pos) < |
| 547 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 548 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 549 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 550 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 551 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 552 | LOG(DEBUG, "Ready to fine tune the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 553 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 554 | } else { |
| 555 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 556 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 557 | LOG(DEBUG, "Going to the start position for the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 558 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 559 | } |
| 560 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 561 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 562 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 563 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 564 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 565 | if (top_claw_.front_or_back_triggered() || |
| 566 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 567 | // this should not happen, but now we know it won't |
| 568 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 569 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 570 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 571 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 572 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 573 | } |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 574 | |
| 575 | if (position && |
| 576 | top_claw_.SawFilteredPosedge(top_claw_.calibration(), |
| 577 | top_claw_.front(), top_claw_.back())) { |
| 578 | // do calibration |
| 579 | top_claw_.SetCalibration( |
| 580 | position->top.posedge_value, |
| 581 | values.claw.upper_claw.calibration.lower_angle); |
| 582 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 583 | // calibrated so we are done fine tuning top |
| 584 | doing_calibration_fine_tune_ = false; |
| 585 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
| 586 | } else if (top_claw_.calibration().last_value()) { |
| 587 | doing_calibration_fine_tune_ = false; |
| 588 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
| 589 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
| 590 | mode_ = PREP_FINE_TUNE_TOP; |
| 591 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 592 | } |
| 593 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 594 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 595 | } |
| 596 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 597 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 598 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 599 | if (position) { |
| 600 | top_claw_goal_ = position->top.position; |
| 601 | bottom_claw_goal_ = position->bottom.position; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 602 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 603 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 604 | } else { |
| 605 | has_top_claw_goal_ = false; |
| 606 | has_bottom_claw_goal_ = false; |
| 607 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 608 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 609 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 610 | if ((bottom_claw_.zeroing_state() != |
| 611 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 612 | bottom_claw_.front().value() || top_claw_.front().value()) && |
| 613 | !top_claw_.back().value() && !bottom_claw_.back().value()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 614 | if (enabled) { |
| 615 | // Time to slowly move back up to find any position to narrow down the |
| 616 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 617 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 618 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 619 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 620 | values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 621 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 622 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 623 | } else { |
| 624 | // We don't know where either claw is. Slowly start moving down to find |
| 625 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 626 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 627 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 628 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 629 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 630 | -values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 631 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 635 | if (position) { |
| 636 | if (enabled) { |
| 637 | top_claw_.SetCalibrationOnEdge( |
| 638 | values.claw.upper_claw, |
| 639 | ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 640 | bottom_claw_.SetCalibrationOnEdge( |
| 641 | values.claw.lower_claw, |
| 642 | ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 643 | } else { |
| 644 | // TODO(austin): Only calibrate on the predetermined edge. |
| 645 | // We might be able to just ignore this since the backlash is soooo |
| 646 | // low. |
| 647 | // :) |
| 648 | top_claw_.SetCalibrationOnEdge( |
| 649 | values.claw.upper_claw, |
| 650 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
| 651 | bottom_claw_.SetCalibrationOnEdge( |
| 652 | values.claw.lower_claw, |
| 653 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
| 654 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 655 | } |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 656 | mode_ = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 659 | // Limit the goals if both claws have been (mostly) found. |
| 660 | if (mode_ != UNKNOWN_LOCATION) { |
| 661 | LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values); |
| 662 | } |
| 663 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 664 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 665 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, |
| 666 | bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 667 | double separation = -971; |
| 668 | if (position != nullptr) { |
| 669 | separation = position->top.position - position->bottom.position; |
| 670 | } |
| 671 | LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0), |
| 672 | claw_.R(1, 0), separation); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 673 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 674 | // Only cap power when one of the halves of the claw is moving slowly and |
| 675 | // could wind up. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 676 | claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP || |
| 677 | mode_ == FINE_TUNE_BOTTOM); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 678 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 679 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 680 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 681 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 682 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 683 | capped_goal_ = false; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 684 | switch (mode_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 685 | case READY: |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 686 | case PREP_FINE_TUNE_TOP: |
| 687 | case PREP_FINE_TUNE_BOTTOM: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 688 | break; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 689 | case FINE_TUNE_BOTTOM: |
| 690 | case FINE_TUNE_TOP: |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 691 | case UNKNOWN_LOCATION: { |
| 692 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
| 693 | double dx = (claw_.uncapped_average_voltage() - |
| 694 | values.claw.max_zeroing_voltage) / |
| 695 | claw_.K(0, 0); |
| 696 | bottom_claw_goal_ -= dx; |
| 697 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 698 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 699 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 700 | LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n", |
| 701 | claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage, |
| 702 | (claw_.uncapped_average_voltage() - |
| 703 | values.claw.max_zeroing_voltage)); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 704 | } else if (claw_.uncapped_average_voltage() < |
| 705 | -values.claw.max_zeroing_voltage) { |
| 706 | double dx = (claw_.uncapped_average_voltage() + |
| 707 | values.claw.max_zeroing_voltage) / |
| 708 | claw_.K(0, 0); |
| 709 | bottom_claw_goal_ -= dx; |
| 710 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 711 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 712 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 713 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 714 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | if (output) { |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 718 | if (goal) { |
| 719 | //setup the intake |
Ben Fredrickson | 6de9b49 | 2014-02-23 06:05:19 +0000 | [diff] [blame] | 720 | output->intake_voltage = (goal->intake > 12.0) ? 12 : |
| 721 | (goal->intake < -12.0) ? -12.0 : goal->intake; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 722 | output->tusk_voltage = goal->centering; |
Ben Fredrickson | 6de9b49 | 2014-02-23 06:05:19 +0000 | [diff] [blame] | 723 | output->tusk_voltage = (goal->centering > 12.0) ? 12 : |
| 724 | (goal->centering < -12.0) ? -12.0 : goal->centering; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 725 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 726 | output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0); |
| 727 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 728 | |
| 729 | if (output->top_claw_voltage > kMaxVoltage) { |
| 730 | output->top_claw_voltage = kMaxVoltage; |
| 731 | } else if (output->top_claw_voltage < -kMaxVoltage) { |
| 732 | output->top_claw_voltage = -kMaxVoltage; |
| 733 | } |
| 734 | |
| 735 | if (output->bottom_claw_voltage > kMaxVoltage) { |
| 736 | output->bottom_claw_voltage = kMaxVoltage; |
| 737 | } else if (output->bottom_claw_voltage < -kMaxVoltage) { |
| 738 | output->bottom_claw_voltage = -kMaxVoltage; |
| 739 | } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 740 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 741 | status->done = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 742 | |
| 743 | was_enabled_ = ::aos::robot_state->enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | } // namespace control_loops |
| 747 | } // namespace frc971 |