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