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" |
| 5 | #include "y2017/constants.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 6 | #include "y2017/control_loops/superstructure/column/column.h" |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 7 | #include "y2017/control_loops/superstructure/hood/hood.h" |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 8 | #include "y2017/control_loops/superstructure/intake/intake.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 9 | #include "y2017/control_loops/superstructure/shooter/shooter.h" |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 10 | #include "y2017/vision/vision.q.h" |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 11 | |
| 12 | namespace y2017 { |
| 13 | namespace control_loops { |
| 14 | namespace superstructure { |
| 15 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 16 | namespace { |
| 17 | // The maximum voltage the intake roller will be allowed to use. |
| 18 | constexpr double kMaxIntakeRollerVoltage = 12.0; |
| 19 | constexpr double kMaxIndexerRollerVoltage = 12.0; |
| 20 | } // namespace |
| 21 | |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 22 | Superstructure::Superstructure( |
| 23 | control_loops::SuperstructureQueue *superstructure_queue) |
| 24 | : aos::controls::ControlLoop<control_loops::SuperstructureQueue>( |
| 25 | superstructure_queue) {} |
| 26 | |
| 27 | void Superstructure::RunIteration( |
| 28 | const control_loops::SuperstructureQueue::Goal *unsafe_goal, |
| 29 | const control_loops::SuperstructureQueue::Position *position, |
| 30 | control_loops::SuperstructureQueue::Output *output, |
| 31 | control_loops::SuperstructureQueue::Status *status) { |
| 32 | if (WasReset()) { |
| 33 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 34 | hood_.Reset(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 35 | intake_.Reset(); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 36 | shooter_.Reset(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 37 | column_.Reset(); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 40 | const vision::VisionStatus *vision_status = nullptr; |
| 41 | if (vision::vision_status.FetchLatest()) { |
| 42 | vision_status = vision::vision_status.get(); |
| 43 | } |
| 44 | |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 45 | // Create a copy of the goals so that we can modify them. |
| 46 | HoodGoal hood_goal; |
| 47 | ShooterGoal shooter_goal; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 48 | IndexerGoal indexer_goal; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 49 | if (unsafe_goal != nullptr) { |
| 50 | hood_goal = unsafe_goal->hood; |
| 51 | shooter_goal = unsafe_goal->shooter; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 52 | indexer_goal = unsafe_goal->indexer; |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 53 | |
| 54 | distance_average_.Tick(::aos::monotonic_clock::now(), vision_status); |
| 55 | status->vision_distance = distance_average_.Get(); |
| 56 | if (distance_average_.Valid()) { |
| 57 | LOG(DEBUG, "VisionDistance %f\n", status->vision_distance); |
| 58 | if (unsafe_goal->use_vision_for_shots) { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 59 | y2017::constants::Values::ShotParams shot_params; |
| 60 | if (constants::GetValues().shot_interpolation_table.GetInRange( |
| 61 | distance_average_.Get(), &shot_params)) { |
| 62 | hood_goal.angle = shot_params.angle; |
| 63 | shooter_goal.angular_velocity = shot_params.power; |
| 64 | if (indexer_goal.angular_velocity != 0.0) { |
| 65 | indexer_goal.angular_velocity = shot_params.indexer_velocity; |
| 66 | } |
| 67 | } |
Parker Schuh | 208a58d | 2017-04-12 20:51:38 -0700 | [diff] [blame] | 68 | } |
| 69 | } else { |
| 70 | LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | hood_.Iterate( |
| 75 | unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood), |
| 76 | output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood)); |
| 77 | shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr, |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 78 | &(position->theta_shooter), position->sent_time, |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 79 | output != nullptr ? &(output->voltage_shooter) : nullptr, |
| 80 | &(status->shooter)); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 81 | |
| 82 | // Implement collision avoidance by passing down a freeze or range restricting |
| 83 | // signal to the column and intake objects. |
| 84 | |
| 85 | // Wait until the column is ready before doing collision avoidance. |
| 86 | if (unsafe_goal && column_.state() == column::Column::State::RUNNING) { |
| 87 | if (!ignore_collisions_) { |
| 88 | // The turret is in a position (or wants to be in a position) where we |
| 89 | // need the intake out. Push it out. |
| 90 | if (::std::abs(unsafe_goal->turret.angle) > |
| 91 | column::Column::kTurretNearZero || |
| 92 | ::std::abs(column_.turret_position()) > |
| 93 | column::Column::kTurretNearZero) { |
| 94 | intake_.set_min_position(column::Column::kIntakeZeroingMinDistance); |
| 95 | } else { |
| 96 | intake_.clear_min_position(); |
| 97 | } |
| 98 | // The intake is in a position where it could hit. Don't move the turret. |
| 99 | if (intake_.position() < column::Column::kIntakeZeroingMinDistance - |
| 100 | column::Column::kIntakeTolerance && |
| 101 | ::std::abs(column_.turret_position()) > |
| 102 | column::Column::kTurretNearZero) { |
| 103 | column_.set_freeze(true); |
| 104 | } else { |
| 105 | column_.set_freeze(false); |
| 106 | } |
| 107 | } else { |
| 108 | // If we are ignoring collisions, unfreeze and un-limit the min. |
| 109 | column_.set_freeze(false); |
| 110 | intake_.clear_min_position(); |
| 111 | } |
| 112 | } else { |
| 113 | column_.set_freeze(false); |
| 114 | } |
| 115 | |
| 116 | // Make some noise if someone left this set... |
| 117 | if (ignore_collisions_) { |
| 118 | LOG(ERROR, "Collisions ignored\n"); |
| 119 | } |
| 120 | |
| 121 | intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr, |
| 122 | &(position->intake), |
| 123 | output != nullptr ? &(output->voltage_intake) : nullptr, |
| 124 | &(status->intake)); |
| 125 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 126 | column_.Iterate(unsafe_goal != nullptr ? &indexer_goal : nullptr, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 127 | unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr, |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 128 | &(position->column), vision_status, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 129 | output != nullptr ? &(output->voltage_indexer) : nullptr, |
| 130 | output != nullptr ? &(output->voltage_turret) : nullptr, |
| 131 | &(status->indexer), &(status->turret), &intake_); |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 132 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 133 | status->estopped = |
| 134 | status->intake.estopped | status->hood.estopped | status->turret.estopped; |
| 135 | |
| 136 | status->zeroed = |
| 137 | status->intake.zeroed && status->hood.zeroed && status->turret.zeroed; |
| 138 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 139 | if (output && unsafe_goal) { |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 140 | output->gear_servo = |
| 141 | ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo)); |
| 142 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 143 | output->voltage_intake_rollers = |
| 144 | ::std::max(-kMaxIntakeRollerVoltage, |
| 145 | ::std::min(unsafe_goal->intake.voltage_rollers, |
| 146 | kMaxIntakeRollerVoltage)); |
| 147 | output->voltage_indexer_rollers = |
| 148 | ::std::max(-kMaxIndexerRollerVoltage, |
| 149 | ::std::min(unsafe_goal->indexer.voltage_rollers, |
| 150 | kMaxIndexerRollerVoltage)); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 151 | |
| 152 | // Set the lights on or off |
| 153 | output->lights_on = unsafe_goal->lights_on; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 154 | |
| 155 | if (status->estopped) { |
| 156 | output->red_light_on = true; |
| 157 | output->green_light_on = false; |
| 158 | output->blue_light_on = false; |
| 159 | } else if (status->turret.vision_tracking) { |
| 160 | output->red_light_on = false; |
| 161 | output->green_light_on = true; |
| 162 | output->blue_light_on = false; |
| 163 | } else if (!status->zeroed) { |
| 164 | output->red_light_on = false; |
| 165 | output->green_light_on = false; |
| 166 | output->blue_light_on = true; |
| 167 | } |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 168 | } |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace superstructure |
| 172 | } // namespace control_loops |
| 173 | } // namespace y2017 |