blob: 7538f6b6757028ac89904e4ee1992396be998dd0 [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 Silverman1c0cb8b2013-03-15 23:19:59 -0700139 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000140 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silverman364cd262013-03-30 22:44:57 -0700141 static const double kWristPickup = -0.580;
Brian Silverman906aef52013-03-17 23:37:41 -0700142 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700143 // Where the wrist gets stored when up.
144 // All the way up is 1.5.
145 static const double kWristUp = 1.43;
146 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700147 double wrist_up_position = kWristUp;
Brian Silverman687f5242013-03-16 13:57:59 -0700148
Brian Silverman8a82f382013-03-16 14:12:01 -0700149 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
150 shooter.goal.MakeMessage();
151 shooter_goal->velocity = 0;
Brian Silverman834a0da2013-03-16 23:49:27 -0700152 static double angle_adjust_goal = 0.42;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700153 if (data.IsPressed(kLongShot)) {
Brian Silverman304b2bf2013-04-04 17:54:41 -0700154#if 0
Brian Silverman32d69142013-03-30 00:02:06 -0700155 target_angle.FetchLatest();
156 if (target_angle.IsNewerThanMS(500)) {
157 shooter_goal->velocity = target_angle->shooter_speed;
158 angle_adjust_goal = target_angle->shooter_angle;
159 // TODO(brians): do the math right here
160 wrist_up_position = 0.70;
161 } else {
162 LOG(WARNING, "camera frame too old\n");
163 // pretend like no button is pressed
164 }
Brian Silverman304b2bf2013-04-04 17:54:41 -0700165#endif
Brian Silvermane296ebd2013-04-05 13:51:13 -0700166 shooter_goal->velocity = 360;
Brian Silverman304b2bf2013-04-04 17:54:41 -0700167 wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700168 angle_adjust_goal = 0.596;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700169 } else if (data.IsPressed(kMediumShot)) {
Brian Silverman947735d2013-03-22 15:15:58 -0700170#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700171 shooter_goal->velocity = 375;
172 wrist_up_position = 0.70;
173 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700174#endif
175 // middle wheel on the back line (same as auto)
Brian Silvermane296ebd2013-04-05 13:51:13 -0700176 shooter_goal->velocity = 395;
Brian Silverman947735d2013-03-22 15:15:58 -0700177 wrist_up_position = 1.23 - 0.4;
Brian Silvermane296ebd2013-04-05 13:51:13 -0700178 angle_adjust_goal = 0.520;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700179 } else if (data.IsPressed(kShortShot)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700180 shooter_goal->velocity = 375;
Brian Silverman947735d2013-03-22 15:15:58 -0700181 angle_adjust_goal = 0.7267;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700182 } else if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) {
Brian Silverman7992d6e2013-03-24 19:20:54 -0700183 shooter_goal->velocity = 131;
184 angle_adjust_goal = 0.70;
Brian Silverman8a82f382013-03-16 14:12:01 -0700185 }
186 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700187
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700188 double wrist_pickup_position = data.IsPressed(kIntake) ?
Brian Silverman906aef52013-03-17 23:37:41 -0700189 kWristPickup : kWristNearGround;
190 index_loop.status.FetchLatest();
191 if (index_loop.status.get()) {
192 if (index_loop.status->hopper_disc_count >= 4) {
193 wrist_down_position = kWristNearGround;
194 } else {
195 wrist_down_position = wrist_pickup_position;
196 }
197 }
198 wrist.goal.MakeWithBuilder()
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700199 .goal(data.IsPressed(kWristDown) ?
200 wrist_down_position :
201 wrist_up_position)
202 .Send();
Brian Silverman906aef52013-03-17 23:37:41 -0700203
Brian Silverman8a82f382013-03-16 14:12:01 -0700204 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
205 index_loop.goal.MakeMessage();
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700206 if (data.IsPressed(kFire)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700207 // FIRE
208 index_goal->goal_state = 4;
209 } else if (shooter_goal->velocity != 0) {
210 // get ready to shoot
211 index_goal->goal_state = 3;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700212 } else if (data.IsPressed(kIntake)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700213 // intake
214 index_goal->goal_state = 2;
215 } else {
216 // get ready to intake
217 index_goal->goal_state = 1;
218 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700219 index_goal->force_fire = data.IsPressed(kForceFire);
Brian Silverman687f5242013-03-16 13:57:59 -0700220
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700221 const bool index_up = data.IsPressed(kForceIndexUp);
222 const bool index_down = data.IsPressed(kForceIndexDown);
Brian Silverman180e2b82013-04-08 14:29:56 -0700223 index_goal->override_index = index_up || index_down;
224 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 }
231
Brian Silverman8a82f382013-03-16 14:12:01 -0700232 index_goal.Send();
233 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000234 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700235
236 static int hanger_cycles = 0;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700237 if (data.IsPressed(kDeployHangers)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700238 ++hanger_cycles;
239 } else {
240 hanger_cycles = 0;
241 }
242 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000243 }
244};
245
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700246} // namespace joysticks
247} // namespace input
brians343bc112013-02-10 01:53:46 +0000248} // namespace frc971
249
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700250int main() {
251 ::aos::Init();
252 ::frc971::input::joysticks::Reader reader;
253 reader.Run();
254 ::aos::Cleanup();
255}