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 | |
| 31 | namespace y2018 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 35 | namespace arm = ::y2018::control_loops::superstructure::arm; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 36 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 37 | const ButtonLocation kIntakeClosed(4, 1); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 38 | const ButtonLocation kSmallBox(4, 4); |
Austin Schuh | 47d7494 | 2018-03-04 01:15:59 -0800 | [diff] [blame] | 39 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 40 | const ButtonLocation kIntakeIn(3, 16); |
| 41 | const ButtonLocation kIntakeOut(4, 3); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 42 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 43 | const ButtonLocation kArmFrontHighBox(4, 5); |
| 44 | const ButtonLocation kArmFrontExtraHighBox(3, 8); |
| 45 | const ButtonLocation kArmFrontMiddle2Box(4, 7); |
| 46 | const ButtonLocation kArmFrontMiddle1Box(4, 9); |
| 47 | const ButtonLocation kArmFrontLowBox(4, 11); |
| 48 | const ButtonLocation kArmFrontSwitch(3, 14); |
| 49 | |
| 50 | const ButtonLocation kArmBackHighBox(4, 6); |
| 51 | const ButtonLocation kArmBackExtraHighBox(3, 10); |
| 52 | const ButtonLocation kArmBackMiddle2Box(4, 8); |
| 53 | const ButtonLocation kArmBackMiddle1Box(4, 10); |
| 54 | const ButtonLocation kArmBackLowBox(4, 12); |
| 55 | const ButtonLocation kArmBackSwitch(3, 12); |
| 56 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 57 | const ButtonLocation kArmAboveHang(3, 7); |
| 58 | const ButtonLocation kArmBelowHang(3, 2); |
| 59 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 60 | const ButtonLocation kWinch(3, 4); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 61 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 62 | const ButtonLocation kArmNeutral(3, 13); |
| 63 | const ButtonLocation kArmUp(3, 9); |
| 64 | |
| 65 | const ButtonLocation kArmPickupBoxFromIntake(3, 6); |
| 66 | |
| 67 | const ButtonLocation kClawOpen(3, 1); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 68 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 69 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 70 | |
| 71 | class Reader : public ::aos::input::JoystickInput { |
| 72 | public: |
| 73 | Reader() { |
| 74 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 75 | DrivetrainInputReader::InputType::kPistol, |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 76 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig()); |
| 77 | } |
| 78 | |
| 79 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 80 | bool last_auto_running = auto_running_; |
| 81 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 82 | data.GetControlBit(ControlBit::kEnabled); |
| 83 | if (auto_running_ != last_auto_running) { |
| 84 | if (auto_running_) { |
| 85 | StartAuto(); |
| 86 | } else { |
| 87 | StopAuto(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!auto_running_) { |
| 92 | HandleDrivetrain(data); |
| 93 | HandleTeleop(data); |
| 94 | } |
| 95 | |
| 96 | // Process any pending actions. |
| 97 | action_queue_.Tick(); |
| 98 | was_running_ = action_queue_.Running(); |
| 99 | } |
| 100 | |
| 101 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 102 | drivetrain_input_reader_->HandleDrivetrain(data); |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 105 | void SendColors(float red, float green, float blue) { |
| 106 | auto new_status_light = status_light.MakeMessage(); |
| 107 | new_status_light->red = red; |
| 108 | new_status_light->green = green; |
| 109 | new_status_light->blue = blue; |
| 110 | |
| 111 | if (!new_status_light.Send()) { |
| 112 | LOG(ERROR, "Failed to send lights.\n"); |
| 113 | } |
| 114 | } |
| 115 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 116 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 117 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 118 | action_queue_.CancelAllActions(); |
| 119 | LOG(DEBUG, "Canceling\n"); |
| 120 | } |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 121 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 122 | superstructure_queue.position.FetchLatest(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 123 | superstructure_queue.status.FetchLatest(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 124 | if (!superstructure_queue.status.get() || |
| 125 | !superstructure_queue.position.get()) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 126 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 130 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 131 | |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame] | 132 | new_superstructure_goal->intake.left_intake_angle = intake_goal_; |
| 133 | new_superstructure_goal->intake.right_intake_angle = intake_goal_; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 134 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 135 | if (data.IsPressed(kIntakeIn)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 136 | // Turn on the rollers. |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 137 | new_superstructure_goal->intake.roller_voltage = 7.5; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 138 | } else if (data.IsPressed(kIntakeOut)) { |
| 139 | // Turn off the rollers. |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 140 | new_superstructure_goal->intake.roller_voltage = -12.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 141 | } else { |
| 142 | // We don't want the rollers on. |
| 143 | new_superstructure_goal->intake.roller_voltage = 0.0; |
| 144 | } |
| 145 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 146 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 147 | SendColors(0.0, 0.5, 0.0); |
| 148 | } else if (superstructure_queue.position->box_distance < 0.2) { |
| 149 | SendColors(0.0, 0.0, 0.5); |
| 150 | } else { |
| 151 | SendColors(0.0, 0.0, 0.0); |
| 152 | } |
| 153 | |
| 154 | if (data.IsPressed(kSmallBox)) { |
| 155 | // Deploy the intake. |
| 156 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 157 | intake_goal_ = 0.30; |
| 158 | } else { |
| 159 | if (new_superstructure_goal->intake.roller_voltage > 0.1 && |
| 160 | superstructure_queue.position->box_distance < 0.15) { |
| 161 | intake_goal_ = 0.18; |
| 162 | } else { |
| 163 | intake_goal_ = -0.60; |
| 164 | } |
| 165 | } |
| 166 | } else if (data.IsPressed(kIntakeClosed)) { |
| 167 | // Deploy the intake. |
| 168 | if (superstructure_queue.position->box_back_beambreak_triggered) { |
| 169 | intake_goal_ = 0.30; |
| 170 | } else { |
| 171 | if (new_superstructure_goal->intake.roller_voltage > 0.1) { |
| 172 | if (superstructure_queue.position->box_distance < 0.15) { |
| 173 | intake_goal_ = 0.23; |
| 174 | } else if (superstructure_queue.position->box_distance < 0.20) { |
| 175 | intake_goal_ = 0.13; |
| 176 | } else if (superstructure_queue.position->box_distance < 0.25) { |
| 177 | intake_goal_ = -0.05; |
| 178 | } else if (superstructure_queue.position->box_distance < 0.28) { |
| 179 | intake_goal_ = -0.20; |
| 180 | } else { |
| 181 | intake_goal_ = -0.40; |
| 182 | } |
| 183 | } else { |
| 184 | intake_goal_ = -0.60; |
| 185 | } |
| 186 | } |
| 187 | } else { |
| 188 | // Bring in the intake. |
| 189 | intake_goal_ = -3.3; |
| 190 | } |
| 191 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 192 | // If we are disabled, stay at the node closest to where we start. This |
| 193 | // should remove long motions when enabled. |
| 194 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 195 | arm_goal_position_ = superstructure_queue.status->arm.current_node; |
| 196 | } |
| 197 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 198 | bool grab_box = false; |
| 199 | if (data.IsPressed(kArmPickupBoxFromIntake)) { |
| 200 | arm_goal_position_ = arm::NeutralIndex(); |
| 201 | grab_box = true; |
| 202 | } else if (data.IsPressed(kArmNeutral)) { |
| 203 | arm_goal_position_ = arm::NeutralIndex(); |
| 204 | } else if (data.IsPressed(kArmUp)) { |
| 205 | arm_goal_position_ = arm::UpIndex(); |
| 206 | } else if (data.IsPressed(kArmFrontSwitch)) { |
| 207 | arm_goal_position_ = arm::FrontSwitchIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 208 | } else if (data.IsPressed(kArmFrontExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 209 | arm_goal_position_ = arm::FrontHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 210 | } else if (data.IsPressed(kArmFrontHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 211 | arm_goal_position_ = arm::FrontMiddle2BoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 212 | } else if (data.IsPressed(kArmFrontMiddle2Box)) { |
| 213 | arm_goal_position_ = arm::FrontMiddle3BoxIndex(); |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 214 | } else if (data.IsPressed(kArmFrontMiddle1Box)) { |
| 215 | arm_goal_position_ = arm::FrontMiddle1BoxIndex(); |
| 216 | } else if (data.IsPressed(kArmFrontLowBox)) { |
| 217 | arm_goal_position_ = arm::FrontLowBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 218 | } else if (data.IsPressed(kArmBackExtraHighBox)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 219 | arm_goal_position_ = arm::BackHighBoxIndex(); |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame^] | 220 | } else if (data.IsPressed(kArmBackHighBox) || |
| 221 | data.IsPressed(kArmBackMiddle2Box)) { |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 222 | arm_goal_position_ = arm::BackMiddle2BoxIndex(); |
| 223 | } else if (data.IsPressed(kArmBackMiddle1Box)) { |
| 224 | arm_goal_position_ = arm::BackMiddle1BoxIndex(); |
| 225 | } else if (data.IsPressed(kArmBackLowBox)) { |
| 226 | arm_goal_position_ = arm::BackLowBoxIndex(); |
| 227 | } else if (data.IsPressed(kArmBackSwitch)) { |
| 228 | arm_goal_position_ = arm::BackSwitchIndex(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 229 | } else if (data.IsPressed(kArmAboveHang)) { |
| 230 | if (data.IsPressed(kIntakeIn)) { |
| 231 | arm_goal_position_ = arm::SelfHangIndex(); |
| 232 | } else if (data.IsPressed(kIntakeOut)) { |
| 233 | arm_goal_position_ = arm::PartnerHangIndex(); |
| 234 | } else { |
| 235 | arm_goal_position_ = arm::AboveHangIndex(); |
| 236 | } |
| 237 | } else if (data.IsPressed(kArmBelowHang)) { |
| 238 | arm_goal_position_ = arm::BelowHangIndex(); |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 241 | new_superstructure_goal->deploy_fork = |
| 242 | data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen); |
| 243 | |
| 244 | if (new_superstructure_goal->deploy_fork) { |
| 245 | intake_goal_ = -2.0; |
| 246 | } |
| 247 | |
| 248 | if (data.IsPressed(kWinch)) { |
| 249 | new_superstructure_goal->voltage_winch = 12.0; |
| 250 | } else { |
| 251 | new_superstructure_goal->voltage_winch = 0.0; |
| 252 | } |
| 253 | |
| 254 | new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang); |
| 255 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 256 | new_superstructure_goal->arm_goal_position = arm_goal_position_; |
| 257 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 258 | if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 259 | new_superstructure_goal->open_claw = true; |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 260 | } else { |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 261 | new_superstructure_goal->open_claw = false; |
| 262 | } |
| 263 | |
Austin Schuh | ab15c4d | 2018-03-09 21:21:03 -0800 | [diff] [blame] | 264 | new_superstructure_goal->grab_box = grab_box; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 265 | |
| 266 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 267 | if (!new_superstructure_goal.Send()) { |
| 268 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 269 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 273 | void StartAuto() { |
| 274 | LOG(INFO, "Starting auto mode\n"); |
| 275 | |
| 276 | ::frc971::autonomous::AutonomousActionParams params; |
| 277 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 278 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 279 | params.mode = ::frc971::autonomous::auto_mode->mode << 2; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 280 | } else { |
| 281 | LOG(WARNING, "no auto mode values\n"); |
| 282 | params.mode = 0; |
| 283 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 284 | // TODO(austin): use the mode later if we care. We don't care right now. |
| 285 | params.mode = static_cast<int>(::aos::joystick_state->switch_left) | |
| 286 | (static_cast<int>(::aos::joystick_state->scale_left) << 1); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 287 | action_queue_.EnqueueAction( |
| 288 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 289 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 290 | |
| 291 | void StopAuto() { |
| 292 | LOG(INFO, "Stopping auto mode\n"); |
| 293 | action_queue_.CancelAllActions(); |
| 294 | } |
| 295 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 296 | // Current goals to send to the robot. |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 297 | double intake_goal_ = 0.0; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 298 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 299 | bool was_running_ = false; |
| 300 | bool auto_running_ = false; |
| 301 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 302 | int arm_goal_position_ = 0; |
| 303 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 304 | ::aos::common::actions::ActionQueue action_queue_; |
| 305 | }; |
| 306 | |
| 307 | } // namespace joysticks |
| 308 | } // namespace input |
| 309 | } // namespace y2018 |
| 310 | |
| 311 | int main() { |
| 312 | ::aos::Init(-1); |
| 313 | ::y2018::input::joysticks::Reader reader; |
| 314 | reader.Run(); |
| 315 | ::aos::Cleanup(); |
| 316 | } |