blob: 71f4ff2f8ffd0870f64e28fbc004a15753c80b4c [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#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"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000015#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000016#include "frc971/queues/gyro.q.h"
17#include "frc971/autonomous/auto.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000018
19using ::frc971::control_loops::drivetrain_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000020
21using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::JoystickAxis;
23using ::aos::input::driver_station::ControlBit;
24
Comran Morshed6c6a0a92016-01-17 12:45:16 +000025namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000026namespace input {
27namespace joysticks {
28
Comran Morshed9a9948c2016-01-16 15:58:04 +000029const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080030const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000031const ButtonLocation kQuickTurn(1, 5);
32
Comran Morshed9a9948c2016-01-16 15:58:04 +000033class Reader : public ::aos::input::JoystickInput {
34 public:
35 Reader()
36 : is_high_gear_(false),
Comran Morshed9a9948c2016-01-16 15:58:04 +000037 was_running_(false) {}
38
39 void RunIteration(const ::aos::input::driver_station::Data &data) override {
40 bool last_auto_running = auto_running_;
41 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
42 data.GetControlBit(ControlBit::kEnabled);
43 if (auto_running_ != last_auto_running) {
44 if (auto_running_) {
45 StartAuto();
46 } else {
47 StopAuto();
48 }
49 }
50
51 if (!data.GetControlBit(ControlBit::kAutonomous)) {
52 HandleDrivetrain(data);
53 HandleTeleop(data);
54 }
55 }
56
57 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
58 bool is_control_loop_driving = false;
59 double left_goal = 0.0;
60 double right_goal = 0.0;
61 const double wheel = -data.GetAxis(kSteeringWheel);
62 const double throttle = -data.GetAxis(kDriveThrottle);
Comran Morshed9a9948c2016-01-16 15:58:04 +000063
Comran Morshed9a9948c2016-01-16 15:58:04 +000064 if (!drivetrain_queue.goal.MakeWithBuilder()
65 .steering(wheel)
66 .throttle(throttle)
67 .highgear(is_high_gear_)
68 .quickturn(data.IsPressed(kQuickTurn))
69 .control_loop_driving(is_control_loop_driving)
70 .left_goal(left_goal)
71 .right_goal(right_goal)
72 .left_velocity_goal(0)
73 .right_velocity_goal(0)
74 .Send()) {
75 LOG(WARNING, "sending stick values failed\n");
76 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +000077
Campbell Crowley5b27f022016-02-20 16:55:35 -080078 if (data.PosEdge(kShiftLow)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +000079 is_high_gear_ = false;
80 }
Comran Morshed6c6a0a92016-01-17 12:45:16 +000081
Campbell Crowley5b27f022016-02-20 16:55:35 -080082 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
Comran Morshed9a9948c2016-01-16 15:58:04 +000083 is_high_gear_ = true;
84 }
85 }
86
Comran Morshed9a9948c2016-01-16 15:58:04 +000087 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
88 if (!data.GetControlBit(ControlBit::kEnabled)) {
89 action_queue_.CancelAllActions();
Comran Morshed6c6a0a92016-01-17 12:45:16 +000090 LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +000091 }
92
Comran Morshed9a9948c2016-01-16 15:58:04 +000093 was_running_ = action_queue_.Running();
94 }
95
Comran Morshed9a9948c2016-01-16 15:58:04 +000096 private:
97 void StartAuto() {
98 LOG(INFO, "Starting auto mode\n");
99 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
100 }
101
102 void StopAuto() {
103 LOG(INFO, "Stopping auto mode\n");
104 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
105 }
106
107 bool is_high_gear_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108 bool was_running_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109 bool auto_running_ = false;
110
111 ::aos::common::actions::ActionQueue action_queue_;
112
113 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
114 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
115 "no drivetrain status");
116};
117
118} // namespace joysticks
119} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000120} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000121
122int main() {
123 ::aos::Init(-1);
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000124 ::y2016::input::joysticks::Reader reader;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000125 reader.Run();
126 ::aos::Cleanup();
127}