blob: ac8017ffb43c6474f9ff19a169fd54c0ae7e050b [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;
Brian Silverman513ad4e2013-03-20 19:59:50 -070026using ::frc971::control_loops::hangers;
brians343bc112013-02-10 01:53:46 +000027
28namespace frc971 {
29
30class JoystickReader : public aos::JoystickInput {
31 public:
Brian Silverman8a82f382013-03-16 14:12:01 -070032 static const bool kWristAlwaysDown = false;
33
brians343bc112013-02-10 01:53:46 +000034 JoystickReader() : aos::JoystickInput() {
35 shifters.MakeWithBuilder().set(true).Send();
36 }
37
38 virtual void RunIteration() {
39 static bool is_high_gear = false;
40
41 if (Pressed(0, AUTONOMOUS)) {
42 if (PosEdge(0, ENABLED)){
43 LOG(INFO, "Starting auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000044 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
brians343bc112013-02-10 01:53:46 +000045 }
46 if (NegEdge(0, ENABLED)) {
47 LOG(INFO, "Stopping auto mode\n");
Austin Schuh6be011a2013-03-19 10:07:02 +000048 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
brians343bc112013-02-10 01:53:46 +000049 }
50 } else { // teleop
51 bool is_control_loop_driving = false;
52 double left_goal = 0.0;
53 double right_goal = 0.0;
54 const double wheel = control_data_.stick0Axis1 / 127.0;
55 const double throttle = -control_data_.stick1Axis2 / 127.0;
Brian Silverman834a0da2013-03-16 23:49:27 -070056 LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle);
brians343bc112013-02-10 01:53:46 +000057 const double kThrottleGain = 1.0 / 2.5;
58 if (Pressed(0, 7) || Pressed(0, 11)) {
59 static double distance = 0.0;
60 static double angle = 0.0;
61 static double filtered_goal_distance = 0.0;
62 if (PosEdge(0, 7) || PosEdge(0, 11)) {
63 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
64 distance = (drivetrain.position->left_encoder +
65 drivetrain.position->right_encoder) / 2.0
66 - throttle * kThrottleGain / 2.0;
67 angle = gyro->angle;
68 filtered_goal_distance = distance;
69 }
70 }
71 is_control_loop_driving = true;
72
73 //const double gyro_angle = Gyro.View().angle;
74 const double goal_theta = angle - wheel * 0.27;
75 const double goal_distance = distance + throttle * kThrottleGain;
76 const double robot_width = 22.0 / 100.0 * 2.54;
77 const double kMaxVelocity = 0.6;
78 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
79 filtered_goal_distance += kMaxVelocity * 0.02;
80 } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) {
81 filtered_goal_distance -= kMaxVelocity * 0.02;
82 } else {
83 filtered_goal_distance = goal_distance;
84 }
85 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
86 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
87 is_high_gear = false;
88
89 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
90 }
91 if (!(drivetrain.goal.MakeWithBuilder()
92 .steering(wheel)
93 .throttle(throttle)
94 .highgear(is_high_gear).quickturn(Pressed(0, 5))
95 .control_loop_driving(is_control_loop_driving)
96 .left_goal(left_goal).right_goal(right_goal).Send())) {
97 LOG(WARNING, "sending stick values failed\n");
98 }
99
100 if (PosEdge(1, 1)) {
101 is_high_gear = false;
102 }
103 if (PosEdge(1, 3)) {
104 is_high_gear = true;
105 }
Brian Silverman687f5242013-03-16 13:57:59 -0700106
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700107 // Where the wrist should be to pick up a frisbee.
Austin Schuh6be011a2013-03-19 10:07:02 +0000108 // TODO(brians): Make these globally accessible and clean up auto.
Brian Silvermanbcaf3842013-03-16 23:49:35 -0700109 static const double kWristPickup = -0.633;
Brian Silverman906aef52013-03-17 23:37:41 -0700110 static const double kWristNearGround = -0.4;
Brian Silverman1c0cb8b2013-03-15 23:19:59 -0700111 // Where the wrist gets stored when up.
112 // All the way up is 1.5.
113 static const double kWristUp = 1.43;
114 static double wrist_down_position = kWristPickup;
Brian Silverman906aef52013-03-17 23:37:41 -0700115 double wrist_up_position = kWristUp;
Brian Silverman687f5242013-03-16 13:57:59 -0700116
Brian Silverman8a82f382013-03-16 14:12:01 -0700117 ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal =
118 shooter.goal.MakeMessage();
119 shooter_goal->velocity = 0;
Brian Silverman834a0da2013-03-16 23:49:27 -0700120 static double angle_adjust_goal = 0.42;
121 if (Pressed(2, 5)) {
Brian Silverman7992d6e2013-03-24 19:20:54 -0700122 // middle wheel on the back line (same as auto)
123 shooter_goal->velocity = 410;
124 wrist_up_position = 1.23 - 0.4;
125 angle_adjust_goal = 0.5101;
Brian Silverman834a0da2013-03-16 23:49:27 -0700126 } else if (Pressed(2, 3)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700127 // medium shot
Brian Silverman947735d2013-03-22 15:15:58 -0700128#if 0
Brian Silverman906aef52013-03-17 23:37:41 -0700129 shooter_goal->velocity = 375;
130 wrist_up_position = 0.70;
131 angle_adjust_goal = 0.564;
Brian Silverman947735d2013-03-22 15:15:58 -0700132#endif
133 // middle wheel on the back line (same as auto)
Brian Silverman7992d6e2013-03-24 19:20:54 -0700134 shooter_goal->velocity = 400;
Brian Silverman947735d2013-03-22 15:15:58 -0700135 wrist_up_position = 1.23 - 0.4;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700136 angle_adjust_goal = 0.5434;
Brian Silverman8a82f382013-03-16 14:12:01 -0700137 } else if (Pressed(2, 6)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700138 // short shot
139 shooter_goal->velocity = 375;
Brian Silverman947735d2013-03-22 15:15:58 -0700140 angle_adjust_goal = 0.7267;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700141 } else if (Pressed(2, 9)) {
142 // pit shot
143 shooter_goal->velocity = 131;
144 angle_adjust_goal = 0.70;
Brian Silverman8a82f382013-03-16 14:12:01 -0700145 }
146 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700147
Brian Silverman906aef52013-03-17 23:37:41 -0700148 double wrist_pickup_position = Pressed(2, 10) /*intake*/ ?
149 kWristPickup : kWristNearGround;
150 index_loop.status.FetchLatest();
151 if (index_loop.status.get()) {
152 if (index_loop.status->hopper_disc_count >= 4) {
153 wrist_down_position = kWristNearGround;
154 } else {
155 wrist_down_position = wrist_pickup_position;
156 }
157 }
158 wrist.goal.MakeWithBuilder()
159 .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send();
160
Brian Silverman8a82f382013-03-16 14:12:01 -0700161 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
162 index_loop.goal.MakeMessage();
163 // TODO(brians): replace these with the enum values
164 if (Pressed(2, 11)) {
165 // FIRE
166 index_goal->goal_state = 4;
167 } else if (shooter_goal->velocity != 0) {
168 // get ready to shoot
169 index_goal->goal_state = 3;
Brian Silverman834a0da2013-03-16 23:49:27 -0700170 } else if (Pressed(2, 10)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700171 // intake
172 index_goal->goal_state = 2;
173 } else {
174 // get ready to intake
175 index_goal->goal_state = 1;
176 }
Brian Silverman687f5242013-03-16 13:57:59 -0700177
Brian Silverman8a82f382013-03-16 14:12:01 -0700178 index_goal.Send();
179 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000180 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700181
182 static int hanger_cycles = 0;
Brian Silvermand3f92f12013-03-20 23:01:33 -0700183 if (Pressed(2, 1)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700184 ++hanger_cycles;
185 } else {
186 hanger_cycles = 0;
187 }
188 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000189 }
190};
191
192} // namespace frc971
193
194AOS_RUN(frc971::JoystickReader)