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" |
| 11 | #include "frc971/control_loops/claw/top_claw_motor_plant.h" |
| 12 | #include "frc971/control_loops/claw/bottom_claw_motor_plant.h" |
| 13 | |
| 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. |
| 40 | // Difference between the arms has a range, and the values of each arm has a range. |
| 41 | // If a claw runs up against a static limit, don't let the goal change outside |
| 42 | // the limit. |
| 43 | // If a claw runs up against a movable limit, move both claws outwards to get |
| 44 | // out of the condition. |
| 45 | |
| 46 | namespace frc971 { |
| 47 | namespace control_loops { |
| 48 | |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame^] | 49 | ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw) |
| 50 | : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw), |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 51 | zeroed_joint_(MakeTopClawLoop()) { |
| 52 | { |
| 53 | using ::frc971::constants::GetValues; |
| 54 | ZeroedJoint<1>::ConfigurationData config_data; |
| 55 | |
| 56 | config_data.lower_limit = GetValues().claw_lower_limit; |
| 57 | config_data.upper_limit = GetValues().claw_upper_limit; |
| 58 | config_data.hall_effect_start_angle[0] = |
| 59 | GetValues().claw_hall_effect_start_angle; |
| 60 | config_data.zeroing_off_speed = GetValues().claw_zeroing_off_speed; |
| 61 | config_data.zeroing_speed = GetValues().claw_zeroing_speed; |
| 62 | |
| 63 | config_data.max_zeroing_voltage = 5.0; |
| 64 | config_data.deadband_voltage = 0.0; |
| 65 | |
| 66 | zeroed_joint_.set_config_data(config_data); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame^] | 71 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 72 | const control_loops::ClawGroup::Position *position, |
| 73 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 74 | ::aos::control_loops::Status *status) { |
| 75 | |
| 76 | // Disable the motors now so that all early returns will return with the |
| 77 | // motors disabled. |
| 78 | if (output) { |
| 79 | output->top_claw_voltage = 0; |
| 80 | output->bottom_claw_voltage = 0; |
| 81 | output->intake_voltage = 0; |
| 82 | } |
| 83 | |
| 84 | ZeroedJoint<1>::PositionData transformed_position; |
| 85 | ZeroedJoint<1>::PositionData *transformed_position_ptr = |
| 86 | &transformed_position; |
| 87 | if (!position) { |
| 88 | transformed_position_ptr = NULL; |
| 89 | } else { |
| 90 | transformed_position.position = position->top_position; |
| 91 | transformed_position.hall_effects[0] = position->top_calibration_hall_effect; |
| 92 | transformed_position.hall_effect_positions[0] = position->top_posedge_value; |
| 93 | } |
| 94 | |
| 95 | const double voltage = |
| 96 | zeroed_joint_.Update(transformed_position_ptr, output != NULL, |
| 97 | goal->bottom_angle + goal->seperation_angle, 0.0); |
| 98 | |
| 99 | if (position) { |
| 100 | LOG(DEBUG, "pos: %f hall: %s absolute: %f\n", position->top_position, |
| 101 | position->top_calibration_hall_effect ? "true" : "false", |
| 102 | zeroed_joint_.absolute_position()); |
| 103 | } |
| 104 | |
| 105 | if (output) { |
| 106 | output->top_claw_voltage = voltage; |
| 107 | } |
| 108 | status->done = ::std::abs(zeroed_joint_.absolute_position() - |
| 109 | goal->bottom_angle - goal->seperation_angle) < 0.004; |
| 110 | } |
| 111 | |
| 112 | } // namespace control_loops |
| 113 | } // namespace frc971 |