Austin Schuh | ea9d602 | 2017-01-02 13:38:07 -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/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
| 12 | #include "aos/common/actions/actions.h" |
| 13 | |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 15 | |
| 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "y2016_bot4/control_loops/drivetrain/drivetrain_base.h" |
| 18 | |
| 19 | using ::frc971::control_loops::drivetrain_queue; |
| 20 | |
| 21 | using ::aos::input::driver_station::ButtonLocation; |
| 22 | using ::aos::input::driver_station::ControlBit; |
| 23 | using ::aos::input::driver_station::JoystickAxis; |
| 24 | using ::aos::input::driver_station::POVLocation; |
| 25 | |
| 26 | namespace y2016_bot4 { |
| 27 | namespace input { |
| 28 | namespace joysticks { |
| 29 | |
| 30 | //#define XBOX |
| 31 | |
| 32 | #ifdef XBOX |
| 33 | // Xbox |
| 34 | const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2); |
| 35 | const ButtonLocation kQuickTurn(1, 5); |
| 36 | #else |
| 37 | // Steering wheel |
| 38 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 39 | const ButtonLocation kQuickTurn(1, 5); |
| 40 | #endif |
| 41 | |
| 42 | const ButtonLocation kTurn1(1, 7); |
| 43 | const ButtonLocation kTurn2(1, 11); |
| 44 | |
| 45 | class Reader : public ::aos::input::JoystickInput { |
| 46 | public: |
| 47 | Reader() {} |
| 48 | |
| 49 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 50 | const bool auto_running = data.GetControlBit(ControlBit::kAutonomous) && |
| 51 | data.GetControlBit(ControlBit::kEnabled); |
| 52 | |
| 53 | if (!auto_running) { |
| 54 | HandleDrivetrain(data); |
| 55 | } else { |
| 56 | drivetrain_queue.goal.MakeWithBuilder() |
| 57 | .steering(0.0) |
| 58 | .throttle(0.0) |
| 59 | .quickturn(false) |
| 60 | .control_loop_driving(false) |
| 61 | .left_goal(0.0) |
| 62 | .right_goal(0.0) |
| 63 | .left_velocity_goal(0) |
| 64 | .right_velocity_goal(0) |
| 65 | .Send(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 70 | bool is_control_loop_driving = false; |
| 71 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 72 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 73 | drivetrain_queue.status.FetchLatest(); |
| 74 | |
| 75 | if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) { |
| 76 | if (drivetrain_queue.status.get()) { |
| 77 | left_goal_ = drivetrain_queue.status->estimated_left_position; |
| 78 | right_goal_ = drivetrain_queue.status->estimated_right_position; |
| 79 | } |
| 80 | } |
| 81 | if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) { |
| 82 | is_control_loop_driving = true; |
| 83 | } |
| 84 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 85 | .steering(wheel) |
| 86 | .throttle(throttle) |
| 87 | .quickturn(data.IsPressed(kQuickTurn)) |
| 88 | .control_loop_driving(is_control_loop_driving) |
| 89 | .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3) |
| 90 | .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3) |
| 91 | .left_velocity_goal(0) |
| 92 | .right_velocity_goal(0) |
| 93 | .Send()) { |
| 94 | LOG(WARNING, "sending stick values failed\n"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | double left_goal_ = 0.0; |
| 100 | double right_goal_ = 0.0; |
| 101 | }; |
| 102 | |
| 103 | } // namespace joysticks |
| 104 | } // namespace input |
| 105 | } // namespace y2016_bot4 |
| 106 | |
| 107 | int main() { |
| 108 | ::aos::Init(-1); |
| 109 | ::y2016_bot4::input::joysticks::Reader reader; |
| 110 | reader.Run(); |
| 111 | ::aos::Cleanup(); |
| 112 | } |