Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +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/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" |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 15 | #include "y2016/constants.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 18 | |
| 19 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 20 | |
| 21 | using ::aos::input::driver_station::ButtonLocation; |
| 22 | using ::aos::input::driver_station::JoystickAxis; |
| 23 | using ::aos::input::driver_station::ControlBit; |
| 24 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 25 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 26 | namespace input { |
| 27 | namespace joysticks { |
| 28 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 29 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 30 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 31 | const ButtonLocation kQuickTurn(1, 5); |
| 32 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 33 | class Reader : public ::aos::input::JoystickInput { |
| 34 | public: |
| 35 | Reader() |
| 36 | : is_high_gear_(false), |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 37 | was_running_(false) {} |
| 38 | |
| 39 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 40 | bool last_auto_running = auto_running_; |
| 41 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 42 | data.GetControlBit(ControlBit::kEnabled); |
| 43 | if (auto_running_ != last_auto_running) { |
| 44 | if (auto_running_) { |
| 45 | StartAuto(); |
| 46 | } else { |
| 47 | StopAuto(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 52 | HandleDrivetrain(data); |
| 53 | HandleTeleop(data); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 58 | bool is_control_loop_driving = false; |
| 59 | double left_goal = 0.0; |
| 60 | double right_goal = 0.0; |
| 61 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 62 | const double throttle = -data.GetAxis(kDriveThrottle); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 63 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 64 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 65 | .steering(wheel) |
| 66 | .throttle(throttle) |
| 67 | .highgear(is_high_gear_) |
| 68 | .quickturn(data.IsPressed(kQuickTurn)) |
| 69 | .control_loop_driving(is_control_loop_driving) |
| 70 | .left_goal(left_goal) |
| 71 | .right_goal(right_goal) |
| 72 | .left_velocity_goal(0) |
| 73 | .right_velocity_goal(0) |
| 74 | .Send()) { |
| 75 | LOG(WARNING, "sending stick values failed\n"); |
| 76 | } |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 77 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 78 | if (data.PosEdge(kShiftHigh)) { |
| 79 | is_high_gear_ = false; |
| 80 | } |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 81 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 82 | if (data.PosEdge(kShiftLow)) { |
| 83 | is_high_gear_ = true; |
| 84 | } |
| 85 | } |
| 86 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 87 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 88 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 89 | action_queue_.CancelAllActions(); |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 90 | LOG(DEBUG, "Canceling\n"); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 93 | was_running_ = action_queue_.Running(); |
| 94 | } |
| 95 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 96 | private: |
| 97 | void StartAuto() { |
| 98 | LOG(INFO, "Starting auto mode\n"); |
| 99 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 100 | } |
| 101 | |
| 102 | void StopAuto() { |
| 103 | LOG(INFO, "Stopping auto mode\n"); |
| 104 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 105 | } |
| 106 | |
| 107 | bool is_high_gear_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 108 | bool was_running_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 109 | bool auto_running_ = false; |
| 110 | |
| 111 | ::aos::common::actions::ActionQueue action_queue_; |
| 112 | |
| 113 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 114 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 115 | "no drivetrain status"); |
| 116 | }; |
| 117 | |
| 118 | } // namespace joysticks |
| 119 | } // namespace input |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 120 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 121 | |
| 122 | int main() { |
| 123 | ::aos::Init(-1); |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 124 | ::y2016::input::joysticks::Reader reader; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 125 | reader.Run(); |
| 126 | ::aos::Cleanup(); |
| 127 | } |