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 | |
| 137 | front_.Reset(); |
| 138 | calibration_.Reset(); |
| 139 | back_.Reset(); |
| 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 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 193 | bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge( |
| 194 | const constants::Values::Claws::AnglePair &angles, double *edge_encoder, |
| 195 | double *edge_angle, const HallEffectTracker &sensor, |
| 196 | const char *hall_effect_name) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 197 | bool found_edge = false; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 198 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 199 | if (sensor.posedge_count_changed()) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 200 | if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) { |
Ben Fredrickson | ade3eab | 2014-02-22 07:30:53 +0000 | [diff] [blame] | 201 | // we oddly got two of the same edge. |
| 202 | *edge_angle = last_edge_value_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 203 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 204 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 205 | const double average_last_encoder = |
| 206 | (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0; |
| 207 | if (posedge_value_ < average_last_encoder) { |
| 208 | *edge_angle = angles.upper_decreasing_angle; |
| 209 | LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n", |
| 210 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 211 | average_last_encoder); |
| 212 | } else { |
| 213 | *edge_angle = angles.lower_angle; |
| 214 | LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n", |
| 215 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 216 | average_last_encoder); |
| 217 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 218 | } |
| 219 | *edge_encoder = posedge_value_; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 220 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 221 | } |
| 222 | if (sensor.negedge_count_changed()) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 223 | if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) { |
Ben Fredrickson | ade3eab | 2014-02-22 07:30:53 +0000 | [diff] [blame] | 224 | *edge_angle = last_edge_value_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 225 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 226 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 227 | const double average_last_encoder = |
| 228 | (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0; |
| 229 | if (negedge_value_ > average_last_encoder) { |
| 230 | *edge_angle = angles.upper_angle; |
| 231 | LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n", |
| 232 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 233 | average_last_encoder); |
| 234 | } else { |
| 235 | *edge_angle = angles.lower_decreasing_angle; |
| 236 | LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n", |
| 237 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 238 | average_last_encoder); |
| 239 | } |
| 240 | *edge_encoder = negedge_value_; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 241 | } |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 242 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 245 | if (found_edge) { |
| 246 | last_edge_value_ = *edge_angle; |
| 247 | } |
| 248 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 249 | return found_edge; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 252 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 253 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 254 | double *edge_angle) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 255 | // TODO(austin): Validate that the hall effect edge makes sense. |
| 256 | // We must now be on the side of the edge that we expect to be, and the |
| 257 | // encoder must have been on either side of the edge before and after. |
| 258 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 259 | // TODO(austin): Compute the last off range min and max and compare the edge |
| 260 | // value to the middle of the range. This will be quite a bit more reliable. |
| 261 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 262 | if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, |
| 263 | front_, "front")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 264 | return true; |
| 265 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 266 | if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle, |
| 267 | calibration_, "calibration")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 268 | return true; |
| 269 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 270 | if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, |
| 271 | back_, "back")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 277 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 278 | double edge_angle) { |
| 279 | double old_offset = offset_; |
| 280 | offset_ = edge_angle - edge_encoder; |
| 281 | const double doffset = offset_ - old_offset; |
| 282 | motor_->ChangeTopOffset(doffset); |
| 283 | } |
| 284 | |
| 285 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 286 | double edge_angle) { |
| 287 | double old_offset = offset_; |
| 288 | offset_ = edge_angle - edge_encoder; |
| 289 | const double doffset = offset_ - old_offset; |
| 290 | motor_->ChangeBottomOffset(doffset); |
| 291 | } |
| 292 | |
| 293 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 294 | claw_.ChangeTopOffset(doffset); |
| 295 | if (has_top_claw_goal_) { |
| 296 | top_claw_goal_ += doffset; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 301 | claw_.ChangeBottomOffset(doffset); |
| 302 | if (has_bottom_claw_goal_) { |
| 303 | bottom_claw_goal_ += doffset; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 308 | Y_(1, 0) += doffset; |
| 309 | X_hat(1, 0) += doffset; |
| 310 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 311 | } |
| 312 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 313 | Y_(0, 0) += doffset; |
| 314 | X_hat(0, 0) += doffset; |
| 315 | X_hat(1, 0) -= doffset; |
| 316 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 317 | } |
joe | 7376ff5 | 2014-02-16 18:28:42 -0800 | [diff] [blame] | 318 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 319 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 320 | const frc971::constants::Values &values) { |
| 321 | // first update position based on angle limit |
| 322 | |
| 323 | const double separation = *top_goal - *bottom_goal; |
| 324 | if (separation > values.claw.claw_max_separation) { |
| 325 | LOG(DEBUG, "Greater than\n"); |
| 326 | const double dsep = (separation - values.claw.claw_max_separation) / 2.0; |
| 327 | *bottom_goal += dsep; |
| 328 | *top_goal -= dsep; |
| 329 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 330 | } |
| 331 | if (separation < values.claw.claw_min_separation) { |
| 332 | LOG(DEBUG, "Less than\n"); |
| 333 | const double dsep = (separation - values.claw.claw_min_separation) / 2.0; |
| 334 | *bottom_goal += dsep; |
| 335 | *top_goal -= dsep; |
| 336 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 337 | } |
| 338 | |
| 339 | // now move both goals in unison |
| 340 | if (*bottom_goal < values.claw.lower_claw.lower_limit) { |
| 341 | *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal; |
| 342 | *bottom_goal = values.claw.lower_claw.lower_limit; |
| 343 | } |
| 344 | if (*bottom_goal > values.claw.lower_claw.upper_limit) { |
| 345 | *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit; |
| 346 | *bottom_goal = values.claw.lower_claw.upper_limit; |
| 347 | } |
| 348 | |
| 349 | if (*top_goal < values.claw.upper_claw.lower_limit) { |
| 350 | *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal; |
| 351 | *top_goal = values.claw.upper_claw.lower_limit; |
| 352 | } |
| 353 | if (*top_goal > values.claw.upper_claw.upper_limit) { |
| 354 | *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit; |
| 355 | *top_goal = values.claw.upper_claw.upper_limit; |
| 356 | } |
| 357 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 358 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 359 | bool ClawMotor::is_ready() const { |
| 360 | return ( |
| 361 | (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 362 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 363 | (::aos::robot_state->autonomous && |
| 364 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 365 | top_claw_.zeroing_state() == |
| 366 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 367 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 368 | bottom_claw_.zeroing_state() == |
| 369 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))); |
| 370 | } |
| 371 | |
| 372 | bool ClawMotor::is_zeroing() const { return !is_ready(); } |
| 373 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 374 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 375 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 376 | const control_loops::ClawGroup::Position *position, |
| 377 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 378 | ::aos::control_loops::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 379 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 380 | |
| 381 | // Disable the motors now so that all early returns will return with the |
| 382 | // motors disabled. |
| 383 | if (output) { |
| 384 | output->top_claw_voltage = 0; |
| 385 | output->bottom_claw_voltage = 0; |
| 386 | output->intake_voltage = 0; |
| 387 | } |
| 388 | |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 389 | if (reset()) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 390 | top_claw_.Reset(position->top); |
| 391 | bottom_claw_.Reset(position->bottom); |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 394 | if (::aos::robot_state.get() == nullptr) { |
| 395 | return; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 398 | const frc971::constants::Values &values = constants::GetValues(); |
| 399 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 400 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 401 | Eigen::Matrix<double, 2, 1> Y; |
| 402 | Y << position->bottom.position + bottom_claw_.offset(), |
| 403 | position->top.position + top_claw_.offset(); |
| 404 | claw_.Correct(Y); |
| 405 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 406 | top_claw_.SetPositionValues(position->top); |
| 407 | bottom_claw_.SetPositionValues(position->bottom); |
| 408 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 409 | if (!has_top_claw_goal_) { |
| 410 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 411 | top_claw_goal_ = top_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 412 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 413 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 414 | } |
| 415 | if (!has_bottom_claw_goal_) { |
| 416 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 417 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 418 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 419 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 420 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 421 | LOG(DEBUG, "Claw position is (top: %f bottom: %f\n", |
| 422 | top_claw_.absolute_position(), bottom_claw_.absolute_position()); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 425 | const bool autonomous = ::aos::robot_state->autonomous; |
| 426 | const bool enabled = ::aos::robot_state->enabled; |
| 427 | |
| 428 | double bottom_claw_velocity_ = 0.0; |
| 429 | double top_claw_velocity_ = 0.0; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 430 | |
| 431 | if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 432 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 433 | (autonomous && |
| 434 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 435 | top_claw_.zeroing_state() == |
| 436 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 437 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 438 | bottom_claw_.zeroing_state() == |
| 439 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) { |
| 440 | // Ready to use the claw. |
| 441 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 442 | bottom_claw_goal_ = goal->bottom_angle; |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 443 | top_claw_goal_ = goal->bottom_angle + goal->separation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 444 | has_bottom_claw_goal_ = true; |
| 445 | has_top_claw_goal_ = true; |
| 446 | doing_calibration_fine_tune_ = false; |
| 447 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 448 | mode_ = READY; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 449 | } else if (top_claw_.zeroing_state() != |
| 450 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
| 451 | bottom_claw_.zeroing_state() != |
| 452 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
| 453 | // Time to fine tune the zero. |
| 454 | // Limit the goals here. |
Austin Schuh | 0c73342 | 2014-02-17 01:17:12 -0800 | [diff] [blame] | 455 | if (!enabled) { |
| 456 | // If we are disabled, start the fine tune process over again. |
| 457 | doing_calibration_fine_tune_ = false; |
| 458 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 459 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 460 | // always get the bottom claw to calibrated first |
| 461 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 462 | if (!doing_calibration_fine_tune_) { |
| 463 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 464 | values.claw.start_fine_tune_pos) < |
| 465 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 466 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 467 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 468 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 469 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 470 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 471 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 472 | } else { |
| 473 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 474 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 475 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 476 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 477 | } |
| 478 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 479 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 480 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 481 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 482 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 483 | if (top_claw_.front_or_back_triggered() || |
| 484 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 485 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 486 | // point and try again. |
| 487 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 488 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 489 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 490 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 491 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 492 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 493 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 494 | if (bottom_claw_.calibration().value()) { |
| 495 | if (bottom_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 496 | position) { |
| 497 | // do calibration |
| 498 | bottom_claw_.SetCalibration( |
| 499 | position->bottom.posedge_value, |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 500 | values.claw.lower_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 501 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 502 | // calibrated so we are done fine tuning bottom |
| 503 | doing_calibration_fine_tune_ = false; |
| 504 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 505 | } else { |
| 506 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 507 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 508 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 509 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 510 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 511 | } else { |
| 512 | LOG(DEBUG, "Fine tuning\n"); |
| 513 | } |
| 514 | } |
| 515 | // now set the top claw to track |
| 516 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 517 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 518 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 519 | // bottom claw must be calibrated, start on the top |
| 520 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 521 | if (::std::abs(top_absolute_position() - |
| 522 | values.claw.start_fine_tune_pos) < |
| 523 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 524 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 525 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 526 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 527 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 528 | LOG(DEBUG, "Ready to fine tune the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 529 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 530 | } else { |
| 531 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 532 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 533 | LOG(DEBUG, "Going to the start position for the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 534 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 535 | } |
| 536 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 537 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 538 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 539 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 540 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 541 | if (top_claw_.front_or_back_triggered() || |
| 542 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 543 | // this should not happen, but now we know it won't |
| 544 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 545 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 546 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 547 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 548 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 549 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 550 | if (top_claw_.calibration().value()) { |
| 551 | if (top_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 552 | position) { |
| 553 | // do calibration |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 554 | top_claw_.SetCalibration( |
| 555 | position->top.posedge_value, |
| 556 | values.claw.upper_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 557 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 558 | // calibrated so we are done fine tuning top |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 559 | doing_calibration_fine_tune_ = false; |
| 560 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
| 561 | } else { |
| 562 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 563 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 564 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 565 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 566 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 570 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 571 | } |
| 572 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 573 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 574 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 575 | if (position) { |
| 576 | top_claw_goal_ = position->top.position; |
| 577 | bottom_claw_goal_ = position->bottom.position; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 578 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 579 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 580 | } else { |
| 581 | has_top_claw_goal_ = false; |
| 582 | has_bottom_claw_goal_ = false; |
| 583 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 584 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 585 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 586 | if ((bottom_claw_.zeroing_state() != |
| 587 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 588 | bottom_claw_.front().value() || top_claw_.front().value()) && |
| 589 | !top_claw_.back().value() && !bottom_claw_.back().value()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 590 | if (enabled) { |
| 591 | // Time to slowly move back up to find any position to narrow down the |
| 592 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 593 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 594 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 595 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 596 | values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 597 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 598 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 599 | } else { |
| 600 | // We don't know where either claw is. Slowly start moving down to find |
| 601 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 602 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 603 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 604 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 605 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 606 | -values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 607 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | if (enabled) { |
| 612 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 613 | values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 614 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 615 | values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 616 | } else { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 617 | // TODO(austin): Only calibrate on the predetermined edge. |
| 618 | // We might be able to just ignore this since the backlash is soooo low. :) |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 619 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 620 | values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 621 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 622 | values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 623 | } |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 624 | mode_ = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 627 | // Limit the goals if both claws have been (mostly) found. |
| 628 | if (mode_ != UNKNOWN_LOCATION) { |
| 629 | LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values); |
| 630 | } |
| 631 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 632 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 633 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, |
| 634 | bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 635 | double separation = -971; |
| 636 | if (position != nullptr) { |
| 637 | separation = position->top.position - position->bottom.position; |
| 638 | } |
| 639 | LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0), |
| 640 | claw_.R(1, 0), separation); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 641 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 642 | // Only cap power when one of the halves of the claw is moving slowly and |
| 643 | // could wind up. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 644 | claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP || |
| 645 | mode_ == FINE_TUNE_BOTTOM); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 646 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 647 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 648 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 649 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 650 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 651 | capped_goal_ = false; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 652 | switch (mode_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 653 | case READY: |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 654 | case PREP_FINE_TUNE_TOP: |
| 655 | case PREP_FINE_TUNE_BOTTOM: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 656 | break; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 657 | case FINE_TUNE_BOTTOM: |
| 658 | case FINE_TUNE_TOP: |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 659 | case UNKNOWN_LOCATION: { |
| 660 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
| 661 | double dx = (claw_.uncapped_average_voltage() - |
| 662 | values.claw.max_zeroing_voltage) / |
| 663 | claw_.K(0, 0); |
| 664 | bottom_claw_goal_ -= dx; |
| 665 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 666 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 667 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 668 | LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n", |
| 669 | claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage, |
| 670 | (claw_.uncapped_average_voltage() - |
| 671 | values.claw.max_zeroing_voltage)); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 672 | } else if (claw_.uncapped_average_voltage() < |
| 673 | -values.claw.max_zeroing_voltage) { |
| 674 | double dx = (claw_.uncapped_average_voltage() + |
| 675 | values.claw.max_zeroing_voltage) / |
| 676 | claw_.K(0, 0); |
| 677 | bottom_claw_goal_ -= dx; |
| 678 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 679 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 680 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 681 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 682 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (output) { |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 686 | if (goal) { |
| 687 | //setup the intake |
Ben Fredrickson | 6de9b49 | 2014-02-23 06:05:19 +0000 | [diff] [blame^] | 688 | output->intake_voltage = (goal->intake > 12.0) ? 12 : |
| 689 | (goal->intake < -12.0) ? -12.0 : goal->intake; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 690 | output->tusk_voltage = goal->centering; |
Ben Fredrickson | 6de9b49 | 2014-02-23 06:05:19 +0000 | [diff] [blame^] | 691 | output->tusk_voltage = (goal->centering > 12.0) ? 12 : |
| 692 | (goal->centering < -12.0) ? -12.0 : goal->centering; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 693 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 694 | output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0); |
| 695 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 696 | |
| 697 | if (output->top_claw_voltage > kMaxVoltage) { |
| 698 | output->top_claw_voltage = kMaxVoltage; |
| 699 | } else if (output->top_claw_voltage < -kMaxVoltage) { |
| 700 | output->top_claw_voltage = -kMaxVoltage; |
| 701 | } |
| 702 | |
| 703 | if (output->bottom_claw_voltage > kMaxVoltage) { |
| 704 | output->bottom_claw_voltage = kMaxVoltage; |
| 705 | } else if (output->bottom_claw_voltage < -kMaxVoltage) { |
| 706 | output->bottom_claw_voltage = -kMaxVoltage; |
| 707 | } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 708 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 709 | status->done = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 710 | |
| 711 | was_enabled_ = ::aos::robot_state->enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | } // namespace control_loops |
| 715 | } // namespace frc971 |