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