blob: b8ac058a8934a34b5ad8321991a5ca69a3c1e2f7 [file] [log] [blame]
Daniel Pettiaece37f2014-10-25 17:13:44 -07001#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/input/driver_station_data.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/common/time.h"
12
Daniel Petti2a85f712014-11-14 12:44:29 -080013#include "bot3/control_loops/drivetrain/drivetrain.q.h"
14#include "bot3/control_loops/drivetrain/drivetrain_constants.h"
15#include "bot3/control_loops/rollers/rollers.q.h"
Daniel Pettiaece37f2014-10-25 17:13:44 -070016#include "frc971/autonomous/auto.q.h"
Daniel Petti2a85f712014-11-14 12:44:29 -080017#include "frc971/queues/other_sensors.q.h"
Daniel Pettiaece37f2014-10-25 17:13:44 -070018
Daniel Petti2a85f712014-11-14 12:44:29 -080019using ::bot3::control_loops::drivetrain;
Daniel Pettiaece37f2014-10-25 17:13:44 -070020using ::frc971::sensors::gyro_reading;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::ControlBit;
25
Daniel Petti2a85f712014-11-14 12:44:29 -080026namespace bot3 {
Daniel Pettiaece37f2014-10-25 17:13:44 -070027namespace input {
28namespace joysticks {
29
30const ButtonLocation kDriveControlLoopEnable1(1, 7),
31 kDriveControlLoopEnable2(1, 11);
Daniel Petti2a85f712014-11-14 12:44:29 -080032const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(4, 2);
33const ButtonLocation kShiftHigh(4, 1), kShiftLow(4, 3);
Daniel Pettiaece37f2014-10-25 17:13:44 -070034const ButtonLocation kQuickTurn(1, 5);
35
Daniel Petti2a85f712014-11-14 12:44:29 -080036const ButtonLocation kFrontRollersIn(3, 5);
37const ButtonLocation kBackRollersIn(3, 3);
38const ButtonLocation kFrontRollersOut(3, 12);
39const ButtonLocation kBackRollersOut(3, 8);
40const ButtonLocation kHumanPlayer(3, 11);
Daniel Pettiaece37f2014-10-25 17:13:44 -070041
42class Reader : public ::aos::input::JoystickInput {
43 public:
44 Reader()
Daniel Petti2a85f712014-11-14 12:44:29 -080045 : is_high_gear_(false) {}
Daniel Pettiaece37f2014-10-25 17:13:44 -070046
47 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
48 if (data.GetControlBit(ControlBit::kAutonomous)) {
49 if (data.PosEdge(ControlBit::kEnabled)){
50 LOG(INFO, "Starting auto mode\n");
51 ::frc971::autonomous::autonomous.MakeWithBuilder()
52 .run_auto(true)
53 .Send();
54 } else if (data.NegEdge(ControlBit::kEnabled)) {
55 LOG(INFO, "Stopping auto mode\n");
56 ::frc971::autonomous::autonomous.MakeWithBuilder()
57 .run_auto(false)
58 .Send();
59 } else if (!data.GetControlBit(ControlBit::kEnabled)) {
Daniel Petti2a85f712014-11-14 12:44:29 -080060 auto goal = drivetrain.goal.MakeMessage();
61 goal->Zero();
62 goal->control_loop_driving = false;
63 goal->left_goal = goal->right_goal = 0;
64 goal->left_velocity_goal = goal->right_velocity_goal = 0;
65 if (!goal.Send()) {
66 LOG(WARNING, "sending 0 drivetrain goal failed\n");
Daniel Pettiaece37f2014-10-25 17:13:44 -070067 }
68 }
69 } else {
70 HandleTeleop(data);
71 }
72 }
73
74 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
75 bool is_control_loop_driving = false;
76 double left_goal = 0.0;
77 double right_goal = 0.0;
78 const double wheel = -data.GetAxis(kSteeringWheel);
79 const double throttle = -data.GetAxis(kDriveThrottle);
80 const double kThrottleGain = 1.0 / 2.5;
81 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
82 data.IsPressed(kDriveControlLoopEnable2))) {
Daniel Pettiaece37f2014-10-25 17:13:44 -070083 static double distance = 0.0;
84 static double angle = 0.0;
85 static double filtered_goal_distance = 0.0;
86 if (data.PosEdge(kDriveControlLoopEnable1) ||
87 data.PosEdge(kDriveControlLoopEnable2)) {
88 if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) {
89 distance = (drivetrain.position->left_encoder +
90 drivetrain.position->right_encoder) /
91 2.0 -
92 throttle * kThrottleGain / 2.0;
93 angle = gyro_reading->angle;
94 filtered_goal_distance = distance;
95 }
96 }
97 is_control_loop_driving = true;
98
99 // const double gyro_angle = Gyro.View().angle;
100 const double goal_theta = angle - wheel * 0.27;
101 const double goal_distance = distance + throttle * kThrottleGain;
102 const double robot_width = 22.0 / 100.0 * 2.54;
103 const double kMaxVelocity = 0.6;
104 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
105 filtered_goal_distance += kMaxVelocity * 0.02;
106 } else if (goal_distance <
107 -kMaxVelocity * 0.02 + filtered_goal_distance) {
108 filtered_goal_distance -= kMaxVelocity * 0.02;
109 } else {
110 filtered_goal_distance = goal_distance;
111 }
112 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
113 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
114 is_high_gear_ = false;
115
116 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
117 }
118 if (!drivetrain.goal.MakeWithBuilder()
119 .steering(wheel)
120 .throttle(throttle)
121 .highgear(is_high_gear_)
122 .quickturn(data.IsPressed(kQuickTurn))
123 .control_loop_driving(is_control_loop_driving)
124 .left_goal(left_goal)
125 .right_goal(right_goal)
126 .left_velocity_goal(0)
127 .right_velocity_goal(0)
128 .Send()) {
129 LOG(WARNING, "sending stick values failed\n");
130 }
131 if (data.PosEdge(kShiftHigh)) {
132 is_high_gear_ = false;
133 }
134 if (data.PosEdge(kShiftLow)) {
135 is_high_gear_ = true;
136 }
137 }
138
Daniel Pettiaece37f2014-10-25 17:13:44 -0700139 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
140 HandleDrivetrain(data);
Daniel Petti2a85f712014-11-14 12:44:29 -0800141
142 // Rollers.
143 auto rollers_goal = control_loops::rollers.goal.MakeMessage();
144 if (data.IsPressed(kFrontRollersIn)) {
145 rollers_goal->intake = 1;
146 } else if (data.IsPressed(kFrontRollersOut)) {
147 rollers_goal->low_spit = 1;
148 } else if (data.IsPressed(kBackRollersIn)) {
149 rollers_goal->intake = -1;
150 } else if (data.IsPressed(kBackRollersOut)) {
151 rollers_goal->low_spit = -1;
152 } else if (data.IsPressed(kHumanPlayer)) {
153 rollers_goal->human_player = true;
Daniel Pettiaece37f2014-10-25 17:13:44 -0700154 }
Daniel Petti2a85f712014-11-14 12:44:29 -0800155 if (!rollers_goal.Send()) {
156 LOG(WARNING, "Sending rollers values failed.\n");
Daniel Pettiaece37f2014-10-25 17:13:44 -0700157 }
Daniel Pettiaece37f2014-10-25 17:13:44 -0700158 }
159
160 private:
161 bool is_high_gear_;
Daniel Pettiaece37f2014-10-25 17:13:44 -0700162};
163
164} // namespace joysticks
165} // namespace input
Daniel Petti2a85f712014-11-14 12:44:29 -0800166} // namespace bot3
Daniel Pettiaece37f2014-10-25 17:13:44 -0700167
168int main() {
169 ::aos::Init();
Daniel Petti2a85f712014-11-14 12:44:29 -0800170 ::bot3::input::joysticks::Reader reader;
Daniel Pettiaece37f2014-10-25 17:13:44 -0700171 reader.Run();
172 ::aos::Cleanup();
173}