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 | |
| 32 | // TODO(sabina): update button locations when the board is done |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 33 | const ButtonLocation kIntakeOut(3, 3); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 34 | const ButtonLocation kElevatorDown(0, 0); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 35 | const ButtonLocation kElevatorFront1(4, 1); |
| 36 | const ButtonLocation kElevatorFront2(4, 11); |
| 37 | const ButtonLocation kElevatorFront3(4, 9); |
| 38 | const ButtonLocation kElevatorFront4(4, 7); |
| 39 | const ButtonLocation kElevatorFront5(4, 5); |
| 40 | |
| 41 | const ButtonLocation kElevatorBack1(3, 14); |
| 42 | const ButtonLocation kElevatorBack2(4, 12); |
| 43 | const ButtonLocation kElevatorBack3(4, 10); |
| 44 | const ButtonLocation kElevatorBack4(4, 8); |
| 45 | const ButtonLocation kElevatorBack5(4, 6); |
| 46 | |
| 47 | const ButtonLocation kElevatorIntaking(3, 4); |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 48 | const ButtonLocation kElevatorOuttake(3, 6); |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 49 | const ButtonLocation kRelease(4, 4); |
| 50 | |
| 51 | const ButtonLocation kSuctionBall(3, 13); |
| 52 | const ButtonLocation kSuctionHatch(3, 12); |
| 53 | const ButtonLocation kDeployStilt(3, 8); |
| 54 | const ButtonLocation kFallOver(3, 9); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 55 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 56 | const ButtonLocation kDiskLoad(0, 0); |
| 57 | const ButtonLocation kDiskRocketMiddle(0, 0); |
| 58 | const ButtonLocation kDiskRocketTop(0, 0); |
| 59 | const ButtonLocation kCargoLoad(0, 0); |
| 60 | const ButtonLocation kCargoBay(0, 0); |
| 61 | const ButtonLocation kCargoRocketBase(0, 0); |
| 62 | const ButtonLocation kCargoRocketMiddle(0, 0); |
| 63 | const ButtonLocation kCargoRocketTop(0, 0); |
| 64 | const ButtonLocation kStow(0, 0); |
| 65 | const ButtonLocation kIntakeExtend(0, 0); |
| 66 | const ButtonLocation kIntake(0, 0); |
| 67 | const ButtonLocation kSpit(0, 0); |
| 68 | const ButtonLocation kCargoSuction(0, 0); |
| 69 | const ButtonLocation kDiskSuction(0, 0); |
| 70 | const ButtonLocation kSuctionOut(0, 0); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 71 | const ButtonLocation kRetractStilt(0, 0); |
| 72 | const ButtonLocation kBackwards(0, 0); |
| 73 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 74 | const ButtonLocation kWristBackwards(3, 10); |
| 75 | const ButtonLocation kWristForwards(3, 7); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 76 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 77 | class Reader : public ::aos::input::ActionJoystickInput { |
| 78 | public: |
| 79 | Reader(::aos::EventLoop *event_loop) |
| 80 | : ::aos::input::ActionJoystickInput( |
| 81 | event_loop, |
| 82 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 83 | |
| 84 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 85 | superstructure_queue.position.FetchLatest(); |
| 86 | superstructure_queue.status.FetchLatest(); |
| 87 | if (!superstructure_queue.status.get() || |
| 88 | !superstructure_queue.position.get()) { |
| 89 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 94 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 95 | /* |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 96 | if (data.IsPressed(kElevatorUp)) { |
| 97 | elevator_height_ += 0.1; |
| 98 | } else if (data.IsPressed(kElevatorDown)) { |
| 99 | elevator_height_ -= 0.1; |
| 100 | } else if (data.IsPressed(kDiskLoad)) { |
| 101 | elevator_height_ = 0.48; |
| 102 | wrist_angle_ = M_PI; |
| 103 | } else if (data.IsPressed(kDiskRocketMiddle)) { |
| 104 | elevator_height_ = 1.19; |
| 105 | wrist_angle_ = M_PI; |
| 106 | } else if (data.IsPressed(kDiskRocketTop)) { |
| 107 | elevator_height_ = 1.90; |
| 108 | wrist_angle_ = M_PI; |
| 109 | } |
| 110 | |
| 111 | // TODO(sabina): do we need an angle here? |
| 112 | else if (data.IsPressed(kCargoLoad)) { |
| 113 | elevator_height_ = 1.12; |
| 114 | wrist_angle_ = M_PI; |
| 115 | } else if (data.IsPressed(kCargoBay)) { |
| 116 | elevator_height_ = 0.0; |
| 117 | wrist_angle_ = M_PI / 3; |
| 118 | } else if (data.IsPressed(kCargoRocketBase)) { |
| 119 | elevator_height_ = 0.7; |
| 120 | wrist_angle_ = M_PI; |
| 121 | } else if (data.IsPressed(kCargoRocketMiddle)) { |
| 122 | elevator_height_ = 1.41; |
| 123 | wrist_angle_ = M_PI; |
| 124 | } else if (data.IsPressed(kCargoRocketTop)) { |
| 125 | elevator_height_ = 2.12; |
| 126 | wrist_angle_ = M_PI; |
| 127 | } else if (data.IsPressed(kStow)) { |
| 128 | elevator_height_ = 0.5; |
| 129 | wrist_angle_ = 0.0; |
| 130 | } else { |
| 131 | } |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 132 | */ |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 133 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 134 | /* |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 135 | // TODO(sabina): get accurate angle. |
| 136 | if (data.IsPressed(kIntakeExtend)) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 137 | new_superstructure_goal->intake.unsafe_goal = 0.5; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 138 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 139 | new_superstructure_goal->intake.unsafe_goal = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (data.IsPressed(kIntake)) { |
| 143 | new_superstructure_goal->suction.bottom = true; |
| 144 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 145 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 146 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 147 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 148 | } |
| 149 | } else if (data.IsPressed(kSpit)) { |
| 150 | new_superstructure_goal->suction.bottom = false; |
| 151 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 152 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 153 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 154 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 155 | } |
| 156 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 157 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 158 | } |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 159 | */ |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 160 | |
| 161 | // TODO(sabina): decide if we should really have disk suction as its own |
| 162 | // button |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 163 | /* |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 164 | if (data.IsPressed(kCargoSuction)) { |
| 165 | new_superstructure_goal->suction.top = false; |
| 166 | new_superstructure_goal->suction.bottom = true; |
| 167 | } else if (data.IsPressed(kDiskSuction)) { |
| 168 | new_superstructure_goal->suction.top = true; |
| 169 | new_superstructure_goal->suction.bottom = true; |
| 170 | } else if (data.IsPressed(kSuctionOut)) { |
| 171 | new_superstructure_goal->suction.top = true; |
| 172 | new_superstructure_goal->suction.bottom = true; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 173 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 174 | */ |
| 175 | |
| 176 | if (data.IsPressed(kSuctionBall)) { |
| 177 | top_ = false; |
| 178 | bottom_ = true; |
| 179 | } else if (data.IsPressed(kSuctionHatch)) { |
| 180 | top_ = true; |
| 181 | bottom_ = true; |
| 182 | } else if (data.IsPressed(kRelease) || |
| 183 | !superstructure_queue.status->has_piece) { |
| 184 | top_ = false; |
| 185 | bottom_ = false; |
| 186 | } |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 187 | |
| 188 | // TODO(sabina): max height please? |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 189 | if (data.IsPressed(kFallOver)) { |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 190 | new_superstructure_goal->stilts.unsafe_goal = 0.71; |
Austin Schuh | 08bd22d | 2019-02-22 20:48:20 -0800 | [diff] [blame] | 191 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.45; |
| 192 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.5; |
Austin Schuh | 77ac321 | 2019-02-19 16:50:14 -0800 | [diff] [blame] | 193 | } else if (data.IsPressed(kDeployStilt)) { |
| 194 | new_superstructure_goal->stilts.unsafe_goal = 0.50; |
Austin Schuh | 08bd22d | 2019-02-22 20:48:20 -0800 | [diff] [blame] | 195 | new_superstructure_goal->stilts.profile_params.max_velocity = 0.45; |
| 196 | new_superstructure_goal->stilts.profile_params.max_acceleration = 0.5; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 197 | } else { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 198 | new_superstructure_goal->stilts.unsafe_goal = 0.01; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 201 | if (data.IsPressed(kElevatorFront1)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 202 | elevator_height_ = 1.5; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 203 | } else if (data.IsPressed(kElevatorFront2)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 204 | elevator_height_ = 1.2; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 205 | } else if (data.IsPressed(kElevatorFront3)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 206 | elevator_height_ = 0.8; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 207 | } else if (data.IsPressed(kElevatorFront4)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 208 | elevator_height_ = 0.3; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 209 | } else if (data.IsPressed(kElevatorFront5)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 210 | elevator_height_ = 0.01; |
| 211 | } |
| 212 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 213 | /* |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 214 | if (data.IsPressed(kWristDown)) { |
| 215 | wrist_angle_ = -M_PI / 3.0; |
| 216 | } else { |
| 217 | wrist_angle_ = M_PI / 3.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 218 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 219 | */ |
| 220 | if (data.IsPressed(kWristBackwards)) { |
| 221 | // Hatch pannel back |
| 222 | elevator_height_ = 0.03; |
| 223 | wrist_angle_ = -M_PI / 2.0; |
| 224 | Disc(); |
| 225 | } else if (data.IsPressed(kWristForwards)) { |
| 226 | // Hatch pannel front |
| 227 | elevator_height_ = 0.03; |
| 228 | wrist_angle_ = M_PI / 2.0; |
| 229 | Disc(); |
| 230 | } else if (data.IsPressed(kElevatorFront5)) { |
| 231 | // Ball front |
| 232 | Ball(); |
| 233 | elevator_height_ = 0.52; |
| 234 | wrist_angle_ = 1.1; |
| 235 | } else if (data.IsPressed(kElevatorBack5)) { |
| 236 | // Ball back |
| 237 | elevator_height_ = 0.52; |
| 238 | wrist_angle_ = -1.1; |
| 239 | } else if (data.IsPressed(kElevatorFront2)) { |
| 240 | elevator_height_ = 1.5; |
| 241 | wrist_angle_ = 0.0; |
| 242 | } else { |
| 243 | wrist_angle_ = 0.0; |
| 244 | elevator_height_ = 0.36; |
| 245 | } |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 246 | |
| 247 | if (data.IsPressed(kElevatorOuttake) || |
| 248 | (data.IsPressed(kIntakeOut) && |
| 249 | !superstructure_queue.status->has_piece)) { |
| 250 | new_superstructure_goal->intake.unsafe_goal = 0.959327; |
| 251 | } |
| 252 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 253 | if (data.IsPressed(kIntakeOut) && !superstructure_queue.status->has_piece) { |
| 254 | elevator_height_ = 0.29; |
| 255 | wrist_angle_ = 2.14; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 256 | if (data.IsPressed(kElevatorIntaking)) { |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 257 | new_superstructure_goal->roller_voltage = 9.0; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 258 | } else { |
| 259 | new_superstructure_goal->roller_voltage = 0.0; |
| 260 | } |
| 261 | Ball(); |
| 262 | } else { |
| 263 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 264 | new_superstructure_goal->roller_voltage = 0.0; |
| 265 | } |
| 266 | |
Austin Schuh | 23a5163 | 2019-02-19 16:50:36 -0800 | [diff] [blame] | 267 | if (data.IsPressed(kElevatorOuttake)) { |
| 268 | new_superstructure_goal->roller_voltage = -6.0; |
| 269 | } |
| 270 | |
| 271 | if (data.IsPressed(kElevatorBack1)) { |
| 272 | wrist_angle_ = -2.451824; |
| 273 | elevator_height_ = 0.430478; |
| 274 | //new_superstructure_goal->wrist.profile_params.max_velocity = 2.0; |
| 275 | //new_superstructure_goal->wrist.profile_params.max_acceleration = 20.0; |
| 276 | } |
| 277 | if (data.IsPressed(kElevatorBack2)) { |
| 278 | wrist_angle_ = -2.400; |
| 279 | elevator_height_ = 0.364108; |
| 280 | new_superstructure_goal->elevator.profile_params.max_velocity = 2.0; |
| 281 | new_superstructure_goal->elevator.profile_params.max_acceleration = 5.0; |
| 282 | new_superstructure_goal->wrist.profile_params.max_velocity = 0.35; |
| 283 | new_superstructure_goal->wrist.profile_params.max_acceleration = 10.0; |
| 284 | } |
| 285 | if (data.IsPressed(kElevatorBack3)) { |
| 286 | wrist_angle_ = -2.211173; |
| 287 | elevator_height_ = 0.25; |
| 288 | new_superstructure_goal->wrist.profile_params.max_velocity = 2.0; |
| 289 | new_superstructure_goal->wrist.profile_params.max_acceleration = 10.0; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | if (data.IsPressed(kRelease)) { |
| 294 | top_ = false; |
| 295 | bottom_ = false; |
| 296 | } |
| 297 | |
| 298 | new_superstructure_goal->suction.top = top_; |
| 299 | new_superstructure_goal->suction.bottom = bottom_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 300 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 301 | new_superstructure_goal->elevator.unsafe_goal = elevator_height_; |
| 302 | new_superstructure_goal->wrist.unsafe_goal = wrist_angle_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 303 | |
| 304 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 305 | if (!new_superstructure_goal.Send()) { |
| 306 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 307 | } |
| 308 | } |
| 309 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 310 | void Disc() { |
| 311 | top_ = true; |
| 312 | bottom_ = true; |
| 313 | } |
| 314 | void Ball() { |
| 315 | top_ = false; |
| 316 | bottom_ = true; |
| 317 | } |
| 318 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 319 | private: |
| 320 | // Current goals here. |
| 321 | double elevator_height_ = 0.0; |
| 322 | double wrist_angle_ = 0.0; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame] | 323 | |
| 324 | bool top_ = false; |
| 325 | bool bottom_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | } // namespace joysticks |
| 329 | } // namespace input |
| 330 | } // namespace y2019 |
| 331 | |
| 332 | int main() { |
| 333 | ::aos::Init(-1); |
| 334 | ::aos::ShmEventLoop event_loop; |
| 335 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 336 | reader.Run(); |
| 337 | ::aos::Cleanup(); |
| 338 | } |