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