blob: 05c5e69f8f41966e341a2a76eb5cbcc206d9809a [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"
Brian Silverman687f5242013-03-16 13:57:59 -070013#include "frc971/control_loops/wrist/wrist_motor.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000014#include "frc971/autonomous/auto.q.h"
Brian Silverman687f5242013-03-16 13:57:59 -070015#include "frc971/control_loops/index/index_motor.q.h"
16#include "frc971/control_loops/shooter/shooter_motor.q.h"
17#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
Brian Silverman32d69142013-03-30 00:02:06 -070018#include "frc971/queues/CameraTarget.q.h"
brians343bc112013-02-10 01:53:46 +000019
20using ::frc971::control_loops::drivetrain;
21using ::frc971::control_loops::shifters;
22using ::frc971::sensors::gyro;
Brian Silverman687f5242013-03-16 13:57:59 -070023using ::frc971::control_loops::wrist;
24using ::frc971::control_loops::index_loop;
25using ::frc971::control_loops::shooter;
26using ::frc971::control_loops::angle_adjust;
Brian Silverman513ad4e2013-03-20 19:59:50 -070027using ::frc971::control_loops::hangers;
Brian Silverman32d69142013-03-30 00:02:06 -070028using ::frc971::vision::target_angle;
brians343bc112013-02-10 01:53:46 +000029
Brian Silvermanba3de7e2013-05-08 16:18:15 -070030using ::aos::input::driver_station::ButtonLocation;
31using ::aos::input::driver_station::JoystickAxis;
32using ::aos::input::driver_station::ControlBit;
brians343bc112013-02-10 01:53:46 +000033
Brian Silvermanba3de7e2013-05-08 16:18:15 -070034namespace frc971 {
35namespace input {
36namespace joysticks {
37
38const ButtonLocation kDriveControlLoopEnable1(1, 7),
39 kDriveControlLoopEnable2(1, 11);
40const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
41const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
42const ButtonLocation kQuickTurn(1, 5);
43
44const ButtonLocation kLongShot(3, 5);
45const ButtonLocation kMediumShot(3, 3);
46const ButtonLocation kShortShot(3, 6);
47const ButtonLocation kPitShot1(2, 7), kPitShot2(2, 10);
48
49const ButtonLocation kWristDown(3, 8);
50
51const ButtonLocation kFire(3, 11);
52const ButtonLocation kIntake(3, 10);
53const ButtonLocation kForceFire(3, 12);
54const ButtonLocation kForceIndexUp(3, 9), kForceIndexDown(3, 7);
55
56const ButtonLocation kDeployHangers(3, 1);
57
58class Reader : public ::aos::input::JoystickInput {
brians343bc112013-02-10 01:53:46 +000059 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070060 static const bool kWristAlwaysDown = false;
61
Brian Silvermanba3de7e2013-05-08 16:18:15 -070062 Reader() {
brians343bc112013-02-10 01:53:46 +000063 shifters.MakeWithBuilder().set(true).Send();
64 }
65
Brian Silvermanba3de7e2013-05-08 16:18:15 -070066 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000067 static bool is_high_gear = false;
68
Brian Silvermanba3de7e2013-05-08 16:18:15 -070069 if (data.GetControlBit(ControlBit::kAutonomous)) {
70 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000071 LOG(INFO, "Starting auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070072 ::frc971::autonomous::autonomous.MakeWithBuilder()
73 .run_auto(true).Send();
74 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000075 LOG(INFO, "Stopping auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070076 ::frc971::autonomous::autonomous.MakeWithBuilder()
77 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000078 }
79 } else { // teleop
80 bool is_control_loop_driving = false;
81 double left_goal = 0.0;
82 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070083 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070084 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070085 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000086 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070087 if (data.IsPressed(kDriveControlLoopEnable1) ||
88 data.IsPressed(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000089 static double distance = 0.0;
90 static double angle = 0.0;
91 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070092 if (data.PosEdge(kDriveControlLoopEnable1) ||
93 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000094 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
95 distance = (drivetrain.position->left_encoder +
96 drivetrain.position->right_encoder) / 2.0
97 - throttle * kThrottleGain / 2.0;
98 angle = gyro->angle;
99 filtered_goal_distance = distance;
100 }
101 }
102 is_control_loop_driving = true;
103
104 //const double gyro_angle = Gyro.View().angle;
105 const double goal_theta = angle - wheel * 0.27;
106 const double goal_distance = distance + throttle * kThrottleGain;
107 const double robot_width = 22.0 / 100.0 * 2.54;
108 const double kMaxVelocity = 0.6;
109 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
110 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700111 } else if (goal_distance < -kMaxVelocity * 0.02 +
112 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000113 filtered_goal_distance -= kMaxVelocity * 0.02;
114 } else {
115 filtered_goal_distance = goal_distance;
116 }
117 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
118 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
119 is_high_gear = false;
120
121 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
122 }
123 if (!(drivetrain.goal.MakeWithBuilder()
124 .steering(wheel)
125 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700126 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000127 .control_loop_driving(is_control_loop_driving)
128 .left_goal(left_goal).right_goal(right_goal).Send())) {
129 LOG(WARNING, "sending stick values failed\n");
130 }
131
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700132 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000133 is_high_gear = false;
134 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700135 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000136 is_high_gear = true;
137 }
Brian Silverman687f5242013-03-16 13:57:59 -0700138
Brian Silverman7d39d862013-09-29 17:00:30 -0700139 // Whether we should change wrist positions to indicate that the hopper is
140 // clear.
141 bool hopper_clear = false;
142
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700143 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000144 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silverman364cd262013-03-30 22:44:57 -0700145 static const double kWristPickup = -0.580;
Brian Silverman906aef52013-03-17 23:37:41 -0700146 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700147 // Where the wrist gets stored when up.
148 // All the way up is 1.5.
149 static const double kWristUp = 1.43;
Brian Silverman7d39d862013-09-29 17:00:30 -0700150 static const double kWristCleared = kWristUp - 0.2;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700151 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700152 double wrist_up_position = kWristUp;
Brian Silverman7d39d862013-09-29 17:00:30 -0700153 double wrist_pickup_position = data.IsPressed(kIntake) ?
154 kWristPickup : kWristNearGround;
155 if (index_loop.status.FetchLatest() || index_loop.status.get()) {
156 if (index_loop.status->hopper_disc_count >= 4) {
157 wrist_down_position = kWristNearGround;
158 } else {
159 wrist_down_position = wrist_pickup_position;
160 }
161 hopper_clear = index_loop.status->hopper_clear;
162 }
Brian Silverman687f5242013-03-16 13:57:59 -0700163
Brian Silverman8a82f382013-03-16 14:12:01 -0700164 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
165 shooter.goal.MakeMessage();
166 shooter_goal->velocity = 0;
Brian Silverman834a0da2013-03-16 23:49:27 -0700167 static double angle_adjust_goal = 0.42;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700168 if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
169 shooter_goal->velocity = 131;
Brian Silverman7d39d862013-09-29 17:00:30 -0700170 if (hopper_clear) wrist_up_position = kWristCleared;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700171 angle_adjust_goal = 0.70;
172 } else if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700173#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700174 target_angle.FetchLatest();
175 if (target_angle.IsNewerThanMS(500)) {
176 shooter_goal->velocity = target_angle->shooter_speed;
177 angle_adjust_goal = target_angle->shooter_angle;
178 // TODO(brians): do the math right here
179 wrist_up_position = 0.70;
180 } else {
181 LOG(WARNING, "camera frame too old\n");
182 // pretend like no button is pressed
183 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700184#endif
Brian Silvermane296ebd2013-04-05 13:51:13 -0700185 shooter_goal->velocity = 360;
Brian Silverman7d39d862013-09-29 17:00:30 -0700186 if (!hopper_clear) wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700187 angle_adjust_goal = 0.596;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700188 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700189#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700190 shooter_goal->velocity = 375;
191 wrist_up_position = 0.70;
192 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700193#endif
194 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700195 shooter_goal->velocity = 395;
Brian Silverman7d39d862013-09-29 17:00:30 -0700196 if (!hopper_clear) wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700197 angle_adjust_goal = 0.520;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700198 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700199 shooter_goal->velocity = 375;
Brian Silverman7d39d862013-09-29 17:00:30 -0700200 if (hopper_clear) wrist_up_position = kWristCleared;
Brian Silverman9d0fa5f2013-09-22 19:44:13 -0700201 angle_adjust_goal = 0.671;
Brian Silverman8a82f382013-03-16 14:12:01 -0700202 }
203 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700204
Brian Silverman906aef52013-03-17 23:37:41 -0700205 wrist.goal.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700206 .goal(data.IsPressed(kWristDown) ?
207 wrist_down_position :
208 wrist_up_position)
209 .Send();
Brian Silverman906aef52013-03-17 23:37:41 -0700210
Brian Silverman8a82f382013-03-16 14:12:01 -0700211 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
212 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700213 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700214 // FIRE
215 index_goal->goal_state = 4;
216 } else if (shooter_goal->velocity != 0) {
217 // get ready to shoot
218 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700219 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700220 // intake
221 index_goal->goal_state = 2;
222 } else {
223 // get ready to intake
224 index_goal->goal_state = 1;
225 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700226 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700227
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700228 const bool index_up = data.IsPressed(kForceIndexUp);
229 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman180e2b82013-04-08 14:29:56 -0700230 index_goal->override_index = index_up || index_down;
231 if (index_up && index_down) {
232 index_goal->index_voltage = 0.0;
233 } else if (index_up) {
234 index_goal->index_voltage = 12.0;
235 } else if (index_down) {
236 index_goal->index_voltage = -12.0;
237 }
238
Brian Silverman8a82f382013-03-16 14:12:01 -0700239 index_goal.Send();
240 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000241 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700242
243 static int hanger_cycles = 0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700244 if (data.IsPressed(kDeployHangers)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700245 ++hanger_cycles;
246 } else {
247 hanger_cycles = 0;
248 }
249 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000250 }
251};
252
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700253} // namespace joysticks
254} // namespace input
brians343bc112013-02-10 01:53:46 +0000255} // namespace frc971
256
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700257int main() {
258 ::aos::Init();
259 ::frc971::input::joysticks::Reader reader;
260 reader.Run();
261 ::aos::Cleanup();
262}