blob: a2404d0903fb8f3dd30a2652be5df2a04355e66a [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);
Brian Silverman759d2632013-10-11 18:01:21 -070055const ButtonLocation kForceSpitOut(2, 11);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056
57const ButtonLocation kDeployHangers(3, 1);
58
59class Reader : public ::aos::input::JoystickInput {
brians343bc112013-02-10 01:53:46 +000060 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070061 static const bool kWristAlwaysDown = false;
62
Brian Silvermanba3de7e2013-05-08 16:18:15 -070063 Reader() {
brians343bc112013-02-10 01:53:46 +000064 shifters.MakeWithBuilder().set(true).Send();
65 }
66
Brian Silvermanba3de7e2013-05-08 16:18:15 -070067 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
brians343bc112013-02-10 01:53:46 +000068 static bool is_high_gear = false;
Brian Silverman46390292013-09-29 17:03:16 -070069 static double angle_adjust_goal = 0.42;
brians343bc112013-02-10 01:53:46 +000070
Brian Silvermanba3de7e2013-05-08 16:18:15 -070071 if (data.GetControlBit(ControlBit::kAutonomous)) {
72 if (data.PosEdge(ControlBit::kEnabled)){
brians343bc112013-02-10 01:53:46 +000073 LOG(INFO, "Starting auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070074 ::frc971::autonomous::autonomous.MakeWithBuilder()
75 .run_auto(true).Send();
76 } else if (data.NegEdge(ControlBit::kEnabled)) {
brians343bc112013-02-10 01:53:46 +000077 LOG(INFO, "Stopping auto mode\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070078 ::frc971::autonomous::autonomous.MakeWithBuilder()
79 .run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000080 }
81 } else { // teleop
82 bool is_control_loop_driving = false;
83 double left_goal = 0.0;
84 double right_goal = 0.0;
Brian Silverman718b1d72013-10-28 16:22:45 -070085 const double wheel = -data.GetAxis(kSteeringWheel);
Brian Silvermanc6064c12013-08-31 10:58:54 -070086 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman834a0da2013-03-16 23:49:27 -070087 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000088 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070089 if (data.IsPressed(kDriveControlLoopEnable1) ||
90 data.IsPressed(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000091 static double distance = 0.0;
92 static double angle = 0.0;
93 static double filtered_goal_distance = 0.0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070094 if (data.PosEdge(kDriveControlLoopEnable1) ||
95 data.PosEdge(kDriveControlLoopEnable2)) {
brians343bc112013-02-10 01:53:46 +000096 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
97 distance = (drivetrain.position->left_encoder +
98 drivetrain.position->right_encoder) / 2.0
99 - throttle * kThrottleGain / 2.0;
100 angle = gyro->angle;
101 filtered_goal_distance = distance;
102 }
103 }
104 is_control_loop_driving = true;
105
106 //const double gyro_angle = Gyro.View().angle;
107 const double goal_theta = angle - wheel * 0.27;
108 const double goal_distance = distance + throttle * kThrottleGain;
109 const double robot_width = 22.0 / 100.0 * 2.54;
110 const double kMaxVelocity = 0.6;
111 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
112 filtered_goal_distance += kMaxVelocity * 0.02;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700113 } else if (goal_distance < -kMaxVelocity * 0.02 +
114 filtered_goal_distance) {
brians343bc112013-02-10 01:53:46 +0000115 filtered_goal_distance -= kMaxVelocity * 0.02;
116 } else {
117 filtered_goal_distance = goal_distance;
118 }
119 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
120 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
121 is_high_gear = false;
122
123 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
124 }
125 if (!(drivetrain.goal.MakeWithBuilder()
126 .steering(wheel)
127 .throttle(throttle)
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700128 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
brians343bc112013-02-10 01:53:46 +0000129 .control_loop_driving(is_control_loop_driving)
130 .left_goal(left_goal).right_goal(right_goal).Send())) {
131 LOG(WARNING, "sending stick values failed\n");
132 }
133
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700134 if (data.PosEdge(kShiftHigh)) {
brians343bc112013-02-10 01:53:46 +0000135 is_high_gear = false;
136 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700137 if (data.PosEdge(kShiftLow)) {
brians343bc112013-02-10 01:53:46 +0000138 is_high_gear = true;
139 }
Brian Silverman687f5242013-03-16 13:57:59 -0700140
Brian Silverman7d39d862013-09-29 17:00:30 -0700141 // Whether we should change wrist positions to indicate that the hopper is
142 // clear.
143 bool hopper_clear = false;
144
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700145 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000146 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silverman364cd262013-03-30 22:44:57 -0700147 static const double kWristPickup = -0.580;
Brian Silverman906aef52013-03-17 23:37:41 -0700148 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700149 // Where the wrist gets stored when up.
150 // All the way up is 1.5.
151 static const double kWristUp = 1.43;
Brian Silverman7d39d862013-09-29 17:00:30 -0700152 static const double kWristCleared = kWristUp - 0.2;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700153 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700154 double wrist_up_position = kWristUp;
Brian Silverman7d39d862013-09-29 17:00:30 -0700155 double wrist_pickup_position = data.IsPressed(kIntake) ?
156 kWristPickup : kWristNearGround;
157 if (index_loop.status.FetchLatest() || index_loop.status.get()) {
158 if (index_loop.status->hopper_disc_count >= 4) {
159 wrist_down_position = kWristNearGround;
160 } else {
161 wrist_down_position = wrist_pickup_position;
162 }
163 hopper_clear = index_loop.status->hopper_clear;
164 }
Brian Silverman687f5242013-03-16 13:57:59 -0700165
Brian Silverman8a82f382013-03-16 14:12:01 -0700166 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
167 shooter.goal.MakeMessage();
168 shooter_goal->velocity = 0;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700169 if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
170 shooter_goal->velocity = 131;
Brian Silverman7d39d862013-09-29 17:00:30 -0700171 if (hopper_clear) wrist_up_position = kWristCleared;
Brian Silvermanf34bbe02013-09-01 09:08:32 -0700172 angle_adjust_goal = 0.70;
173 } else if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700174#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700175 target_angle.FetchLatest();
176 if (target_angle.IsNewerThanMS(500)) {
177 shooter_goal->velocity = target_angle->shooter_speed;
178 angle_adjust_goal = target_angle->shooter_angle;
179 // TODO(brians): do the math right here
180 wrist_up_position = 0.70;
181 } else {
182 LOG(WARNING, "camera frame too old\n");
183 // pretend like no button is pressed
184 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700185#endif
Brian Silverman7bd7fad2013-10-11 17:59:02 -0700186 // This shot is from 30'.
Brian Silvermane296ebd2013-04-05 13:51:13 -0700187 shooter_goal->velocity = 360;
Brian Silverman7d39d862013-09-29 17:00:30 -0700188 if (!hopper_clear) wrist_up_position = 1.23 - 0.4;
Brian Silverman7bd7fad2013-10-11 17:59:02 -0700189 angle_adjust_goal = 0.630;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700190 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700191#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700192 shooter_goal->velocity = 375;
193 wrist_up_position = 0.70;
194 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700195#endif
196 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700197 shooter_goal->velocity = 395;
Brian Silverman7d39d862013-09-29 17:00:30 -0700198 if (!hopper_clear) wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700199 angle_adjust_goal = 0.520;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700200 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700201 shooter_goal->velocity = 375;
Brian Silverman7d39d862013-09-29 17:00:30 -0700202 if (hopper_clear) wrist_up_position = kWristCleared;
Brian Silverman9d0fa5f2013-09-22 19:44:13 -0700203 angle_adjust_goal = 0.671;
Brian Silverman8a82f382013-03-16 14:12:01 -0700204 }
205 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700206
Brian Silverman906aef52013-03-17 23:37:41 -0700207 wrist.goal.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700208 .goal(data.IsPressed(kWristDown) ?
209 wrist_down_position :
210 wrist_up_position)
211 .Send();
Brian Silverman906aef52013-03-17 23:37:41 -0700212
Brian Silverman8a82f382013-03-16 14:12:01 -0700213 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
214 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700215 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700216 // FIRE
217 index_goal->goal_state = 4;
218 } else if (shooter_goal->velocity != 0) {
219 // get ready to shoot
220 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700221 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700222 // intake
223 index_goal->goal_state = 2;
224 } else {
225 // get ready to intake
226 index_goal->goal_state = 1;
227 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700228 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700229
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700230 const bool index_up = data.IsPressed(kForceIndexUp);
231 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman759d2632013-10-11 18:01:21 -0700232 const bool spit_out = data.IsPressed(kForceSpitOut);
233 index_goal->override_index = index_up || index_down || spit_out;
234 index_goal->override_transfer = spit_out;
Brian Silverman180e2b82013-04-08 14:29:56 -0700235 if (index_up && index_down) {
236 index_goal->index_voltage = 0.0;
237 } else if (index_up) {
238 index_goal->index_voltage = 12.0;
239 } else if (index_down) {
240 index_goal->index_voltage = -12.0;
241 }
Brian Silverman759d2632013-10-11 18:01:21 -0700242 if (spit_out) {
243 index_goal->index_voltage = -12.0;
244 index_goal->transfer_voltage = -12.0;
245 }
Brian Silverman180e2b82013-04-08 14:29:56 -0700246
Brian Silverman8a82f382013-03-16 14:12:01 -0700247 index_goal.Send();
248 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000249 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700250
251 static int hanger_cycles = 0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700252 if (data.IsPressed(kDeployHangers)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700253 ++hanger_cycles;
Brian Silverman46390292013-09-29 17:03:16 -0700254 angle_adjust_goal = 0.4;
Brian Silverman513ad4e2013-03-20 19:59:50 -0700255 } else {
256 hanger_cycles = 0;
257 }
258 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000259 }
260};
261
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700262} // namespace joysticks
263} // namespace input
brians343bc112013-02-10 01:53:46 +0000264} // namespace frc971
265
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700266int main() {
267 ::aos::Init();
268 ::frc971::input::joysticks::Reader reader;
269 reader.Run();
270 ::aos::Cleanup();
271}