Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 1 | #include "y2017/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/common/controls/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 5 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 6 | #include "y2017/constants.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 7 | #include "y2017/control_loops/superstructure/column/column.h" |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 8 | #include "y2017/control_loops/superstructure/hood/hood.h" |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 9 | #include "y2017/control_loops/superstructure/intake/intake.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 10 | #include "y2017/control_loops/superstructure/shooter/shooter.h" |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 11 | #include "y2017/vision/vision.q.h" |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 12 | |
| 13 | namespace y2017 { |
| 14 | namespace control_loops { |
| 15 | namespace superstructure { |
| 16 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | // The maximum voltage the intake roller will be allowed to use. |
| 19 | constexpr double kMaxIntakeRollerVoltage = 12.0; |
| 20 | constexpr double kMaxIndexerRollerVoltage = 12.0; |
Austin Schuh | 3ae4743 | 2017-04-16 19:15:46 -0700 | [diff] [blame^] | 21 | constexpr double kTurretTuckAngle = M_PI / 2.0; |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 22 | } // namespace |
| 23 | |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 24 | typedef ::y2017::constants::Values::ShotParams ShotParams; |
| 25 | using ::frc971::control_loops::drivetrain_queue; |
| 26 | |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 27 | Superstructure::Superstructure( |
| 28 | control_loops::SuperstructureQueue *superstructure_queue) |
| 29 | : aos::controls::ControlLoop<control_loops::SuperstructureQueue>( |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 30 | superstructure_queue) { |
| 31 | shot_interpolation_table_ = |
| 32 | ::frc971::shooter_interpolation::InterpolationTable<ShotParams>({ |
| 33 | // { distance_to_target, { shot_angle, shot_power, indexer_velocity }}, |
| 34 | {1.21, {0.29, 301.0, -1.0 * M_PI}}, // table entry |
| 35 | {1.55, {0.305, 316.0, -1.1 * M_PI}}, // table entry |
| 36 | {1.82, {0.33, 325.0, -1.3 * M_PI}}, // table entry |
| 37 | {2.00, {0.34, 328.0, -1.4 * M_PI}}, // table entry |
| 38 | {2.28, {0.36, 338.0, -1.5 * M_PI}}, // table entry |
| 39 | {2.55, {0.395, 342.0, -1.8 * M_PI}}, // table entry |
| 40 | {2.81, {0.41, 351.0, -1.90 * M_PI}}, // table entry |
| 41 | }); |
| 42 | } |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 43 | |
| 44 | void Superstructure::RunIteration( |
| 45 | const control_loops::SuperstructureQueue::Goal *unsafe_goal, |
| 46 | const control_loops::SuperstructureQueue::Position *position, |
| 47 | control_loops::SuperstructureQueue::Output *output, |
| 48 | control_loops::SuperstructureQueue::Status *status) { |
| 49 | if (WasReset()) { |
| 50 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 51 | hood_.Reset(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 52 | intake_.Reset(); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 53 | shooter_.Reset(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 54 | column_.Reset(); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 57 | const vision::VisionStatus *vision_status = nullptr; |
| 58 | if (vision::vision_status.FetchLatest()) { |
| 59 | vision_status = vision::vision_status.get(); |
| 60 | } |
| 61 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 62 | // Create a copy of the goals so that we can modify them. |
| 63 | HoodGoal hood_goal; |
| 64 | ShooterGoal shooter_goal; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 65 | IndexerGoal indexer_goal; |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 66 | bool in_range = true; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 67 | if (unsafe_goal != nullptr) { |
| 68 | hood_goal = unsafe_goal->hood; |
| 69 | shooter_goal = unsafe_goal->shooter; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 70 | indexer_goal = unsafe_goal->indexer; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 71 | |
Austin Schuh | d85c66e | 2017-04-16 10:50:33 -0700 | [diff] [blame] | 72 | if (!unsafe_goal->use_vision_for_shots) { |
| 73 | distance_average_.Reset(); |
| 74 | } |
| 75 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 76 | distance_average_.Tick(::aos::monotonic_clock::now(), vision_status); |
| 77 | status->vision_distance = distance_average_.Get(); |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 78 | |
| 79 | // If we are moving too fast, disable shooting and clear the accumulator. |
| 80 | double robot_velocity = 0.0; |
| 81 | drivetrain_queue.status.FetchLatest(); |
| 82 | if (drivetrain_queue.status.get()) { |
| 83 | robot_velocity = drivetrain_queue.status->robot_speed; |
| 84 | } |
| 85 | |
| 86 | if (::std::abs(robot_velocity) > 0.2) { |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 87 | if (unsafe_goal->use_vision_for_shots) { |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 88 | LOG(INFO, "Moving too fast, resetting\n"); |
| 89 | } |
| 90 | distance_average_.Reset(); |
| 91 | } |
| 92 | if (distance_average_.Valid()) { |
| 93 | if (unsafe_goal->use_vision_for_shots) { |
| 94 | ShotParams shot_params; |
| 95 | if (shot_interpolation_table_.GetInRange( |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 96 | distance_average_.Get(), &shot_params)) { |
| 97 | hood_goal.angle = shot_params.angle; |
| 98 | shooter_goal.angular_velocity = shot_params.power; |
| 99 | if (indexer_goal.angular_velocity != 0.0) { |
| 100 | indexer_goal.angular_velocity = shot_params.indexer_velocity; |
| 101 | } |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 102 | } else { |
| 103 | in_range = false; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 104 | } |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 105 | } |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 106 | LOG(DEBUG, "VisionDistance %f, hood %f shooter %f, indexer %f * M_PI\n", |
| 107 | status->vision_distance, hood_goal.angle, |
| 108 | shooter_goal.angular_velocity, indexer_goal.angular_velocity / M_PI); |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 109 | } else { |
| 110 | LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance); |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 111 | if (unsafe_goal->use_vision_for_shots) { |
| 112 | in_range = false; |
| 113 | indexer_goal.angular_velocity = 0.0; |
| 114 | } |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | hood_.Iterate( |
| 119 | unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood), |
| 120 | output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood)); |
| 121 | shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr, |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 122 | &(position->theta_shooter), position->sent_time, |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 123 | output != nullptr ? &(output->voltage_shooter) : nullptr, |
| 124 | &(status->shooter)); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 125 | |
| 126 | // Implement collision avoidance by passing down a freeze or range restricting |
| 127 | // signal to the column and intake objects. |
| 128 | |
| 129 | // Wait until the column is ready before doing collision avoidance. |
| 130 | if (unsafe_goal && column_.state() == column::Column::State::RUNNING) { |
| 131 | if (!ignore_collisions_) { |
| 132 | // The turret is in a position (or wants to be in a position) where we |
| 133 | // need the intake out. Push it out. |
Austin Schuh | 3ae4743 | 2017-04-16 19:15:46 -0700 | [diff] [blame^] | 134 | const bool column_goal_not_safe = |
| 135 | unsafe_goal->turret.angle > column::Column::kTurretMax || |
| 136 | unsafe_goal->turret.angle < column::Column::kTurretMin; |
| 137 | const bool column_position_not_safe = |
| 138 | column_.turret_position() > column::Column::kTurretMax || |
| 139 | column_.turret_position() < column::Column::kTurretMin; |
| 140 | |
| 141 | if (column_goal_not_safe || column_position_not_safe) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 142 | intake_.set_min_position(column::Column::kIntakeZeroingMinDistance); |
| 143 | } else { |
| 144 | intake_.clear_min_position(); |
| 145 | } |
| 146 | // The intake is in a position where it could hit. Don't move the turret. |
| 147 | if (intake_.position() < column::Column::kIntakeZeroingMinDistance - |
| 148 | column::Column::kIntakeTolerance && |
Austin Schuh | 3ae4743 | 2017-04-16 19:15:46 -0700 | [diff] [blame^] | 149 | column_position_not_safe) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 150 | column_.set_freeze(true); |
| 151 | } else { |
| 152 | column_.set_freeze(false); |
| 153 | } |
| 154 | } else { |
| 155 | // If we are ignoring collisions, unfreeze and un-limit the min. |
| 156 | column_.set_freeze(false); |
| 157 | intake_.clear_min_position(); |
| 158 | } |
| 159 | } else { |
| 160 | column_.set_freeze(false); |
| 161 | } |
| 162 | |
| 163 | // Make some noise if someone left this set... |
| 164 | if (ignore_collisions_) { |
| 165 | LOG(ERROR, "Collisions ignored\n"); |
| 166 | } |
| 167 | |
| 168 | intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr, |
| 169 | &(position->intake), |
| 170 | output != nullptr ? &(output->voltage_intake) : nullptr, |
| 171 | &(status->intake)); |
| 172 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 173 | column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 174 | unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr, |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 175 | &(position->column), vision_status, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 176 | output != nullptr ? &(output->voltage_indexer) : nullptr, |
| 177 | output != nullptr ? &(output->voltage_turret) : nullptr, |
| 178 | &(status->indexer), &(status->turret), &intake_); |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 179 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 180 | status->estopped = |
| 181 | status->intake.estopped | status->hood.estopped | status->turret.estopped; |
| 182 | |
| 183 | status->zeroed = |
| 184 | status->intake.zeroed && status->hood.zeroed && status->turret.zeroed; |
| 185 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 186 | if (output && unsafe_goal) { |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 187 | output->gear_servo = |
| 188 | ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo)); |
| 189 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 190 | output->voltage_intake_rollers = |
| 191 | ::std::max(-kMaxIntakeRollerVoltage, |
| 192 | ::std::min(unsafe_goal->intake.voltage_rollers, |
| 193 | kMaxIntakeRollerVoltage)); |
| 194 | output->voltage_indexer_rollers = |
| 195 | ::std::max(-kMaxIndexerRollerVoltage, |
| 196 | ::std::min(unsafe_goal->indexer.voltage_rollers, |
| 197 | kMaxIndexerRollerVoltage)); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 198 | |
| 199 | // Set the lights on or off |
| 200 | output->lights_on = unsafe_goal->lights_on; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 201 | |
| 202 | if (status->estopped) { |
| 203 | output->red_light_on = true; |
| 204 | output->green_light_on = false; |
| 205 | output->blue_light_on = false; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 206 | } else if (!status->zeroed) { |
| 207 | output->red_light_on = false; |
| 208 | output->green_light_on = false; |
| 209 | output->blue_light_on = true; |
Austin Schuh | e8b0075 | 2017-04-16 19:14:31 -0700 | [diff] [blame] | 210 | } else if (status->turret.vision_tracking && in_range) { |
| 211 | output->red_light_on = false; |
| 212 | output->green_light_on = true; |
| 213 | output->blue_light_on = false; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 214 | } |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 215 | } |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace superstructure |
| 219 | } // namespace control_loops |
| 220 | } // namespace y2017 |