blob: e7252f3d4d553f6c14656a33b988657059679ea0 [file] [log] [blame]
Austin Schuhea9d6022017-01-02 13:38:07 -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/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#include "aos/common/actions/actions.h"
13
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
15
16#include "frc971/queues/gyro.q.h"
17#include "y2016_bot4/control_loops/drivetrain/drivetrain_base.h"
18
19using ::frc971::control_loops::drivetrain_queue;
20
21using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::ControlBit;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::POVLocation;
25
26namespace y2016_bot4 {
27namespace input {
28namespace joysticks {
29
30//#define XBOX
31
32#ifdef XBOX
33// Xbox
34const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2);
35const ButtonLocation kQuickTurn(1, 5);
36#else
37// Steering wheel
38const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
39const ButtonLocation kQuickTurn(1, 5);
40#endif
41
42const ButtonLocation kTurn1(1, 7);
43const ButtonLocation kTurn2(1, 11);
44
45class Reader : public ::aos::input::JoystickInput {
46 public:
47 Reader() {}
48
49 void RunIteration(const ::aos::input::driver_station::Data &data) override {
50 const bool auto_running = data.GetControlBit(ControlBit::kAutonomous) &&
51 data.GetControlBit(ControlBit::kEnabled);
52
53 if (!auto_running) {
54 HandleDrivetrain(data);
55 } else {
56 drivetrain_queue.goal.MakeWithBuilder()
57 .steering(0.0)
58 .throttle(0.0)
59 .quickturn(false)
60 .control_loop_driving(false)
61 .left_goal(0.0)
62 .right_goal(0.0)
63 .left_velocity_goal(0)
64 .right_velocity_goal(0)
65 .Send();
66 }
67 }
68
69 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
70 bool is_control_loop_driving = false;
71 const double wheel = -data.GetAxis(kSteeringWheel);
72 const double throttle = -data.GetAxis(kDriveThrottle);
73 drivetrain_queue.status.FetchLatest();
74
75 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
76 if (drivetrain_queue.status.get()) {
77 left_goal_ = drivetrain_queue.status->estimated_left_position;
78 right_goal_ = drivetrain_queue.status->estimated_right_position;
79 }
80 }
81 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
82 is_control_loop_driving = true;
83 }
84 if (!drivetrain_queue.goal.MakeWithBuilder()
85 .steering(wheel)
86 .throttle(throttle)
87 .quickturn(data.IsPressed(kQuickTurn))
88 .control_loop_driving(is_control_loop_driving)
89 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
90 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
91 .left_velocity_goal(0)
92 .right_velocity_goal(0)
93 .Send()) {
94 LOG(WARNING, "sending stick values failed\n");
95 }
96 }
97
98 private:
99 double left_goal_ = 0.0;
100 double right_goal_ = 0.0;
101};
102
103} // namespace joysticks
104} // namespace input
105} // namespace y2016_bot4
106
107int main() {
108 ::aos::Init(-1);
109 ::y2016_bot4::input::joysticks::Reader reader;
110 reader.Run();
111 ::aos::Cleanup();
112}