Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +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" |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 7 | #include "aos/input/joystick_input.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 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 | |
| 13 | #include "frc971/queues/gyro.q.h" |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 15 | #include "y2014_bot3/autonomous/auto.q.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 16 | #include "y2014_bot3/control_loops/rollers/rollers.q.h" |
| 17 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 18 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 19 | using ::y2014_bot3::control_loops::rollers_queue; |
| 20 | using ::frc971::sensors::gyro_reading; |
| 21 | |
| 22 | using ::aos::input::driver_station::ButtonLocation; |
| 23 | using ::aos::input::driver_station::POVLocation; |
| 24 | using ::aos::input::driver_station::JoystickAxis; |
| 25 | using ::aos::input::driver_station::ControlBit; |
| 26 | |
| 27 | namespace y2014_bot3 { |
| 28 | namespace input { |
| 29 | namespace joysticks { |
| 30 | |
| 31 | // Joystick & button addresses. |
| 32 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 33 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 34 | const ButtonLocation kQuickTurn(1, 5); |
| 35 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 36 | const ButtonLocation kTurn1(1, 7); |
| 37 | const ButtonLocation kTurn2(1, 11); |
| 38 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 39 | const ButtonLocation kFrontRollersIn(3, 8); |
| 40 | const ButtonLocation kBackRollersIn(3, 7); |
| 41 | const ButtonLocation kFrontRollersOut(3, 6); |
| 42 | const ButtonLocation kBackRollersOut(4, 12); |
| 43 | const ButtonLocation kHumanPlayer(4, 11); |
| 44 | |
| 45 | class Reader : public ::aos::input::JoystickInput { |
| 46 | public: |
| 47 | Reader() : is_high_gear_(false) {} |
| 48 | |
| 49 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 50 | bool last_auto_running = auto_running_; |
| 51 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 52 | data.GetControlBit(ControlBit::kEnabled); |
| 53 | if (auto_running_ != last_auto_running) { |
| 54 | if (auto_running_) { |
| 55 | StartAuto(); |
| 56 | } else { |
| 57 | StopAuto(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 62 | HandleDrivetrain(data); |
| 63 | HandleTeleop(data); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 68 | bool is_control_loop_driving = false; |
| 69 | static double left_goal = 0.0; |
| 70 | static double right_goal = 0.0; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 71 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 72 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 73 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 74 | if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) { |
| 75 | drivetrain_queue.status.FetchLatest(); |
| 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 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 85 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 86 | .steering(wheel) |
| 87 | .throttle(throttle) |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 88 | .highgear(is_high_gear_) |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 89 | .quickturn(data.IsPressed(kQuickTurn)) |
| 90 | .control_loop_driving(is_control_loop_driving) |
| 91 | .left_goal(left_goal - wheel * 0.5 + throttle * 0.3) |
| 92 | .right_goal(right_goal + wheel * 0.5 + throttle * 0.3) |
| 93 | .left_velocity_goal(0) |
| 94 | .right_velocity_goal(0) |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 95 | .Send()) { |
| 96 | LOG(WARNING, "sending stick values failed\n"); |
| 97 | } |
| 98 | |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 99 | if (data.PosEdge(kShiftLow)) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 100 | is_high_gear_ = false; |
| 101 | } |
| 102 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 103 | if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 104 | is_high_gear_ = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 109 | // Rollers. |
| 110 | auto rollers_goal = control_loops::rollers_queue.goal.MakeMessage(); |
| 111 | rollers_goal->Zero(); |
| 112 | if (data.IsPressed(kFrontRollersIn)) { |
| 113 | rollers_goal->intake = 1; |
| 114 | } else if (data.IsPressed(kFrontRollersOut)) { |
| 115 | rollers_goal->low_spit = 1; |
| 116 | } else if (data.IsPressed(kBackRollersIn)) { |
| 117 | rollers_goal->intake = -1; |
| 118 | } else if (data.IsPressed(kBackRollersOut)) { |
| 119 | rollers_goal->low_spit = -1; |
| 120 | } else if (data.IsPressed(kHumanPlayer)) { |
| 121 | rollers_goal->human_player = true; |
| 122 | } |
| 123 | if (!rollers_goal.Send()) { |
| 124 | LOG(WARNING, "Sending rollers values failed.\n"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | void StartAuto() { |
| 130 | LOG(INFO, "Starting auto mode.\n"); |
| 131 | ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 132 | } |
| 133 | |
| 134 | void StopAuto() { |
| 135 | LOG(INFO, "Stopping auto mode\n"); |
| 136 | ::y2014_bot3::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 137 | } |
| 138 | |
| 139 | bool auto_running_ = false; |
| 140 | |
| 141 | bool is_high_gear_; |
| 142 | |
| 143 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 144 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 145 | "no drivetrain status"); |
| 146 | }; |
| 147 | |
| 148 | } // namespace joysticks |
| 149 | } // namespace input |
| 150 | } // namespace y2014_bot3 |
| 151 | |
| 152 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 153 | ::aos::Init(-1); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 154 | ::y2014_bot3::input::joysticks::Reader reader; |
| 155 | reader.Run(); |
| 156 | ::aos::Cleanup(); |
| 157 | } |