blob: d751a43314a5feb77e006af3cf0ed7857fbaf86c [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 Schuh01c652b2014-02-21 23:13:42 -080033const ButtonLocation kClawClosed(3, 7);
34const ButtonLocation kClawOpen(3, 9);
Austin Schuh06cbbf12014-02-22 02:07:31 -080035const ButtonLocation kFire(3, 11);
36const ButtonLocation kUnload(3, 12);
Brian Silverman756f9ff2014-01-17 23:40:23 -080037
38class Reader : public ::aos::input::JoystickInput {
39 public:
Austin Schuh01c652b2014-02-21 23:13:42 -080040 Reader() : closed_(true) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -080041
42 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
43 static bool is_high_gear = false;
44
45 if (data.GetControlBit(ControlBit::kAutonomous)) {
46 if (data.PosEdge(ControlBit::kEnabled)){
47 LOG(INFO, "Starting auto mode\n");
48 ::frc971::autonomous::autonomous.MakeWithBuilder()
49 .run_auto(true).Send();
50 } else if (data.NegEdge(ControlBit::kEnabled)) {
51 LOG(INFO, "Stopping auto mode\n");
52 ::frc971::autonomous::autonomous.MakeWithBuilder()
53 .run_auto(false).Send();
54 }
55 } else { // teleop
56 bool is_control_loop_driving = false;
57 double left_goal = 0.0;
58 double right_goal = 0.0;
59 const double wheel = -data.GetAxis(kSteeringWheel);
60 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman756f9ff2014-01-17 23:40:23 -080061 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanfac5c292014-02-17 15:26:57 -080062 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
63 data.IsPressed(kDriveControlLoopEnable2))) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080064 static double distance = 0.0;
65 static double angle = 0.0;
66 static double filtered_goal_distance = 0.0;
67 if (data.PosEdge(kDriveControlLoopEnable1) ||
68 data.PosEdge(kDriveControlLoopEnable2)) {
69 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
70 distance = (drivetrain.position->left_encoder +
71 drivetrain.position->right_encoder) / 2.0
72 - throttle * kThrottleGain / 2.0;
73 angle = gyro->angle;
74 filtered_goal_distance = distance;
75 }
76 }
77 is_control_loop_driving = true;
78
79 //const double gyro_angle = Gyro.View().angle;
80 const double goal_theta = angle - wheel * 0.27;
81 const double goal_distance = distance + throttle * kThrottleGain;
82 const double robot_width = 22.0 / 100.0 * 2.54;
83 const double kMaxVelocity = 0.6;
84 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
85 filtered_goal_distance += kMaxVelocity * 0.02;
86 } else if (goal_distance < -kMaxVelocity * 0.02 +
87 filtered_goal_distance) {
88 filtered_goal_distance -= kMaxVelocity * 0.02;
89 } else {
90 filtered_goal_distance = goal_distance;
91 }
92 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
93 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
94 is_high_gear = false;
95
96 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
97 }
Brian Silvermanfac5c292014-02-17 15:26:57 -080098 if (!drivetrain.goal.MakeWithBuilder()
99 .steering(wheel)
100 .throttle(throttle)
101 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
102 .control_loop_driving(is_control_loop_driving)
103 .left_goal(left_goal).right_goal(right_goal).Send()) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800104 LOG(WARNING, "sending stick values failed\n");
105 }
106
107 if (data.PosEdge(kShiftHigh)) {
108 is_high_gear = false;
109 }
110 if (data.PosEdge(kShiftLow)) {
111 is_high_gear = true;
112 }
Austin Schuh01c652b2014-02-21 23:13:42 -0800113 if (data.PosEdge(kClawClosed)) {
114 closed_ = true;
115 }
116 if (data.PosEdge(kClawOpen)) {
117 closed_ = false;
118 }
Brian Silvermanfac5c292014-02-17 15:26:57 -0800119
Austin Schuh01c652b2014-02-21 23:13:42 -0800120 double separation_angle = closed_ ? 0.0 : 0.5;
121 double goal_angle = closed_ ? 0.0 : -0.2;
Brian Silvermanfac5c292014-02-17 15:26:57 -0800122 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh06cbbf12014-02-22 02:07:31 -0800123 .bottom_angle(goal_angle)
124 .separation_angle(separation_angle)
125 .intake(false)
126 .Send()) {
Brian Silvermanfac5c292014-02-17 15:26:57 -0800127 LOG(WARNING, "sending claw goal failed\n");
128 }
129 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
Austin Schuh06cbbf12014-02-22 02:07:31 -0800130 .shot_power(0.25)
131 .shot_requested(data.IsPressed(kFire))
132 .unload_requested(data.IsPressed(kUnload))
133 .Send()) {
Brian Silvermanfac5c292014-02-17 15:26:57 -0800134 LOG(WARNING, "sending shooter goal failed\n");
135 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800136 }
137 }
Austin Schuh01c652b2014-02-21 23:13:42 -0800138
139 private:
140 bool closed_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800141};
142
143} // namespace joysticks
144} // namespace input
145} // namespace frc971
146
147int main() {
148 ::aos::Init();
149 ::frc971::input::joysticks::Reader reader;
150 reader.Run();
151 ::aos::Cleanup();
152}