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