Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/superstructure.h" |
| 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "aos/events/event_loop.h" |
| 4 | #include "frc971/control_loops/control_loops_generated.h" |
| 5 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "y2019/status_light_generated.h" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 8 | |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 9 | namespace y2019 { |
| 10 | namespace control_loops { |
| 11 | namespace superstructure { |
| 12 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 14 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 16 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 17 | const ::std::string &name) |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 18 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 19 | name), |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 20 | status_light_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | event_loop->MakeSender<::y2019::StatusLight>("/superstructure")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 22 | drivetrain_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Status>( |
| 24 | "/drivetrain")), |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 25 | elevator_(constants::GetValues().elevator.subsystem_params), |
| 26 | wrist_(constants::GetValues().wrist.subsystem_params), |
| 27 | intake_(constants::GetValues().intake), |
Austin Schuh | 59dddac | 2019-12-22 16:44:49 -0800 | [diff] [blame] | 28 | stilts_(constants::GetValues().stilts.subsystem_params) { |
| 29 | event_loop->SetRuntimeRealtimePriority(30); |
| 30 | } |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 31 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 33 | const Position *position, |
| 34 | aos::Sender<Output>::Builder *output, |
| 35 | aos::Sender<Status>::Builder *status) { |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 36 | if (WasReset()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 37 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 38 | elevator_.Reset(); |
| 39 | wrist_.Reset(); |
| 40 | intake_.Reset(); |
| 41 | stilts_.Reset(); |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 42 | } |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 43 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | OutputT output_struct; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 45 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 47 | elevator_status_offset = elevator_.Iterate( |
| 48 | unsafe_goal != nullptr ? unsafe_goal->elevator() : nullptr, |
| 49 | position->elevator(), |
| 50 | output != nullptr ? &(output_struct.elevator_voltage) : nullptr, |
| 51 | status->fbb()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 52 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 53 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 54 | wrist_status_offset = wrist_.Iterate( |
| 55 | unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr, |
| 56 | position->wrist(), |
| 57 | output != nullptr ? &(output_struct.wrist_voltage) : nullptr, |
| 58 | status->fbb()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 59 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset = |
| 61 | intake_.Iterate( |
| 62 | unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr, |
| 63 | position->intake_joint(), |
| 64 | output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr, |
| 65 | status->fbb()); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 66 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 68 | stilts_status_offset = stilts_.Iterate( |
| 69 | unsafe_goal != nullptr ? unsafe_goal->stilts() : nullptr, |
| 70 | position->stilts(), |
| 71 | output != nullptr ? &(output_struct.stilts_voltage) : nullptr, |
| 72 | status->fbb()); |
| 73 | |
| 74 | bool has_piece; |
| 75 | vacuum_.Iterate(unsafe_goal != nullptr ? unsafe_goal->suction() : nullptr, |
| 76 | position->suction_pressure(), &output_struct, &has_piece, |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 77 | event_loop()); |
Austin Schuh | e835475 | 2019-02-17 14:59:05 -0800 | [diff] [blame] | 78 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | bool zeroed; |
| 80 | bool estopped; |
| 81 | { |
| 82 | PotAndAbsoluteEncoderProfiledJointStatus *elevator_status = |
| 83 | GetMutableTemporaryPointer(*status->fbb(), elevator_status_offset); |
| 84 | PotAndAbsoluteEncoderProfiledJointStatus *wrist_status = |
| 85 | GetMutableTemporaryPointer(*status->fbb(), wrist_status_offset); |
| 86 | AbsoluteEncoderProfiledJointStatus *intake_status = |
| 87 | GetMutableTemporaryPointer(*status->fbb(), intake_status_offset); |
| 88 | PotAndAbsoluteEncoderProfiledJointStatus *stilts_status = |
| 89 | GetMutableTemporaryPointer(*status->fbb(), stilts_status_offset); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 90 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | zeroed = elevator_status->zeroed() && wrist_status->zeroed() && |
| 92 | intake_status->zeroed() && stilts_status->zeroed(); |
| 93 | |
| 94 | estopped = elevator_status->estopped() || wrist_status->estopped() || |
| 95 | intake_status->estopped() || stilts_status->estopped(); |
| 96 | } |
| 97 | |
| 98 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 99 | |
| 100 | status_builder.add_zeroed(zeroed); |
| 101 | status_builder.add_estopped(estopped); |
| 102 | status_builder.add_has_piece(has_piece); |
| 103 | |
| 104 | status_builder.add_elevator(elevator_status_offset); |
| 105 | status_builder.add_wrist(wrist_status_offset); |
| 106 | status_builder.add_intake(intake_status_offset); |
| 107 | status_builder.add_stilts(stilts_status_offset); |
| 108 | |
| 109 | flatbuffers::Offset<Status> status_offset = status_builder.Finish(); |
| 110 | |
| 111 | Status *status_flatbuffer = |
| 112 | GetMutableTemporaryPointer(*status->fbb(), status_offset); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 113 | |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 114 | if (output) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | if (unsafe_goal && |
| 116 | status_flatbuffer->intake()->position() > kMinIntakeAngleForRollers) { |
| 117 | output_struct.intake_roller_voltage = unsafe_goal->roller_voltage(); |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 118 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | output_struct.intake_roller_voltage = 0.0; |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 120 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 121 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 122 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Tyler Chatow | 993fe28 | 2019-04-06 22:24:36 -0700 | [diff] [blame] | 125 | if (unsafe_goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | if (!unsafe_goal->has_suction() || !unsafe_goal->suction()->grab_piece()) { |
Tyler Chatow | 993fe28 | 2019-04-06 22:24:36 -0700 | [diff] [blame] | 127 | wrist_.set_controller_index(0); |
| 128 | elevator_.set_controller_index(0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 129 | } else if (unsafe_goal->suction()->gamepiece_mode() == 0) { |
Tyler Chatow | 993fe28 | 2019-04-06 22:24:36 -0700 | [diff] [blame] | 130 | wrist_.set_controller_index(1); |
| 131 | elevator_.set_controller_index(1); |
| 132 | } else { |
| 133 | wrist_.set_controller_index(2); |
| 134 | elevator_.set_controller_index(2); |
| 135 | } |
| 136 | } |
| 137 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 138 | // TODO(theo) move these up when Iterate() is split |
| 139 | // update the goals |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | collision_avoidance_.UpdateGoal(status_flatbuffer, unsafe_goal); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 141 | |
| 142 | elevator_.set_min_position(collision_avoidance_.min_elevator_goal()); |
| 143 | wrist_.set_min_position(collision_avoidance_.min_wrist_goal()); |
| 144 | wrist_.set_max_position(collision_avoidance_.max_wrist_goal()); |
| 145 | intake_.set_min_position(collision_avoidance_.min_intake_goal()); |
| 146 | intake_.set_max_position(collision_avoidance_.max_intake_goal()); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 147 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 148 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 149 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 150 | if (status && unsafe_goal) { |
| 151 | // Light Logic |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | if (status_flatbuffer->estopped()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 153 | // Estop is red |
Sabina Davis | 77a11cf | 2019-03-09 18:20:26 -0800 | [diff] [blame] | 154 | SendColors(1.0, 0.0, 0.0); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 155 | } else if (drivetrain_status_fetcher_.get() && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 156 | drivetrain_status_fetcher_->line_follow_logging()->frozen()) { |
James Kuszmaul | 4c1d4f4 | 2019-03-24 13:10:00 -0700 | [diff] [blame] | 157 | // Vision align is flashing white for button pressed, purple for target |
| 158 | // acquired. |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 159 | ++line_blink_count_; |
| 160 | if (line_blink_count_ < 20) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 161 | if (drivetrain_status_fetcher_->line_follow_logging()->have_target()) { |
James Kuszmaul | 4c1d4f4 | 2019-03-24 13:10:00 -0700 | [diff] [blame] | 162 | SendColors(1.0, 0.0, 1.0); |
| 163 | } else { |
| 164 | SendColors(1.0, 1.0, 1.0); |
| 165 | } |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 166 | } else { |
| 167 | // And then flash with green if we have a game piece. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | if (status_flatbuffer->has_piece()) { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 169 | SendColors(0.0, 1.0, 0.0); |
| 170 | } else { |
| 171 | SendColors(0.0, 0.0, 0.0); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (line_blink_count_ > 40) { |
| 176 | line_blink_count_ = 0; |
| 177 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 178 | } else { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 179 | line_blink_count_ = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 180 | if (status_flatbuffer->has_piece()) { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 181 | // Green if we have a game piece. |
| 182 | SendColors(0.0, 1.0, 0.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 183 | } else if ((!unsafe_goal->has_suction() || |
| 184 | unsafe_goal->suction()->gamepiece_mode() == 0) && |
| 185 | !status_flatbuffer->has_piece()) { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 186 | // Ball mode is orange |
| 187 | SendColors(1.0, 0.1, 0.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 188 | } else if (unsafe_goal->has_suction() && |
| 189 | unsafe_goal->suction()->gamepiece_mode() == 1 && |
| 190 | !status_flatbuffer->has_piece()) { |
Austin Schuh | 194c43c | 2019-03-22 20:40:53 -0700 | [diff] [blame] | 191 | // Disk mode is deep blue |
| 192 | SendColors(0.05, 0.1, 0.5); |
| 193 | } else { |
| 194 | SendColors(0.0, 0.0, 0.0); |
| 195 | } |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 199 | (void)status->Send(status_offset); |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 202 | void Superstructure::SendColors(float red, float green, float blue) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 203 | auto builder = status_light_sender_.MakeBuilder(); |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 204 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 205 | StatusLight::Builder status_light_builder = |
| 206 | builder.MakeBuilder<StatusLight>(); |
| 207 | status_light_builder.add_red(red); |
| 208 | status_light_builder.add_green(green); |
| 209 | status_light_builder.add_blue(blue); |
| 210 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 211 | if (builder.Send(status_light_builder.Finish()) != |
| 212 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 213 | AOS_LOG(ERROR, "Failed to send lights.\n"); |
Austin Schuh | ff97355 | 2019-05-19 16:49:28 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 217 | } // namespace superstructure |
| 218 | } // namespace control_loops |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 219 | } // namespace y2019 |