Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame^] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include "aos/common/actions/actions.h" |
| 7 | #include "aos/common/input/driver_station_data.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | #include "aos/common/time.h" |
| 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/input/drivetrain_input.h" |
| 12 | #include "aos/input/joystick_input.h" |
| 13 | #include "aos/linux_code/init.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 15 | #include "y2018/control_loops/drivetrain/drivetrain_base.h" |
| 16 | |
| 17 | using ::frc971::control_loops::drivetrain_queue; |
| 18 | |
| 19 | using ::aos::input::driver_station::ButtonLocation; |
| 20 | using ::aos::input::driver_station::ControlBit; |
| 21 | using ::aos::input::driver_station::JoystickAxis; |
| 22 | using ::aos::input::driver_station::POVLocation; |
| 23 | using ::aos::input::DrivetrainInputReader; |
| 24 | |
| 25 | namespace y2018 { |
| 26 | namespace input { |
| 27 | namespace joysticks { |
| 28 | |
| 29 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 30 | |
| 31 | class Reader : public ::aos::input::JoystickInput { |
| 32 | public: |
| 33 | Reader() { |
| 34 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
| 35 | DrivetrainInputReader::InputType::kSteeringWheel, |
| 36 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig()); |
| 37 | } |
| 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 (!auto_running_) { |
| 52 | HandleDrivetrain(data); |
| 53 | HandleTeleop(data); |
| 54 | } |
| 55 | |
| 56 | // Process any pending actions. |
| 57 | action_queue_.Tick(); |
| 58 | was_running_ = action_queue_.Running(); |
| 59 | } |
| 60 | |
| 61 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 62 | drivetrain_input_reader_->HandleDrivetrain(data); |
| 63 | robot_velocity_ = drivetrain_input_reader_->robot_velocity(); |
| 64 | } |
| 65 | |
| 66 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 67 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 68 | action_queue_.CancelAllActions(); |
| 69 | LOG(DEBUG, "Canceling\n"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | void StartAuto() { LOG(INFO, "Starting auto mode\n"); } |
| 75 | |
| 76 | void StopAuto() { |
| 77 | LOG(INFO, "Stopping auto mode\n"); |
| 78 | action_queue_.CancelAllActions(); |
| 79 | } |
| 80 | |
| 81 | bool was_running_ = false; |
| 82 | bool auto_running_ = false; |
| 83 | |
| 84 | double robot_velocity_ = 0.0; |
| 85 | |
| 86 | ::aos::common::actions::ActionQueue action_queue_; |
| 87 | }; |
| 88 | |
| 89 | } // namespace joysticks |
| 90 | } // namespace input |
| 91 | } // namespace y2018 |
| 92 | |
| 93 | int main() { |
| 94 | ::aos::Init(-1); |
| 95 | ::y2018::input::joysticks::Reader reader; |
| 96 | reader.Run(); |
| 97 | ::aos::Cleanup(); |
| 98 | } |