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; |
| 48 | if (unsafe_goal != nullptr) { |
| 49 | hood_goal = unsafe_goal->hood; |
| 50 | shooter_goal = unsafe_goal->shooter; |
| 51 | |
| 52 | distance_average_.Tick(::aos::monotonic_clock::now(), vision_status); |
| 53 | status->vision_distance = distance_average_.Get(); |
| 54 | if (distance_average_.Valid()) { |
| 55 | LOG(DEBUG, "VisionDistance %f\n", status->vision_distance); |
| 56 | if (unsafe_goal->use_vision_for_shots) { |
| 57 | auto shot_params = |
| 58 | constants::GetValues().shot_interpolation_table.GetShooterData( |
| 59 | distance_average_.Get()); |
| 60 | hood_goal.angle = shot_params.angle; |
| 61 | shooter_goal.angular_velocity = shot_params.power; |
| 62 | } |
| 63 | } else { |
| 64 | LOG(DEBUG, "VisionNotValid %f\n", status->vision_distance); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | hood_.Iterate( |
| 69 | unsafe_goal != nullptr ? &hood_goal : nullptr, &(position->hood), |
| 70 | output != nullptr ? &(output->voltage_hood) : nullptr, &(status->hood)); |
| 71 | shooter_.Iterate(unsafe_goal != nullptr ? &shooter_goal : nullptr, |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 72 | &(position->theta_shooter), position->sent_time, |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 73 | output != nullptr ? &(output->voltage_shooter) : nullptr, |
| 74 | &(status->shooter)); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 75 | |
| 76 | // Implement collision avoidance by passing down a freeze or range restricting |
| 77 | // signal to the column and intake objects. |
| 78 | |
| 79 | // Wait until the column is ready before doing collision avoidance. |
| 80 | if (unsafe_goal && column_.state() == column::Column::State::RUNNING) { |
| 81 | if (!ignore_collisions_) { |
| 82 | // The turret is in a position (or wants to be in a position) where we |
| 83 | // need the intake out. Push it out. |
| 84 | if (::std::abs(unsafe_goal->turret.angle) > |
| 85 | column::Column::kTurretNearZero || |
| 86 | ::std::abs(column_.turret_position()) > |
| 87 | column::Column::kTurretNearZero) { |
| 88 | intake_.set_min_position(column::Column::kIntakeZeroingMinDistance); |
| 89 | } else { |
| 90 | intake_.clear_min_position(); |
| 91 | } |
| 92 | // The intake is in a position where it could hit. Don't move the turret. |
| 93 | if (intake_.position() < column::Column::kIntakeZeroingMinDistance - |
| 94 | column::Column::kIntakeTolerance && |
| 95 | ::std::abs(column_.turret_position()) > |
| 96 | column::Column::kTurretNearZero) { |
| 97 | column_.set_freeze(true); |
| 98 | } else { |
| 99 | column_.set_freeze(false); |
| 100 | } |
| 101 | } else { |
| 102 | // If we are ignoring collisions, unfreeze and un-limit the min. |
| 103 | column_.set_freeze(false); |
| 104 | intake_.clear_min_position(); |
| 105 | } |
| 106 | } else { |
| 107 | column_.set_freeze(false); |
| 108 | } |
| 109 | |
| 110 | // Make some noise if someone left this set... |
| 111 | if (ignore_collisions_) { |
| 112 | LOG(ERROR, "Collisions ignored\n"); |
| 113 | } |
| 114 | |
| 115 | intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr, |
| 116 | &(position->intake), |
| 117 | output != nullptr ? &(output->voltage_intake) : nullptr, |
| 118 | &(status->intake)); |
| 119 | |
| 120 | column_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->indexer) : nullptr, |
| 121 | unsafe_goal != nullptr ? &(unsafe_goal->turret) : nullptr, |
Philipp Schrader | 8e3ac0f | 2017-04-09 23:36:17 +0000 | [diff] [blame] | 122 | &(position->column), vision_status, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 123 | output != nullptr ? &(output->voltage_indexer) : nullptr, |
| 124 | output != nullptr ? &(output->voltage_turret) : nullptr, |
| 125 | &(status->indexer), &(status->turret), &intake_); |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 126 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 127 | status->estopped = |
| 128 | status->intake.estopped | status->hood.estopped | status->turret.estopped; |
| 129 | |
| 130 | status->zeroed = |
| 131 | status->intake.zeroed && status->hood.zeroed && status->turret.zeroed; |
| 132 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 133 | if (output && unsafe_goal) { |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 134 | output->gear_servo = |
| 135 | ::std::min(1.0, ::std::max(0.0, unsafe_goal->intake.gear_servo)); |
| 136 | |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 137 | output->voltage_intake_rollers = |
| 138 | ::std::max(-kMaxIntakeRollerVoltage, |
| 139 | ::std::min(unsafe_goal->intake.voltage_rollers, |
| 140 | kMaxIntakeRollerVoltage)); |
| 141 | output->voltage_indexer_rollers = |
| 142 | ::std::max(-kMaxIndexerRollerVoltage, |
| 143 | ::std::min(unsafe_goal->indexer.voltage_rollers, |
| 144 | kMaxIndexerRollerVoltage)); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 145 | |
| 146 | // Set the lights on or off |
| 147 | output->lights_on = unsafe_goal->lights_on; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 148 | |
| 149 | if (status->estopped) { |
| 150 | output->red_light_on = true; |
| 151 | output->green_light_on = false; |
| 152 | output->blue_light_on = false; |
| 153 | } else if (status->turret.vision_tracking) { |
| 154 | output->red_light_on = false; |
| 155 | output->green_light_on = true; |
| 156 | output->blue_light_on = false; |
| 157 | } else if (!status->zeroed) { |
| 158 | output->red_light_on = false; |
| 159 | output->green_light_on = false; |
| 160 | output->blue_light_on = true; |
| 161 | } |
Campbell Crowley | 651c4b4 | 2017-02-17 22:30:50 -0800 | [diff] [blame] | 162 | } |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace superstructure |
| 166 | } // namespace control_loops |
| 167 | } // namespace y2017 |