Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/input/driver_station_data.h" |
| 8 | #include "aos/logging/logging.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/util/log_interval.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 11 | #include "aos/input/drivetrain_input.h" |
| 12 | #include "aos/input/joystick_input.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 13 | #include "aos/init.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 14 | #include "frc971/autonomous/auto.q.h" |
| 15 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 16 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 17 | #include "y2017_bot3/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 18 | #include "y2017_bot3/control_loops/drivetrain/drivetrain_base.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 19 | |
| 20 | using ::frc971::control_loops::drivetrain_queue; |
| 21 | using ::y2017_bot3::control_loops::superstructure_queue; |
| 22 | |
| 23 | using ::aos::input::driver_station::ButtonLocation; |
| 24 | using ::aos::input::driver_station::ControlBit; |
| 25 | using ::aos::input::driver_station::JoystickAxis; |
| 26 | using ::aos::input::driver_station::POVLocation; |
| 27 | using ::aos::input::DrivetrainInputReader; |
| 28 | |
| 29 | namespace y2017_bot3 { |
| 30 | namespace input { |
| 31 | namespace joysticks { |
| 32 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 33 | const ButtonLocation kHangerOn(2, 11); |
| 34 | const ButtonLocation kGearOut(2, 10); |
| 35 | const ButtonLocation kRollerOn(2, 7); |
| 36 | const ButtonLocation kRollerSpit(2, 6); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 37 | |
| 38 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 39 | |
| 40 | class Reader : public ::aos::input::JoystickInput { |
| 41 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 42 | Reader(::aos::EventLoop *event_loop) |
| 43 | : ::aos::input::JoystickInput(event_loop) { |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 44 | // Setting driver station type to Steering Wheel |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 45 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 46 | DrivetrainInputReader::InputType::kSteeringWheel, |
| 47 | ::y2017_bot3::control_loops::drivetrain::GetDrivetrainConfig()); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
Sabina Davis | ff3e51d | 2017-10-26 21:40:22 -0700 | [diff] [blame] | 51 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 52 | action_queue_.CancelAllActions(); |
| 53 | LOG(DEBUG, "Canceling\n"); |
| 54 | } |
| 55 | |
| 56 | const bool last_auto_running = auto_running_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 57 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 58 | data.GetControlBit(ControlBit::kEnabled); |
| 59 | if (auto_running_ != last_auto_running) { |
| 60 | if (auto_running_) { |
| 61 | StartAuto(); |
| 62 | } else { |
| 63 | StopAuto(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (!auto_running_) { |
| 68 | HandleDrivetrain(data); |
| 69 | HandleTeleop(data); |
| 70 | } |
| 71 | |
| 72 | // Process pending actions. |
| 73 | action_queue_.Tick(); |
| 74 | was_running_ = action_queue_.Running(); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 75 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 76 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 77 | action_queue_.CancelAllActions(); |
| 78 | LOG(DEBUG, "Canceling\n"); |
| 79 | } |
| 80 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 81 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 82 | drivetrain_input_reader_->HandleDrivetrain(data); |
| 83 | robot_velocity_ = drivetrain_input_reader_->robot_velocity(); |
| 84 | } |
| 85 | |
| 86 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 87 | superstructure_queue.status.FetchLatest(); |
| 88 | if (!superstructure_queue.status.get()) { |
| 89 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 94 | new_superstructure_goal->voltage_rollers = 0.0; |
| 95 | |
| 96 | if (data.IsPressed(kRollerOn)) { |
| 97 | new_superstructure_goal->voltage_rollers = 12.0; |
| 98 | } |
| 99 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 100 | if (data.IsPressed(kRollerSpit)) { |
| 101 | new_superstructure_goal->voltage_rollers = -12.0; |
| 102 | } |
| 103 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 104 | if (data.IsPressed(kHangerOn)) { |
| 105 | new_superstructure_goal->hanger_voltage = 12.0; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | if (data.IsPressed(kGearOut)) { |
| 110 | new_superstructure_goal->fingers_out = true; |
| 111 | } else { |
| 112 | new_superstructure_goal->fingers_out = false; |
| 113 | } |
| 114 | |
| 115 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 116 | if (!new_superstructure_goal.Send()) { |
| 117 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | void StartAuto() { |
| 123 | LOG(INFO, "Starting auto mode\n"); |
| 124 | ::frc971::autonomous::AutonomousActionParams params; |
| 125 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 126 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
| 127 | params.mode = ::frc971::autonomous::auto_mode->mode; |
| 128 | } else { |
| 129 | LOG(WARNING, "no auto mode values\n"); |
| 130 | params.mode = 0; |
| 131 | } |
| 132 | action_queue_.EnqueueAction( |
| 133 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 134 | } |
| 135 | |
| 136 | void StopAuto() { |
| 137 | LOG(INFO, "Stopping auto mode\n"); |
| 138 | action_queue_.CancelAllActions(); |
| 139 | } |
| 140 | double robot_velocity_ = 0.0; |
| 141 | ::aos::common::actions::ActionQueue action_queue_; |
| 142 | |
| 143 | bool was_running_ = false; |
| 144 | bool auto_running_ = false; |
| 145 | }; |
| 146 | |
| 147 | } // namespace joysticks |
| 148 | } // namespace input |
| 149 | } // namespace y2017_bot3 |
| 150 | |
| 151 | int main() { |
| 152 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 153 | ::aos::ShmEventLoop event_loop; |
| 154 | ::y2017_bot3::input::joysticks::Reader reader(&event_loop); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 155 | reader.Run(); |
| 156 | ::aos::Cleanup(); |
| 157 | } |