blob: da8a8e271a86d9c60918ce8d79f1775db1f0060d [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
James Kuszmaulf254c1a2013-03-10 16:31:26 -070010#include "frc971/control_loops/drivetrain/drivetrain.q.h"
brians343bc112013-02-10 01:53:46 +000011#include "frc971/queues/GyroAngle.q.h"
12#include "frc971/queues/Piston.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000013#include "frc971/autonomous/auto.q.h"
Brian Silverman687f5242013-03-16 13:57:59 -070014#include "frc971/control_loops/index/index_motor.q.h"
15#include "frc971/control_loops/shooter/shooter_motor.q.h"
Brian Silverman32d69142013-03-30 00:02:06 -070016#include "frc971/queues/CameraTarget.q.h"
brians343bc112013-02-10 01:53:46 +000017
18using ::frc971::control_loops::drivetrain;
19using ::frc971::control_loops::shifters;
20using ::frc971::sensors::gyro;
Brian Silverman687f5242013-03-16 13:57:59 -070021using ::frc971::control_loops::index_loop;
22using ::frc971::control_loops::shooter;
Brian Silverman513ad4e2013-03-20 19:59:50 -070023using ::frc971::control_loops::hangers;
Brian Silverman32d69142013-03-30 00:02:06 -070024using ::frc971::vision::target_angle;
brians343bc112013-02-10 01:53:46 +000025
Brian Silvermanba3de7e2013-05-08 16:18:15 -070026using ::aos::input::driver_station::ButtonLocation;
27using ::aos::input::driver_station::JoystickAxis;
28using ::aos::input::driver_station::ControlBit;
brians343bc112013-02-10 01:53:46 +000029
Daniel Petti3fe36542013-09-25 04:18:24 +000030namespace bot3 {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070031namespace input {
32namespace joysticks {
33
34const ButtonLocation kDriveControlLoopEnable1(1, 7),
35 kDriveControlLoopEnable2(1, 11);
36const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
37const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
38const ButtonLocation kQuickTurn(1, 5);
39
40const ButtonLocation kLongShot(3, 5);
41const ButtonLocation kMediumShot(3, 3);
42const ButtonLocation kShortShot(3, 6);
43const ButtonLocation kPitShot1(2, 7), kPitShot2(2, 10);
44
Brian Silvermanba3de7e2013-05-08 16:18:15 -070045const ButtonLocation kFire(3, 11);
46const ButtonLocation kIntake(3, 10);
47const ButtonLocation kForceFire(3, 12);
48const ButtonLocation kForceIndexUp(3, 9), kForceIndexDown(3, 7);
49
50const ButtonLocation kDeployHangers(3, 1);
51
52class Reader : public ::aos::input::JoystickInput {
brians343bc112013-02-10 01:53:46 +000053 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070054 static const bool kWristAlwaysDown = false;
55
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056 Reader() {
Daniel Petti3fe36542013-09-25 04:18:24 +000057 printf("\nRunning Bot3 JoystickReader!\n");
brians343bc112013-02-10 01:53:46 +000058 shifters.MakeWithBuilder().set(true).Send();
59 }
60
Brian Silvermanba3de7e2013-05-08 16:18:15 -070061 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000062 static bool is_high_gear = false;
63
Brian Silvermanba3de7e2013-05-08 16:18:15 -070064 if (data.GetControlBit(ControlBit::kAutonomous)) {
65 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000066 LOG(INFO, "Starting auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070067 ::frc971::autonomous::autonomous.MakeWithBuilder()
68 .run_auto(true).Send();
69 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000070 LOG(INFO, "Stopping auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070071 ::frc971::autonomous::autonomous.MakeWithBuilder()
72 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000073 }
74 } else { // teleop
75 bool is_control_loop_driving = false;
76 double left_goal = 0.0;
77 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070078 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070079 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070080 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000081 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070082 if (data.IsPressed(kDriveControlLoopEnable1) ||
83 data.IsPressed(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000084 static double distance = 0.0;
85 static double angle = 0.0;
86 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070087 if (data.PosEdge(kDriveControlLoopEnable1) ||
88 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000089 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
90 distance = (drivetrain.position->left_encoder +
91 drivetrain.position->right_encoder) / 2.0
92 - throttle * kThrottleGain / 2.0;
93 angle = gyro->angle;
94 filtered_goal_distance = distance;
95 }
96 }
97 is_control_loop_driving = true;
98
99 //const double gyro_angle = Gyro.View().angle;
100 const double goal_theta = angle - wheel * 0.27;
101 const double goal_distance = distance + throttle * kThrottleGain;
Daniel Petti3fe36542013-09-25 04:18:24 +0000102 //TODO(danielp) Change this after a look in the CAD.
brians343bc112013-02-10 01:53:46 +0000103 const double robot_width = 22.0 / 100.0 * 2.54;
104 const double kMaxVelocity = 0.6;
105 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
106 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700107 } else if (goal_distance < -kMaxVelocity * 0.02 +
108 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000109 filtered_goal_distance -= kMaxVelocity * 0.02;
110 } else {
111 filtered_goal_distance = goal_distance;
112 }
113 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
114 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
115 is_high_gear = false;
116
117 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
118 }
119 if (!(drivetrain.goal.MakeWithBuilder()
120 .steering(wheel)
121 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700122 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000123 .control_loop_driving(is_control_loop_driving)
124 .left_goal(left_goal).right_goal(right_goal).Send())) {
125 LOG(WARNING, "sending stick values failed\n");
126 }
127
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700128 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000129 is_high_gear = false;
130 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700131 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000132 is_high_gear = true;
133 }
Brian Silverman687f5242013-03-16 13:57:59 -0700134
Daniel Petti3fe36542013-09-25 04:18:24 +0000135 ::aos::ScopedMessagePtr<frc971::control_loops::ShooterLoop::Goal> shooter_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700136 shooter.goal.MakeMessage();
137 shooter_goal->velocity = 0;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700138 if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
139 shooter_goal->velocity = 131;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700140 } else if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700141#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700142 target_angle.FetchLatest();
143 if (target_angle.IsNewerThanMS(500)) {
144 shooter_goal->velocity = target_angle->shooter_speed;
145 angle_adjust_goal = target_angle->shooter_angle;
146 // TODO(brians): do the math right here
147 wrist_up_position = 0.70;
148 } else {
149 LOG(WARNING, "camera frame too old\n");
150 // pretend like no button is pressed
151 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700152#endif
Brian Silvermane296ebd2013-04-05 13:51:13 -0700153 shooter_goal->velocity = 360;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700154 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700155#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700156 shooter_goal->velocity = 375;
157 wrist_up_position = 0.70;
158 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700159#endif
160 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700161 shooter_goal->velocity = 395;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700162 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700163 shooter_goal->velocity = 375;
Brian Silverman8a82f382013-03-16 14:12:01 -0700164 }
Brian Silverman687f5242013-03-16 13:57:59 -0700165
Daniel Petti3fe36542013-09-25 04:18:24 +0000166 //TODO (daniel) Modify this for hopper.
167 ::aos::ScopedMessagePtr<frc971::control_loops::IndexLoop::Goal> index_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700168 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700169 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700170 // FIRE
171 index_goal->goal_state = 4;
172 } else if (shooter_goal->velocity != 0) {
173 // get ready to shoot
174 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700175 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700176 // intake
177 index_goal->goal_state = 2;
178 } else {
179 // get ready to intake
180 index_goal->goal_state = 1;
181 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700182 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700183
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700184 const bool index_up = data.IsPressed(kForceIndexUp);
185 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman180e2b82013-04-08 14:29:56 -0700186 index_goal->override_index = index_up || index_down;
187 if (index_up && index_down) {
188 index_goal->index_voltage = 0.0;
189 } else if (index_up) {
190 index_goal->index_voltage = 12.0;
191 } else if (index_down) {
192 index_goal->index_voltage = -12.0;
193 }
194
Brian Silverman8a82f382013-03-16 14:12:01 -0700195 index_goal.Send();
196 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000197 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700198
199 static int hanger_cycles = 0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700200 if (data.IsPressed(kDeployHangers)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700201 ++hanger_cycles;
202 } else {
203 hanger_cycles = 0;
204 }
205 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000206 }
207};
208
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700209} // namespace joysticks
210} // namespace input
Daniel Petti3fe36542013-09-25 04:18:24 +0000211} // namespace bot3
brians343bc112013-02-10 01:53:46 +0000212
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700213int main() {
214 ::aos::Init();
Daniel Petti3fe36542013-09-25 04:18:24 +0000215 ::bot3::input::joysticks::Reader reader;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700216 reader.Run();
217 ::aos::Cleanup();
218}