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