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