blob: dbfd92ae7f94d0351e7b99325901f9cb3b6d03c6 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
Brian Silvermanba3de7e2013-05-08 16:18:15 -07006#include "aos/atom_code/init.h"
7#include "aos/atom_code/input/joystick_input.h"
8#include "aos/common/logging/logging.h"
brians343bc112013-02-10 01:53:46 +00009
Daniel Petti1f448512013-10-19 19:35:55 +000010#include "bot3/control_loops/drivetrain/drivetrain.q.h"
James Kuszmaulb74c8112013-11-03 16:13:45 -080011#include "bot3/control_loops/shooter/shooter_motor.q.h"
Daniel Petti1f448512013-10-19 19:35:55 +000012#include "bot3/autonomous/auto.q.h"
brians343bc112013-02-10 01:53:46 +000013#include "frc971/queues/GyroAngle.q.h"
14#include "frc971/queues/Piston.q.h"
Brian Silverman32d69142013-03-30 00:02:06 -070015#include "frc971/queues/CameraTarget.q.h"
brians343bc112013-02-10 01:53:46 +000016
Daniel Petti1f448512013-10-19 19:35:55 +000017using ::bot3::control_loops::drivetrain;
James Kuszmaulb74c8112013-11-03 16:13:45 -080018using ::bot3::control_loops::shooter;
brians343bc112013-02-10 01:53:46 +000019using ::frc971::control_loops::shifters;
20using ::frc971::sensors::gyro;
Daniel Petti1f448512013-10-19 19:35:55 +000021// using ::frc971::vision::target_angle;
brians343bc112013-02-10 01:53:46 +000022
Brian Silvermanba3de7e2013-05-08 16:18:15 -070023using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::ControlBit;
brians343bc112013-02-10 01:53:46 +000026
Daniel Petti3fe36542013-09-25 04:18:24 +000027namespace bot3 {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070028namespace input {
29namespace joysticks {
30
31const ButtonLocation kDriveControlLoopEnable1(1, 7),
32 kDriveControlLoopEnable2(1, 11);
33const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
34const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
35const ButtonLocation kQuickTurn(1, 5);
36
James Kuszmaulb74c8112013-11-03 16:13:45 -080037const ButtonLocation kPush(3, 9);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070038
James Kuszmaulb74c8112013-11-03 16:13:45 -080039const ButtonLocation kFire(3, 3);
40const ButtonLocation kIntake(3, 4);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070041
Brian Silvermanba3de7e2013-05-08 16:18:15 -070042class Reader : public ::aos::input::JoystickInput {
Daniel Petti96c1b572013-11-12 05:47:16 +000043 bool last_push_;
44 bool shooting_;
brians343bc112013-02-10 01:53:46 +000045 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070046 static const bool kWristAlwaysDown = false;
47
Daniel Petti96c1b572013-11-12 05:47:16 +000048 Reader() : shooting_(false) {
Daniel Petti3fe36542013-09-25 04:18:24 +000049 printf("\nRunning Bot3 JoystickReader!\n");
brians343bc112013-02-10 01:53:46 +000050 shifters.MakeWithBuilder().set(true).Send();
51 }
52
Brian Silvermanba3de7e2013-05-08 16:18:15 -070053 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000054 static bool is_high_gear = false;
55
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056 if (data.GetControlBit(ControlBit::kAutonomous)) {
57 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000058 LOG(INFO, "Starting auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000059 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070060 .run_auto(true).Send();
61 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000062 LOG(INFO, "Stopping auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000063 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070064 .run_auto(false).Send();
Daniel Petti96c1b572013-11-12 05:47:16 +000065 } else {
66 LOG(DEBUG, "Running auto\n");
brians343bc112013-02-10 01:53:46 +000067 }
68 } else { // teleop
69 bool is_control_loop_driving = false;
70 double left_goal = 0.0;
71 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070072 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070073 const double throttle = -data.GetAxis(kDriveThrottle);
Daniel Petti96c1b572013-11-12 05:47:16 +000074 const bool quickturn = data.IsPressed(kQuickTurn);
75 LOG(DEBUG, "wheel %f throttle %f quickturn %d\n", wheel, throttle, quickturn);
Daniel Petti1f448512013-10-19 19:35:55 +000076 //const double kThrottleGain = 1.0 / 2.5;
Daniel Petti96c1b572013-11-12 05:47:16 +000077 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");
brians343bc112013-02-10 01:53:46 +000085 }
brians343bc112013-02-10 01:53:46 +000086 }
87
Brian Silvermanba3de7e2013-05-08 16:18:15 -070088 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +000089 is_high_gear = false;
90 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070091 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +000092 is_high_gear = true;
93 }
James Kuszmaulb74c8112013-11-03 16:13:45 -080094
95 // 3, 4, 9, 9 fires, 3 pickups
96
97 shooter.status.FetchLatest();
98 bool push = false;
Daniel Petti96c1b572013-11-12 05:47:16 +000099 bool full_power = false;
James Kuszmaulb74c8112013-11-03 16:13:45 -0800100 double velocity = 0.0;
101 double intake = 0.0;
102 if (data.IsPressed(kPush) && shooter.status->ready) {
103 push = true;
104 }
Daniel Petti96c1b572013-11-12 05:47:16 +0000105 if (!push && last_push_) {
106 //Falling edge
107 full_power = true;
108 }
109 last_push_ = push;
James Kuszmaulb74c8112013-11-03 16:13:45 -0800110 if (data.IsPressed(kFire)) {
James Kuszmaul229ca352013-11-04 17:42:37 -0800111 velocity = 500;
112 }
113 else if (data.IsPressed(ButtonLocation(3, 1))) {
Daniel Petti96c1b572013-11-12 05:47:16 +0000114 velocity = 100;
James Kuszmaul229ca352013-11-04 17:42:37 -0800115 }
116 else if (data.IsPressed(ButtonLocation(3, 2))) {
James Kuszmaulb74c8112013-11-03 16:13:45 -0800117 velocity = 250;
118 }
James Kuszmaul229ca352013-11-04 17:42:37 -0800119 else if (data.IsPressed(ButtonLocation(3, 5))) {
120 velocity = 300;
121 }
122 else if (data.IsPressed(ButtonLocation(3, 7))) {
123 velocity = 350;
124 }
125 else if (data.IsPressed(ButtonLocation(3, 8))) {
126 velocity = 400;
127 }
128 else if (data.IsPressed(ButtonLocation(3, 10))) {
129 velocity = 450;
130 }
James Kuszmaulb74c8112013-11-03 16:13:45 -0800131 if (data.IsPressed(kIntake)) {
James Kuszmaul229ca352013-11-04 17:42:37 -0800132 intake = 0.8;
James Kuszmaulb74c8112013-11-03 16:13:45 -0800133 }
Daniel Petti96c1b572013-11-12 05:47:16 +0000134 if (abs(throttle) < 0.2 && !quickturn) {
135 shooting_ = true;
136 shooter.goal.MakeWithBuilder().intake(intake).velocity(velocity).push(push).Send();
137 if (full_power) {
138 LOG(DEBUG, "Zeroing position.velocity\n");
139 shooter.position.MakeWithBuilder().velocity(0).Send();
Brian Silverman32d69142013-03-30 00:02:06 -0700140 }
Brian Silverman8a82f382013-03-16 14:12:01 -0700141 } else {
Daniel Petti96c1b572013-11-12 05:47:16 +0000142 shooting_ = false;
Brian Silverman8a82f382013-03-16 14:12:01 -0700143 }
Daniel Petti96c1b572013-11-12 05:47:16 +0000144 if (!velocity) {
145 shooting_ = false;
Brian Silverman180e2b82013-04-08 14:29:56 -0700146 }
Daniel Petti96c1b572013-11-12 05:47:16 +0000147 }
brians343bc112013-02-10 01:53:46 +0000148 }
149};
150
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700151} // namespace joysticks
152} // namespace input
Daniel Petti3fe36542013-09-25 04:18:24 +0000153} // namespace bot3
brians343bc112013-02-10 01:53:46 +0000154
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700155int main() {
156 ::aos::Init();
Daniel Petti3fe36542013-09-25 04:18:24 +0000157 ::bot3::input::joysticks::Reader reader;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700158 reader.Run();
159 ::aos::Cleanup();
160}