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