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); |
| 48 | const ButtonLocation kElevatorIntakingUp(3, 6); |
| 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? |
| 189 | if (data.IsPressed(kDeployStilt)) { |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 190 | new_superstructure_goal->stilts.unsafe_goal = 0.50; |
| 191 | } else if (data.IsPressed(kFallOver)) { |
| 192 | new_superstructure_goal->stilts.unsafe_goal = 0.71; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 193 | } else { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 194 | new_superstructure_goal->stilts.unsafe_goal = 0.01; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 197 | if (data.IsPressed(kElevatorFront1)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 198 | elevator_height_ = 1.5; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 199 | } else if (data.IsPressed(kElevatorFront2)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 200 | elevator_height_ = 1.2; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 201 | } else if (data.IsPressed(kElevatorFront3)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 202 | elevator_height_ = 0.8; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 203 | } else if (data.IsPressed(kElevatorFront4)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 204 | elevator_height_ = 0.3; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 205 | } else if (data.IsPressed(kElevatorFront5)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 206 | elevator_height_ = 0.01; |
| 207 | } |
| 208 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 209 | /* |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame] | 210 | if (data.IsPressed(kWristDown)) { |
| 211 | wrist_angle_ = -M_PI / 3.0; |
| 212 | } else { |
| 213 | wrist_angle_ = M_PI / 3.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 214 | } |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 215 | */ |
| 216 | if (data.IsPressed(kWristBackwards)) { |
| 217 | // Hatch pannel back |
| 218 | elevator_height_ = 0.03; |
| 219 | wrist_angle_ = -M_PI / 2.0; |
| 220 | Disc(); |
| 221 | } else if (data.IsPressed(kWristForwards)) { |
| 222 | // Hatch pannel front |
| 223 | elevator_height_ = 0.03; |
| 224 | wrist_angle_ = M_PI / 2.0; |
| 225 | Disc(); |
| 226 | } else if (data.IsPressed(kElevatorFront5)) { |
| 227 | // Ball front |
| 228 | Ball(); |
| 229 | elevator_height_ = 0.52; |
| 230 | wrist_angle_ = 1.1; |
| 231 | } else if (data.IsPressed(kElevatorBack5)) { |
| 232 | // Ball back |
| 233 | elevator_height_ = 0.52; |
| 234 | wrist_angle_ = -1.1; |
| 235 | } else if (data.IsPressed(kElevatorFront2)) { |
| 236 | elevator_height_ = 1.5; |
| 237 | wrist_angle_ = 0.0; |
| 238 | } else { |
| 239 | wrist_angle_ = 0.0; |
| 240 | elevator_height_ = 0.36; |
| 241 | } |
| 242 | //if (data.IsPressed(kElevatorIntaking)) { |
| 243 | //} |
| 244 | if (data.IsPressed(kIntakeOut) && !superstructure_queue.status->has_piece) { |
| 245 | elevator_height_ = 0.29; |
| 246 | wrist_angle_ = 2.14; |
| 247 | new_superstructure_goal->intake.unsafe_goal = 0.52; |
| 248 | if (data.IsPressed(kElevatorIntaking)) { |
| 249 | new_superstructure_goal->roller_voltage = 6.0; |
| 250 | } else { |
| 251 | new_superstructure_goal->roller_voltage = 0.0; |
| 252 | } |
| 253 | Ball(); |
| 254 | } else { |
| 255 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 256 | new_superstructure_goal->roller_voltage = 0.0; |
| 257 | } |
| 258 | |
| 259 | if (data.IsPressed(kElevatorIntakingUp)) { |
| 260 | elevator_height_ = 0.29 + 0.3; |
| 261 | wrist_angle_ = 2.14; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | if (data.IsPressed(kRelease)) { |
| 266 | top_ = false; |
| 267 | bottom_ = false; |
| 268 | } |
| 269 | |
| 270 | new_superstructure_goal->suction.top = top_; |
| 271 | new_superstructure_goal->suction.bottom = bottom_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 272 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 273 | new_superstructure_goal->elevator.unsafe_goal = elevator_height_; |
| 274 | new_superstructure_goal->wrist.unsafe_goal = wrist_angle_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 275 | |
| 276 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 277 | if (!new_superstructure_goal.Send()) { |
| 278 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 279 | } |
| 280 | } |
| 281 | |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 282 | void Disc() { |
| 283 | top_ = true; |
| 284 | bottom_ = true; |
| 285 | } |
| 286 | void Ball() { |
| 287 | top_ = false; |
| 288 | bottom_ = true; |
| 289 | } |
| 290 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 291 | private: |
| 292 | // Current goals here. |
| 293 | double elevator_height_ = 0.0; |
| 294 | double wrist_angle_ = 0.0; |
Austin Schuh | 1a17e13 | 2019-02-17 15:05:06 -0800 | [diff] [blame^] | 295 | |
| 296 | bool top_ = false; |
| 297 | bool bottom_ = false; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | } // namespace joysticks |
| 301 | } // namespace input |
| 302 | } // namespace y2019 |
| 303 | |
| 304 | int main() { |
| 305 | ::aos::Init(-1); |
| 306 | ::aos::ShmEventLoop event_loop; |
| 307 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 308 | reader.Run(); |
| 309 | ::aos::Cleanup(); |
| 310 | } |