blob: 115030446f519790b67eaa2940ecd906df80e263 [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);
32
33class Reader : public ::aos::input::JoystickInput {
34 public:
Brian Silverman61e41fd2014-02-16 19:08:50 -080035 Reader() {}
Brian Silverman756f9ff2014-01-17 23:40:23 -080036
37 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
38 static bool is_high_gear = false;
39
40 if (data.GetControlBit(ControlBit::kAutonomous)) {
41 if (data.PosEdge(ControlBit::kEnabled)){
42 LOG(INFO, "Starting auto mode\n");
43 ::frc971::autonomous::autonomous.MakeWithBuilder()
44 .run_auto(true).Send();
45 } else if (data.NegEdge(ControlBit::kEnabled)) {
46 LOG(INFO, "Stopping auto mode\n");
47 ::frc971::autonomous::autonomous.MakeWithBuilder()
48 .run_auto(false).Send();
49 }
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 = -data.GetAxis(kSteeringWheel);
55 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman756f9ff2014-01-17 23:40:23 -080056 const double kThrottleGain = 1.0 / 2.5;
Brian Silvermanfac5c292014-02-17 15:26:57 -080057 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
58 data.IsPressed(kDriveControlLoopEnable2))) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080059 static double distance = 0.0;
60 static double angle = 0.0;
61 static double filtered_goal_distance = 0.0;
62 if (data.PosEdge(kDriveControlLoopEnable1) ||
63 data.PosEdge(kDriveControlLoopEnable2)) {
64 if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) {
65 distance = (drivetrain.position->left_encoder +
66 drivetrain.position->right_encoder) / 2.0
67 - throttle * kThrottleGain / 2.0;
68 angle = gyro->angle;
69 filtered_goal_distance = distance;
70 }
71 }
72 is_control_loop_driving = true;
73
74 //const double gyro_angle = Gyro.View().angle;
75 const double goal_theta = angle - wheel * 0.27;
76 const double goal_distance = distance + throttle * kThrottleGain;
77 const double robot_width = 22.0 / 100.0 * 2.54;
78 const double kMaxVelocity = 0.6;
79 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
80 filtered_goal_distance += kMaxVelocity * 0.02;
81 } else if (goal_distance < -kMaxVelocity * 0.02 +
82 filtered_goal_distance) {
83 filtered_goal_distance -= kMaxVelocity * 0.02;
84 } else {
85 filtered_goal_distance = goal_distance;
86 }
87 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
88 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
89 is_high_gear = false;
90
91 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
92 }
Brian Silvermanfac5c292014-02-17 15:26:57 -080093 if (!drivetrain.goal.MakeWithBuilder()
94 .steering(wheel)
95 .throttle(throttle)
96 .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn))
97 .control_loop_driving(is_control_loop_driving)
98 .left_goal(left_goal).right_goal(right_goal).Send()) {
Brian Silverman756f9ff2014-01-17 23:40:23 -080099 LOG(WARNING, "sending stick values failed\n");
100 }
101
102 if (data.PosEdge(kShiftHigh)) {
103 is_high_gear = false;
104 }
105 if (data.PosEdge(kShiftLow)) {
106 is_high_gear = true;
107 }
Brian Silvermanfac5c292014-02-17 15:26:57 -0800108
109 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
110 .bottom_angle(0)
111 .separation_angle(0)
112 .intake(false).Send()) {
113 LOG(WARNING, "sending claw goal failed\n");
114 }
115 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
116 .shot_power(0)
117 .shot_requested(false)
118 .unload_requested(true)
119 .Send()) {
120 LOG(WARNING, "sending shooter goal failed\n");
121 }
Brian Silverman756f9ff2014-01-17 23:40:23 -0800122 }
123 }
124};
125
126} // namespace joysticks
127} // namespace input
128} // namespace frc971
129
130int main() {
131 ::aos::Init();
132 ::frc971::input::joysticks::Reader reader;
133 reader.Run();
134 ::aos::Cleanup();
135}