blob: 387bc10db9b04e4e38f389d7d2dbccdbd310ca37 [file] [log] [blame]
Brian Silverman756f9ff2014-01-17 23:40:23 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
6#include "aos/linux_code/init.h"
7#include "aos/prime/input/joystick_input.h"
Austin Schuh06cbbf12014-02-22 02:07:31 -08008#include "aos/common/input/driver_station_data.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -08009#include "aos/common/logging/logging.h"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/queues/gyro_angle.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080013#include "frc971/autonomous/auto.q.h"
Brian Silvermanfac5c292014-02-17 15:26:57 -080014#include "frc971/control_loops/claw/claw.q.h"
15#include "frc971/control_loops/shooter/shooter.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080016
17using ::frc971::control_loops::drivetrain;
Brian Silverman756f9ff2014-01-17 23:40:23 -080018using ::frc971::sensors::gyro;
19
20using ::aos::input::driver_station::ButtonLocation;
21using ::aos::input::driver_station::JoystickAxis;
22using ::aos::input::driver_station::ControlBit;
23
24namespace frc971 {
25namespace input {
26namespace joysticks {
27
28const ButtonLocation kDriveControlLoopEnable1(1, 7),
29 kDriveControlLoopEnable2(1, 11);
30const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
31const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
32const ButtonLocation kQuickTurn(1, 5);
Austin Schuh58d23682014-02-23 01:39:50 -080033
Austin Schuh06cbbf12014-02-22 02:07:31 -080034const ButtonLocation kFire(3, 11);
Austin Schuh9cb836e2014-02-23 19:25:55 -080035const ButtonLocation kUnload(2, 11);
36const ButtonLocation kReload(2, 6);
Austin Schuh58d23682014-02-23 01:39:50 -080037
38const ButtonLocation kRollersOut(3, 12);
39const ButtonLocation kRollersIn(3, 10);
40
41const ButtonLocation kTuck(3, 8);
Austin Schuh9cb836e2014-02-23 19:25:55 -080042const ButtonLocation kIntakeOpenPosition(3, 9);
Austin Schuh58d23682014-02-23 01:39:50 -080043const ButtonLocation kIntakePosition(3, 7);
44
45const ButtonLocation kLongShot(3, 5);
46const ButtonLocation kMediumShot(3, 3);
47const ButtonLocation kShortShot(3, 6);
48
49struct ClawGoal {
50 double angle;
51 double separation;
52};
53
54const ClawGoal kTuckGoal = {-2.273474, -0.749484};
55const ClawGoal kIntakeGoal = {-2.273474, 0.0};
Austin Schuh9cb836e2014-02-23 19:25:55 -080056const ClawGoal kIntakeOpenGoal = {-2.0, 1.2};
Austin Schuh58d23682014-02-23 01:39:50 -080057
Austin Schuh9cb836e2014-02-23 19:25:55 -080058const ClawGoal kLongShotGoal = {-M_PI / 2.0 + 0.43, 0.10};
59const ClawGoal kMediumShotGoal = {-0.9, 0.10};
60const ClawGoal kShortShotGoal = {-0.04, 0.11};
Brian Silverman756f9ff2014-01-17 23:40:23 -080061
62class Reader : public ::aos::input::JoystickInput {
63 public:
Austin Schuh58d23682014-02-23 01:39:50 -080064 Reader()
65 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -080066 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -080067 goal_angle_(0.0),
68 separation_angle_(0.0) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -080069
70 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080071
72 if (data.GetControlBit(ControlBit::kAutonomous)) {
73 if (data.PosEdge(ControlBit::kEnabled)){
74 LOG(INFO, "Starting auto mode\n");
75 ::frc971::autonomous::autonomous.MakeWithBuilder()
76 .run_auto(true).Send();
77 } else if (data.NegEdge(ControlBit::kEnabled)) {
78 LOG(INFO, "Stopping auto mode\n");
79 ::frc971::autonomous::autonomous.MakeWithBuilder()
80 .run_auto(false).Send();
81 }
Austin Schuh58d23682014-02-23 01:39:50 -080082 } else {
83 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -080084 }
85 }
Austin Schuh58d23682014-02-23 01:39:50 -080086
87 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
88 bool is_control_loop_driving = false;
89 double left_goal = 0.0;
90 double right_goal = 0.0;
91 const double wheel = -data.GetAxis(kSteeringWheel);
92 const double throttle = -data.GetAxis(kDriveThrottle);
93 const double kThrottleGain = 1.0 / 2.5;
94 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
95 data.IsPressed(kDriveControlLoopEnable2))) {
96 // TODO(austin): Static sucks!
97 static double distance = 0.0;
98 static double angle = 0.0;
99 static double filtered_goal_distance = 0.0;
100 if (data.PosEdge(kDriveControlLoopEnable1) ||
101 data.PosEdge(kDriveControlLoopEnable2)) {
102 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
103 distance = (drivetrain.position->left_encoder +
104 drivetrain.position->right_encoder) /
105 2.0 -
106 throttle * kThrottleGain / 2.0;
107 angle = gyro->angle;
108 filtered_goal_distance = distance;
109 }
110 }
111 is_control_loop_driving = true;
112
113 // const double gyro_angle = Gyro.View().angle;
114 const double goal_theta = angle - wheel * 0.27;
115 const double goal_distance = distance + throttle * kThrottleGain;
116 const double robot_width = 22.0 / 100.0 * 2.54;
117 const double kMaxVelocity = 0.6;
118 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
119 filtered_goal_distance += kMaxVelocity * 0.02;
120 } else if (goal_distance <
121 -kMaxVelocity * 0.02 + filtered_goal_distance) {
122 filtered_goal_distance -= kMaxVelocity * 0.02;
123 } else {
124 filtered_goal_distance = goal_distance;
125 }
126 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
127 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
128 is_high_gear_ = false;
129
130 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
131 }
132 if (!drivetrain.goal.MakeWithBuilder()
133 .steering(wheel)
134 .throttle(throttle)
135 .highgear(is_high_gear_)
136 .quickturn(data.IsPressed(kQuickTurn))
137 .control_loop_driving(is_control_loop_driving)
138 .left_goal(left_goal)
139 .right_goal(right_goal)
140 .Send()) {
141 LOG(WARNING, "sending stick values failed\n");
142 }
143 if (data.PosEdge(kShiftHigh)) {
144 is_high_gear_ = false;
145 }
146 if (data.PosEdge(kShiftLow)) {
147 is_high_gear_ = true;
148 }
149 }
150
151 void SetGoal(ClawGoal goal) {
152 goal_angle_ = goal.angle;
153 separation_angle_ = goal.separation;
154 }
155
156 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
157 HandleDrivetrain(data);
158
Austin Schuh9cb836e2014-02-23 19:25:55 -0800159 if (data.IsPressed(kIntakeOpenPosition)) {
160 SetGoal(kIntakeOpenGoal);
161 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuh58d23682014-02-23 01:39:50 -0800162 SetGoal(kIntakeGoal);
163 } else if (data.IsPressed(kTuck)) {
164 SetGoal(kTuckGoal);
165 }
166
167 // TODO(austin): Wait for the claw to go to position before shooting, and
168 // open the claw as part of the actual fire step.
169 if (data.IsPressed(kLongShot)) {
Austin Schuh9cb836e2014-02-23 19:25:55 -0800170 shot_power_ = 160.0;
Austin Schuh58d23682014-02-23 01:39:50 -0800171 SetGoal(kLongShotGoal);
172 } else if (data.IsPressed(kMediumShot)) {
Austin Schuh9cb836e2014-02-23 19:25:55 -0800173 shot_power_ = 100.0;
Austin Schuh58d23682014-02-23 01:39:50 -0800174 SetGoal(kMediumShotGoal);
175 } else if (data.IsPressed(kShortShot)) {
Austin Schuh9cb836e2014-02-23 19:25:55 -0800176 shot_power_ = 70.0;
Austin Schuh58d23682014-02-23 01:39:50 -0800177 SetGoal(kShortShotGoal);
178 }
179
180 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
181 .bottom_angle(goal_angle_)
182 .separation_angle(separation_angle_)
183 .intake(data.IsPressed(kRollersIn)
184 ? 12.0
185 : (data.IsPressed(kRollersOut) ? -12.0 : 0.0))
186 .centering(data.IsPressed(kRollersIn) ? 12.0 : 0.0)
187 .Send()) {
188 LOG(WARNING, "sending claw goal failed\n");
189 }
190
191 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
192 .shot_power(shot_power_)
193 .shot_requested(data.IsPressed(kFire))
194 .unload_requested(data.IsPressed(kUnload))
195 .load_requested(data.IsPressed(kReload))
196 .Send()) {
197 LOG(WARNING, "sending shooter goal failed\n");
198 }
199 }
200
Austin Schuh01c652b2014-02-21 23:13:42 -0800201 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800202 bool is_high_gear_;
203 double shot_power_;
204 double goal_angle_;
205 double separation_angle_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800206};
207
208} // namespace joysticks
209} // namespace input
210} // namespace frc971
211
212int main() {
213 ::aos::Init();
214 ::frc971::input::joysticks::Reader reader;
215 reader.Run();
216 ::aos::Cleanup();
217}