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> |
| 5 | |
| 6 | #include "aos/common/actions/actions.h" |
| 7 | #include "aos/common/input/driver_station_data.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | #include "aos/common/time.h" |
| 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/input/drivetrain_input.h" |
| 12 | #include "aos/input/joystick_input.h" |
| 13 | #include "aos/linux_code/init.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 14 | #include "frc971/autonomous/auto.q.h" |
| 15 | #include "frc971/autonomous/base_autonomous_actor.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 17 | #include "y2018/control_loops/drivetrain/drivetrain_base.h" |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 18 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 19 | #include "y2018/control_loops/superstructure/superstructure.q.h" |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 20 | #include "y2018/status_light.q.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 21 | |
| 22 | using ::frc971::control_loops::drivetrain_queue; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 23 | using ::y2018::control_loops::superstructure_queue; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 24 | |
| 25 | using ::aos::input::driver_station::ButtonLocation; |
| 26 | using ::aos::input::driver_station::ControlBit; |
| 27 | using ::aos::input::driver_station::JoystickAxis; |
| 28 | using ::aos::input::driver_station::POVLocation; |
| 29 | using ::aos::input::DrivetrainInputReader; |
| 30 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 31 | using ::y2018::control_loops::superstructure::arm::FrontPoints; |
| 32 | using ::y2018::control_loops::superstructure::arm::BackPoints; |
| 33 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 34 | namespace y2018 { |
| 35 | namespace input { |
| 36 | namespace joysticks { |
| 37 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 38 | namespace arm = ::y2018::control_loops::superstructure::arm; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 39 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 40 | const ButtonLocation kIntakeClosed(3, 2); |
| 41 | const ButtonLocation kDuck(3, 9); |
| 42 | const ButtonLocation kSmallBox(3, 1); |
Austin Schuh | 47d7494 | 2018-03-04 01:15:59 -0800 | [diff] [blame] | 43 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 44 | const ButtonLocation kIntakeIn(3, 4); |
| 45 | const ButtonLocation kIntakeOut(3, 3); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 46 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 47 | const ButtonLocation kArmFrontHighBox(4, 11); |
| 48 | const ButtonLocation kArmFrontExtraHighBox(4, 1); |
| 49 | const ButtonLocation kArmFrontMiddle2Box(4, 9); |
| 50 | const ButtonLocation kArmFrontMiddle1Box(4, 7); |
| 51 | const ButtonLocation kArmFrontLowBox(4, 5); |
| 52 | const ButtonLocation kArmFrontSwitch(3, 7); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 53 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 54 | const ButtonLocation kArmBackHighBox(4, 12); |
| 55 | const ButtonLocation kArmBackExtraHighBox(3, 14); |
| 56 | const ButtonLocation kArmBackMiddle2Box(4, 10); |
| 57 | const ButtonLocation kArmBackMiddle1Box(4, 8); |
| 58 | const ButtonLocation kArmBackLowBox(4, 6); |
| 59 | const ButtonLocation kArmBackSwitch(3, 10); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 60 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 61 | const ButtonLocation kArmAboveHang(3, 15); |
| 62 | const ButtonLocation kArmBelowHang(3, 16); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 63 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 64 | const ButtonLocation kWinch(4, 2); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 65 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 66 | const ButtonLocation kArmNeutral(3, 8); |
| 67 | const ButtonLocation kArmUp(3, 11); |
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 kArmStepUp(3, 13); |
| 70 | const ButtonLocation kArmStepDown(3, 12); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 71 | |
Sabina Davis | 833ccf6 | 2018-04-06 20:52:31 -0700 | [diff] [blame] | 72 | const ButtonLocation kArmPickupBoxFromIntake(4, 3); |
| 73 | |
| 74 | const ButtonLocation kClawOpen(4, 4); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 75 | const ButtonLocation kDriverClawOpen(2, 4); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 76 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 77 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 78 | |
| 79 | class Reader : public ::aos::input::JoystickInput { |
| 80 | public: |
| 81 | Reader() { |
| 82 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 83 | DrivetrainInputReader::InputType::kPistol, |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 84 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig()); |
| 85 | } |
| 86 | |
| 87 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 88 | bool last_auto_running = auto_running_; |
| 89 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 90 | data.GetControlBit(ControlBit::kEnabled); |
| 91 | if (auto_running_ != last_auto_running) { |
| 92 | if (auto_running_) { |
| 93 | StartAuto(); |
| 94 | } else { |
| 95 | StopAuto(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (!auto_running_) { |
| 100 | HandleDrivetrain(data); |
| 101 | HandleTeleop(data); |
| 102 | } |
| 103 | |
| 104 | // Process any pending actions. |
| 105 | action_queue_.Tick(); |
| 106 | was_running_ = action_queue_.Running(); |
| 107 | } |
| 108 | |
| 109 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 110 | drivetrain_input_reader_->HandleDrivetrain(data); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 113 | void SendColors(float red, float green, float blue) { |
| 114 | auto new_status_light = status_light.MakeMessage(); |
| 115 | new_status_light->red = red; |
| 116 | new_status_light->green = green; |
| 117 | new_status_light->blue = blue; |
| 118 | |
| 119 | if (!new_status_light.Send()) { |
| 120 | LOG(ERROR, "Failed to send lights.\n"); |
| 121 | } |
| 122 | } |
| 123 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 124 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 125 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 126 | action_queue_.CancelAllActions(); |
| 127 | LOG(DEBUG, "Canceling\n"); |
| 128 | } |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 129 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 130 | superstructure_queue.position.FetchLatest(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 131 | superstructure_queue.status.FetchLatest(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 132 | if (!superstructure_queue.status.get() || |
| 133 | !superstructure_queue.position.get()) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 134 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 135 | return; |
| 136 | } |
| 137 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 138 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 139 | |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame] | 140 | new_superstructure_goal->intake.left_intake_angle = intake_goal_; |
| 141 | new_superstructure_goal->intake.right_intake_angle = intake_goal_; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 143 | if (data.IsPressed(kIntakeIn)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 144 | // Turn on the rollers. |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 145 | new_superstructure_goal->intake.roller_voltage = 8.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 146 | } else if (data.IsPressed(kIntakeOut)) { |
| 147 | // Turn off the rollers. |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 148 | new_superstructure_goal->intake.roller_voltage = -12.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 149 | } else { |
| 150 | // We don't want the rollers on. |
| 151 | new_superstructure_goal->intake.roller_voltage = 0.0; |
| 152 | } |
| 153 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 154 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 155 | SendColors(0.0, 0.5, 0.0); |
| 156 | } else if (superstructure_queue.position->box_distance < 0.2) { |
| 157 | SendColors(0.0, 0.0, 0.5); |
| 158 | } else { |
| 159 | SendColors(0.0, 0.0, 0.0); |
| 160 | } |
| 161 | |
| 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)) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 258 | arm_goal_position_ = arm::NeutralIndex(); |
| 259 | } else if (data.IsPressed(kDuck)) { |
| 260 | arm_goal_position_ = arm::DuckIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 261 | } else if (data.IsPressed(kArmNeutral)) { |
| 262 | arm_goal_position_ = arm::NeutralIndex(); |
| 263 | } else if (data.IsPressed(kArmUp)) { |
| 264 | arm_goal_position_ = arm::UpIndex(); |
| 265 | } else if (data.IsPressed(kArmFrontSwitch)) { |
| 266 | arm_goal_position_ = arm::FrontSwitchIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 267 | } else if (data.IsPressed(kArmFrontExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 268 | arm_goal_position_ = arm::FrontHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 269 | } else if (data.IsPressed(kArmFrontHighBox)) { |
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)) { |
| 272 | arm_goal_position_ = arm::FrontMiddle3BoxIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 273 | } else if (data.IsPressed(kArmFrontMiddle1Box)) { |
| 274 | arm_goal_position_ = arm::FrontMiddle1BoxIndex(); |
| 275 | } else if (data.IsPressed(kArmFrontLowBox)) { |
| 276 | arm_goal_position_ = arm::FrontLowBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 277 | } else if (data.IsPressed(kArmBackExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 278 | arm_goal_position_ = arm::BackHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 279 | } else if (data.IsPressed(kArmBackHighBox) || |
| 280 | data.IsPressed(kArmBackMiddle2Box)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 281 | arm_goal_position_ = arm::BackMiddle2BoxIndex(); |
| 282 | } else if (data.IsPressed(kArmBackMiddle1Box)) { |
| 283 | arm_goal_position_ = arm::BackMiddle1BoxIndex(); |
| 284 | } else if (data.IsPressed(kArmBackLowBox)) { |
| 285 | arm_goal_position_ = arm::BackLowBoxIndex(); |
| 286 | } else if (data.IsPressed(kArmBackSwitch)) { |
| 287 | arm_goal_position_ = arm::BackSwitchIndex(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 288 | } else if (data.IsPressed(kArmAboveHang)) { |
| 289 | if (data.IsPressed(kIntakeIn)) { |
| 290 | arm_goal_position_ = arm::SelfHangIndex(); |
| 291 | } else if (data.IsPressed(kIntakeOut)) { |
| 292 | arm_goal_position_ = arm::PartnerHangIndex(); |
| 293 | } else { |
| 294 | arm_goal_position_ = arm::AboveHangIndex(); |
| 295 | } |
| 296 | } else if (data.IsPressed(kArmBelowHang)) { |
| 297 | arm_goal_position_ = arm::BelowHangIndex(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 300 | new_superstructure_goal->deploy_fork = |
| 301 | data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen); |
| 302 | |
| 303 | if (new_superstructure_goal->deploy_fork) { |
| 304 | intake_goal_ = -2.0; |
| 305 | } |
| 306 | |
| 307 | if (data.IsPressed(kWinch)) { |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 308 | LOG(INFO, "Winching\n"); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 309 | new_superstructure_goal->voltage_winch = 12.0; |
| 310 | } else { |
| 311 | new_superstructure_goal->voltage_winch = 0.0; |
| 312 | } |
| 313 | |
| 314 | new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang); |
| 315 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 316 | new_superstructure_goal->arm_goal_position = arm_goal_position_; |
| 317 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 318 | if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) || |
| 319 | data.PosEdge(kArmPickupBoxFromIntake)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 320 | new_superstructure_goal->open_claw = true; |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 321 | } else { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 322 | new_superstructure_goal->open_claw = false; |
| 323 | } |
| 324 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 325 | new_superstructure_goal->grab_box = grab_box; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 326 | |
| 327 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 328 | if (!new_superstructure_goal.Send()) { |
| 329 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 330 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | private: |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 334 | void StartAuto() { |
| 335 | LOG(INFO, "Starting auto mode\n"); |
| 336 | |
| 337 | ::frc971::autonomous::AutonomousActionParams params; |
| 338 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 339 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 340 | params.mode = ::frc971::autonomous::auto_mode->mode << 2; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 341 | } else { |
| 342 | LOG(WARNING, "no auto mode values\n"); |
| 343 | params.mode = 0; |
| 344 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 345 | // TODO(austin): use the mode later if we care. We don't care right now. |
| 346 | params.mode = static_cast<int>(::aos::joystick_state->switch_left) | |
| 347 | (static_cast<int>(::aos::joystick_state->scale_left) << 1); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 348 | action_queue_.EnqueueAction( |
| 349 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 350 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 351 | |
| 352 | void StopAuto() { |
| 353 | LOG(INFO, "Stopping auto mode\n"); |
| 354 | action_queue_.CancelAllActions(); |
| 355 | } |
| 356 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 357 | // Current goals to send to the robot. |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 358 | double intake_goal_ = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 359 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 360 | bool was_running_ = false; |
| 361 | bool auto_running_ = false; |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 362 | bool never_disabled_ = true; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 363 | |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 364 | uint32_t arm_goal_position_ = 0; |
| 365 | |
| 366 | decltype(FrontPoints()) front_points_ = FrontPoints(); |
| 367 | decltype(BackPoints()) back_points_ = BackPoints(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 368 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 369 | ::aos::common::actions::ActionQueue action_queue_; |
| 370 | }; |
| 371 | |
| 372 | } // namespace joysticks |
| 373 | } // namespace input |
| 374 | } // namespace y2018 |
| 375 | |
| 376 | int main() { |
| 377 | ::aos::Init(-1); |
| 378 | ::y2018::input::joysticks::Reader reader; |
| 379 | reader.Run(); |
| 380 | ::aos::Cleanup(); |
| 381 | } |