blob: 46e0655fa643fc353dbbb94cb959fa86221bbd68 [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
6#include "aos/aos_core.h"
7#include "aos/atom_code/input/FRCComm.h"
8#include "aos/atom_code/input/JoystickInput.h"
9
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
30namespace frc971 {
31
32class JoystickReader : public aos::JoystickInput {
33 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070034 static const bool kWristAlwaysDown = false;
35
brians343bc112013-02-10 01:53:46 +000036 JoystickReader() : aos::JoystickInput() {
37 shifters.MakeWithBuilder().set(true).Send();
38 }
39
40 virtual void RunIteration() {
41 static bool is_high_gear = false;
42
43 if (Pressed(0, AUTONOMOUS)) {
44 if (PosEdge(0, ENABLED)){
45 LOG(INFO, "Starting auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000046 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
brians343bc112013-02-10 01:53:46 +000047 }
48 if (NegEdge(0, ENABLED)) {
49 LOG(INFO, "Stopping auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000050 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000051 }
52 } else { // teleop
53 bool is_control_loop_driving = false;
54 double left_goal = 0.0;
55 double right_goal = 0.0;
56 const double wheel = control_data_.stick0Axis1 / 127.0;
57 const double throttle = -control_data_.stick1Axis2 / 127.0;
Brian Silverman834a0da2013-03-16 23:49:27 -070058 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000059 const double kThrottleGain = 1.0 / 2.5;
60 if (Pressed(0, 7) || Pressed(0, 11)) {
61 static double distance = 0.0;
62 static double angle = 0.0;
63 static double filtered_goal_distance = 0.0;
64 if (PosEdge(0, 7) || PosEdge(0, 11)) {
65 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
66 distance = (drivetrain.position->left_encoder +
67 drivetrain.position->right_encoder) / 2.0
68 - throttle * kThrottleGain / 2.0;
69 angle = gyro->angle;
70 filtered_goal_distance = distance;
71 }
72 }
73 is_control_loop_driving = true;
74
75 //const double gyro_angle = Gyro.View().angle;
76 const double goal_theta = angle - wheel * 0.27;
77 const double goal_distance = distance + throttle * kThrottleGain;
78 const double robot_width = 22.0 / 100.0 * 2.54;
79 const double kMaxVelocity = 0.6;
80 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
81 filtered_goal_distance += kMaxVelocity * 0.02;
82 } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) {
83 filtered_goal_distance -= kMaxVelocity * 0.02;
84 } else {
85 filtered_goal_distance = goal_distance;
86 }
87 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
88 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
89 is_high_gear = false;
90
91 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
92 }
93 if (!(drivetrain.goal.MakeWithBuilder()
94 .steering(wheel)
95 .throttle(throttle)
96 .highgear(is_high_gear).quickturn(Pressed(0, 5))
97 .control_loop_driving(is_control_loop_driving)
98 .left_goal(left_goal).right_goal(right_goal).Send())) {
99 LOG(WARNING, "sending stick values failed\n");
100 }
101
102 if (PosEdge(1, 1)) {
103 is_high_gear = false;
104 }
105 if (PosEdge(1, 3)) {
106 is_high_gear = true;
107 }
Brian Silverman687f5242013-03-16 13:57:59 -0700108
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700109 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000110 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silvermanbcaf3842013-03-16 23:49:35 -0700111 static const double kWristPickup = -0.633;
Brian Silverman906aef52013-03-17 23:37:41 -0700112 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700113 // Where the wrist gets stored when up.
114 // All the way up is 1.5.
115 static const double kWristUp = 1.43;
116 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700117 double wrist_up_position = kWristUp;
Brian Silverman687f5242013-03-16 13:57:59 -0700118
Brian Silverman8a82f382013-03-16 14:12:01 -0700119 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
120 shooter.goal.MakeMessage();
121 shooter_goal->velocity = 0;
Brian Silverman834a0da2013-03-16 23:49:27 -0700122 static double angle_adjust_goal = 0.42;
123 if (Pressed(2, 5)) {
Brian Silverman32d69142013-03-30 00:02:06 -0700124 target_angle.FetchLatest();
125 if (target_angle.IsNewerThanMS(500)) {
126 shooter_goal->velocity = target_angle->shooter_speed;
127 angle_adjust_goal = target_angle->shooter_angle;
128 // TODO(brians): do the math right here
129 wrist_up_position = 0.70;
130 } else {
131 LOG(WARNING, "camera frame too old\n");
132 // pretend like no button is pressed
133 }
Brian Silverman834a0da2013-03-16 23:49:27 -0700134 } else if (Pressed(2, 3)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700135 // medium shot
Brian Silverman947735d2013-03-22 15:15:58 -0700136#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700137 shooter_goal->velocity = 375;
138 wrist_up_position = 0.70;
139 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700140#endif
141 // middle wheel on the back line (same as auto)
Brian Silverman7992d6e2013-03-24 19:20:54 -0700142 shooter_goal->velocity = 400;
Brian Silverman947735d2013-03-22 15:15:58 -0700143 wrist_up_position = 1.23 - 0.4;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700144 angle_adjust_goal = 0.5434;
Brian Silverman8a82f382013-03-16 14:12:01 -0700145 } else if (Pressed(2, 6)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700146 // short shot
147 shooter_goal->velocity = 375;
Brian Silverman947735d2013-03-22 15:15:58 -0700148 angle_adjust_goal = 0.7267;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700149 } else if (Pressed(2, 9)) {
150 // pit shot
151 shooter_goal->velocity = 131;
152 angle_adjust_goal = 0.70;
Brian Silverman8a82f382013-03-16 14:12:01 -0700153 }
154 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700155
Brian Silverman906aef52013-03-17 23:37:41 -0700156 double wrist_pickup_position = Pressed(2, 10) /*intake*/ ?
157 kWristPickup : kWristNearGround;
158 index_loop.status.FetchLatest();
159 if (index_loop.status.get()) {
160 if (index_loop.status->hopper_disc_count >= 4) {
161 wrist_down_position = kWristNearGround;
162 } else {
163 wrist_down_position = wrist_pickup_position;
164 }
165 }
166 wrist.goal.MakeWithBuilder()
167 .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send();
168
Brian Silverman8a82f382013-03-16 14:12:01 -0700169 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
170 index_loop.goal.MakeMessage();
171 // TODO(brians): replace these with the enum values
172 if (Pressed(2, 11)) {
173 // FIRE
174 index_goal->goal_state = 4;
175 } else if (shooter_goal->velocity != 0) {
176 // get ready to shoot
177 index_goal->goal_state = 3;
Brian Silverman834a0da2013-03-16 23:49:27 -0700178 } else if (Pressed(2, 10)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700179 // intake
180 index_goal->goal_state = 2;
181 } else {
182 // get ready to intake
183 index_goal->goal_state = 1;
184 }
Brian Silverman687f5242013-03-16 13:57:59 -0700185
Brian Silverman8a82f382013-03-16 14:12:01 -0700186 index_goal.Send();
187 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000188 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700189
190 static int hanger_cycles = 0;
Brian Silvermand3f92f12013-03-20 23:01:33 -0700191 if (Pressed(2, 1)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700192 ++hanger_cycles;
193 } else {
194 hanger_cycles = 0;
195 }
196 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000197 }
198};
199
200} // namespace frc971
201
202AOS_RUN(frc971::JoystickReader)