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