blob: d9db969f9c800acb36b15f46981cb31ca678c530 [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;
Brian Silverman46390292013-09-29 17:03:16 -070068 static double angle_adjust_goal = 0.42;
brians343bc112013-02-10 01:53:46 +000069
Brian Silvermanba3de7e2013-05-08 16:18:15 -070070 if (data.GetControlBit(ControlBit::kAutonomous)) {
71 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000072 LOG(INFO, "Starting auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070073 ::frc971::autonomous::autonomous.MakeWithBuilder()
74 .run_auto(true).Send();
75 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000076 LOG(INFO, "Stopping auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070077 ::frc971::autonomous::autonomous.MakeWithBuilder()
78 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000079 }
80 } else { // teleop
81 bool is_control_loop_driving = false;
82 double left_goal = 0.0;
83 double right_goal = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070084 const double wheel = data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070085 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070086 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000087 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070088 if (data.IsPressed(kDriveControlLoopEnable1) ||
89 data.IsPressed(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000090 static double distance = 0.0;
91 static double angle = 0.0;
92 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070093 if (data.PosEdge(kDriveControlLoopEnable1) ||
94 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000095 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
96 distance = (drivetrain.position->left_encoder +
97 drivetrain.position->right_encoder) / 2.0
98 - throttle * kThrottleGain / 2.0;
99 angle = gyro->angle;
100 filtered_goal_distance = distance;
101 }
102 }
103 is_control_loop_driving = true;
104
105 //const double gyro_angle = Gyro.View().angle;
106 const double goal_theta = angle - wheel * 0.27;
107 const double goal_distance = distance + throttle * kThrottleGain;
108 const double robot_width = 22.0 / 100.0 * 2.54;
109 const double kMaxVelocity = 0.6;
110 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
111 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700112 } else if (goal_distance < -kMaxVelocity * 0.02 +
113 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000114 filtered_goal_distance -= kMaxVelocity * 0.02;
115 } else {
116 filtered_goal_distance = goal_distance;
117 }
118 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
119 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
120 is_high_gear = false;
121
122 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
123 }
124 if (!(drivetrain.goal.MakeWithBuilder()
125 .steering(wheel)
126 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700127 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000128 .control_loop_driving(is_control_loop_driving)
129 .left_goal(left_goal).right_goal(right_goal).Send())) {
130 LOG(WARNING, "sending stick values failed\n");
131 }
132
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700133 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000134 is_high_gear = false;
135 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700136 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000137 is_high_gear = true;
138 }
Brian Silverman687f5242013-03-16 13:57:59 -0700139
Brian Silverman7d39d862013-09-29 17:00:30 -0700140 // Whether we should change wrist positions to indicate that the hopper is
141 // clear.
142 bool hopper_clear = false;
143
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700144 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000145 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silverman364cd262013-03-30 22:44:57 -0700146 static const double kWristPickup = -0.580;
Brian Silverman906aef52013-03-17 23:37:41 -0700147 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700148 // Where the wrist gets stored when up.
149 // All the way up is 1.5.
150 static const double kWristUp = 1.43;
Brian Silverman7d39d862013-09-29 17:00:30 -0700151 static const double kWristCleared = kWristUp - 0.2;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700152 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700153 double wrist_up_position = kWristUp;
Brian Silverman7d39d862013-09-29 17:00:30 -0700154 double wrist_pickup_position = data.IsPressed(kIntake) ?
155 kWristPickup : kWristNearGround;
156 if (index_loop.status.FetchLatest() || index_loop.status.get()) {
157 if (index_loop.status->hopper_disc_count >= 4) {
158 wrist_down_position = kWristNearGround;
159 } else {
160 wrist_down_position = wrist_pickup_position;
161 }
162 hopper_clear = index_loop.status->hopper_clear;
163 }
Brian Silverman687f5242013-03-16 13:57:59 -0700164
Brian Silverman8a82f382013-03-16 14:12:01 -0700165 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
166 shooter.goal.MakeMessage();
167 shooter_goal->velocity = 0;
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;
Brian Silverman46390292013-09-29 17:03:16 -0700246 angle_adjust_goal = 0.4;
Brian Silverman513ad4e2013-03-20 19:59:50 -0700247 } else {
248 hanger_cycles = 0;
249 }
250 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000251 }
252};
253
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700254} // namespace joysticks
255} // namespace input
brians343bc112013-02-10 01:53:46 +0000256} // namespace frc971
257
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700258int main() {
259 ::aos::Init();
260 ::frc971::input::joysticks::Reader reader;
261 reader.Run();
262 ::aos::Cleanup();
263}