Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/common/controls/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | #include "frc971/control_loops/control_loops.q.h" |
| 6 | #include "y2018/constants.h" |
| 7 | #include "y2018/control_loops/superstructure/intake/intake.h" |
| 8 | |
| 9 | namespace y2018 { |
| 10 | namespace control_loops { |
| 11 | namespace superstructure { |
| 12 | |
| 13 | namespace { |
| 14 | // The maximum voltage the intake roller will be allowed to use. |
| 15 | constexpr double kMaxIntakeRollerVoltage = 12.0; |
| 16 | } // namespace |
| 17 | |
| 18 | Superstructure::Superstructure( |
| 19 | control_loops::SuperstructureQueue *superstructure_queue) |
| 20 | : aos::controls::ControlLoop<control_loops::SuperstructureQueue>( |
| 21 | superstructure_queue), |
| 22 | intake_left_(constants::GetValues().left_intake.zeroing), |
| 23 | intake_right_(constants::GetValues().right_intake.zeroing) {} |
| 24 | |
| 25 | void Superstructure::RunIteration( |
| 26 | const control_loops::SuperstructureQueue::Goal *unsafe_goal, |
| 27 | const control_loops::SuperstructureQueue::Position *position, |
| 28 | control_loops::SuperstructureQueue::Output *output, |
| 29 | control_loops::SuperstructureQueue::Status *status) { |
| 30 | if (WasReset()) { |
| 31 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 32 | intake_left_.Reset(); |
| 33 | intake_right_.Reset(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 34 | arm_.Reset(); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 37 | const double left_intake_goal = ::std::min( |
| 38 | arm_.max_intake_override(), |
| 39 | (unsafe_goal == nullptr ? 0.0 : unsafe_goal->intake.left_intake_angle)); |
| 40 | intake_left_.Iterate(unsafe_goal != nullptr ? &(left_intake_goal) : nullptr, |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 41 | &(position->left_intake), |
| 42 | output != nullptr ? &(output->left_intake) : nullptr, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 43 | &(status->left_intake)); |
| 44 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 45 | const double right_intake_goal = ::std::min( |
| 46 | arm_.max_intake_override(), |
| 47 | (unsafe_goal == nullptr ? 0.0 : unsafe_goal->intake.right_intake_angle)); |
| 48 | |
| 49 | intake_right_.Iterate(unsafe_goal != nullptr ? &(right_intake_goal) : nullptr, |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 50 | &(position->right_intake), |
| 51 | output != nullptr ? &(output->right_intake) : nullptr, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 52 | &(status->right_intake)); |
| 53 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 54 | const bool intake_clear_of_box = |
| 55 | intake_left_.clear_of_box() && intake_right_.clear_of_box(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 56 | arm_.Iterate( |
| 57 | unsafe_goal != nullptr ? &(unsafe_goal->arm_goal_position) : nullptr, |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 58 | unsafe_goal != nullptr ? unsafe_goal->grab_box : false, |
| 59 | unsafe_goal != nullptr ? unsafe_goal->open_claw : false, &(position->arm), |
| 60 | position->claw_beambreak_triggered, |
| 61 | position->box_back_beambreak_triggered, intake_clear_of_box, |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 62 | output != nullptr ? &(output->voltage_proximal) : nullptr, |
| 63 | output != nullptr ? &(output->voltage_distal) : nullptr, |
| 64 | output != nullptr ? &(output->release_arm_brake) : nullptr, |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 65 | output != nullptr ? &(output->claw_grabbed) : nullptr, &(status->arm), |
| 66 | unsafe_goal != nullptr ? unsafe_goal->voltage_winch > 1.0 : false); |
| 67 | |
| 68 | if (output) { |
| 69 | if (unsafe_goal) { |
| 70 | output->hook_release = unsafe_goal->hook_release; |
| 71 | output->voltage_winch = unsafe_goal->voltage_winch; |
| 72 | output->forks_release = unsafe_goal->deploy_fork; |
| 73 | } else { |
| 74 | output->voltage_winch = 0.0; |
| 75 | output->hook_release = false; |
| 76 | output->forks_release = false; |
| 77 | } |
| 78 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 79 | |
| 80 | status->estopped = status->left_intake.estopped || |
| 81 | status->right_intake.estopped || status->arm.estopped; |
| 82 | |
| 83 | status->zeroed = status->left_intake.zeroed && status->right_intake.zeroed && |
| 84 | status->arm.zeroed; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 85 | |
| 86 | if (output && unsafe_goal) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 87 | double roller_voltage = ::std::max( |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 88 | -kMaxIntakeRollerVoltage, ::std::min(unsafe_goal->intake.roller_voltage, |
| 89 | kMaxIntakeRollerVoltage)); |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 90 | constexpr int kReverseTime = 15; |
| 91 | if (unsafe_goal->intake.roller_voltage < 0.0) { |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 92 | output->left_intake.voltage_rollers = roller_voltage; |
| 93 | output->right_intake.voltage_rollers = roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 94 | rotation_state_ = RotationState::NOT_ROTATING; |
| 95 | rotation_count_ = 0; |
| 96 | } else { |
| 97 | switch (rotation_state_) { |
| 98 | case RotationState::NOT_ROTATING: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 99 | if (position->left_intake.beam_break) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 100 | rotation_state_ = RotationState::ROTATING_RIGHT; |
| 101 | rotation_count_ = kReverseTime; |
| 102 | break; |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 103 | } else if (position->right_intake.beam_break) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 104 | rotation_state_ = RotationState::ROTATING_LEFT; |
| 105 | rotation_count_ = kReverseTime; |
| 106 | break; |
| 107 | } else { |
| 108 | break; |
| 109 | } |
| 110 | case RotationState::ROTATING_LEFT: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 111 | if (position->right_intake.beam_break) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 112 | rotation_count_ = kReverseTime; |
| 113 | } else { |
| 114 | --rotation_count_; |
| 115 | } |
| 116 | if (rotation_count_ == 0) { |
| 117 | rotation_state_ = RotationState::NOT_ROTATING; |
| 118 | } |
| 119 | break; |
| 120 | case RotationState::ROTATING_RIGHT: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 121 | if (position->left_intake.beam_break) { |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 122 | rotation_count_ = kReverseTime; |
| 123 | } else { |
| 124 | --rotation_count_; |
| 125 | } |
| 126 | if (rotation_count_ == 0) { |
| 127 | rotation_state_ = RotationState::NOT_ROTATING; |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 132 | if (position->box_back_beambreak_triggered && roller_voltage > 0.0) { |
| 133 | roller_voltage = 0; |
| 134 | } |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 135 | switch (rotation_state_) { |
| 136 | case RotationState::NOT_ROTATING: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 137 | output->left_intake.voltage_rollers = roller_voltage; |
| 138 | output->right_intake.voltage_rollers = roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 139 | break; |
| 140 | case RotationState::ROTATING_LEFT: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 141 | output->left_intake.voltage_rollers = roller_voltage; |
| 142 | output->right_intake.voltage_rollers = -roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 143 | break; |
| 144 | case RotationState::ROTATING_RIGHT: |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 145 | output->left_intake.voltage_rollers = -roller_voltage; |
| 146 | output->right_intake.voltage_rollers = roller_voltage; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | } // namespace superstructure |
| 154 | } // namespace control_loops |
| 155 | } // namespace y2018 |