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