brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 6 | #include "aos/atom_code/init.h" |
| 7 | #include "aos/atom_code/input/joystick_input.h" |
| 8 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 10 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 11 | #include "bot3/control_loops/shooter/shooter_motor.q.h" |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 12 | #include "bot3/autonomous/auto.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | #include "frc971/queues/GyroAngle.q.h" |
| 14 | #include "frc971/queues/Piston.q.h" |
Brian Silverman | 32d6914 | 2013-03-30 00:02:06 -0700 | [diff] [blame] | 15 | #include "frc971/queues/CameraTarget.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 17 | using ::bot3::control_loops::drivetrain; |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 18 | using ::bot3::control_loops::shooter; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 19 | using ::frc971::control_loops::shifters; |
| 20 | using ::frc971::sensors::gyro; |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 21 | // using ::frc971::vision::target_angle; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 23 | using ::aos::input::driver_station::ButtonLocation; |
| 24 | using ::aos::input::driver_station::JoystickAxis; |
| 25 | using ::aos::input::driver_station::ControlBit; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame] | 27 | namespace bot3 { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 28 | namespace input { |
| 29 | namespace joysticks { |
| 30 | |
| 31 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 32 | kDriveControlLoopEnable2(1, 11); |
| 33 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 34 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 35 | const ButtonLocation kQuickTurn(1, 5); |
| 36 | |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 37 | const ButtonLocation kPush(3, 9); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 38 | |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 39 | const ButtonLocation kFire(3, 3); |
| 40 | const ButtonLocation kIntake(3, 4); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 41 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 42 | class Reader : public ::aos::input::JoystickInput { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 43 | bool last_push_; |
| 44 | bool shooting_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 45 | public: |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 46 | static const bool kWristAlwaysDown = false; |
| 47 | |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 48 | Reader() : shooting_(false) { |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame] | 49 | printf("\nRunning Bot3 JoystickReader!\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | shifters.MakeWithBuilder().set(true).Send(); |
| 51 | } |
| 52 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 53 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 54 | static bool is_high_gear = false; |
| 55 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 56 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 57 | if (data.PosEdge(ControlBit::kEnabled)){ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | LOG(INFO, "Starting auto mode\n"); |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 59 | ::bot3::autonomous::autonomous.MakeWithBuilder() |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 60 | .run_auto(true).Send(); |
| 61 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 62 | LOG(INFO, "Stopping auto mode\n"); |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 63 | ::bot3::autonomous::autonomous.MakeWithBuilder() |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 64 | .run_auto(false).Send(); |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 65 | } else { |
| 66 | LOG(DEBUG, "Running auto\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 67 | } |
| 68 | } else { // teleop |
| 69 | bool is_control_loop_driving = false; |
| 70 | double left_goal = 0.0; |
| 71 | double right_goal = 0.0; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 72 | const double wheel = data.GetAxis(kSteeringWheel); |
Brian Silverman | c6064c1 | 2013-08-31 10:58:54 -0700 | [diff] [blame] | 73 | const double throttle = -data.GetAxis(kDriveThrottle); |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 74 | const bool quickturn = data.IsPressed(kQuickTurn); |
| 75 | LOG(DEBUG, "wheel %f throttle %f quickturn %d\n", wheel, throttle, quickturn); |
Daniel Petti | 1f44851 | 2013-10-19 19:35:55 +0000 | [diff] [blame] | 76 | //const double kThrottleGain = 1.0 / 2.5; |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 77 | if (!shooting_) { |
| 78 | if (!drivetrain.goal.MakeWithBuilder() |
| 79 | .steering(wheel) |
| 80 | .throttle(throttle) |
| 81 | .highgear(is_high_gear).quickturn(quickturn) |
| 82 | .control_loop_driving(is_control_loop_driving) |
| 83 | .left_goal(left_goal).right_goal(right_goal).Send()) { |
| 84 | LOG(WARNING, "sending stick values failed\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 88 | if (data.PosEdge(kShiftHigh)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | is_high_gear = false; |
| 90 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 91 | if (data.PosEdge(kShiftLow)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 92 | is_high_gear = true; |
| 93 | } |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 94 | |
| 95 | // 3, 4, 9, 9 fires, 3 pickups |
| 96 | |
| 97 | shooter.status.FetchLatest(); |
| 98 | bool push = false; |
| 99 | double velocity = 0.0; |
| 100 | double intake = 0.0; |
| 101 | if (data.IsPressed(kPush) && shooter.status->ready) { |
| 102 | push = true; |
| 103 | } |
| 104 | if (data.IsPressed(kFire)) { |
James Kuszmaul | 229ca35 | 2013-11-04 17:42:37 -0800 | [diff] [blame] | 105 | velocity = 500; |
| 106 | } |
| 107 | else if (data.IsPressed(ButtonLocation(3, 1))) { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 108 | velocity = 100; |
James Kuszmaul | 229ca35 | 2013-11-04 17:42:37 -0800 | [diff] [blame] | 109 | } |
| 110 | else if (data.IsPressed(ButtonLocation(3, 2))) { |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 111 | velocity = 250; |
| 112 | } |
James Kuszmaul | 229ca35 | 2013-11-04 17:42:37 -0800 | [diff] [blame] | 113 | else if (data.IsPressed(ButtonLocation(3, 5))) { |
| 114 | velocity = 300; |
| 115 | } |
| 116 | else if (data.IsPressed(ButtonLocation(3, 7))) { |
| 117 | velocity = 350; |
| 118 | } |
| 119 | else if (data.IsPressed(ButtonLocation(3, 8))) { |
| 120 | velocity = 400; |
| 121 | } |
| 122 | else if (data.IsPressed(ButtonLocation(3, 10))) { |
| 123 | velocity = 450; |
| 124 | } |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 125 | if (data.IsPressed(kIntake)) { |
James Kuszmaul | 229ca35 | 2013-11-04 17:42:37 -0800 | [diff] [blame] | 126 | intake = 0.8; |
James Kuszmaul | b74c811 | 2013-11-03 16:13:45 -0800 | [diff] [blame] | 127 | } |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 128 | if (abs(throttle) < 0.2 && !quickturn) { |
| 129 | shooting_ = true; |
| 130 | shooter.goal.MakeWithBuilder().intake(intake).velocity(velocity).push(push).Send(); |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 131 | } else { |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 132 | shooting_ = false; |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 133 | } |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 134 | if (!velocity) { |
| 135 | shooting_ = false; |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 136 | } |
Daniel Petti | 96c1b57 | 2013-11-12 05:47:16 +0000 | [diff] [blame] | 137 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 141 | } // namespace joysticks |
| 142 | } // namespace input |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame] | 143 | } // namespace bot3 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 144 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 145 | int main() { |
| 146 | ::aos::Init(); |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame] | 147 | ::bot3::input::joysticks::Reader reader; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 148 | reader.Run(); |
| 149 | ::aos::Cleanup(); |
| 150 | } |