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 | |
| 6 | #include "aos/linux_code/init.h" |
| 7 | #include "aos/input/joystick_input.h" |
| 8 | #include "aos/common/input/driver_station_data.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/time.h" |
| 11 | #include "aos/common/actions/actions.h" |
| 12 | |
| 13 | #include "y2012/control_loops/drivetrain/drivetrain.q.h" |
| 14 | #include "y2012/control_loops/accessories/accessories.q.h" |
| 15 | |
| 16 | using ::y2012::control_loops::drivetrain_queue; |
| 17 | using ::y2012::control_loops::accessories_queue; |
| 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: |
| 83 | Reader() |
| 84 | : is_high_gear_(false) {} |
| 85 | |
| 86 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 87 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 88 | HandleDrivetrain(data); |
| 89 | HandleTeleop(data); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 94 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 95 | const double throttle = -data.GetAxis(kDriveThrottle); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame^] | 96 | if (data.PosEdge(kShiftLow)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 97 | is_high_gear_ = false; |
| 98 | } |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame^] | 99 | if (data.PosEdge(kShiftHigh)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 100 | is_high_gear_ = true; |
| 101 | } |
| 102 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 103 | .steering(wheel) |
| 104 | .throttle(throttle) |
| 105 | .highgear(is_high_gear_) |
| 106 | .quickturn(data.IsPressed(kQuickTurn)) |
| 107 | .left_velocity_goal(0) |
| 108 | .right_velocity_goal(0) |
| 109 | .Send()) { |
| 110 | LOG(WARNING, "sending stick values failed\n"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 115 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 116 | action_queue_.CancelAllActions(); |
| 117 | LOG(DEBUG, "Canceling\n"); |
| 118 | } |
| 119 | |
| 120 | { |
| 121 | auto accessories_message = accessories_queue.goal.MakeMessage(); |
| 122 | accessories_message->solenoids[0] = data.IsPressed(kLongShot); |
| 123 | accessories_message->solenoids[1] = data.IsPressed(kCloseShot); |
| 124 | accessories_message->solenoids[2] = data.IsPressed(kFenderShot); |
| 125 | accessories_message->sticks[0] = data.GetAxis(kAdjustClawGoal); |
| 126 | accessories_message->sticks[1] = data.GetAxis(kAdjustClawSeparation); |
| 127 | if (!accessories_message.Send()) { |
| 128 | LOG(WARNING, "sending accessories goal failed\n"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | action_queue_.Tick(); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | bool is_high_gear_; |
| 137 | |
| 138 | ::aos::common::actions::ActionQueue action_queue_; |
| 139 | }; |
| 140 | |
| 141 | } // namespace joysticks |
| 142 | } // namespace input |
| 143 | } // namespace y2012 |
| 144 | |
| 145 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 146 | ::aos::Init(-1); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 147 | ::y2012::input::joysticks::Reader reader; |
| 148 | reader.Run(); |
| 149 | ::aos::Cleanup(); |
| 150 | } |