Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +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/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" |
| 12 | #include "aos/common/actions/actions.h" |
| 13 | |
| 14 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
| 15 | #include "frc971/queues/gyro.q.h" |
| 16 | #include "bot3/autonomous/auto.q.h" |
| 17 | |
| 18 | using ::bot3::control_loops::drivetrain_queue; |
| 19 | using ::frc971::sensors::gyro_reading; |
| 20 | |
| 21 | using ::aos::input::driver_station::ButtonLocation; |
| 22 | using ::aos::input::driver_station::POVLocation; |
| 23 | using ::aos::input::driver_station::JoystickAxis; |
| 24 | using ::aos::input::driver_station::ControlBit; |
| 25 | |
| 26 | namespace bot3 { |
| 27 | namespace input { |
| 28 | namespace joysticks { |
| 29 | |
| 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 | |
| 34 | // TODO(comran): Figure out the actions we need. |
| 35 | |
| 36 | class Reader : public ::aos::input::JoystickInput { |
| 37 | public: |
| 38 | Reader() : was_running_(false) {} |
| 39 | |
| 40 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 41 | bool last_auto_running = auto_running_; |
| 42 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 43 | data.GetControlBit(ControlBit::kEnabled); |
| 44 | if (auto_running_ != last_auto_running) { |
| 45 | if (auto_running_) { |
| 46 | StartAuto(); |
| 47 | } else { |
| 48 | StopAuto(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 53 | HandleDrivetrain(data); |
| 54 | HandleTeleop(data); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 59 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 60 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 61 | |
| 62 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 63 | .steering(wheel) |
| 64 | .throttle(throttle) |
| 65 | .quickturn(data.IsPressed(kQuickTurn)) |
| 66 | .control_loop_driving(false) |
| 67 | .Send()) { |
| 68 | LOG(WARNING, "sending stick values failed\n"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 73 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 74 | action_queue_.CancelAllActions(); |
| 75 | LOG(DEBUG, "Canceling\n"); |
| 76 | } |
| 77 | |
| 78 | // TODO(comran): Run stuff when buttons are pushed. |
| 79 | |
| 80 | // TODO(comran): If it cannot get a status from the superstructure... |
| 81 | /* |
| 82 | if (!superstructure_queue.status.get()) { |
| 83 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 84 | } |
| 85 | */ |
| 86 | |
| 87 | // TODO(comran): If everything looks good, start teleop. |
| 88 | /*if (superstructure_queue.status.get() && superstructure_queue.status->zeroed) { |
| 89 | if (waiting_for_zero_) { |
| 90 | LOG(INFO, "Zeroed! Starting teleop mode.\n"); |
| 91 | waiting_for_zero_ = false; |
| 92 | |
| 93 | // Set the initial goals to where we are now. |
| 94 | superstructure_goal_ = 0.0; |
| 95 | } |
| 96 | } else { |
| 97 | waiting_for_zero_ = true; |
| 98 | }*/ |
| 99 | |
| 100 | if (!waiting_for_zero_) { |
| 101 | if (!action_queue_.Running()) { |
| 102 | // TODO(comran): Send some goals. |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (action_queue_.Running()) { |
| 107 | // If we are running an action, update our goals to the current goals. |
| 108 | // TODO(comran): Do this for each superstructure queue. |
| 109 | } |
| 110 | action_queue_.Tick(); |
| 111 | was_running_ = action_queue_.Running(); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | void StartAuto() { |
| 116 | LOG(INFO, "Starting auto mode\n"); |
| 117 | ::bot3::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 118 | } |
| 119 | |
| 120 | void StopAuto() { |
| 121 | LOG(INFO, "Stopping auto mode\n"); |
| 122 | ::bot3::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 123 | } |
| 124 | |
| 125 | bool was_running_; |
| 126 | |
| 127 | // If we're waiting for the subsystems to zero. |
| 128 | bool waiting_for_zero_ = true; |
| 129 | |
| 130 | bool auto_running_ = false; |
| 131 | |
| 132 | ::aos::common::actions::ActionQueue action_queue_; |
| 133 | |
| 134 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 135 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 136 | "no drivetrain status"); |
| 137 | }; |
| 138 | |
| 139 | } // namespace joysticks |
| 140 | } // namespace input |
| 141 | } // namespace bot3 |
| 142 | |
| 143 | int main() { |
| 144 | ::aos::Init(); |
| 145 | ::bot3::input::joysticks::Reader reader; |
| 146 | reader.Run(); |
| 147 | ::aos::Cleanup(); |
| 148 | } |