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" |
| 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/logging/logging.h" |
| 11 | #include "aos/network/team_number.h" |
| 12 | #include "aos/stl_mutex/stl_mutex.h" |
| 13 | #include "aos/time/time.h" |
| 14 | #include "aos/util/log_interval.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 15 | #include "aos/input/drivetrain_input.h" |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 16 | #include "aos/input/action_joystick_input.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 17 | #include "aos/init.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/auto.q.h" |
| 20 | #include "frc971/autonomous/base_autonomous_actor.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 21 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 22 | #include "y2018/control_loops/drivetrain/drivetrain_base.h" |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 23 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 24 | #include "y2018/control_loops/superstructure/superstructure.q.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; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 28 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 29 | using ::aos::events::ProtoTXUdpSocket; |
| 30 | using ::aos::events::RXUdpSocket; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 31 | using ::aos::input::driver_station::ButtonLocation; |
| 32 | using ::aos::input::driver_station::ControlBit; |
| 33 | using ::aos::input::driver_station::JoystickAxis; |
| 34 | using ::aos::input::driver_station::POVLocation; |
| 35 | using ::aos::input::DrivetrainInputReader; |
| 36 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 37 | using ::y2018::control_loops::superstructure::arm::FrontPoints; |
| 38 | using ::y2018::control_loops::superstructure::arm::BackPoints; |
| 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 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 87 | class Reader : public ::aos::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) |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 90 | : ::aos::input::ActionJoystickInput( |
| 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_( |
| 98 | event_loop->MakeFetcher< |
| 99 | ::y2018::control_loops::SuperstructureQueue::Position>( |
| 100 | ".frc971.control_loops.superstructure_queue.position")), |
| 101 | superstructure_status_fetcher_( |
| 102 | event_loop->MakeFetcher< |
| 103 | ::y2018::control_loops::SuperstructureQueue::Status>( |
| 104 | ".frc971.control_loops.superstructure_queue.status")), |
| 105 | superstructure_goal_sender_( |
| 106 | event_loop |
| 107 | ->MakeSender<::y2018::control_loops::SuperstructureQueue::Goal>( |
| 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 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 115 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 116 | superstructure_position_fetcher_.Fetch(); |
| 117 | superstructure_status_fetcher_.Fetch(); |
| 118 | if (!superstructure_status_fetcher_.get() || |
| 119 | !superstructure_position_fetcher_.get()) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 120 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 121 | return; |
| 122 | } |
| 123 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 124 | auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 125 | |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame] | 126 | new_superstructure_goal->intake.left_intake_angle = intake_goal_; |
| 127 | new_superstructure_goal->intake.right_intake_angle = intake_goal_; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 128 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 129 | if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) || |
| 130 | data.PosEdge(kIntakeClosed)) { |
| 131 | vision_control_.set_high_video(false); |
| 132 | } |
| 133 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 134 | if (data.IsPressed(kIntakeIn)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 135 | // Turn on the rollers. |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 136 | new_superstructure_goal->intake.roller_voltage = 8.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 137 | } else if (data.IsPressed(kIntakeOut)) { |
| 138 | // Turn off the rollers. |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 139 | new_superstructure_goal->intake.roller_voltage = -12.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 140 | } else { |
| 141 | // We don't want the rollers on. |
| 142 | new_superstructure_goal->intake.roller_voltage = 0.0; |
| 143 | } |
| 144 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 145 | if (data.IsPressed(kSmallBox)) { |
| 146 | // Deploy the intake. |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 147 | if (superstructure_position_fetcher_->box_back_beambreak_triggered) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 148 | intake_goal_ = 0.30; |
| 149 | } else { |
| 150 | if (new_superstructure_goal->intake.roller_voltage > 0.1 && |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 151 | superstructure_position_fetcher_->box_distance < 0.15) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 152 | intake_goal_ = 0.18; |
| 153 | } else { |
| 154 | intake_goal_ = -0.60; |
| 155 | } |
| 156 | } |
| 157 | } else if (data.IsPressed(kIntakeClosed)) { |
| 158 | // Deploy the intake. |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 159 | if (superstructure_position_fetcher_->box_back_beambreak_triggered) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 160 | intake_goal_ = 0.30; |
| 161 | } else { |
| 162 | if (new_superstructure_goal->intake.roller_voltage > 0.1) { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 163 | if (superstructure_position_fetcher_->box_distance < 0.10) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 164 | new_superstructure_goal->intake.roller_voltage -= 3.0; |
| 165 | intake_goal_ = 0.22; |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 166 | } else if (superstructure_position_fetcher_->box_distance < 0.17) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 167 | intake_goal_ = 0.13; |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 168 | } else if (superstructure_position_fetcher_->box_distance < 0.25) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 169 | intake_goal_ = 0.05; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 170 | } else { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 171 | intake_goal_ = -0.10; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 172 | } |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 173 | if (robot_velocity() < -0.1 && |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 174 | superstructure_position_fetcher_->box_distance > 0.15) { |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 175 | intake_goal_ += 0.10; |
| 176 | } |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 177 | } else { |
| 178 | intake_goal_ = -0.60; |
| 179 | } |
| 180 | } |
| 181 | } else { |
| 182 | // Bring in the intake. |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 183 | intake_goal_ = -3.20; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 186 | if (new_superstructure_goal->intake.roller_voltage > 0.1 && |
| 187 | intake_goal_ > 0.0) { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 188 | if (superstructure_position_fetcher_->box_distance < 0.10) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 189 | new_superstructure_goal->intake.roller_voltage -= 3.0; |
| 190 | } |
| 191 | new_superstructure_goal->intake.roller_voltage += 3.0; |
| 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_) { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 197 | arm_goal_position_ = 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 = |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 206 | superstructure_status_fetcher_->arm.current_node == |
| 207 | arm_goal_position_ && |
| 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(); |
| 295 | new_superstructure_goal->trajectory_override = true; |
| 296 | } else if (data.IsPressed(kEmergencyUp)) { |
| 297 | arm_goal_position_ = arm::UpIndex(); |
| 298 | new_superstructure_goal->trajectory_override = true; |
| 299 | } else { |
| 300 | new_superstructure_goal->trajectory_override = false; |
| 301 | } |
| 302 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 303 | new_superstructure_goal->deploy_fork = |
| 304 | data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen); |
| 305 | |
| 306 | if (new_superstructure_goal->deploy_fork) { |
| 307 | intake_goal_ = -2.0; |
| 308 | } |
| 309 | |
| 310 | if (data.IsPressed(kWinch)) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 311 | LOG(INFO, "Winching\n"); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 312 | new_superstructure_goal->voltage_winch = 12.0; |
| 313 | } else { |
| 314 | new_superstructure_goal->voltage_winch = 0.0; |
| 315 | } |
| 316 | |
| 317 | new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang); |
| 318 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 319 | new_superstructure_goal->arm_goal_position = arm_goal_position_; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 320 | if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) { |
| 321 | new_superstructure_goal->open_threshold = 0.35; |
| 322 | } else { |
| 323 | new_superstructure_goal->open_threshold = 0.0; |
| 324 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 325 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 326 | if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) || |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 327 | data.PosEdge(kArmPickupBoxFromIntake) || |
| 328 | (data.IsPressed(kClawOpen) && |
| 329 | (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 330 | new_superstructure_goal->open_claw = true; |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 331 | } else { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 332 | new_superstructure_goal->open_claw = false; |
| 333 | } |
| 334 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 335 | new_superstructure_goal->grab_box = grab_box; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 336 | |
| 337 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 338 | if (!new_superstructure_goal.Send()) { |
| 339 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 340 | } |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 341 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 342 | video_tx_->Send(vision_control_); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | private: |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 346 | uint32_t GetAutonomousMode() override { |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 347 | // 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] | 348 | return mode(); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame^] | 351 | ::aos::Fetcher<::y2018::control_loops::SuperstructureQueue::Position> |
| 352 | superstructure_position_fetcher_; |
| 353 | ::aos::Fetcher<::y2018::control_loops::SuperstructureQueue::Status> |
| 354 | superstructure_status_fetcher_; |
| 355 | ::aos::Sender<::y2018::control_loops::SuperstructureQueue::Goal> |
| 356 | superstructure_goal_sender_; |
| 357 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 358 | // Current goals to send to the robot. |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 359 | double intake_goal_ = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 360 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 361 | bool never_disabled_ = true; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 362 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 363 | uint32_t arm_goal_position_ = 0; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 364 | VisionControl vision_control_; |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 365 | |
| 366 | decltype(FrontPoints()) front_points_ = FrontPoints(); |
| 367 | decltype(BackPoints()) back_points_ = BackPoints(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 368 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 369 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | } // namespace joysticks |
| 373 | } // namespace input |
| 374 | } // namespace y2018 |
| 375 | |
| 376 | int main() { |
| 377 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 378 | ::aos::ShmEventLoop event_loop; |
| 379 | ::y2018::input::joysticks::Reader reader(&event_loop); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 380 | reader.Run(); |
| 381 | ::aos::Cleanup(); |
| 382 | } |