Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/events/shm-event-loop.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 8 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/input/driver_station_data.h" |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 10 | #include "aos/input/joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
| 12 | #include "aos/time/time.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 15 | #include "y2012/control_loops/accessories/accessories.q.h" |
| 16 | |
Austin Schuh | 2a671df | 2016-11-26 15:00:06 -0800 | [diff] [blame] | 17 | using ::frc971::control_loops::drivetrain_queue; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 18 | |
| 19 | using ::aos::input::driver_station::ButtonLocation; |
| 20 | using ::aos::input::driver_station::JoystickAxis; |
| 21 | using ::aos::input::driver_station::ControlBit; |
| 22 | |
| 23 | #define OLD_DS 0 |
| 24 | |
| 25 | namespace y2012 { |
| 26 | namespace input { |
| 27 | namespace joysticks { |
| 28 | |
| 29 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 30 | const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 31 | const ButtonLocation kQuickTurn(1, 5); |
| 32 | |
| 33 | const ButtonLocation kCatch(3, 10); |
| 34 | |
| 35 | #if OLD_DS |
| 36 | const ButtonLocation kFire(3, 11); |
| 37 | const ButtonLocation kUnload(1, 4); |
| 38 | const ButtonLocation kReload(1, 2); |
| 39 | |
| 40 | const ButtonLocation kRollersOut(3, 12); |
| 41 | const ButtonLocation kRollersIn(3, 7); |
| 42 | |
| 43 | const ButtonLocation kTuck(3, 9); |
| 44 | const ButtonLocation kIntakePosition(3, 8); |
| 45 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 46 | const ButtonLocation kVerticalTuck(3, 1); |
| 47 | const JoystickAxis kFlipRobot(3, 3); |
| 48 | |
| 49 | const ButtonLocation kLongShot(3, 5); |
| 50 | const ButtonLocation kCloseShot(3, 2); |
| 51 | const ButtonLocation kFenderShot(3, 3); |
| 52 | const ButtonLocation kTrussShot(2, 11); |
| 53 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 54 | #else |
| 55 | const ButtonLocation kFire(3, 9); |
| 56 | const ButtonLocation kUnload(1, 4); |
| 57 | const ButtonLocation kReload(1, 2); |
| 58 | |
| 59 | const ButtonLocation kRollersOut(3, 8); |
| 60 | const ButtonLocation kRollersIn(3, 3); |
| 61 | |
| 62 | const ButtonLocation kTuck(3, 4); |
| 63 | const ButtonLocation kIntakePosition(3, 5); |
| 64 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 65 | const ButtonLocation kVerticalTuck(2, 6); |
| 66 | const JoystickAxis kFlipRobot(3, 3); |
| 67 | |
| 68 | const ButtonLocation kLongShot(3, 7); |
| 69 | const ButtonLocation kCloseShot(3, 6); |
| 70 | const ButtonLocation kFenderShot(3, 2); |
| 71 | const ButtonLocation kTrussShot(2, 11); |
| 72 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 73 | #endif |
| 74 | |
| 75 | const ButtonLocation kUserLeft(2, 7); |
| 76 | const ButtonLocation kUserRight(2, 10); |
| 77 | |
| 78 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 79 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 80 | |
| 81 | class Reader : public ::aos::input::JoystickInput { |
| 82 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 83 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame^] | 84 | : ::aos::input::JoystickInput(event_loop), |
| 85 | accessories_goal_sender_( |
| 86 | event_loop |
| 87 | ->MakeSender<::y2012::control_loops::AccessoriesQueue::Message>( |
| 88 | ".y2012.control_loops.accessories_queue.goal")), |
| 89 | is_high_gear_(false) {} |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 90 | |
| 91 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 92 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 93 | HandleDrivetrain(data); |
| 94 | HandleTeleop(data); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 99 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 100 | const double throttle = -data.GetAxis(kDriveThrottle); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 101 | if (data.PosEdge(kShiftLow)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 102 | is_high_gear_ = false; |
| 103 | } |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 104 | if (data.PosEdge(kShiftHigh)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 105 | is_high_gear_ = true; |
| 106 | } |
| 107 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 108 | .wheel(wheel) |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 109 | .throttle(throttle) |
| 110 | .highgear(is_high_gear_) |
| 111 | .quickturn(data.IsPressed(kQuickTurn)) |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 112 | .Send()) { |
| 113 | LOG(WARNING, "sending stick values failed\n"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame^] | 118 | auto accessories_message = accessories_goal_sender_.MakeMessage(); |
| 119 | accessories_message->solenoids[0] = data.IsPressed(kLongShot); |
| 120 | accessories_message->solenoids[1] = data.IsPressed(kCloseShot); |
| 121 | accessories_message->solenoids[2] = data.IsPressed(kFenderShot); |
| 122 | accessories_message->sticks[0] = data.GetAxis(kAdjustClawGoal); |
| 123 | accessories_message->sticks[1] = data.GetAxis(kAdjustClawSeparation); |
| 124 | if (!accessories_message.Send()) { |
| 125 | LOG(WARNING, "sending accessories goal failed\n"); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 126 | } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame^] | 130 | ::aos::Sender<::y2012::control_loops::AccessoriesQueue::Message> |
| 131 | accessories_goal_sender_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 132 | |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame^] | 133 | bool is_high_gear_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace joysticks |
| 137 | } // namespace input |
| 138 | } // namespace y2012 |
| 139 | |
| 140 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 141 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 142 | ::aos::ShmEventLoop event_loop; |
| 143 | ::y2012::input::joysticks::Reader reader(&event_loop); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 144 | reader.Run(); |
| 145 | ::aos::Cleanup(); |
| 146 | } |