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