blob: 8dfec5a3b0eec51f9736b1c8a29928e1b30a387f [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"
8#include "aos/common/logging/logging.h"
9
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/queues/gyro_angle.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080012#include "frc971/autonomous/auto.q.h"
Brian Silvermanfac5c292014-02-17 15:26:57 -080013#include "frc971/control_loops/claw/claw.q.h"
14#include "frc971/control_loops/shooter/shooter.q.h"
Brian Silverman756f9ff2014-01-17 23:40:23 -080015
16using ::frc971::control_loops::drivetrain;
Brian Silverman756f9ff2014-01-17 23:40:23 -080017using ::frc971::sensors::gyro;
18
19using ::aos::input::driver_station::ButtonLocation;
20using ::aos::input::driver_station::JoystickAxis;
21using ::aos::input::driver_station::ControlBit;
22
23namespace frc971 {
24namespace input {
25namespace joysticks {
26
27const ButtonLocation kDriveControlLoopEnable1(1, 7),
28 kDriveControlLoopEnable2(1, 11);
29const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
30const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
31const ButtonLocation kQuickTurn(1, 5);
Austin Schuh01c652b2014-02-21 23:13:42 -080032const ButtonLocation kClawClosed(3, 7);
33const ButtonLocation kClawOpen(3, 9);
Brian Silverman756f9ff2014-01-17 23:40:23 -080034
35class Reader : public ::aos::input::JoystickInput {
36 public:
Austin Schuh01c652b2014-02-21 23:13:42 -080037 Reader() : closed_(true) {}
Brian Silverman756f9ff2014-01-17 23:40:23 -080038
39 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
40 static bool is_high_gear = false;
41
42 if (data.GetControlBit(ControlBit::kAutonomous)) {
43 if (data.PosEdge(ControlBit::kEnabled)){
44 LOG(INFO, "Starting auto mode\n");
45 ::frc971::autonomous::autonomous.MakeWithBuilder()
46 .run_auto(true).Send();
47 } else if (data.NegEdge(ControlBit::kEnabled)) {
48 LOG(INFO, "Stopping auto mode\n");
49 ::frc971::autonomous::autonomous.MakeWithBuilder()
50 .run_auto(false).Send();
51 }
52 } else { // teleop
53 bool is_control_loop_driving = false;
54 double left_goal = 0.0;
55 double right_goal = 0.0;
56 const double wheel = -data.GetAxis(kSteeringWheel);
57 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman756f9ff2014-01-17 23:40:23 -080058 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanfac5c292014-02-17 15:26:57 -080059 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
60 data.IsPressed(kDriveControlLoopEnable2))) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080061 static double distance = 0.0;
62 static double angle = 0.0;
63 static double filtered_goal_distance = 0.0;
64 if (data.PosEdge(kDriveControlLoopEnable1) ||
65 data.PosEdge(kDriveControlLoopEnable2)) {
66 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
67 distance = (drivetrain.position->left_encoder +
68 drivetrain.position->right_encoder) / 2.0
69 - throttle * kThrottleGain / 2.0;
70 angle = gyro->angle;
71 filtered_goal_distance = distance;
72 }
73 }
74 is_control_loop_driving = true;
75
76 //const double gyro_angle = Gyro.View().angle;
77 const double goal_theta = angle - wheel * 0.27;
78 const double goal_distance = distance + throttle * kThrottleGain;
79 const double robot_width = 22.0 / 100.0 * 2.54;
80 const double kMaxVelocity = 0.6;
81 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
82 filtered_goal_distance += kMaxVelocity * 0.02;
83 } else if (goal_distance < -kMaxVelocity * 0.02 +
84 filtered_goal_distance) {
85 filtered_goal_distance -= kMaxVelocity * 0.02;
86 } else {
87 filtered_goal_distance = goal_distance;
88 }
89 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
90 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
91 is_high_gear = false;
92
93 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
94 }
Brian Silvermanfac5c292014-02-17 15:26:57 -080095 if (!drivetrain.goal.MakeWithBuilder()
96 .steering(wheel)
97 .throttle(throttle)
98 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
99 .control_loop_driving(is_control_loop_driving)
100 .left_goal(left_goal).right_goal(right_goal).Send()) {
Brian Silverman756f9ff2014-01-17 23:40:23 -0800101 LOG(WARNING, "sending stick values failed\n");
102 }
103
104 if (data.PosEdge(kShiftHigh)) {
105 is_high_gear = false;
106 }
107 if (data.PosEdge(kShiftLow)) {
108 is_high_gear = true;
109 }
Austin Schuh01c652b2014-02-21 23:13:42 -0800110 if (data.PosEdge(kClawClosed)) {
111 closed_ = true;
112 }
113 if (data.PosEdge(kClawOpen)) {
114 closed_ = false;
115 }
Brian Silvermanfac5c292014-02-17 15:26:57 -0800116
Austin Schuh01c652b2014-02-21 23:13:42 -0800117 double separation_angle = closed_ ? 0.0 : 0.5;
118 double goal_angle = closed_ ? 0.0 : -0.2;
Brian Silvermanfac5c292014-02-17 15:26:57 -0800119 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuh01c652b2014-02-21 23:13:42 -0800120 .bottom_angle(goal_angle)
121 .separation_angle(separation_angle)
Brian Silvermanfac5c292014-02-17 15:26:57 -0800122 .intake(false).Send()) {
123 LOG(WARNING, "sending claw goal failed\n");
124 }
125 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
126 .shot_power(0)
127 .shot_requested(false)
128 .unload_requested(true)
129 .Send()) {
130 LOG(WARNING, "sending shooter goal failed\n");
131 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800132 }
133 }
Austin Schuh01c652b2014-02-21 23:13:42 -0800134
135 private:
136 bool closed_;
Brian Silverman756f9ff2014-01-17 23:40:23 -0800137};
138
139} // namespace joysticks
140} // namespace input
141} // namespace frc971
142
143int main() {
144 ::aos::Init();
145 ::frc971::input::joysticks::Reader reader;
146 reader.Run();
147 ::aos::Cleanup();
148}