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