Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -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/prime/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" |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 12 | #include "aos/common/actions/actions.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 13 | |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 15 | #include "frc971/constants.h" |
| 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 18 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 19 | using ::frc971::control_loops::drivetrain_queue; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 20 | using ::frc971::sensors::gyro_reading; |
| 21 | |
| 22 | using ::aos::input::driver_station::ButtonLocation; |
| 23 | using ::aos::input::driver_station::JoystickAxis; |
| 24 | using ::aos::input::driver_station::ControlBit; |
| 25 | |
| 26 | namespace frc971 { |
| 27 | namespace input { |
| 28 | namespace joysticks { |
| 29 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 30 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 31 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 32 | const ButtonLocation kQuickTurn(1, 5); |
| 33 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 34 | |
| 35 | class Reader : public ::aos::input::JoystickInput { |
| 36 | public: |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame^] | 37 | Reader() : was_running_(false) {} |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 38 | |
| 39 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 40 | bool last_auto_running = auto_running_; |
| 41 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
Brian Silverman | e1103e6 | 2015-02-15 02:03:38 -0500 | [diff] [blame] | 42 | data.GetControlBit(ControlBit::kEnabled); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 43 | if (auto_running_ != last_auto_running) { |
| 44 | if (auto_running_) { |
| 45 | StartAuto(); |
| 46 | } else { |
| 47 | StopAuto(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 48 | } |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 52 | HandleTeleop(data); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 57 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 58 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 59 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 60 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 61 | .steering(wheel) |
| 62 | .throttle(throttle) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 63 | .quickturn(data.IsPressed(kQuickTurn)) |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame^] | 64 | .control_loop_driving(false) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 65 | .Send()) { |
| 66 | LOG(WARNING, "sending stick values failed\n"); |
| 67 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 71 | HandleDrivetrain(data); |
| 72 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 73 | action_queue_.CancelAllActions(); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 74 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | action_queue_.Tick(); |
| 78 | was_running_ = action_queue_.Running(); |
| 79 | } |
| 80 | |
| 81 | private: |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 82 | void StartAuto() { |
| 83 | LOG(INFO, "Starting auto mode\n"); |
| 84 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 85 | } |
| 86 | |
| 87 | void StopAuto() { |
| 88 | LOG(INFO, "Stopping auto mode\n"); |
| 89 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 90 | } |
| 91 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 92 | bool was_running_; |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 93 | bool auto_running_ = false; |
| 94 | |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame^] | 95 | ::aos::common::actions::ActionQueue action_queue_; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 96 | |
| 97 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 98 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 99 | "no drivetrain status"); |
| 100 | }; |
| 101 | |
| 102 | } // namespace joysticks |
| 103 | } // namespace input |
| 104 | } // namespace frc971 |
| 105 | |
| 106 | int main() { |
| 107 | ::aos::Init(); |
| 108 | ::frc971::input::joysticks::Reader reader; |
| 109 | reader.Run(); |
| 110 | ::aos::Cleanup(); |
| 111 | } |