blob: 092ae118a1c2f354c7dd863c37d5b646afdfb146 [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
Brian Silvermanc5277542013-03-22 13:33:07 -0700123 shooter_goal->velocity = 131;
Brian Silverman906aef52013-03-17 23:37:41 -0700124 angle_adjust_goal = 0.70;
Brian Silverman834a0da2013-03-16 23:49:27 -0700125 } else if (Pressed(2, 3)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700126 // medium shot
Brian Silverman947735d2013-03-22 15:15:58 -0700127#if 0
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 Silverman947735d2013-03-22 15:15:58 -0700131#endif
132 // middle wheel on the back line (same as auto)
133 shooter_goal->velocity = 410;
134 wrist_up_position = 1.23 - 0.4;
135 angle_adjust_goal = 0.5101;
Brian Silverman8a82f382013-03-16 14:12:01 -0700136 } else if (Pressed(2, 6)) {
Brian Silverman906aef52013-03-17 23:37:41 -0700137 // short shot
138 shooter_goal->velocity = 375;
Brian Silverman947735d2013-03-22 15:15:58 -0700139 angle_adjust_goal = 0.7267;
Brian Silverman8a82f382013-03-16 14:12:01 -0700140 }
141 angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send();
Brian Silverman687f5242013-03-16 13:57:59 -0700142
Brian Silverman906aef52013-03-17 23:37:41 -0700143 double wrist_pickup_position = Pressed(2, 10) /*intake*/ ?
144 kWristPickup : kWristNearGround;
145 index_loop.status.FetchLatest();
146 if (index_loop.status.get()) {
147 if (index_loop.status->hopper_disc_count >= 4) {
148 wrist_down_position = kWristNearGround;
149 } else {
150 wrist_down_position = wrist_pickup_position;
151 }
152 }
153 wrist.goal.MakeWithBuilder()
154 .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send();
155
Brian Silverman8a82f382013-03-16 14:12:01 -0700156 ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal =
157 index_loop.goal.MakeMessage();
158 // TODO(brians): replace these with the enum values
159 if (Pressed(2, 11)) {
160 // FIRE
161 index_goal->goal_state = 4;
162 } else if (shooter_goal->velocity != 0) {
163 // get ready to shoot
164 index_goal->goal_state = 3;
Brian Silverman834a0da2013-03-16 23:49:27 -0700165 } else if (Pressed(2, 10)) {
Brian Silverman8a82f382013-03-16 14:12:01 -0700166 // intake
167 index_goal->goal_state = 2;
168 } else {
169 // get ready to intake
170 index_goal->goal_state = 1;
171 }
Brian Silverman687f5242013-03-16 13:57:59 -0700172
Brian Silverman8a82f382013-03-16 14:12:01 -0700173 index_goal.Send();
174 shooter_goal.Send();
brians343bc112013-02-10 01:53:46 +0000175 }
Brian Silverman513ad4e2013-03-20 19:59:50 -0700176
177 static int hanger_cycles = 0;
Brian Silvermand3f92f12013-03-20 23:01:33 -0700178 if (Pressed(2, 1)) {
Brian Silverman513ad4e2013-03-20 19:59:50 -0700179 ++hanger_cycles;
180 } else {
181 hanger_cycles = 0;
182 }
183 hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send();
brians343bc112013-02-10 01:53:46 +0000184 }
185};
186
187} // namespace frc971
188
189AOS_RUN(frc971::JoystickReader)