blob: 3cbe3c51dff6132aaab03dc7c849baf9ca1b6b34 [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 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
Brian Silverman2728e192013-11-02 17:38:12 -0700179 if (!hopper_clear) wrist_up_position = 0.70;
Brian Silverman32d69142013-03-30 00:02:06 -0700180 } else {
181 LOG(WARNING, "camera frame too old\n");
Brian Silverman2728e192013-11-02 17:38:12 -0700182 // Pretend like no button is pressed.
Brian Silverman32d69142013-03-30 00:02:06 -0700183 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700184 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700185 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700186 shooter_goal->velocity = 395;
Brian Silverman7d39d862013-09-29 17:00:30 -0700187 if (!hopper_clear) wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700188 angle_adjust_goal = 0.520;
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 Silverman7d39d862013-09-29 17:00:30 -0700191 if (hopper_clear) wrist_up_position = kWristCleared;
Brian Silverman9d0fa5f2013-09-22 19:44:13 -0700192 angle_adjust_goal = 0.671;
Brian Silverman8a82f382013-03-16 14:12:01 -0700193 }
194 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700195
Brian Silverman906aef52013-03-17 23:37:41 -0700196 wrist.goal.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700197 .goal(data.IsPressed(kWristDown) ?
198 wrist_down_position :
199 wrist_up_position)
200 .Send();
Brian Silverman906aef52013-03-17 23:37:41 -0700201
Brian Silverman8a82f382013-03-16 14:12:01 -0700202 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
203 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700204 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700205 // FIRE
206 index_goal->goal_state = 4;
207 } else if (shooter_goal->velocity != 0) {
208 // get ready to shoot
209 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700210 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700211 // intake
212 index_goal->goal_state = 2;
213 } else {
214 // get ready to intake
215 index_goal->goal_state = 1;
216 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700217 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700218
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700219 const bool index_up = data.IsPressed(kForceIndexUp);
220 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman759d2632013-10-11 18:01:21 -0700221 const bool spit_out = data.IsPressed(kForceSpitOut);
222 index_goal->override_index = index_up || index_down || spit_out;
223 index_goal->override_transfer = spit_out;
Brian Silverman180e2b82013-04-08 14:29:56 -0700224 if (index_up && index_down) {
225 index_goal->index_voltage = 0.0;
226 } else if (index_up) {
227 index_goal->index_voltage = 12.0;
228 } else if (index_down) {
229 index_goal->index_voltage = -12.0;
230 }
Brian Silverman759d2632013-10-11 18:01:21 -0700231 if (spit_out) {
232 index_goal->index_voltage = -12.0;
233 index_goal->transfer_voltage = -12.0;
234 }
Brian Silverman180e2b82013-04-08 14:29:56 -0700235
Brian Silverman8a82f382013-03-16 14:12:01 -0700236 index_goal.Send();
237 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000238 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700239
240 static int hanger_cycles = 0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700241 if (data.IsPressed(kDeployHangers)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700242 ++hanger_cycles;
Brian Silverman46390292013-09-29 17:03:16 -0700243 angle_adjust_goal = 0.4;
Brian Silverman513ad4e2013-03-20 19:59:50 -0700244 } else {
245 hanger_cycles = 0;
246 }
247 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000248 }
249};
250
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700251} // namespace joysticks
252} // namespace input
brians343bc112013-02-10 01:53:46 +0000253} // namespace frc971
254
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700255int main() {
256 ::aos::Init();
257 ::frc971::input::joysticks::Reader reader;
258 reader.Run();
259 ::aos::Cleanup();
260}