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