Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/input/action_joystick_input.h" |
| 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/input/drivetrain_input.h" |
| 11 | #include "aos/input/joystick_input.h" |
| 12 | #include "aos/logging/logging.h" |
| 13 | #include "aos/logging/logging.h" |
| 14 | #include "aos/util/log_interval.h" |
| 15 | #include "frc971/autonomous/auto.q.h" |
| 16 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 17 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 18 | |
| 19 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
| 20 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 21 | #include "y2019/status_light.q.h" |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 22 | |
| 23 | using ::y2019::control_loops::superstructure::superstructure_queue; |
| 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 | |
| 29 | namespace y2019 { |
| 30 | namespace input { |
| 31 | namespace joysticks { |
| 32 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 33 | const ButtonLocation kSuctionBall(3, 13); |
| 34 | const ButtonLocation kSuctionHatch(3, 12); |
| 35 | const ButtonLocation kDeployStilt(3, 8); |
| 36 | const ButtonLocation kFallOver(3, 9); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 37 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 38 | struct ElevatorWristPosition { |
| 39 | double elevator; |
| 40 | double wrist; |
| 41 | }; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 42 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 43 | const ButtonLocation kRocketForwardLower(5, 1); |
| 44 | const ButtonLocation kRocketForwardMiddle(5, 2); |
| 45 | const ButtonLocation kRocketForwardUpper(5, 4); |
| 46 | const ButtonLocation kCargoForward(5, 3); |
| 47 | |
| 48 | const POVLocation kRocketBackwardUnpressed(5, -1); |
| 49 | const POVLocation kRocketBackwardLower(5, 180); |
| 50 | const POVLocation kRocketBackwardMiddle(5, 90); |
| 51 | const POVLocation kRocketBackwardUpper(5, 0); |
| 52 | const POVLocation kCargoBackward(5, 270); |
| 53 | |
| 54 | const ButtonLocation kPanelSwitch(5, 7); |
| 55 | const ButtonLocation kCargoSwitch(5, 8); |
| 56 | |
| 57 | const ButtonLocation kBallHPIntakeForward(5, 6); |
| 58 | const ButtonLocation kBallHPIntakeBackward(5, 5); |
| 59 | const JoystickAxis kBallOutake(5, 3); |
| 60 | const JoystickAxis kBallIntake(5, 4); |
| 61 | |
| 62 | const ButtonLocation kPanelHPIntakeForward(5, 6); |
| 63 | const ButtonLocation kPanelHPIntakeBackward(5, 5); |
| 64 | |
| 65 | const ButtonLocation kRelease(2, 4); |
| 66 | |
| 67 | const ElevatorWristPosition kStowPos{0.36, 0.0}; |
| 68 | |
| 69 | const ElevatorWristPosition kPanelHPIntakeForwrdPos{0.04, M_PI / 2.0}; |
| 70 | const ElevatorWristPosition kPanelHPIntakeBackwardPos{0.04, -M_PI / 2.0}; |
| 71 | |
| 72 | const ElevatorWristPosition kPanelForwardLowerPos{0.0, M_PI / 2.0}; |
| 73 | const ElevatorWristPosition kPanelBackwardLowerPos{0.0, -M_PI / 2.0}; |
| 74 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 75 | const ElevatorWristPosition kPanelForwardMiddlePos{0.75, M_PI / 2.0}; |
| 76 | const ElevatorWristPosition kPanelBackwardMiddlePos{0.78, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 77 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 78 | const ElevatorWristPosition kPanelForwardUpperPos{1.51, M_PI / 2.0}; |
| 79 | const ElevatorWristPosition kPanelBackwardUpperPos{1.50, -M_PI / 2.0}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 80 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 81 | const ElevatorWristPosition kBallForwardLowerPos{0.4598, 1.5863}; |
| 82 | const ElevatorWristPosition kBallBackwardLowerPos{0.175, -1.61}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 83 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 84 | const ElevatorWristPosition kBallForwardMiddlePos{1.16, 1.546}; |
| 85 | const ElevatorWristPosition kBallBackwardMiddlePos{0.876021, -1.546}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 86 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 87 | const ElevatorWristPosition kBallForwardUpperPos{1.50, 0.961}; |
| 88 | const ElevatorWristPosition kBallBackwardUpperPos{1.41, -1.217}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 89 | |
Sabina Davis | e48004f | 2019-03-02 23:15:24 -0800 | [diff] [blame] | 90 | const ElevatorWristPosition kBallCargoForwardPos{0.699044, 1.353}; |
| 91 | const ElevatorWristPosition kBallCargoBackwardPos{0.828265, -1.999}; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 92 | |
| 93 | const ElevatorWristPosition kBallHPIntakeForwardPos{0.340, 0.737}; |
| 94 | const ElevatorWristPosition kBallHPIntakeBackwardPos{0.52, -1.1}; |
| 95 | |
| 96 | const ElevatorWristPosition kBallIntakePos{0.29, 2.14}; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 97 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 98 | class Reader : public ::aos::input::ActionJoystickInput { |
| 99 | public: |
| 100 | Reader(::aos::EventLoop *event_loop) |
| 101 | : ::aos::input::ActionJoystickInput( |
| 102 | event_loop, |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 103 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) { |
| 104 | superstructure_queue.goal.FetchLatest(); |
| 105 | if (superstructure_queue.goal.get()) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 106 | grab_piece_ = superstructure_queue.goal->suction.grab_piece; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 109 | |
| 110 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 111 | superstructure_queue.position.FetchLatest(); |
| 112 | superstructure_queue.status.FetchLatest(); |
| 113 | if (!superstructure_queue.status.get() || |
| 114 | !superstructure_queue.position.get()) { |
| 115 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 120 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 121 | if (data.IsPressed(kSuctionBall)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 122 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 123 | } else if (data.IsPressed(kSuctionHatch)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 124 | grab_piece_ = true; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 125 | } else if (data.IsPressed(kRelease) || |
| 126 | !superstructure_queue.status->has_piece) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 127 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 128 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 129 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 130 | if (data.IsPressed(kRocketBackwardUnpressed)) { |
| 131 | elevator_wrist_pos_ = kStowPos; |
| 132 | } |
| 133 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 134 | new_superstructure_goal->roller_voltage = 0.0; |
| 135 | |
| 136 | const bool kDoBallIntake = data.GetAxis(kBallIntake) > 0.9; |
| 137 | const bool kDoBallOutake = data.GetAxis(kBallOutake) > 0.9; |
| 138 | |
| 139 | if (data.IsPressed(kPanelSwitch)) { |
| 140 | switch_ball_ = false; |
| 141 | } else if (data.IsPressed(kCargoSwitch)) { |
| 142 | switch_ball_ = true; |
| 143 | } |
| 144 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 145 | // TODO(sabina): max height please? |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 146 | if (data.IsPressed(kFallOver)) { |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 147 | new_superstructure_goal->stilts.unsafe_goal = 0.71; |
Michael Schuh | 587dcb5 | 2019-02-28 21:31:03 -0800 | [diff] [blame] | 148 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 149 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 150 | } else if (data.IsPressed(kDeployStilt)) { |
| 151 | new_superstructure_goal->stilts.unsafe_goal = 0.50; |
Michael Schuh | 587dcb5 | 2019-02-28 21:31:03 -0800 | [diff] [blame] | 152 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.65; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 153 | if (stilts_was_above_) { |
| 154 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.75; |
| 155 | } else { |
| 156 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
| 157 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 158 | } else { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 159 | new_superstructure_goal->stilts.unsafe_goal = 0.01; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 160 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.25; |
| 161 | new_superstructure_goal->stilts.profile_params.max_acceleration = 2.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 164 | if (superstructure_queue.status->stilts.position > 0.65) { |
| 165 | stilts_was_above_ = true; |
| 166 | } else if (superstructure_queue.status->stilts.position < 0.1) { |
| 167 | stilts_was_above_ = false; |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 170 | if (switch_ball_) { |
| 171 | if (superstructure_queue.status->has_piece) { |
| 172 | new_superstructure_goal->wrist.profile_params.max_acceleration = 20; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 173 | } |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 174 | |
| 175 | // Go to intake position and apply vacuum |
| 176 | if (data.IsPressed(kBallHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 177 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 178 | elevator_wrist_pos_ = kBallHPIntakeForwardPos; |
| 179 | } else if (data.IsPressed(kBallHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 180 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 181 | elevator_wrist_pos_ = kBallHPIntakeBackwardPos; |
| 182 | } |
| 183 | |
| 184 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 185 | // we can re-grab the ball. |
| 186 | if (data.IsPressed(kRocketForwardLower)) { |
| 187 | elevator_wrist_pos_ = kBallForwardLowerPos; |
| 188 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 189 | elevator_wrist_pos_ = kBallBackwardLowerPos; |
| 190 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 191 | elevator_wrist_pos_ = kBallForwardMiddlePos; |
| 192 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 193 | elevator_wrist_pos_ = kBallBackwardMiddlePos; |
| 194 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 195 | elevator_wrist_pos_ = kBallForwardUpperPos; |
| 196 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 197 | elevator_wrist_pos_ = kBallBackwardUpperPos; |
| 198 | } else if (data.IsPressed(kCargoForward)) { |
| 199 | elevator_wrist_pos_ = kBallCargoForwardPos; |
| 200 | } else if (data.IsPressed(kCargoBackward)) { |
| 201 | elevator_wrist_pos_ = kBallCargoBackwardPos; |
| 202 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 203 | } else { |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 204 | if (data.IsPressed(kPanelHPIntakeForward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 205 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 206 | elevator_wrist_pos_ = kPanelHPIntakeForwrdPos; |
| 207 | } else if (data.IsPressed(kPanelHPIntakeBackward)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 208 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 209 | elevator_wrist_pos_ = kPanelHPIntakeBackwardPos; |
| 210 | } |
| 211 | |
| 212 | // Go to elevator/wrist position. Overrides intake position if pressed so |
| 213 | // we can re-grab the panel. |
| 214 | if (data.IsPressed(kRocketForwardLower)) { |
| 215 | elevator_wrist_pos_ = kPanelForwardLowerPos; |
| 216 | } else if (data.IsPressed(kRocketBackwardLower)) { |
| 217 | elevator_wrist_pos_ = kPanelBackwardLowerPos; |
| 218 | } else if (data.IsPressed(kRocketForwardMiddle)) { |
| 219 | elevator_wrist_pos_ = kPanelForwardMiddlePos; |
| 220 | } else if (data.IsPressed(kRocketBackwardMiddle)) { |
| 221 | elevator_wrist_pos_ = kPanelBackwardMiddlePos; |
| 222 | } else if (data.IsPressed(kRocketForwardUpper)) { |
| 223 | elevator_wrist_pos_ = kPanelForwardUpperPos; |
| 224 | } else if (data.IsPressed(kRocketBackwardUpper)) { |
| 225 | elevator_wrist_pos_ = kPanelBackwardUpperPos; |
| 226 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 229 | if (switch_ball_) { |
| 230 | if (kDoBallOutake || |
| 231 | (kDoBallIntake && !superstructure_queue.status->has_piece)) { |
| 232 | new_superstructure_goal->intake.unsafe_goal = 0.959327; |
| 233 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 234 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 235 | if (kDoBallIntake && !superstructure_queue.status->has_piece) { |
| 236 | elevator_wrist_pos_ = kBallIntakePos; |
| 237 | new_superstructure_goal->roller_voltage = 9.0; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 238 | grab_piece_ = true; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 239 | } else { |
| 240 | if (kDoBallOutake) { |
| 241 | new_superstructure_goal->roller_voltage = -6.0; |
| 242 | } else { |
| 243 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 244 | new_superstructure_goal->roller_voltage = 0.0; |
| 245 | } |
| 246 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 247 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 248 | |
| 249 | if (data.IsPressed(kRelease)) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 250 | grab_piece_ = false; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 253 | if (switch_ball_) { |
| 254 | new_superstructure_goal->suction.gamepiece_mode = 0; |
| 255 | } else { |
| 256 | new_superstructure_goal->suction.gamepiece_mode = 1; |
| 257 | } |
| 258 | |
| 259 | new_superstructure_goal->suction.grab_piece = grab_piece_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 260 | |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 261 | new_superstructure_goal->elevator.unsafe_goal = |
| 262 | elevator_wrist_pos_.elevator; |
| 263 | new_superstructure_goal->wrist.unsafe_goal = elevator_wrist_pos_.wrist; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 264 | |
| 265 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 266 | if (!new_superstructure_goal.Send()) { |
| 267 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private: |
| 272 | // Current goals here. |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 273 | ElevatorWristPosition elevator_wrist_pos_ = kStowPos; |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 274 | bool grab_piece_ = false; |
Tyler Chatow | 7bcb52f | 2019-02-24 00:16:54 -0800 | [diff] [blame] | 275 | |
| 276 | bool switch_ball_ = false; |
| 277 | bool stilts_was_above_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | } // namespace joysticks |
| 281 | } // namespace input |
| 282 | } // namespace y2019 |
| 283 | |
| 284 | int main() { |
| 285 | ::aos::Init(-1); |
| 286 | ::aos::ShmEventLoop event_loop; |
| 287 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 288 | reader.Run(); |
| 289 | ::aos::Cleanup(); |
| 290 | } |