blob: 5efe8b4d5bc3a324874ae912bc37cb6b2d5c25ed [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"
11#include "bot3/autonomous/auto.q.h"
brians343bc112013-02-10 01:53:46 +000012#include "frc971/queues/GyroAngle.q.h"
13#include "frc971/queues/Piston.q.h"
Brian Silverman32d69142013-03-30 00:02:06 -070014#include "frc971/queues/CameraTarget.q.h"
brians343bc112013-02-10 01:53:46 +000015
Daniel Petti1f448512013-10-19 19:35:55 +000016using ::bot3::control_loops::drivetrain;
brians343bc112013-02-10 01:53:46 +000017using ::frc971::control_loops::shifters;
18using ::frc971::sensors::gyro;
Daniel Petti1f448512013-10-19 19:35:55 +000019// using ::frc971::vision::target_angle;
brians343bc112013-02-10 01:53:46 +000020
Brian Silvermanba3de7e2013-05-08 16:18:15 -070021using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::JoystickAxis;
23using ::aos::input::driver_station::ControlBit;
brians343bc112013-02-10 01:53:46 +000024
Daniel Petti3fe36542013-09-25 04:18:24 +000025namespace bot3 {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070026namespace input {
27namespace joysticks {
28
29const ButtonLocation kDriveControlLoopEnable1(1, 7),
30 kDriveControlLoopEnable2(1, 11);
31const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
32const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
33const ButtonLocation kQuickTurn(1, 5);
34
35const ButtonLocation kLongShot(3, 5);
36const ButtonLocation kMediumShot(3, 3);
37const ButtonLocation kShortShot(3, 6);
38const ButtonLocation kPitShot1(2, 7), kPitShot2(2, 10);
39
Brian Silvermanba3de7e2013-05-08 16:18:15 -070040const ButtonLocation kFire(3, 11);
41const ButtonLocation kIntake(3, 10);
42const ButtonLocation kForceFire(3, 12);
43const ButtonLocation kForceIndexUp(3, 9), kForceIndexDown(3, 7);
44
Brian Silvermanba3de7e2013-05-08 16:18:15 -070045class Reader : public ::aos::input::JoystickInput {
brians343bc112013-02-10 01:53:46 +000046 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070047 static const bool kWristAlwaysDown = false;
48
Brian Silvermanba3de7e2013-05-08 16:18:15 -070049 Reader() {
Daniel Petti3fe36542013-09-25 04:18:24 +000050 printf("\nRunning Bot3 JoystickReader!\n");
brians343bc112013-02-10 01:53:46 +000051 shifters.MakeWithBuilder().set(true).Send();
52 }
53
Brian Silvermanba3de7e2013-05-08 16:18:15 -070054 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000055 static bool is_high_gear = false;
56
Brian Silvermanba3de7e2013-05-08 16:18:15 -070057 if (data.GetControlBit(ControlBit::kAutonomous)) {
58 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000059 LOG(INFO, "Starting auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000060 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070061 .run_auto(true).Send();
62 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000063 LOG(INFO, "Stopping auto mode\n");
Daniel Petti1f448512013-10-19 19:35:55 +000064 ::bot3::autonomous::autonomous.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -070065 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000066 }
67 } else { // teleop
68 bool is_control_loop_driving = false;
69 double left_goal = 0.0;
70 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070071 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070072 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070073 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
Daniel Petti1f448512013-10-19 19:35:55 +000074 //const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070075 if (data.IsPressed(kDriveControlLoopEnable1) ||
76 data.IsPressed(kDriveControlLoopEnable2)) {
Daniel Petti1f448512013-10-19 19:35:55 +000077 LOG(INFO, "Control loop driving is currently not supported by this robot.\n");
78#if 0
brians343bc112013-02-10 01:53:46 +000079 static double distance = 0.0;
80 static double angle = 0.0;
81 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070082 if (data.PosEdge(kDriveControlLoopEnable1) ||
83 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000084 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
85 distance = (drivetrain.position->left_encoder +
86 drivetrain.position->right_encoder) / 2.0
87 - throttle * kThrottleGain / 2.0;
88 angle = gyro->angle;
89 filtered_goal_distance = distance;
90 }
91 }
92 is_control_loop_driving = true;
93
94 //const double gyro_angle = Gyro.View().angle;
95 const double goal_theta = angle - wheel * 0.27;
96 const double goal_distance = distance + throttle * kThrottleGain;
97 const double robot_width = 22.0 / 100.0 * 2.54;
98 const double kMaxVelocity = 0.6;
99 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
100 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700101 } else if (goal_distance < -kMaxVelocity * 0.02 +
102 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000103 filtered_goal_distance -= kMaxVelocity * 0.02;
104 } else {
105 filtered_goal_distance = goal_distance;
106 }
107 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
108 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
109 is_high_gear = false;
110
111 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
Daniel Petti1f448512013-10-19 19:35:55 +0000112#endif
brians343bc112013-02-10 01:53:46 +0000113 }
114 if (!(drivetrain.goal.MakeWithBuilder()
115 .steering(wheel)
116 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700117 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000118 .control_loop_driving(is_control_loop_driving)
119 .left_goal(left_goal).right_goal(right_goal).Send())) {
120 LOG(WARNING, "sending stick values failed\n");
121 }
122
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700123 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000124 is_high_gear = false;
125 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700126 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000127 is_high_gear = true;
128 }
Daniel Petti1f448512013-10-19 19:35:55 +0000129#if 0
Daniel Petti3fe36542013-09-25 04:18:24 +0000130 ::aos::ScopedMessagePtr<frc971::control_loops::ShooterLoop::Goal> shooter_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700131 shooter.goal.MakeMessage();
132 shooter_goal->velocity = 0;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700133 if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
134 shooter_goal->velocity = 131;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700135 } else if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700136#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700137 target_angle.FetchLatest();
138 if (target_angle.IsNewerThanMS(500)) {
139 shooter_goal->velocity = target_angle->shooter_speed;
140 angle_adjust_goal = target_angle->shooter_angle;
141 // TODO(brians): do the math right here
142 wrist_up_position = 0.70;
143 } else {
144 LOG(WARNING, "camera frame too old\n");
145 // pretend like no button is pressed
146 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700147#endif
Brian Silvermane296ebd2013-04-05 13:51:13 -0700148 shooter_goal->velocity = 360;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700149 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700150#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700151 shooter_goal->velocity = 375;
152 wrist_up_position = 0.70;
153 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700154#endif
155 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700156 shooter_goal->velocity = 395;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700157 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700158 shooter_goal->velocity = 375;
Brian Silverman8a82f382013-03-16 14:12:01 -0700159 }
Brian Silverman687f5242013-03-16 13:57:59 -0700160
Daniel Petti1f448512013-10-19 19:35:55 +0000161 //TODO (daniel) Modify this for hopper and shooter.
Daniel Petti3fe36542013-09-25 04:18:24 +0000162 ::aos::ScopedMessagePtr<frc971::control_loops::IndexLoop::Goal> index_goal =
Brian Silverman8a82f382013-03-16 14:12:01 -0700163 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700164 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700165 // FIRE
166 index_goal->goal_state = 4;
167 } else if (shooter_goal->velocity != 0) {
168 // get ready to shoot
169 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700170 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700171 // intake
172 index_goal->goal_state = 2;
173 } else {
174 // get ready to intake
175 index_goal->goal_state = 1;
176 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700177 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700178
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700179 const bool index_up = data.IsPressed(kForceIndexUp);
180 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman180e2b82013-04-08 14:29:56 -0700181 index_goal->override_index = index_up || index_down;
182 if (index_up && index_down) {
183 index_goal->index_voltage = 0.0;
184 } else if (index_up) {
185 index_goal->index_voltage = 12.0;
186 } else if (index_down) {
187 index_goal->index_voltage = -12.0;
188 }
189
Brian Silverman8a82f382013-03-16 14:12:01 -0700190 index_goal.Send();
191 shooter_goal.Send();
Daniel Petti1f448512013-10-19 19:35:55 +0000192#endif
brians343bc112013-02-10 01:53:46 +0000193 }
brians343bc112013-02-10 01:53:46 +0000194 }
195};
196
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700197} // namespace joysticks
198} // namespace input
Daniel Petti3fe36542013-09-25 04:18:24 +0000199} // namespace bot3
brians343bc112013-02-10 01:53:46 +0000200
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700201int main() {
202 ::aos::Init();
Daniel Petti3fe36542013-09-25 04:18:24 +0000203 ::bot3::input::joysticks::Reader reader;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700204 reader.Run();
205 ::aos::Cleanup();
206}