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