blob: f3304264d71735956fe673935c085cfd603ab980 [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"
Ben Fredricksonaa450452014-03-01 09:41:18 +000016#include "frc971/actions/shoot_action.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080017
18using ::frc971::control_loops::drivetrain;
Brian Silverman756f9ff2014-01-17 23:40:23 -080019using ::frc971::sensors::gyro;
20
21using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::JoystickAxis;
23using ::aos::input::driver_station::ControlBit;
24
25namespace frc971 {
26namespace input {
27namespace joysticks {
28
29const ButtonLocation kDriveControlLoopEnable1(1, 7),
30 kDriveControlLoopEnable2(1, 11);
31const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
32const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
33const ButtonLocation kQuickTurn(1, 5);
Austin Schuh58d23682014-02-23 01:39:50 -080034
Austin Schuh06cbbf12014-02-22 02:07:31 -080035const ButtonLocation kFire(3, 11);
Austin Schuh9cb836e2014-02-23 19:25:55 -080036const ButtonLocation kUnload(2, 11);
37const ButtonLocation kReload(2, 6);
Austin Schuh58d23682014-02-23 01:39:50 -080038
39const ButtonLocation kRollersOut(3, 12);
40const ButtonLocation kRollersIn(3, 10);
41
42const ButtonLocation kTuck(3, 8);
Austin Schuh9cb836e2014-02-23 19:25:55 -080043const ButtonLocation kIntakeOpenPosition(3, 9);
Austin Schuh58d23682014-02-23 01:39:50 -080044const ButtonLocation kIntakePosition(3, 7);
45
46const ButtonLocation kLongShot(3, 5);
47const ButtonLocation kMediumShot(3, 3);
48const ButtonLocation kShortShot(3, 6);
49
50struct ClawGoal {
51 double angle;
52 double separation;
53};
54
55const ClawGoal kTuckGoal = {-2.273474, -0.749484};
56const ClawGoal kIntakeGoal = {-2.273474, 0.0};
Austin Schuh9cb836e2014-02-23 19:25:55 -080057const ClawGoal kIntakeOpenGoal = {-2.0, 1.2};
Austin Schuh58d23682014-02-23 01:39:50 -080058
Austin Schuh9cb836e2014-02-23 19:25:55 -080059const ClawGoal kLongShotGoal = {-M_PI / 2.0 + 0.43, 0.10};
60const ClawGoal kMediumShotGoal = {-0.9, 0.10};
61const ClawGoal kShortShotGoal = {-0.04, 0.11};
Brian Silverman756f9ff2014-01-17 23:40:23 -080062
63class Reader : public ::aos::input::JoystickInput {
64 public:
Austin Schuh58d23682014-02-23 01:39:50 -080065 Reader()
66 : is_high_gear_(false),
Austin Schuh9cb836e2014-02-23 19:25:55 -080067 shot_power_(80.0),
Austin Schuh58d23682014-02-23 01:39:50 -080068 goal_angle_(0.0),
69 separation_angle_(0.0) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -080070
71 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080072
73 if (data.GetControlBit(ControlBit::kAutonomous)) {
74 if (data.PosEdge(ControlBit::kEnabled)){
75 LOG(INFO, "Starting auto mode\n");
76 ::frc971::autonomous::autonomous.MakeWithBuilder()
77 .run_auto(true).Send();
78 } else if (data.NegEdge(ControlBit::kEnabled)) {
79 LOG(INFO, "Stopping auto mode\n");
80 ::frc971::autonomous::autonomous.MakeWithBuilder()
81 .run_auto(false).Send();
82 }
Austin Schuh58d23682014-02-23 01:39:50 -080083 } else {
84 HandleTeleop(data);
Brian Silverman756f9ff2014-01-17 23:40:23 -080085 }
86 }
Austin Schuh58d23682014-02-23 01:39:50 -080087
88 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
89 bool is_control_loop_driving = false;
90 double left_goal = 0.0;
91 double right_goal = 0.0;
92 const double wheel = -data.GetAxis(kSteeringWheel);
93 const double throttle = -data.GetAxis(kDriveThrottle);
94 const double kThrottleGain = 1.0 / 2.5;
95 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
96 data.IsPressed(kDriveControlLoopEnable2))) {
97 // TODO(austin): Static sucks!
98 static double distance = 0.0;
99 static double angle = 0.0;
100 static double filtered_goal_distance = 0.0;
101 if (data.PosEdge(kDriveControlLoopEnable1) ||
102 data.PosEdge(kDriveControlLoopEnable2)) {
103 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
104 distance = (drivetrain.position->left_encoder +
105 drivetrain.position->right_encoder) /
106 2.0 -
107 throttle * kThrottleGain / 2.0;
108 angle = gyro->angle;
109 filtered_goal_distance = distance;
110 }
111 }
112 is_control_loop_driving = true;
113
114 // const double gyro_angle = Gyro.View().angle;
115 const double goal_theta = angle - wheel * 0.27;
116 const double goal_distance = distance + throttle * kThrottleGain;
117 const double robot_width = 22.0 / 100.0 * 2.54;
118 const double kMaxVelocity = 0.6;
119 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
120 filtered_goal_distance += kMaxVelocity * 0.02;
121 } else if (goal_distance <
122 -kMaxVelocity * 0.02 + filtered_goal_distance) {
123 filtered_goal_distance -= kMaxVelocity * 0.02;
124 } else {
125 filtered_goal_distance = goal_distance;
126 }
127 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
128 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
129 is_high_gear_ = false;
130
131 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
132 }
133 if (!drivetrain.goal.MakeWithBuilder()
134 .steering(wheel)
135 .throttle(throttle)
136 .highgear(is_high_gear_)
137 .quickturn(data.IsPressed(kQuickTurn))
138 .control_loop_driving(is_control_loop_driving)
139 .left_goal(left_goal)
140 .right_goal(right_goal)
141 .Send()) {
142 LOG(WARNING, "sending stick values failed\n");
143 }
144 if (data.PosEdge(kShiftHigh)) {
145 is_high_gear_ = false;
146 }
147 if (data.PosEdge(kShiftLow)) {
148 is_high_gear_ = true;
149 }
150 }
151
152 void SetGoal(ClawGoal goal) {
153 goal_angle_ = goal.angle;
154 separation_angle_ = goal.separation;
155 }
156
Ben Fredricksonaa450452014-03-01 09:41:18 +0000157 // cycle throught all known actions and if they are running
158 // cancel them all.
159 // TODO(ben): make this more generic and s.t. we can only
160 // cancel if we need those exact resources (ie claw shooter drivetrain)
161 void CancelAllActions() {
162 frc971::actions::shoot_action.status
163 .FetchLatest();
164 while (::frc971::actions::shoot_action.status->running) {
165 ::frc971::actions::shoot_action.goal.MakeWithBuilder().run(false);
166 ::frc971::actions::shoot_action.status.FetchLatest();
167 }
168 }
169
Austin Schuh58d23682014-02-23 01:39:50 -0800170 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
171 HandleDrivetrain(data);
172
Austin Schuh9cb836e2014-02-23 19:25:55 -0800173 if (data.IsPressed(kIntakeOpenPosition)) {
Ben Fredricksonaa450452014-03-01 09:41:18 +0000174 CancelAllActions();
Austin Schuh9cb836e2014-02-23 19:25:55 -0800175 SetGoal(kIntakeOpenGoal);
176 } else if (data.IsPressed(kIntakePosition)) {
Ben Fredricksonaa450452014-03-01 09:41:18 +0000177 CancelAllActions();
Austin Schuh58d23682014-02-23 01:39:50 -0800178 SetGoal(kIntakeGoal);
179 } else if (data.IsPressed(kTuck)) {
Ben Fredricksonaa450452014-03-01 09:41:18 +0000180 CancelAllActions();
Austin Schuh58d23682014-02-23 01:39:50 -0800181 SetGoal(kTuckGoal);
182 }
183
Ben Fredricksonaa450452014-03-01 09:41:18 +0000184 if (data.PosEdge(kLongShot)) {
185 CancelAllActions();
186 ::frc971::actions::shoot_action.goal.MakeWithBuilder().run(true)
187 .shot_power(160.0).shot_angle(kLongShotGoal.angle).Send();
Austin Schuh58d23682014-02-23 01:39:50 -0800188 } else if (data.IsPressed(kMediumShot)) {
Ben Fredricksonaa450452014-03-01 09:41:18 +0000189 CancelAllActions();
190 ::frc971::actions::shoot_action.goal.MakeWithBuilder().run(true)
191 .shot_power(100.0).shot_angle(kMediumShotGoal.angle).Send();
Austin Schuh58d23682014-02-23 01:39:50 -0800192 } else if (data.IsPressed(kShortShot)) {
Ben Fredricksonaa450452014-03-01 09:41:18 +0000193 CancelAllActions();
194 ::frc971::actions::shoot_action.goal.MakeWithBuilder().run(true)
195 .shot_power(70.0).shot_angle(kShortShotGoal.angle).Send();
Austin Schuh58d23682014-02-23 01:39:50 -0800196 }
197 }
198
Austin Schuh01c652b2014-02-21 23:13:42 -0800199 private:
Austin Schuh58d23682014-02-23 01:39:50 -0800200 bool is_high_gear_;
201 double shot_power_;
202 double goal_angle_;
203 double separation_angle_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800204};
205
206} // namespace joysticks
207} // namespace input
208} // namespace frc971
209
210int main() {
211 ::aos::Init();
212 ::frc971::input::joysticks::Reader reader;
213 reader.Run();
214 ::aos::Cleanup();
215}