blob: 84f20b5016861a716340fe512729f40d2a3f085b [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 Silverman906aef52013-03-17 23:37:41 -0700122 // long shot
123 shooter_goal->velocity = 375;
124 angle_adjust_goal = 0.70;
125 angle_adjust_goal = 0.564;
Brian Silverman834a0da2013-03-16 23:49:27 -0700126 } else if (Pressed(2, 3)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700127 // medium shot
Brian Silverman906aef52013-03-17 23:37:41 -0700128 shooter_goal->velocity = 375;
129 wrist_up_position = 0.70;
130 angle_adjust_goal = 0.564;
Brian Silverman8a82f382013-03-16 14:12:01 -0700131 } else if (Pressed(2, 6)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700132 // short shot
133 shooter_goal->velocity = 375;
134 angle_adjust_goal = 0.685;
Brian Silverman8a82f382013-03-16 14:12:01 -0700135 }
136 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700137
Brian Silverman906aef52013-03-17 23:37:41 -0700138 double wrist_pickup_position = Pressed(2, 10) /*intake*/ ?
139 kWristPickup : kWristNearGround;
140 index_loop.status.FetchLatest();
141 if (index_loop.status.get()) {
142 if (index_loop.status->hopper_disc_count >= 4) {
143 wrist_down_position = kWristNearGround;
144 } else {
145 wrist_down_position = wrist_pickup_position;
146 }
147 }
148 wrist.goal.MakeWithBuilder()
149 .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send();
150
Brian Silverman8a82f382013-03-16 14:12:01 -0700151 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
152 index_loop.goal.MakeMessage();
153 // TODO(brians): replace these with the enum values
154 if (Pressed(2, 11)) {
155 // FIRE
156 index_goal->goal_state = 4;
157 } else if (shooter_goal->velocity != 0) {
158 // get ready to shoot
159 index_goal->goal_state = 3;
Brian Silverman834a0da2013-03-16 23:49:27 -0700160 } else if (Pressed(2, 10)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700161 // intake
162 index_goal->goal_state = 2;
163 } else {
164 // get ready to intake
165 index_goal->goal_state = 1;
166 }
Brian Silverman687f5242013-03-16 13:57:59 -0700167
Brian Silverman8a82f382013-03-16 14:12:01 -0700168 index_goal.Send();
169 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000170 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700171
172 static int hanger_cycles = 0;
Brian Silvermand3f92f12013-03-20 23:01:33 -0700173 if (Pressed(2, 1)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700174 ++hanger_cycles;
175 } else {
176 hanger_cycles = 0;
177 }
178 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000179 }
180};
181
182} // namespace frc971
183
184AOS_RUN(frc971::JoystickReader)