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