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" |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 16 | #include "y2018/control_loops/superstructure/superstructure.q.h" |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 17 | |
| 18 | using ::frc971::control_loops::drivetrain_queue; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 19 | using ::y2018::control_loops::superstructure_queue; |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 20 | |
| 21 | using ::aos::input::driver_station::ButtonLocation; |
| 22 | using ::aos::input::driver_station::ControlBit; |
| 23 | using ::aos::input::driver_station::JoystickAxis; |
| 24 | using ::aos::input::driver_station::POVLocation; |
| 25 | using ::aos::input::DrivetrainInputReader; |
| 26 | |
| 27 | namespace y2018 { |
| 28 | namespace input { |
| 29 | namespace joysticks { |
| 30 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 31 | //TODO(Neil): Change button locations to real ones on driverstation. |
| 32 | const ButtonLocation kIntakeDown(3, 9); |
| 33 | const POVLocation kIntakeUp(3, 90); |
| 34 | const ButtonLocation kIntakeIn(3, 12); |
| 35 | const ButtonLocation kIntakeOut(3, 8); |
| 36 | |
| 37 | const ButtonLocation kArmDown(3, 3); |
| 38 | const ButtonLocation kArmSwitch(3, 7); |
| 39 | const ButtonLocation kArmScale(3, 6); |
| 40 | |
| 41 | const ButtonLocation kClawOpen(3, 5); |
| 42 | const ButtonLocation kClawClose(3, 4); |
| 43 | |
| 44 | const ButtonLocation kForkDeploy(3, 11); |
| 45 | const ButtonLocation kForkStow(3, 10); |
| 46 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 47 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_; |
| 48 | |
| 49 | class Reader : public ::aos::input::JoystickInput { |
| 50 | public: |
| 51 | Reader() { |
| 52 | drivetrain_input_reader_ = DrivetrainInputReader::Make( |
| 53 | DrivetrainInputReader::InputType::kSteeringWheel, |
| 54 | ::y2018::control_loops::drivetrain::GetDrivetrainConfig()); |
| 55 | } |
| 56 | |
| 57 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 58 | bool last_auto_running = auto_running_; |
| 59 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 60 | data.GetControlBit(ControlBit::kEnabled); |
| 61 | if (auto_running_ != last_auto_running) { |
| 62 | if (auto_running_) { |
| 63 | StartAuto(); |
| 64 | } else { |
| 65 | StopAuto(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!auto_running_) { |
| 70 | HandleDrivetrain(data); |
| 71 | HandleTeleop(data); |
| 72 | } |
| 73 | |
| 74 | // Process any pending actions. |
| 75 | action_queue_.Tick(); |
| 76 | was_running_ = action_queue_.Running(); |
| 77 | } |
| 78 | |
| 79 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 80 | drivetrain_input_reader_->HandleDrivetrain(data); |
| 81 | robot_velocity_ = drivetrain_input_reader_->robot_velocity(); |
| 82 | } |
| 83 | |
| 84 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 85 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 86 | action_queue_.CancelAllActions(); |
| 87 | LOG(DEBUG, "Canceling\n"); |
| 88 | } |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 89 | |
| 90 | superstructure_queue.status.FetchLatest(); |
| 91 | if (!superstructure_queue.status.get()) { |
| 92 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (data.IsPressed(kIntakeUp)) { |
| 97 | // Bring in the intake. |
| 98 | intake_goal_ = -M_PI; //TODO(Neil): Add real value here once we have one. |
| 99 | } |
| 100 | if (data.IsPressed(kIntakeDown)) { |
| 101 | // Deploy the intake. |
| 102 | intake_goal_ = 0; //TODO(Neil): Add real value here once we have one. |
| 103 | } |
| 104 | |
| 105 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 106 | |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame^] | 107 | new_superstructure_goal->intake.left_intake_angle = intake_goal_; |
| 108 | new_superstructure_goal->intake.right_intake_angle = intake_goal_; |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 109 | |
| 110 | if (data.IsPressed(kIntakeIn)) { |
| 111 | // Turn on the rollers. |
| 112 | new_superstructure_goal->intake.roller_voltage = |
| 113 | 9.0; //TODO(Neil): Add real value once we have one. |
| 114 | } else if (data.IsPressed(kIntakeOut)) { |
| 115 | // Turn off the rollers. |
| 116 | new_superstructure_goal->intake.roller_voltage = |
| 117 | -9.0; //TODO(Neil): Add real value once we have one. |
| 118 | } else { |
| 119 | // We don't want the rollers on. |
| 120 | new_superstructure_goal->intake.roller_voltage = 0.0; |
| 121 | } |
| 122 | |
| 123 | if (data.IsPressed(kArmDown)) { |
| 124 | // Put the arm down to the intake level. |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame^] | 125 | new_superstructure_goal->arm_goal_position = |
| 126 | 1; // TODO(Neil): Add real value once we have it. |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 127 | } else if (data.IsPressed(kArmSwitch)) { |
| 128 | // Put the arm up to the level of the switch. |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame^] | 129 | new_superstructure_goal->arm_goal_position = |
| 130 | 1; // TODO(Neil): Add real value once we have it. |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 131 | } else if (data.IsPressed(kArmScale)) { |
| 132 | // Put the arm up to the level of the switch. |
Sabina Davis | fdd7a11 | 2018-02-04 16:16:23 -0800 | [diff] [blame^] | 133 | new_superstructure_goal->arm_goal_position = |
| 134 | 1; // TODO(Neil): Add real value once we have it. |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (data.IsPressed(kClawOpen)) { |
| 138 | new_superstructure_goal->open_claw = true; |
| 139 | } else if (data.IsPressed(kClawClose)) { |
| 140 | new_superstructure_goal->open_claw = false; |
| 141 | } |
| 142 | |
| 143 | if (data.IsPressed(kForkDeploy)) { |
| 144 | new_superstructure_goal->deploy_fork = true; |
| 145 | } else if (data.IsPressed(kForkStow)) { |
| 146 | new_superstructure_goal->deploy_fork = false; |
| 147 | } |
| 148 | |
| 149 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 150 | if (!new_superstructure_goal.Send()) { |
| 151 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 152 | } |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
| 156 | void StartAuto() { LOG(INFO, "Starting auto mode\n"); } |
| 157 | |
| 158 | void StopAuto() { |
| 159 | LOG(INFO, "Stopping auto mode\n"); |
| 160 | action_queue_.CancelAllActions(); |
| 161 | } |
| 162 | |
Neil Balch | 07fee58 | 2018-01-27 15:46:49 -0800 | [diff] [blame] | 163 | // Current goals to send to the robot. |
| 164 | double intake_goal_ = 0.0; |
| 165 | |
Neil Balch | acfca5b | 2018-01-28 14:04:08 -0800 | [diff] [blame] | 166 | bool was_running_ = false; |
| 167 | bool auto_running_ = false; |
| 168 | |
| 169 | double robot_velocity_ = 0.0; |
| 170 | |
| 171 | ::aos::common::actions::ActionQueue action_queue_; |
| 172 | }; |
| 173 | |
| 174 | } // namespace joysticks |
| 175 | } // namespace input |
| 176 | } // namespace y2018 |
| 177 | |
| 178 | int main() { |
| 179 | ::aos::Init(-1); |
| 180 | ::y2018::input::joysticks::Reader reader; |
| 181 | reader.Run(); |
| 182 | ::aos::Cleanup(); |
| 183 | } |