blob: 199d1e60a5bfbf07e01b9574162e1da244405a2b [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 {
brians343bc112013-02-10 01:53:46 +000043 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070044 static const bool kWristAlwaysDown = false;
45
Brian Silvermanba3de7e2013-05-08 16:18:15 -070046 Reader() {
Daniel Petti3fe36542013-09-25 04:18:24 +000047 printf("\nRunning Bot3 JoystickReader!\n");
brians343bc112013-02-10 01:53:46 +000048 shifters.MakeWithBuilder().set(true).Send();
49 }
50
Brian Silvermanba3de7e2013-05-08 16:18:15 -070051 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000052 static bool is_high_gear = false;
53
Brian Silvermanba3de7e2013-05-08 16:18:15 -070054 if (data.GetControlBit(ControlBit::kAutonomous)) {
55 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000056 LOG(INFO, "Starting auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000057 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070058 .run_auto(true).Send();
59 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000060 LOG(INFO, "Stopping auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000061 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070062 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000063 }
64 } else { // teleop
65 bool is_control_loop_driving = false;
66 double left_goal = 0.0;
67 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070068 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070069 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070070 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
Daniel Petti1f448512013-10-19 19:35:55 +000071 //const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070072 if (data.IsPressed(kDriveControlLoopEnable1) ||
73 data.IsPressed(kDriveControlLoopEnable2)) {
Daniel Petti1f448512013-10-19 19:35:55 +000074 LOG(INFO, "Control loop driving is currently not supported by this robot.\n");
75#if 0
brians343bc112013-02-10 01:53:46 +000076 static double distance = 0.0;
77 static double angle = 0.0;
78 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070079 if (data.PosEdge(kDriveControlLoopEnable1) ||
80 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000081 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
82 distance = (drivetrain.position->left_encoder +
83 drivetrain.position->right_encoder) / 2.0
84 - throttle * kThrottleGain / 2.0;
85 angle = gyro->angle;
86 filtered_goal_distance = distance;
87 }
88 }
89 is_control_loop_driving = true;
90
91 //const double gyro_angle = Gyro.View().angle;
92 const double goal_theta = angle - wheel * 0.27;
93 const double goal_distance = distance + throttle * kThrottleGain;
94 const double robot_width = 22.0 / 100.0 * 2.54;
95 const double kMaxVelocity = 0.6;
96 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
97 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070098 } else if (goal_distance < -kMaxVelocity * 0.02 +
99 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000100 filtered_goal_distance -= kMaxVelocity * 0.02;
101 } else {
102 filtered_goal_distance = goal_distance;
103 }
104 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
105 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
106 is_high_gear = false;
107
108 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
Daniel Petti1f448512013-10-19 19:35:55 +0000109#endif
brians343bc112013-02-10 01:53:46 +0000110 }
111 if (!(drivetrain.goal.MakeWithBuilder()
112 .steering(wheel)
113 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700114 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000115 .control_loop_driving(is_control_loop_driving)
116 .left_goal(left_goal).right_goal(right_goal).Send())) {
117 LOG(WARNING, "sending stick values failed\n");
118 }
119
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700120 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000121 is_high_gear = false;
122 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700123 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000124 is_high_gear = true;
125 }
James Kuszmaulb74c8112013-11-03 16:13:45 -0800126
127 // 3, 4, 9, 9 fires, 3 pickups
128
129 shooter.status.FetchLatest();
130 bool push = false;
131 double velocity = 0.0;
132 double intake = 0.0;
133 if (data.IsPressed(kPush) && shooter.status->ready) {
134 push = true;
135 }
136 if (data.IsPressed(kFire)) {
James Kuszmaul229ca352013-11-04 17:42:37 -0800137 velocity = 500;
138 }
139 else if (data.IsPressed(ButtonLocation(3, 1))) {
140 velocity = 50;
141 }
142 else if (data.IsPressed(ButtonLocation(3, 2))) {
James Kuszmaulb74c8112013-11-03 16:13:45 -0800143 velocity = 250;
144 }
James Kuszmaul229ca352013-11-04 17:42:37 -0800145 else if (data.IsPressed(ButtonLocation(3, 5))) {
146 velocity = 300;
147 }
148 else if (data.IsPressed(ButtonLocation(3, 7))) {
149 velocity = 350;
150 }
151 else if (data.IsPressed(ButtonLocation(3, 8))) {
152 velocity = 400;
153 }
154 else if (data.IsPressed(ButtonLocation(3, 10))) {
155 velocity = 450;
156 }
James Kuszmaulb74c8112013-11-03 16:13:45 -0800157 if (data.IsPressed(kIntake)) {
James Kuszmaul229ca352013-11-04 17:42:37 -0800158 intake = 0.8;
James Kuszmaulb74c8112013-11-03 16:13:45 -0800159 }
160 shooter.goal.MakeWithBuilder().intake(intake).velocity(velocity).push(push).Send();
Daniel Petti1f448512013-10-19 19:35:55 +0000161#if 0
Daniel Petti3fe36542013-09-25 04:18:24 +0000162 ::aos::ScopedMessagePtr<frc971::control_loops::ShooterLoop::Goal> shooter_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700163 shooter.goal.MakeMessage();
164 shooter_goal->velocity = 0;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700165 if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
166 shooter_goal->velocity = 131;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700167 } else if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700168#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700169 target_angle.FetchLatest();
170 if (target_angle.IsNewerThanMS(500)) {
171 shooter_goal->velocity = target_angle->shooter_speed;
172 angle_adjust_goal = target_angle->shooter_angle;
173 // TODO(brians): do the math right here
174 wrist_up_position = 0.70;
175 } else {
176 LOG(WARNING, "camera frame too old\n");
177 // pretend like no button is pressed
178 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700179#endif
Brian Silvermane296ebd2013-04-05 13:51:13 -0700180 shooter_goal->velocity = 360;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700181 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700182#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700183 shooter_goal->velocity = 375;
184 wrist_up_position = 0.70;
185 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700186#endif
187 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700188 shooter_goal->velocity = 395;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700189 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700190 shooter_goal->velocity = 375;
Brian Silverman8a82f382013-03-16 14:12:01 -0700191 }
Brian Silverman687f5242013-03-16 13:57:59 -0700192
Daniel Petti1f448512013-10-19 19:35:55 +0000193 //TODO (daniel) Modify this for hopper and shooter.
Daniel Petti3fe36542013-09-25 04:18:24 +0000194 ::aos::ScopedMessagePtr<frc971::control_loops::IndexLoop::Goal> index_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700195 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700196 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700197 // FIRE
198 index_goal->goal_state = 4;
199 } else if (shooter_goal->velocity != 0) {
200 // get ready to shoot
201 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700202 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700203 // intake
204 index_goal->goal_state = 2;
205 } else {
206 // get ready to intake
207 index_goal->goal_state = 1;
208 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700209 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700210
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700211 const bool index_up = data.IsPressed(kForceIndexUp);
212 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman180e2b82013-04-08 14:29:56 -0700213 index_goal->override_index = index_up || index_down;
214 if (index_up && index_down) {
215 index_goal->index_voltage = 0.0;
216 } else if (index_up) {
217 index_goal->index_voltage = 12.0;
218 } else if (index_down) {
219 index_goal->index_voltage = -12.0;
220 }
221
Brian Silverman8a82f382013-03-16 14:12:01 -0700222 index_goal.Send();
223 shooter_goal.Send();
Daniel Petti1f448512013-10-19 19:35:55 +0000224#endif
brians343bc112013-02-10 01:53:46 +0000225 }
brians343bc112013-02-10 01:53:46 +0000226 }
227};
228
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700229} // namespace joysticks
230} // namespace input
Daniel Petti3fe36542013-09-25 04:18:24 +0000231} // namespace bot3
brians343bc112013-02-10 01:53:46 +0000232
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700233int main() {
234 ::aos::Init();
Daniel Petti3fe36542013-09-25 04:18:24 +0000235 ::bot3::input::joysticks::Reader reader;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700236 reader.Run();
237 ::aos::Cleanup();
238}