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