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 | |
| 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/logging/logging.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/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: |
| 42 | Reader() { |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame^] | 43 | // Setting driver station type to Steering Wheel |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 44 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame^] | 45 | DrivetrainInputReader::InputType::kSteeringWheel, |
| 46 | ::y2017_bot3::control_loops::drivetrain::GetDrivetrainConfig()); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
Sabina Davis | ff3e51d | 2017-10-26 21:40:22 -0700 | [diff] [blame] | 50 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 51 | action_queue_.CancelAllActions(); |
| 52 | LOG(DEBUG, "Canceling\n"); |
| 53 | } |
| 54 | |
| 55 | const bool last_auto_running = auto_running_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 56 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 57 | data.GetControlBit(ControlBit::kEnabled); |
| 58 | if (auto_running_ != last_auto_running) { |
| 59 | if (auto_running_) { |
| 60 | StartAuto(); |
| 61 | } else { |
| 62 | StopAuto(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (!auto_running_) { |
| 67 | HandleDrivetrain(data); |
| 68 | HandleTeleop(data); |
| 69 | } |
| 70 | |
| 71 | // Process pending actions. |
| 72 | action_queue_.Tick(); |
| 73 | was_running_ = action_queue_.Running(); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 74 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame^] | 75 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 76 | action_queue_.CancelAllActions(); |
| 77 | LOG(DEBUG, "Canceling\n"); |
| 78 | } |
| 79 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 80 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 81 | drivetrain_input_reader_->HandleDrivetrain(data); |
| 82 | robot_velocity_ = drivetrain_input_reader_->robot_velocity(); |
| 83 | } |
| 84 | |
| 85 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 86 | superstructure_queue.status.FetchLatest(); |
| 87 | if (!superstructure_queue.status.get()) { |
| 88 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 93 | new_superstructure_goal->voltage_rollers = 0.0; |
| 94 | |
| 95 | if (data.IsPressed(kRollerOn)) { |
| 96 | new_superstructure_goal->voltage_rollers = 12.0; |
| 97 | } |
| 98 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame^] | 99 | if (data.IsPressed(kRollerSpit)) { |
| 100 | new_superstructure_goal->voltage_rollers = -12.0; |
| 101 | } |
| 102 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 103 | if (data.IsPressed(kHangerOn)) { |
| 104 | new_superstructure_goal->hanger_voltage = 12.0; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | if (data.IsPressed(kGearOut)) { |
| 109 | new_superstructure_goal->fingers_out = true; |
| 110 | } else { |
| 111 | new_superstructure_goal->fingers_out = false; |
| 112 | } |
| 113 | |
| 114 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 115 | if (!new_superstructure_goal.Send()) { |
| 116 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | void StartAuto() { |
| 122 | LOG(INFO, "Starting auto mode\n"); |
| 123 | ::frc971::autonomous::AutonomousActionParams params; |
| 124 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 125 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
| 126 | params.mode = ::frc971::autonomous::auto_mode->mode; |
| 127 | } else { |
| 128 | LOG(WARNING, "no auto mode values\n"); |
| 129 | params.mode = 0; |
| 130 | } |
| 131 | action_queue_.EnqueueAction( |
| 132 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 133 | } |
| 134 | |
| 135 | void StopAuto() { |
| 136 | LOG(INFO, "Stopping auto mode\n"); |
| 137 | action_queue_.CancelAllActions(); |
| 138 | } |
| 139 | double robot_velocity_ = 0.0; |
| 140 | ::aos::common::actions::ActionQueue action_queue_; |
| 141 | |
| 142 | bool was_running_ = false; |
| 143 | bool auto_running_ = false; |
| 144 | }; |
| 145 | |
| 146 | } // namespace joysticks |
| 147 | } // namespace input |
| 148 | } // namespace y2017_bot3 |
| 149 | |
| 150 | int main() { |
| 151 | ::aos::Init(-1); |
| 152 | ::y2017_bot3::input::joysticks::Reader reader; |
| 153 | reader.Run(); |
| 154 | ::aos::Cleanup(); |
| 155 | } |