James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 1 | #include <google/protobuf/stubs/stringprintf.h> |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 2 | #include <unistd.h> |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cmath> |
| 5 | #include <cstdio> |
| 6 | #include <cstring> |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 7 | #include <mutex> |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/actions/actions.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
| 12 | #include "aos/network/team_number.h" |
| 13 | #include "aos/stl_mutex/stl_mutex.h" |
| 14 | #include "aos/time/time.h" |
| 15 | #include "aos/util/log_interval.h" |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 16 | #include "aos/vision/events/udp.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 17 | #include "frc971/autonomous/base_autonomous_actor.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 18 | #include "frc971/input/action_joystick_input.h" |
| 19 | #include "frc971/input/driver_station_data.h" |
| 20 | #include "frc971/input/drivetrain_input.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 21 | #include "y2018/control_loops/drivetrain/drivetrain_base.h" |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 22 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | #include "y2018/control_loops/superstructure/superstructure_goal_generated.h" |
| 24 | #include "y2018/control_loops/superstructure/superstructure_position_generated.h" |
| 25 | #include "y2018/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 26 | #include "y2018/vision.pb.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 27 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 28 | using ::aos::monotonic_clock; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 29 | using ::aos::events::ProtoTXUdpSocket; |
| 30 | using ::aos::events::RXUdpSocket; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 31 | using ::frc971::input::DrivetrainInputReader; |
| 32 | using ::frc971::input::driver_station::ButtonLocation; |
| 33 | using ::frc971::input::driver_station::ControlBit; |
| 34 | using ::frc971::input::driver_station::JoystickAxis; |
| 35 | using ::frc971::input::driver_station::POVLocation; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 37 | using ::y2018::control_loops::superstructure::arm::BackPoints; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 38 | using ::y2018::control_loops::superstructure::arm::FrontPoints; |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 39 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 40 | namespace y2018 { |
| 41 | namespace input { |
| 42 | namespace joysticks { |
| 43 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 44 | namespace arm = ::y2018::control_loops::superstructure::arm; |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 45 | using google::protobuf::StringPrintf; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 46 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 47 | const ButtonLocation kIntakeClosed(3, 2); |
| 48 | const ButtonLocation kDuck(3, 9); |
| 49 | const ButtonLocation kSmallBox(3, 1); |
Austin Schuh | 47d7494 | 2018-03-04 01:15:59 -0800 | [diff] [blame] | 50 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 51 | const ButtonLocation kIntakeIn(3, 4); |
| 52 | const ButtonLocation kIntakeOut(3, 3); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 53 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 54 | const ButtonLocation kArmBackHighBox(4, 11); |
| 55 | const ButtonLocation kArmBackExtraHighBox(4, 1); |
| 56 | const ButtonLocation kArmBackMiddle2Box(4, 9); |
| 57 | const ButtonLocation kArmBackMiddle1Box(4, 7); |
| 58 | const ButtonLocation kArmBackLowBox(4, 5); |
| 59 | const ButtonLocation kArmBackSwitch(3, 7); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 60 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 61 | const ButtonLocation kArmFrontHighBox(4, 12); |
| 62 | const ButtonLocation kArmFrontExtraHighBox(3, 14); |
| 63 | const ButtonLocation kArmFrontMiddle2Box(4, 10); |
| 64 | const ButtonLocation kArmFrontMiddle1Box(4, 8); |
| 65 | const ButtonLocation kArmFrontLowBox(4, 6); |
| 66 | const ButtonLocation kArmFrontSwitch(3, 10); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 67 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 68 | const ButtonLocation kArmAboveHang(3, 15); |
| 69 | const ButtonLocation kArmBelowHang(3, 16); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 70 | |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 71 | const ButtonLocation kEmergencyUp(3, 5); |
| 72 | const ButtonLocation kEmergencyDown(3, 6); |
| 73 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 74 | const ButtonLocation kWinch(4, 2); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 75 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 76 | const ButtonLocation kArmNeutral(3, 8); |
| 77 | const ButtonLocation kArmUp(3, 11); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 78 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 79 | const ButtonLocation kArmStepUp(3, 13); |
| 80 | const ButtonLocation kArmStepDown(3, 12); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 81 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 82 | const ButtonLocation kArmPickupBoxFromIntake(4, 3); |
| 83 | |
| 84 | const ButtonLocation kClawOpen(4, 4); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 85 | const ButtonLocation kDriverClawOpen(2, 4); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 86 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 87 | class Reader : public ::frc971::input::ActionJoystickInput { |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 88 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 89 | Reader(::aos::EventLoop *event_loop) |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 90 | : ::frc971::input::ActionJoystickInput( |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 91 | event_loop, |
| 92 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig(), |
| 93 | ::aos::network::GetTeamNumber() == 971 |
| 94 | ? DrivetrainInputReader::InputType::kPistol |
| 95 | : DrivetrainInputReader::InputType::kSteeringWheel, |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 96 | {}), |
| 97 | superstructure_position_fetcher_( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 98 | event_loop |
| 99 | ->MakeFetcher<::y2018::control_loops::superstructure::Position>( |
| 100 | ".frc971.control_loops.superstructure_queue.position")), |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 101 | superstructure_status_fetcher_( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 102 | event_loop |
| 103 | ->MakeFetcher<::y2018::control_loops::superstructure::Status>( |
| 104 | ".frc971.control_loops.superstructure_queue.status")), |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 105 | superstructure_goal_sender_( |
| 106 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 107 | ->MakeSender<::y2018::control_loops::superstructure::Goal>( |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 108 | ".frc971.control_loops.superstructure_queue.goal")) { |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 109 | const uint16_t team = ::aos::network::GetTeamNumber(); |
| 110 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 111 | video_tx_.reset(new ProtoTXUdpSocket<VisionControl>( |
| 112 | StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000)); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 113 | } |
| 114 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 115 | void HandleTeleop( |
| 116 | const ::frc971::input::driver_station::Data &data) override { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 117 | superstructure_position_fetcher_.Fetch(); |
| 118 | superstructure_status_fetcher_.Fetch(); |
| 119 | if (!superstructure_status_fetcher_.get() || |
| 120 | !superstructure_position_fetcher_.get()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 121 | AOS_LOG(ERROR, "Got no superstructure status packet.\n"); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 125 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 126 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 127 | double roller_voltage = 0.0; |
| 128 | bool trajectory_override = false; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 129 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 130 | if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) || |
| 131 | data.PosEdge(kIntakeClosed)) { |
| 132 | vision_control_.set_high_video(false); |
| 133 | } |
| 134 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 135 | if (data.IsPressed(kIntakeIn)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 136 | // Turn on the rollers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 137 | roller_voltage = 8.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 138 | } else if (data.IsPressed(kIntakeOut)) { |
| 139 | // Turn off the rollers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | roller_voltage = -12.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 141 | } else { |
| 142 | // We don't want the rollers on. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | roller_voltage = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 146 | if (data.IsPressed(kSmallBox)) { |
| 147 | // Deploy the intake. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | if (superstructure_position_fetcher_->box_back_beambreak_triggered()) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 149 | intake_goal_ = 0.30; |
| 150 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | if (roller_voltage > 0.1 && |
| 152 | superstructure_position_fetcher_->box_distance() < 0.15) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 153 | intake_goal_ = 0.18; |
| 154 | } else { |
| 155 | intake_goal_ = -0.60; |
| 156 | } |
| 157 | } |
| 158 | } else if (data.IsPressed(kIntakeClosed)) { |
| 159 | // Deploy the intake. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | if (superstructure_position_fetcher_->box_back_beambreak_triggered()) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 161 | intake_goal_ = 0.30; |
| 162 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | if (roller_voltage > 0.1) { |
| 164 | if (superstructure_position_fetcher_->box_distance() < 0.10) { |
| 165 | roller_voltage -= 3.0; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 166 | intake_goal_ = 0.22; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 167 | } else if (superstructure_position_fetcher_->box_distance() < 0.17) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 168 | intake_goal_ = 0.13; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | } else if (superstructure_position_fetcher_->box_distance() < 0.25) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 170 | intake_goal_ = 0.05; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 171 | } else { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 172 | intake_goal_ = -0.10; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 173 | } |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 174 | if (robot_velocity() < -0.1 && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 175 | superstructure_position_fetcher_->box_distance() > 0.15) { |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 176 | intake_goal_ += 0.10; |
| 177 | } |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 178 | } else { |
| 179 | intake_goal_ = -0.60; |
| 180 | } |
| 181 | } |
| 182 | } else { |
| 183 | // Bring in the intake. |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 184 | intake_goal_ = -3.20; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 185 | } |
| 186 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 187 | if (roller_voltage > 0.1 && intake_goal_ > 0.0) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 188 | if (superstructure_position_fetcher_->box_distance() < 0.10) { |
| 189 | roller_voltage -= 3.0; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 190 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | roller_voltage += 3.0; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 194 | // If we are disabled, stay at the node closest to where we start. This |
| 195 | // should remove long motions when enabled. |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 196 | if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | arm_goal_position_ = |
| 198 | superstructure_status_fetcher_->arm()->current_node(); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 199 | never_disabled_ = false; |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 202 | bool grab_box = false; |
| 203 | if (data.IsPressed(kArmPickupBoxFromIntake)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 204 | grab_box = true; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 205 | } |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 206 | const bool near_goal = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 207 | superstructure_status_fetcher_->arm()->current_node() == |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 208 | arm_goal_position_ && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 209 | superstructure_status_fetcher_->arm()->path_distance_to_go() < 1e-3; |
Austin Schuh | 822a1e5 | 2018-04-06 22:50:21 -0700 | [diff] [blame] | 210 | if (data.IsPressed(kArmStepDown) && near_goal) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 211 | uint32_t *front_point = ::std::find( |
| 212 | front_points_.begin(), front_points_.end(), arm_goal_position_); |
| 213 | uint32_t *back_point = ::std::find( |
| 214 | back_points_.begin(), back_points_.end(), arm_goal_position_); |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 215 | if (front_point != front_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 216 | ++front_point; |
| 217 | if (front_point != front_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 218 | arm_goal_position_ = *front_point; |
| 219 | } |
| 220 | } else if (back_point != back_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 221 | ++back_point; |
| 222 | if (back_point != back_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 223 | arm_goal_position_ = *back_point; |
| 224 | } |
| 225 | } |
Austin Schuh | 822a1e5 | 2018-04-06 22:50:21 -0700 | [diff] [blame] | 226 | } else if (data.IsPressed(kArmStepUp) && near_goal) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 227 | const uint32_t *front_point = ::std::find( |
| 228 | front_points_.begin(), front_points_.end(), arm_goal_position_); |
| 229 | const uint32_t *back_point = ::std::find( |
| 230 | back_points_.begin(), back_points_.end(), arm_goal_position_); |
| 231 | if (front_point != front_points_.end()) { |
| 232 | if (front_point != front_points_.begin()) { |
| 233 | --front_point; |
| 234 | arm_goal_position_ = *front_point; |
| 235 | } |
| 236 | } else if (back_point != back_points_.end()) { |
| 237 | if (back_point != back_points_.begin()) { |
| 238 | --back_point; |
| 239 | arm_goal_position_ = *back_point; |
| 240 | } |
| 241 | } |
| 242 | } else if (data.PosEdge(kArmPickupBoxFromIntake)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 243 | vision_control_.set_high_video(false); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 244 | arm_goal_position_ = arm::NeutralIndex(); |
| 245 | } else if (data.IsPressed(kDuck)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 246 | vision_control_.set_high_video(false); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 247 | arm_goal_position_ = arm::DuckIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 248 | } else if (data.IsPressed(kArmNeutral)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 249 | vision_control_.set_high_video(false); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 250 | arm_goal_position_ = arm::NeutralIndex(); |
| 251 | } else if (data.IsPressed(kArmUp)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 252 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 253 | arm_goal_position_ = arm::UpIndex(); |
| 254 | } else if (data.IsPressed(kArmFrontSwitch)) { |
| 255 | arm_goal_position_ = arm::FrontSwitchIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 256 | } else if (data.IsPressed(kArmFrontExtraHighBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 257 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 258 | arm_goal_position_ = arm::FrontHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 259 | } else if (data.IsPressed(kArmFrontHighBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 260 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 261 | arm_goal_position_ = arm::FrontMiddle2BoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 262 | } else if (data.IsPressed(kArmFrontMiddle2Box)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 263 | vision_control_.set_high_video(true); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 264 | arm_goal_position_ = arm::FrontMiddle3BoxIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 265 | } else if (data.IsPressed(kArmFrontMiddle1Box)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 266 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 267 | arm_goal_position_ = arm::FrontMiddle1BoxIndex(); |
| 268 | } else if (data.IsPressed(kArmFrontLowBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 269 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 270 | arm_goal_position_ = arm::FrontLowBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 271 | } else if (data.IsPressed(kArmBackExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 272 | arm_goal_position_ = arm::BackHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 273 | } else if (data.IsPressed(kArmBackHighBox) || |
| 274 | data.IsPressed(kArmBackMiddle2Box)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 275 | arm_goal_position_ = arm::BackMiddle2BoxIndex(); |
| 276 | } else if (data.IsPressed(kArmBackMiddle1Box)) { |
| 277 | arm_goal_position_ = arm::BackMiddle1BoxIndex(); |
| 278 | } else if (data.IsPressed(kArmBackLowBox)) { |
| 279 | arm_goal_position_ = arm::BackLowBoxIndex(); |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 280 | } else if (data.IsPressed(kArmBackSwitch)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 281 | arm_goal_position_ = arm::BackSwitchIndex(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 282 | } else if (data.IsPressed(kArmAboveHang)) { |
| 283 | if (data.IsPressed(kIntakeIn)) { |
| 284 | arm_goal_position_ = arm::SelfHangIndex(); |
| 285 | } else if (data.IsPressed(kIntakeOut)) { |
| 286 | arm_goal_position_ = arm::PartnerHangIndex(); |
| 287 | } else { |
| 288 | arm_goal_position_ = arm::AboveHangIndex(); |
| 289 | } |
| 290 | } else if (data.IsPressed(kArmBelowHang)) { |
| 291 | arm_goal_position_ = arm::BelowHangIndex(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 294 | if (data.IsPressed(kEmergencyDown)) { |
| 295 | arm_goal_position_ = arm::NeutralIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 296 | trajectory_override = true; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 297 | } else if (data.IsPressed(kEmergencyUp)) { |
| 298 | arm_goal_position_ = arm::UpIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 299 | trajectory_override = true; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 300 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | trajectory_override = false; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 304 | const bool deploy_fork = |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 305 | data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen); |
| 306 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 307 | if (deploy_fork) { |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 308 | intake_goal_ = -2.0; |
| 309 | } |
| 310 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 311 | control_loops::superstructure::IntakeGoal::Builder intake_goal_builder = |
| 312 | builder.MakeBuilder<control_loops::superstructure::IntakeGoal>(); |
| 313 | |
| 314 | intake_goal_builder.add_left_intake_angle(intake_goal_); |
| 315 | intake_goal_builder.add_right_intake_angle(intake_goal_); |
| 316 | intake_goal_builder.add_roller_voltage(roller_voltage); |
| 317 | |
| 318 | flatbuffers::Offset<control_loops::superstructure::IntakeGoal> |
| 319 | intake_goal_offset = intake_goal_builder.Finish(); |
| 320 | |
| 321 | control_loops::superstructure::Goal::Builder superstructure_builder = |
| 322 | builder.MakeBuilder<control_loops::superstructure::Goal>(); |
| 323 | |
| 324 | superstructure_builder.add_intake(intake_goal_offset); |
| 325 | superstructure_builder.add_grab_box(grab_box); |
| 326 | superstructure_builder.add_trajectory_override(trajectory_override); |
| 327 | superstructure_builder.add_deploy_fork(deploy_fork); |
| 328 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 329 | if (data.IsPressed(kWinch)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 330 | AOS_LOG(INFO, "Winching\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | superstructure_builder.add_voltage_winch(12.0); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 332 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 333 | superstructure_builder.add_voltage_winch(0.0); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 336 | superstructure_builder.add_hook_release(data.IsPressed(kArmBelowHang)); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 337 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 338 | superstructure_builder.add_arm_goal_position(arm_goal_position_); |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 339 | if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 340 | superstructure_builder.add_open_threshold(0.35); |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 341 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | superstructure_builder.add_open_threshold(0.0); |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 343 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 344 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 345 | if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) || |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 346 | data.PosEdge(kArmPickupBoxFromIntake) || |
| 347 | (data.IsPressed(kClawOpen) && |
| 348 | (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 349 | superstructure_builder.add_open_claw(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 350 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | superstructure_builder.add_open_claw(false); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 352 | } |
| 353 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 354 | if (builder.Send(superstructure_builder.Finish()) != |
| 355 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 356 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 357 | } |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 358 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 359 | video_tx_->Send(vision_control_); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | private: |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 363 | uint32_t GetAutonomousMode() override { |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 364 | // Low bit is switch, high bit is scale. 1 means left, 0 means right. |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 365 | return mode(); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | ::aos::Fetcher<control_loops::superstructure::Position> |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 369 | superstructure_position_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 370 | ::aos::Fetcher<control_loops::superstructure::Status> |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 371 | superstructure_status_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 372 | ::aos::Sender<control_loops::superstructure::Goal> |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 373 | superstructure_goal_sender_; |
| 374 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 375 | // Current goals to send to the robot. |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 376 | double intake_goal_ = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 377 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 378 | bool never_disabled_ = true; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 379 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 380 | uint32_t arm_goal_position_ = 0; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 381 | VisionControl vision_control_; |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 382 | |
| 383 | decltype(FrontPoints()) front_points_ = FrontPoints(); |
| 384 | decltype(BackPoints()) back_points_ = BackPoints(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 385 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 386 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | } // namespace joysticks |
| 390 | } // namespace input |
| 391 | } // namespace y2018 |
| 392 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 393 | int main(int argc, char **argv) { |
| 394 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 395 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 396 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame^] | 397 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 398 | |
| 399 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 400 | ::y2018::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 401 | |
| 402 | event_loop.Run(); |
| 403 | |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 404 | return 0; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 405 | } |