Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/controls/control_loops.q.h" |
| 4 | #include "frc971/control_loops/control_loops.q.h" |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 5 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 7 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 8 | #include "y2019/status_light.q.h" |
| 9 | |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 10 | namespace y2019 { |
| 11 | namespace control_loops { |
| 12 | namespace superstructure { |
| 13 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | void SendColors(float red, float green, float blue) { |
| 17 | auto new_status_light = status_light.MakeMessage(); |
| 18 | new_status_light->red = red; |
| 19 | new_status_light->green = green; |
| 20 | new_status_light->blue = blue; |
| 21 | |
| 22 | if (!new_status_light.Send()) { |
| 23 | LOG(ERROR, "Failed to send lights.\n"); |
| 24 | } |
| 25 | } |
| 26 | } // namespace |
| 27 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 28 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 29 | const ::std::string &name) |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 30 | : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name), |
| 31 | elevator_(constants::GetValues().elevator.subsystem_params), |
| 32 | wrist_(constants::GetValues().wrist.subsystem_params), |
| 33 | intake_(constants::GetValues().intake), |
| 34 | stilts_(constants::GetValues().stilts.subsystem_params) {} |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 35 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 36 | void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal, |
| 37 | const SuperstructureQueue::Position *position, |
| 38 | SuperstructureQueue::Output *output, |
| 39 | SuperstructureQueue::Status *status) { |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 40 | if (WasReset()) { |
| 41 | LOG(ERROR, "WPILib reset, restarting\n"); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 42 | elevator_.Reset(); |
| 43 | wrist_.Reset(); |
| 44 | intake_.Reset(); |
| 45 | stilts_.Reset(); |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 46 | } |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 47 | |
| 48 | elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr, |
| 49 | &(position->elevator), |
| 50 | output != nullptr ? &(output->elevator_voltage) : nullptr, |
| 51 | &(status->elevator)); |
| 52 | |
| 53 | wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr, |
| 54 | &(position->wrist), |
| 55 | output != nullptr ? &(output->wrist_voltage) : nullptr, |
| 56 | &(status->wrist)); |
| 57 | |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 58 | intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr, |
| 59 | &(position->intake_joint), |
| 60 | output != nullptr ? &(output->intake_joint_voltage) : nullptr, |
| 61 | &(status->intake)); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 62 | |
| 63 | stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr, |
| 64 | &(position->stilts), |
| 65 | output != nullptr ? &(output->stilts_voltage) : nullptr, |
| 66 | &(status->stilts)); |
| 67 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 68 | vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr, |
| 69 | position->suction_pressure, output, &(status->has_piece), |
| 70 | event_loop()); |
Austin Schuh | e835475 | 2019-02-17 14:59:05 -0800 | [diff] [blame] | 71 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 72 | status->zeroed = status->elevator.zeroed && status->wrist.zeroed && |
| 73 | status->intake.zeroed && status->stilts.zeroed; |
| 74 | |
| 75 | status->estopped = status->elevator.estopped || status->wrist.estopped || |
| 76 | status->intake.estopped || status->stilts.estopped; |
| 77 | |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 78 | if (output) { |
Austin Schuh | 85e2e91 | 2019-02-17 15:04:29 -0800 | [diff] [blame] | 79 | if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) { |
| 80 | output->intake_roller_voltage = unsafe_goal->roller_voltage; |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 81 | } else { |
| 82 | output->intake_roller_voltage = 0.0; |
| 83 | } |
| 84 | } |
| 85 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 86 | // TODO(theo) move these up when Iterate() is split |
| 87 | // update the goals |
| 88 | collision_avoidance_.UpdateGoal(status, unsafe_goal); |
| 89 | |
| 90 | elevator_.set_min_position(collision_avoidance_.min_elevator_goal()); |
| 91 | wrist_.set_min_position(collision_avoidance_.min_wrist_goal()); |
| 92 | wrist_.set_max_position(collision_avoidance_.max_wrist_goal()); |
| 93 | intake_.set_min_position(collision_avoidance_.min_intake_goal()); |
| 94 | intake_.set_max_position(collision_avoidance_.max_intake_goal()); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 95 | |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 96 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
| 97 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 98 | if (status && unsafe_goal) { |
| 99 | // Light Logic |
| 100 | if (status->estopped) { |
| 101 | // Estop is red |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 102 | SendColors(1.0, 0.0, 0.0); |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 103 | } else if (::frc971::control_loops::drivetrain_queue.status.get() && |
| 104 | ::frc971::control_loops::drivetrain_queue.status |
| 105 | ->line_follow_logging.frozen) { |
James Kuszmaul | 4c1d4f4 | 2019-03-24 13:10:00 -0700 | [diff] [blame] | 106 | // Vision align is flashing white for button pressed, purple for target |
| 107 | // acquired. |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 108 | ++line_blink_count_; |
| 109 | if (line_blink_count_ < 20) { |
James Kuszmaul | 4c1d4f4 | 2019-03-24 13:10:00 -0700 | [diff] [blame] | 110 | if (::frc971::control_loops::drivetrain_queue.status |
| 111 | ->line_follow_logging.have_target) { |
| 112 | SendColors(1.0, 0.0, 1.0); |
| 113 | } else { |
| 114 | SendColors(1.0, 1.0, 1.0); |
| 115 | } |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 116 | } else { |
| 117 | // And then flash with green if we have a game piece. |
| 118 | if (status->has_piece) { |
| 119 | SendColors(0.0, 1.0, 0.0); |
| 120 | } else { |
| 121 | SendColors(0.0, 0.0, 0.0); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (line_blink_count_ > 40) { |
| 126 | line_blink_count_ = 0; |
| 127 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 128 | } else { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 129 | line_blink_count_ = 0; |
| 130 | if (status->has_piece) { |
| 131 | // Green if we have a game piece. |
| 132 | SendColors(0.0, 1.0, 0.0); |
| 133 | } else if (unsafe_goal->suction.gamepiece_mode == 0 && |
| 134 | !status->has_piece) { |
| 135 | // Ball mode is orange |
| 136 | SendColors(1.0, 0.1, 0.0); |
| 137 | } else if (unsafe_goal->suction.gamepiece_mode == 1 && |
| 138 | !status->has_piece) { |
| 139 | // Disk mode is deep blue |
| 140 | SendColors(0.05, 0.1, 0.5); |
| 141 | } else { |
| 142 | SendColors(0.0, 0.0, 0.0); |
| 143 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace superstructure |
| 149 | } // namespace control_loops |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 150 | } // namespace y2019 |