Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "aos/linux_code/init.h" |
| 7 | #include "aos/prime/input/joystick_input.h" |
| 8 | #include "aos/common/input/driver_station_data.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
| 12 | #include "aos/common/actions/actions.h" |
| 13 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 14 | #include "frc971/queues/gyro.q.h" |
| 15 | #include "bot3/autonomous/auto.q.h" |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 16 | #include "bot3/control_loops/elevator/elevator.h" |
| 17 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
| 18 | #include "bot3/control_loops/elevator/elevator.q.h" |
| 19 | #include "bot3/control_loops/intake/intake.q.h" |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 20 | |
| 21 | using ::bot3::control_loops::drivetrain_queue; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 22 | using ::bot3::control_loops::elevator_queue; |
| 23 | using ::bot3::control_loops::intake_queue; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 24 | using ::frc971::sensors::gyro_reading; |
| 25 | |
| 26 | using ::aos::input::driver_station::ButtonLocation; |
| 27 | using ::aos::input::driver_station::POVLocation; |
| 28 | using ::aos::input::driver_station::JoystickAxis; |
| 29 | using ::aos::input::driver_station::ControlBit; |
| 30 | |
| 31 | namespace bot3 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 35 | struct ProfileParams { |
| 36 | double velocity; |
| 37 | double acceleration; |
| 38 | }; |
| 39 | |
| 40 | // Preset motion limits. |
| 41 | constexpr ProfileParams kElevatorMove{0.3, 1.0}; |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 42 | constexpr ProfileParams kFastElevatorMove{1.0, 5.0}; |
| 43 | |
| 44 | // Preset goals for autostacking |
Austin Schuh | 1ed71ad | 2015-09-14 21:24:03 +0000 | [diff] [blame^] | 45 | constexpr double kOneToteHeight{0.46}; |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 46 | constexpr double kCarryHeight{0.180}; |
| 47 | constexpr double kGroundHeight{0.030}; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 48 | |
| 49 | // Joystick & button addresses. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 50 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 51 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 52 | const ButtonLocation kQuickTurn(1, 5); |
| 53 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 54 | // essential |
| 55 | const ButtonLocation kMoveToteHeight(4, 9); |
| 56 | const ButtonLocation kOpenIntake(3, 8); |
| 57 | const ButtonLocation kCloseIntake(3, 6); |
| 58 | const ButtonLocation kOpenCanRestraint(4, 5); |
| 59 | const ButtonLocation kCloseCanRestraint(4, 1); |
| 60 | const POVLocation kOpenPassiveSupport(4, 0); |
| 61 | const POVLocation kClosePassiveSupport(4, 180); |
| 62 | |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 63 | const ButtonLocation kIntakeOut(3, 7); |
| 64 | const ButtonLocation kIntakeIn(4, 12); |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 65 | |
| 66 | const ButtonLocation kAutoStack(4, 7); |
| 67 | const ButtonLocation kManualStack(4, 6); |
| 68 | |
| 69 | const ButtonLocation kCarry(4, 10); |
| 70 | const ButtonLocation kSetDown(3, 9); |
| 71 | const ButtonLocation kSkyscraper(4, 11); |
| 72 | |
| 73 | const ButtonLocation kScoreBegin(4, 8); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 74 | |
| 75 | class Reader : public ::aos::input::JoystickInput { |
| 76 | public: |
| 77 | Reader() : was_running_(false) {} |
| 78 | |
| 79 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 80 | bool last_auto_running = auto_running_; |
| 81 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 82 | data.GetControlBit(ControlBit::kEnabled); |
| 83 | if (auto_running_ != last_auto_running) { |
| 84 | if (auto_running_) { |
| 85 | StartAuto(); |
| 86 | } else { |
| 87 | StopAuto(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 92 | HandleDrivetrain(data); |
| 93 | HandleTeleop(data); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 98 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 99 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 100 | |
| 101 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 102 | .steering(wheel) |
| 103 | .throttle(throttle) |
| 104 | .quickturn(data.IsPressed(kQuickTurn)) |
| 105 | .control_loop_driving(false) |
| 106 | .Send()) { |
| 107 | LOG(WARNING, "sending stick values failed\n"); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 112 | double intake_goal = 0; |
| 113 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 114 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 115 | action_queue_.CancelAllActions(); |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 116 | intake_closed_ = false; |
| 117 | can_restraint_open_ = true; |
| 118 | passive_support_open_ = true; |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 119 | LOG(DEBUG, "Canceling\n"); |
| 120 | } |
| 121 | |
| 122 | elevator_queue.status.FetchLatest(); |
| 123 | if (!elevator_queue.status.get()) { |
| 124 | LOG(ERROR, "Got no elevator status packet.\n"); |
| 125 | } |
| 126 | |
| 127 | if (elevator_queue.status.get() && elevator_queue.status->zeroed) { |
| 128 | if (waiting_for_zero_) { |
| 129 | LOG(INFO, "Zeroed! Starting teleop mode.\n"); |
| 130 | waiting_for_zero_ = false; |
| 131 | |
| 132 | // Set the initial goals to the bottom, since otherwise the driver seems |
| 133 | // to get confused and think that the end of the zeroing sequence is the |
| 134 | // same location and then wonders why it doesn't work... |
| 135 | elevator_goal_ = kGroundHeight; |
| 136 | } |
| 137 | } else { |
| 138 | waiting_for_zero_ = true; |
| 139 | } |
| 140 | |
| 141 | if (data.PosEdge(kAutoStack)) { |
| 142 | action_queue_.CancelAllActions(); |
| 143 | if (tote_count_ == 6) { |
| 144 | stacking_state_machine_ = FULL; |
| 145 | } else { |
| 146 | stacking_state_machine_ = WAITING; |
| 147 | } |
| 148 | } |
| 149 | if (data.IsPressed(kAutoStack)) { |
| 150 | switch (stacking_state_machine_) { |
| 151 | case WAITING: |
| 152 | elevator_params_ = kFastElevatorMove; |
| 153 | elevator_goal_ = kOneToteHeight; |
| 154 | if (elevator_queue.status->has_tote) { |
| 155 | stacking_state_machine_ = GOING_DOWN; |
| 156 | } |
| 157 | break; |
| 158 | case GOING_DOWN: |
| 159 | elevator_params_ = kFastElevatorMove; |
| 160 | elevator_goal_ = kGroundHeight; |
| 161 | if (elevator_queue.status->height < 0.05) { |
| 162 | ++tote_count_; |
| 163 | if (tote_count_ == 6) { |
| 164 | stacking_state_machine_ = FULL; |
| 165 | } else { |
| 166 | stacking_state_machine_ = GOING_UP; |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | case GOING_UP: |
| 171 | elevator_params_ = kFastElevatorMove; |
| 172 | elevator_goal_ = kOneToteHeight; |
| 173 | if (elevator_queue.status->height > kOneToteHeight - 0.05) { |
| 174 | stacking_state_machine_ = WAITING; |
| 175 | } |
| 176 | break; |
| 177 | case FULL: |
| 178 | elevator_goal_ = kCarryHeight; |
| 179 | elevator_params_ = kFastElevatorMove; |
| 180 | break; |
| 181 | case OFF: |
| 182 | stacking_state_machine_ = WAITING; |
| 183 | break; |
| 184 | } |
| 185 | } else { |
| 186 | stacking_state_machine_ = OFF; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 189 | // Buttons for intaking. |
| 190 | if (data.IsPressed(kIntakeIn)) { |
| 191 | intake_goal = 10.0; |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 192 | if (stacking_state_machine_ != OFF && |
| 193 | elevator_queue.status->height < 0.43) { |
| 194 | intake_goal = 0.0; |
| 195 | } |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 196 | } else if (data.IsPressed(kIntakeOut)) { |
| 197 | intake_goal = -10.0; |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 198 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 199 | action_queue_.CancelAllActions(); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 200 | } |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 201 | // TODO(Adam): Implement essential actors/goals. |
| 202 | if (data.PosEdge(kMoveToteHeight)) { |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 203 | elevator_goal_ = 0.45; |
| 204 | elevator_params_ = {1.0, 5.0}; |
| 205 | action_queue_.CancelAllActions(); |
| 206 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 207 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 208 | if (data.PosEdge(kOpenIntake)) { |
| 209 | intake_closed_ = false; |
| 210 | } |
| 211 | |
| 212 | if (data.PosEdge(kCloseIntake)) { |
| 213 | intake_closed_ = true; |
| 214 | } |
| 215 | |
| 216 | if (data.PosEdge(kOpenCanRestraint)) { |
| 217 | can_restraint_open_ = true; |
| 218 | } |
| 219 | |
| 220 | if (data.PosEdge(kCloseCanRestraint)) { |
| 221 | can_restraint_open_ = false; |
| 222 | } |
| 223 | |
| 224 | if (data.PosEdge(kOpenPassiveSupport)) { |
| 225 | passive_support_open_ = true; |
| 226 | } |
| 227 | |
| 228 | if (data.PosEdge(kClosePassiveSupport)) { |
| 229 | passive_support_open_ = false; |
| 230 | } |
| 231 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 232 | // Buttons for elevator. |
| 233 | |
| 234 | if (data.PosEdge(kCarry)) { |
| 235 | // TODO(comran): Get actual height/velocity/acceleration values. |
| 236 | elevator_goal_ = 0.180; |
| 237 | elevator_params_ = {1.0, 2.0}; |
| 238 | action_queue_.CancelAllActions(); |
| 239 | } |
| 240 | |
| 241 | if (data.PosEdge(kSetDown)) { |
| 242 | // TODO(comran): Get actual height/velocity/acceleration values. |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 243 | elevator_goal_ = 0.005; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 244 | elevator_params_ = {1.0, 5.0}; |
| 245 | action_queue_.CancelAllActions(); |
| 246 | } |
| 247 | |
| 248 | if (data.PosEdge(kSkyscraper)) { |
| 249 | // TODO(comran): Get actual height/velocity/acceleration values. |
| 250 | elevator_goal_ = 1.0; |
Austin Schuh | d54e0c4 | 2015-09-13 08:15:55 +0000 | [diff] [blame] | 251 | elevator_params_ = {1.0, 5.0}; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (data.PosEdge(kScoreBegin)) { |
| 255 | // TODO(comran): Get actual height/velocity/acceleration values. |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 256 | elevator_goal_ = 0.030; |
| 257 | elevator_params_ = {1.0, 5.0}; |
| 258 | intake_closed_ = false; |
| 259 | can_restraint_open_ = true; |
| 260 | passive_support_open_ = true; |
| 261 | tote_count_ = 0; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 262 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 263 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 264 | // Send our goals if everything looks OK. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 265 | if (!waiting_for_zero_) { |
| 266 | if (!action_queue_.Running()) { |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 267 | // Send our elevator goals, with limits set in the profile params. |
| 268 | auto new_elevator_goal = elevator_queue.goal.MakeMessage(); |
| 269 | new_elevator_goal->max_velocity = elevator_params_.velocity; |
| 270 | new_elevator_goal->max_acceleration = elevator_params_.acceleration; |
| 271 | new_elevator_goal->height = elevator_goal_; |
| 272 | new_elevator_goal->velocity = 0.0; |
| 273 | new_elevator_goal->passive_support = passive_support_open_; |
| 274 | new_elevator_goal->can_support = can_restraint_open_; |
| 275 | |
| 276 | if (new_elevator_goal.Send()) { |
| 277 | LOG(DEBUG, "sending goals: elevator: %f\n", elevator_goal_); |
| 278 | } else { |
| 279 | LOG(ERROR, "Sending elevator goal failed.\n"); |
| 280 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 284 | // Send our intake goals. |
| 285 | if (!intake_queue.goal.MakeWithBuilder().movement(intake_goal) |
| 286 | .claw_closed(intake_closed_).Send()) { |
| 287 | LOG(ERROR, "Sending intake goal failed.\n"); |
| 288 | } |
| 289 | |
| 290 | // If an action is running, use the action's goals for the profiled |
| 291 | // superstructure subsystems & bypass others. |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 292 | if (action_queue_.Running()) { |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 293 | control_loops::elevator_queue.status.FetchLatest(); |
| 294 | if (control_loops::elevator_queue.status.get()) { |
| 295 | elevator_goal_ = control_loops::elevator_queue.status->goal_height; |
| 296 | } else { |
| 297 | LOG(ERROR, "No elevator status!\n"); |
| 298 | } |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 299 | } |
| 300 | action_queue_.Tick(); |
| 301 | was_running_ = action_queue_.Running(); |
| 302 | } |
| 303 | |
| 304 | private: |
| 305 | void StartAuto() { |
| 306 | LOG(INFO, "Starting auto mode\n"); |
| 307 | ::bot3::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 308 | } |
| 309 | |
| 310 | void StopAuto() { |
| 311 | LOG(INFO, "Stopping auto mode\n"); |
| 312 | ::bot3::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 313 | } |
| 314 | |
| 315 | bool was_running_; |
| 316 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 317 | double elevator_goal_ = 0.2; |
| 318 | |
| 319 | ProfileParams elevator_params_ = kElevatorMove; |
| 320 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 321 | // If we're waiting for the subsystems to zero. |
| 322 | bool waiting_for_zero_ = true; |
| 323 | |
| 324 | bool auto_running_ = false; |
| 325 | |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 326 | bool intake_closed_ = false; |
| 327 | |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 328 | bool can_restraint_open_ = true; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 329 | |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 330 | bool passive_support_open_ = true; |
| 331 | |
| 332 | int tote_count_ = 0; |
Austin Schuh | edd6301 | 2015-09-13 05:09:03 +0000 | [diff] [blame] | 333 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 334 | ::aos::common::actions::ActionQueue action_queue_; |
| 335 | |
Austin Schuh | 8fc29c5 | 2015-09-13 20:19:05 +0000 | [diff] [blame] | 336 | enum StackingStateMachine { |
| 337 | WAITING, |
| 338 | GOING_DOWN, |
| 339 | GOING_UP, |
| 340 | FULL, |
| 341 | OFF |
| 342 | }; |
| 343 | |
| 344 | StackingStateMachine stacking_state_machine_; |
| 345 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 346 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 347 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 348 | "no drivetrain status"); |
| 349 | }; |
| 350 | |
| 351 | } // namespace joysticks |
| 352 | } // namespace input |
| 353 | } // namespace bot3 |
| 354 | |
| 355 | int main() { |
| 356 | ::aos::Init(); |
| 357 | ::bot3::input::joysticks::Reader reader; |
| 358 | reader.Run(); |
| 359 | ::aos::Cleanup(); |
| 360 | } |