blob: b40643ecfc566f67d79b2b8bc2e7c9289114bf6e [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"
brians343bc112013-02-10 01:53:46 +000018
19using ::frc971::control_loops::drivetrain;
20using ::frc971::control_loops::shifters;
21using ::frc971::sensors::gyro;
Brian Silverman687f5242013-03-16 13:57:59 -070022using ::frc971::control_loops::wrist;
23using ::frc971::control_loops::index_loop;
24using ::frc971::control_loops::shooter;
25using ::frc971::control_loops::angle_adjust;
brians343bc112013-02-10 01:53:46 +000026
27namespace frc971 {
28
29class JoystickReader : public aos::JoystickInput {
30 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070031 static const bool kWristAlwaysDown = false;
32
brians343bc112013-02-10 01:53:46 +000033 JoystickReader() : aos::JoystickInput() {
34 shifters.MakeWithBuilder().set(true).Send();
35 }
36
37 virtual void RunIteration() {
38 static bool is_high_gear = false;
39
40 if (Pressed(0, AUTONOMOUS)) {
41 if (PosEdge(0, ENABLED)){
42 LOG(INFO, "Starting auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000043 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
brians343bc112013-02-10 01:53:46 +000044 }
45 if (NegEdge(0, ENABLED)) {
46 LOG(INFO, "Stopping auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000047 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000048 }
49 } else { // teleop
50 bool is_control_loop_driving = false;
51 double left_goal = 0.0;
52 double right_goal = 0.0;
53 const double wheel = control_data_.stick0Axis1 / 127.0;
54 const double throttle = -control_data_.stick1Axis2 / 127.0;
Brian Silverman834a0da2013-03-16 23:49:27 -070055 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000056 const double kThrottleGain = 1.0 / 2.5;
57 if (Pressed(0, 7) || Pressed(0, 11)) {
58 static double distance = 0.0;
59 static double angle = 0.0;
60 static double filtered_goal_distance = 0.0;
61 if (PosEdge(0, 7) || PosEdge(0, 11)) {
62 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
63 distance = (drivetrain.position->left_encoder +
64 drivetrain.position->right_encoder) / 2.0
65 - throttle * kThrottleGain / 2.0;
66 angle = gyro->angle;
67 filtered_goal_distance = distance;
68 }
69 }
70 is_control_loop_driving = true;
71
72 //const double gyro_angle = Gyro.View().angle;
73 const double goal_theta = angle - wheel * 0.27;
74 const double goal_distance = distance + throttle * kThrottleGain;
75 const double robot_width = 22.0 / 100.0 * 2.54;
76 const double kMaxVelocity = 0.6;
77 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
78 filtered_goal_distance += kMaxVelocity * 0.02;
79 } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) {
80 filtered_goal_distance -= kMaxVelocity * 0.02;
81 } else {
82 filtered_goal_distance = goal_distance;
83 }
84 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
85 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
86 is_high_gear = false;
87
88 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
89 }
90 if (!(drivetrain.goal.MakeWithBuilder()
91 .steering(wheel)
92 .throttle(throttle)
93 .highgear(is_high_gear).quickturn(Pressed(0, 5))
94 .control_loop_driving(is_control_loop_driving)
95 .left_goal(left_goal).right_goal(right_goal).Send())) {
96 LOG(WARNING, "sending stick values failed\n");
97 }
98
99 if (PosEdge(1, 1)) {
100 is_high_gear = false;
101 }
102 if (PosEdge(1, 3)) {
103 is_high_gear = true;
104 }
Brian Silverman687f5242013-03-16 13:57:59 -0700105
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700106 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000107 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silvermanbcaf3842013-03-16 23:49:35 -0700108 static const double kWristPickup = -0.633;
Brian Silverman906aef52013-03-17 23:37:41 -0700109 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700110 // Where the wrist gets stored when up.
111 // All the way up is 1.5.
112 static const double kWristUp = 1.43;
113 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700114 double wrist_up_position = kWristUp;
Brian Silverman687f5242013-03-16 13:57:59 -0700115
Brian Silverman8a82f382013-03-16 14:12:01 -0700116 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
117 shooter.goal.MakeMessage();
118 shooter_goal->velocity = 0;
Brian Silverman834a0da2013-03-16 23:49:27 -0700119 static double angle_adjust_goal = 0.42;
120 if (Pressed(2, 5)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700121 // long shot
122 shooter_goal->velocity = 375;
123 angle_adjust_goal = 0.70;
124 angle_adjust_goal = 0.564;
Brian Silverman834a0da2013-03-16 23:49:27 -0700125 } else if (Pressed(2, 3)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700126 // medium shot
Brian Silverman906aef52013-03-17 23:37:41 -0700127 shooter_goal->velocity = 375;
128 wrist_up_position = 0.70;
129 angle_adjust_goal = 0.564;
Brian Silverman8a82f382013-03-16 14:12:01 -0700130 } else if (Pressed(2, 6)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700131 // short shot
132 shooter_goal->velocity = 375;
133 angle_adjust_goal = 0.685;
Brian Silverman8a82f382013-03-16 14:12:01 -0700134 }
135 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700136
Brian Silverman906aef52013-03-17 23:37:41 -0700137 double wrist_pickup_position = Pressed(2, 10) /*intake*/ ?
138 kWristPickup : kWristNearGround;
139 index_loop.status.FetchLatest();
140 if (index_loop.status.get()) {
141 if (index_loop.status->hopper_disc_count >= 4) {
142 wrist_down_position = kWristNearGround;
143 } else {
144 wrist_down_position = wrist_pickup_position;
145 }
146 }
147 wrist.goal.MakeWithBuilder()
148 .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send();
149
Brian Silverman8a82f382013-03-16 14:12:01 -0700150 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
151 index_loop.goal.MakeMessage();
152 // TODO(brians): replace these with the enum values
153 if (Pressed(2, 11)) {
154 // FIRE
155 index_goal->goal_state = 4;
156 } else if (shooter_goal->velocity != 0) {
157 // get ready to shoot
158 index_goal->goal_state = 3;
Brian Silverman834a0da2013-03-16 23:49:27 -0700159 } else if (Pressed(2, 10)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700160 // intake
161 index_goal->goal_state = 2;
162 } else {
163 // get ready to intake
164 index_goal->goal_state = 1;
165 }
Brian Silverman687f5242013-03-16 13:57:59 -0700166
Brian Silverman8a82f382013-03-16 14:12:01 -0700167 index_goal.Send();
168 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000169 }
170 }
171};
172
173} // namespace frc971
174
175AOS_RUN(frc971::JoystickReader)