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" |
| 16 | #include "aos/input/joystick_input.h" |
| 17 | #include "aos/linux_code/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; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 29 | using ::y2018::control_loops::superstructure_queue; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 30 | using ::aos::events::ProtoTXUdpSocket; |
| 31 | using ::aos::events::RXUdpSocket; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 32 | using ::aos::input::driver_station::ButtonLocation; |
| 33 | using ::aos::input::driver_station::ControlBit; |
| 34 | using ::aos::input::driver_station::JoystickAxis; |
| 35 | using ::aos::input::driver_station::POVLocation; |
| 36 | using ::aos::input::DrivetrainInputReader; |
| 37 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 38 | using ::y2018::control_loops::superstructure::arm::FrontPoints; |
| 39 | using ::y2018::control_loops::superstructure::arm::BackPoints; |
| 40 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 41 | namespace y2018 { |
| 42 | namespace input { |
| 43 | namespace joysticks { |
| 44 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 45 | namespace arm = ::y2018::control_loops::superstructure::arm; |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 46 | using google::protobuf::StringPrintf; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 47 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 48 | const ButtonLocation kIntakeClosed(3, 2); |
| 49 | const ButtonLocation kDuck(3, 9); |
| 50 | const ButtonLocation kSmallBox(3, 1); |
Austin Schuh | 47d7494 | 2018-03-04 01:15:59 -0800 | [diff] [blame] | 51 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 52 | const ButtonLocation kIntakeIn(3, 4); |
| 53 | const ButtonLocation kIntakeOut(3, 3); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 54 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 55 | const ButtonLocation kArmBackHighBox(4, 11); |
| 56 | const ButtonLocation kArmBackExtraHighBox(4, 1); |
| 57 | const ButtonLocation kArmBackMiddle2Box(4, 9); |
| 58 | const ButtonLocation kArmBackMiddle1Box(4, 7); |
| 59 | const ButtonLocation kArmBackLowBox(4, 5); |
| 60 | const ButtonLocation kArmBackSwitch(3, 7); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 61 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 62 | const ButtonLocation kArmFrontHighBox(4, 12); |
| 63 | const ButtonLocation kArmFrontExtraHighBox(3, 14); |
| 64 | const ButtonLocation kArmFrontMiddle2Box(4, 10); |
| 65 | const ButtonLocation kArmFrontMiddle1Box(4, 8); |
| 66 | const ButtonLocation kArmFrontLowBox(4, 6); |
| 67 | const ButtonLocation kArmFrontSwitch(3, 10); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 68 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 69 | const ButtonLocation kArmAboveHang(3, 15); |
| 70 | const ButtonLocation kArmBelowHang(3, 16); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 71 | |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 72 | const ButtonLocation kEmergencyUp(3, 5); |
| 73 | const ButtonLocation kEmergencyDown(3, 6); |
| 74 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 75 | const ButtonLocation kWinch(4, 2); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 76 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 77 | const ButtonLocation kArmNeutral(3, 8); |
| 78 | const ButtonLocation kArmUp(3, 11); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 79 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 80 | const ButtonLocation kArmStepUp(3, 13); |
| 81 | const ButtonLocation kArmStepDown(3, 12); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 82 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 83 | const ButtonLocation kArmPickupBoxFromIntake(4, 3); |
| 84 | |
| 85 | const ButtonLocation kClawOpen(4, 4); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 86 | const ButtonLocation kDriverClawOpen(2, 4); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 87 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 88 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 89 | |
| 90 | class Reader : public ::aos::input::JoystickInput { |
| 91 | public: |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 92 | Reader() { |
| 93 | const uint16_t team = ::aos::network::GetTeamNumber(); |
| 94 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 95 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 96 | team == 971 ? DrivetrainInputReader::InputType::kPistol |
| 97 | : DrivetrainInputReader::InputType::kSteeringWheel, |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 98 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig()); |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 99 | video_tx_.reset(new ProtoTXUdpSocket<VisionControl>( |
| 100 | StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000)); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 104 | bool last_auto_running = auto_running_; |
| 105 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 106 | data.GetControlBit(ControlBit::kEnabled); |
| 107 | if (auto_running_ != last_auto_running) { |
| 108 | if (auto_running_) { |
| 109 | StartAuto(); |
| 110 | } else { |
| 111 | StopAuto(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!auto_running_) { |
| 116 | HandleDrivetrain(data); |
| 117 | HandleTeleop(data); |
| 118 | } |
| 119 | |
| 120 | // Process any pending actions. |
| 121 | action_queue_.Tick(); |
| 122 | was_running_ = action_queue_.Running(); |
| 123 | } |
| 124 | |
| 125 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 126 | drivetrain_input_reader_->HandleDrivetrain(data); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 130 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 131 | action_queue_.CancelAllActions(); |
| 132 | LOG(DEBUG, "Canceling\n"); |
| 133 | } |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 134 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 135 | superstructure_queue.position.FetchLatest(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 136 | superstructure_queue.status.FetchLatest(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 137 | if (!superstructure_queue.status.get() || |
| 138 | !superstructure_queue.position.get()) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 139 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 140 | return; |
| 141 | } |
| 142 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 143 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 144 | |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame] | 145 | new_superstructure_goal->intake.left_intake_angle = intake_goal_; |
| 146 | new_superstructure_goal->intake.right_intake_angle = intake_goal_; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 148 | if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) || |
| 149 | data.PosEdge(kIntakeClosed)) { |
| 150 | vision_control_.set_high_video(false); |
| 151 | } |
| 152 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 153 | if (data.IsPressed(kIntakeIn)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 154 | // Turn on the rollers. |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 155 | new_superstructure_goal->intake.roller_voltage = 8.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 156 | } else if (data.IsPressed(kIntakeOut)) { |
| 157 | // Turn off the rollers. |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 158 | new_superstructure_goal->intake.roller_voltage = -12.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 159 | } else { |
| 160 | // We don't want the rollers on. |
| 161 | new_superstructure_goal->intake.roller_voltage = 0.0; |
| 162 | } |
| 163 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 164 | if (data.IsPressed(kSmallBox)) { |
| 165 | // Deploy the intake. |
| 166 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 167 | intake_goal_ = 0.30; |
| 168 | } else { |
| 169 | if (new_superstructure_goal->intake.roller_voltage > 0.1 && |
| 170 | superstructure_queue.position->box_distance < 0.15) { |
| 171 | intake_goal_ = 0.18; |
| 172 | } else { |
| 173 | intake_goal_ = -0.60; |
| 174 | } |
| 175 | } |
| 176 | } else if (data.IsPressed(kIntakeClosed)) { |
| 177 | // Deploy the intake. |
| 178 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 179 | intake_goal_ = 0.30; |
| 180 | } else { |
| 181 | if (new_superstructure_goal->intake.roller_voltage > 0.1) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 182 | if (superstructure_queue.position->box_distance < 0.10) { |
| 183 | new_superstructure_goal->intake.roller_voltage -= 3.0; |
| 184 | intake_goal_ = 0.22; |
| 185 | } else if (superstructure_queue.position->box_distance < 0.17) { |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 186 | intake_goal_ = 0.13; |
| 187 | } else if (superstructure_queue.position->box_distance < 0.25) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 188 | intake_goal_ = 0.05; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 189 | } else { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 190 | intake_goal_ = -0.10; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 191 | } |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 192 | if (drivetrain_input_reader_->robot_velocity() < -0.1 && |
| 193 | superstructure_queue.position->box_distance > 0.15) { |
| 194 | intake_goal_ += 0.10; |
| 195 | } |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 196 | } else { |
| 197 | intake_goal_ = -0.60; |
| 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | // Bring in the intake. |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 202 | intake_goal_ = -3.20; |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 205 | if (new_superstructure_goal->intake.roller_voltage > 0.1 && |
| 206 | intake_goal_ > 0.0) { |
| 207 | if (superstructure_queue.position->box_distance < 0.10) { |
| 208 | new_superstructure_goal->intake.roller_voltage -= 3.0; |
| 209 | } |
| 210 | new_superstructure_goal->intake.roller_voltage += 3.0; |
| 211 | } |
| 212 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 213 | // If we are disabled, stay at the node closest to where we start. This |
| 214 | // should remove long motions when enabled. |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 215 | if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) { |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 216 | arm_goal_position_ = superstructure_queue.status->arm.current_node; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 217 | never_disabled_ = false; |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 220 | bool grab_box = false; |
| 221 | if (data.IsPressed(kArmPickupBoxFromIntake)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 222 | grab_box = true; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 223 | } |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 224 | const bool near_goal = |
| 225 | superstructure_queue.status->arm.current_node == arm_goal_position_ && |
| 226 | superstructure_queue.status->arm.path_distance_to_go < 1e-3; |
Austin Schuh | 822a1e5 | 2018-04-06 22:50:21 -0700 | [diff] [blame] | 227 | if (data.IsPressed(kArmStepDown) && near_goal) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 228 | uint32_t *front_point = ::std::find( |
| 229 | front_points_.begin(), front_points_.end(), arm_goal_position_); |
| 230 | uint32_t *back_point = ::std::find( |
| 231 | back_points_.begin(), back_points_.end(), arm_goal_position_); |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 232 | if (front_point != front_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 233 | ++front_point; |
| 234 | if (front_point != front_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 235 | arm_goal_position_ = *front_point; |
| 236 | } |
| 237 | } else if (back_point != back_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 238 | ++back_point; |
| 239 | if (back_point != back_points_.end()) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 240 | arm_goal_position_ = *back_point; |
| 241 | } |
| 242 | } |
Austin Schuh | 822a1e5 | 2018-04-06 22:50:21 -0700 | [diff] [blame] | 243 | } else if (data.IsPressed(kArmStepUp) && near_goal) { |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 244 | const uint32_t *front_point = ::std::find( |
| 245 | front_points_.begin(), front_points_.end(), arm_goal_position_); |
| 246 | const uint32_t *back_point = ::std::find( |
| 247 | back_points_.begin(), back_points_.end(), arm_goal_position_); |
| 248 | if (front_point != front_points_.end()) { |
| 249 | if (front_point != front_points_.begin()) { |
| 250 | --front_point; |
| 251 | arm_goal_position_ = *front_point; |
| 252 | } |
| 253 | } else if (back_point != back_points_.end()) { |
| 254 | if (back_point != back_points_.begin()) { |
| 255 | --back_point; |
| 256 | arm_goal_position_ = *back_point; |
| 257 | } |
| 258 | } |
| 259 | } else if (data.PosEdge(kArmPickupBoxFromIntake)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 260 | vision_control_.set_high_video(false); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 261 | arm_goal_position_ = arm::NeutralIndex(); |
| 262 | } else if (data.IsPressed(kDuck)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 263 | vision_control_.set_high_video(false); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 264 | arm_goal_position_ = arm::DuckIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 265 | } else if (data.IsPressed(kArmNeutral)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 266 | vision_control_.set_high_video(false); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 267 | arm_goal_position_ = arm::NeutralIndex(); |
| 268 | } else if (data.IsPressed(kArmUp)) { |
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::UpIndex(); |
| 271 | } else if (data.IsPressed(kArmFrontSwitch)) { |
| 272 | arm_goal_position_ = arm::FrontSwitchIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 273 | } else if (data.IsPressed(kArmFrontExtraHighBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 274 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 275 | arm_goal_position_ = arm::FrontHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 276 | } else if (data.IsPressed(kArmFrontHighBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 277 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 278 | arm_goal_position_ = arm::FrontMiddle2BoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 279 | } else if (data.IsPressed(kArmFrontMiddle2Box)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 280 | vision_control_.set_high_video(true); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 281 | arm_goal_position_ = arm::FrontMiddle3BoxIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 282 | } else if (data.IsPressed(kArmFrontMiddle1Box)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 283 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 284 | arm_goal_position_ = arm::FrontMiddle1BoxIndex(); |
| 285 | } else if (data.IsPressed(kArmFrontLowBox)) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 286 | vision_control_.set_high_video(true); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 287 | arm_goal_position_ = arm::FrontLowBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 288 | } else if (data.IsPressed(kArmBackExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 289 | arm_goal_position_ = arm::BackHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 290 | } else if (data.IsPressed(kArmBackHighBox) || |
| 291 | data.IsPressed(kArmBackMiddle2Box)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 292 | arm_goal_position_ = arm::BackMiddle2BoxIndex(); |
| 293 | } else if (data.IsPressed(kArmBackMiddle1Box)) { |
| 294 | arm_goal_position_ = arm::BackMiddle1BoxIndex(); |
| 295 | } else if (data.IsPressed(kArmBackLowBox)) { |
| 296 | arm_goal_position_ = arm::BackLowBoxIndex(); |
| 297 | } else if (data.IsPressed(kArmBackSwitch)) { |
| 298 | arm_goal_position_ = arm::BackSwitchIndex(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 299 | } else if (data.IsPressed(kArmAboveHang)) { |
| 300 | if (data.IsPressed(kIntakeIn)) { |
| 301 | arm_goal_position_ = arm::SelfHangIndex(); |
| 302 | } else if (data.IsPressed(kIntakeOut)) { |
| 303 | arm_goal_position_ = arm::PartnerHangIndex(); |
| 304 | } else { |
| 305 | arm_goal_position_ = arm::AboveHangIndex(); |
| 306 | } |
| 307 | } else if (data.IsPressed(kArmBelowHang)) { |
| 308 | arm_goal_position_ = arm::BelowHangIndex(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 311 | if (data.IsPressed(kEmergencyDown)) { |
| 312 | arm_goal_position_ = arm::NeutralIndex(); |
| 313 | new_superstructure_goal->trajectory_override = true; |
| 314 | } else if (data.IsPressed(kEmergencyUp)) { |
| 315 | arm_goal_position_ = arm::UpIndex(); |
| 316 | new_superstructure_goal->trajectory_override = true; |
| 317 | } else { |
| 318 | new_superstructure_goal->trajectory_override = false; |
| 319 | } |
| 320 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 321 | new_superstructure_goal->deploy_fork = |
| 322 | data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen); |
| 323 | |
| 324 | if (new_superstructure_goal->deploy_fork) { |
| 325 | intake_goal_ = -2.0; |
| 326 | } |
| 327 | |
| 328 | if (data.IsPressed(kWinch)) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 329 | LOG(INFO, "Winching\n"); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 330 | new_superstructure_goal->voltage_winch = 12.0; |
| 331 | } else { |
| 332 | new_superstructure_goal->voltage_winch = 0.0; |
| 333 | } |
| 334 | |
| 335 | new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang); |
| 336 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 337 | new_superstructure_goal->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)) { |
| 339 | new_superstructure_goal->open_threshold = 0.35; |
| 340 | } else { |
| 341 | new_superstructure_goal->open_threshold = 0.0; |
| 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)))) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 348 | new_superstructure_goal->open_claw = true; |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 349 | } else { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 350 | new_superstructure_goal->open_claw = false; |
| 351 | } |
| 352 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 353 | new_superstructure_goal->grab_box = grab_box; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 354 | |
| 355 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 356 | if (!new_superstructure_goal.Send()) { |
| 357 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 358 | } |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 359 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 360 | video_tx_->Send(vision_control_); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | private: |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 364 | void StartAuto() { |
| 365 | LOG(INFO, "Starting auto mode\n"); |
| 366 | |
| 367 | ::frc971::autonomous::AutonomousActionParams params; |
| 368 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 369 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 370 | params.mode = ::frc971::autonomous::auto_mode->mode << 2; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 371 | } else { |
| 372 | LOG(WARNING, "no auto mode values\n"); |
| 373 | params.mode = 0; |
| 374 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 375 | // TODO(austin): use the mode later if we care. We don't care right now. |
| 376 | params.mode = static_cast<int>(::aos::joystick_state->switch_left) | |
| 377 | (static_cast<int>(::aos::joystick_state->scale_left) << 1); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 378 | action_queue_.EnqueueAction( |
| 379 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 380 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 381 | |
| 382 | void StopAuto() { |
| 383 | LOG(INFO, "Stopping auto mode\n"); |
| 384 | action_queue_.CancelAllActions(); |
| 385 | } |
| 386 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 387 | // Current goals to send to the robot. |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 388 | double intake_goal_ = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 389 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 390 | bool was_running_ = false; |
| 391 | bool auto_running_ = false; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 392 | bool never_disabled_ = true; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 393 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 394 | uint32_t arm_goal_position_ = 0; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 395 | VisionControl vision_control_; |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 396 | |
| 397 | decltype(FrontPoints()) front_points_ = FrontPoints(); |
| 398 | decltype(BackPoints()) back_points_ = BackPoints(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 399 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 400 | ::aos::common::actions::ActionQueue action_queue_; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 401 | |
Austin Schuh | a8de4a6 | 2018-09-03 18:04:28 -0700 | [diff] [blame] | 402 | ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 403 | }; |
| 404 | |
| 405 | } // namespace joysticks |
| 406 | } // namespace input |
| 407 | } // namespace y2018 |
| 408 | |
| 409 | int main() { |
| 410 | ::aos::Init(-1); |
| 411 | ::y2018::input::joysticks::Reader reader; |
| 412 | reader.Run(); |
| 413 | ::aos::Cleanup(); |
| 414 | } |