blob: 7b6d9503f3d748c504465dbe966c25cbf3209a50 [file] [log] [blame]
Comran Morshed41ed7c22015-11-04 21:03:37 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
Brian Silvermanc2065732015-11-28 22:55:30 +00007#include "aos/input/joystick_input.h"
John Park33858a32018-09-28 23:05:48 -07008#include "aos/input/driver_station_data.h"
9#include "aos/logging/logging.h"
10#include "aos/util/log_interval.h"
11#include "aos/time/time.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000012
Austin Schuh0b560152019-01-04 17:02:27 -080013#include "aos/input/drivetrain_input.h"
14#include "frc971/autonomous/auto.q.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
Adam Snaider83eae562016-09-10 16:47:33 -070016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh0b560152019-01-04 17:02:27 -080017#include "frc971/queues/gyro.q.h"
18#include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000019#include "y2014_bot3/control_loops/rollers/rollers.q.h"
20
Adam Snaider83eae562016-09-10 16:47:33 -070021using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000022
23using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::POVLocation;
25using ::aos::input::driver_station::JoystickAxis;
26using ::aos::input::driver_station::ControlBit;
Austin Schuh0b560152019-01-04 17:02:27 -080027using ::aos::input::DrivetrainInputReader;
Comran Morshed41ed7c22015-11-04 21:03:37 +000028
29namespace y2014_bot3 {
30namespace input {
31namespace joysticks {
32
33// Joystick & button addresses.
34const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Adam Snaider83eae562016-09-10 16:47:33 -070035const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed41ed7c22015-11-04 21:03:37 +000036const ButtonLocation kQuickTurn(1, 5);
37
Adam Snaider83eae562016-09-10 16:47:33 -070038const ButtonLocation kTurn1(1, 7);
39const ButtonLocation kTurn2(1, 11);
40
Comran Morshed41ed7c22015-11-04 21:03:37 +000041const ButtonLocation kFrontRollersIn(3, 8);
42const ButtonLocation kBackRollersIn(3, 7);
43const ButtonLocation kFrontRollersOut(3, 6);
44const ButtonLocation kBackRollersOut(4, 12);
45const ButtonLocation kHumanPlayer(4, 11);
46
47class Reader : public ::aos::input::JoystickInput {
48 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080049 Reader(::aos::EventLoop *event_loop)
Austin Schuh1bf8a212019-05-26 22:13:14 -070050 : ::aos::input::JoystickInput(event_loop),
Austin Schuh30020d92019-05-27 13:07:02 -070051 rollers_goal_sender_(
52 event_loop
53 ->MakeSender<::y2014_bot3::control_loops::RollersQueue::Goal>(
54 ".y2014_bot3.control_loops.rollers_queue.goal")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070055 autonomous_action_factory_(
56 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(
57 event_loop)) {
Austin Schuh0b560152019-01-04 17:02:27 -080058 drivetrain_input_reader_ = DrivetrainInputReader::Make(
59 DrivetrainInputReader::InputType::kSteeringWheel,
60 ::y2014_bot3::control_loops::drivetrain::GetDrivetrainConfig());
61 }
Comran Morshed41ed7c22015-11-04 21:03:37 +000062
63 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
64 bool last_auto_running = auto_running_;
65 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
66 data.GetControlBit(ControlBit::kEnabled);
67 if (auto_running_ != last_auto_running) {
68 if (auto_running_) {
69 StartAuto();
70 } else {
71 StopAuto();
72 }
73 }
74
75 if (!data.GetControlBit(ControlBit::kAutonomous)) {
76 HandleDrivetrain(data);
77 HandleTeleop(data);
78 }
Austin Schuh0b560152019-01-04 17:02:27 -080079
80 action_queue_.Tick();
Comran Morshed41ed7c22015-11-04 21:03:37 +000081 }
82
83 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080084 drivetrain_input_reader_->HandleDrivetrain(data);
Comran Morshed41ed7c22015-11-04 21:03:37 +000085 }
86
87 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuh0b560152019-01-04 17:02:27 -080088 if (!data.GetControlBit(ControlBit::kEnabled)) {
89 action_queue_.CancelAllActions();
90 LOG(DEBUG, "Canceling\n");
91 }
92
Comran Morshed41ed7c22015-11-04 21:03:37 +000093 // Rollers.
Austin Schuh30020d92019-05-27 13:07:02 -070094 auto rollers_goal = rollers_goal_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +000095 rollers_goal->Zero();
96 if (data.IsPressed(kFrontRollersIn)) {
97 rollers_goal->intake = 1;
98 } else if (data.IsPressed(kFrontRollersOut)) {
99 rollers_goal->low_spit = 1;
100 } else if (data.IsPressed(kBackRollersIn)) {
101 rollers_goal->intake = -1;
102 } else if (data.IsPressed(kBackRollersOut)) {
103 rollers_goal->low_spit = -1;
104 } else if (data.IsPressed(kHumanPlayer)) {
105 rollers_goal->human_player = true;
106 }
107 if (!rollers_goal.Send()) {
108 LOG(WARNING, "Sending rollers values failed.\n");
109 }
110 }
111
112 private:
113 void StartAuto() {
114 LOG(INFO, "Starting auto mode.\n");
Austin Schuh0b560152019-01-04 17:02:27 -0800115 ::frc971::autonomous::AutonomousActionParams params;
116 params.mode = 0;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700117 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000118 }
119
120 void StopAuto() {
121 LOG(INFO, "Stopping auto mode\n");
Austin Schuh0b560152019-01-04 17:02:27 -0800122 action_queue_.CancelAllActions();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000123 }
124
125 bool auto_running_ = false;
126
Comran Morshed41ed7c22015-11-04 21:03:37 +0000127 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800128 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000129 "no drivetrain status");
Austin Schuh0b560152019-01-04 17:02:27 -0800130
131 ::aos::common::actions::ActionQueue action_queue_;
132
133 ::std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
Austin Schuh30020d92019-05-27 13:07:02 -0700134 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Goal>
135 rollers_goal_sender_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700136
137 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000138};
139
140} // namespace joysticks
141} // namespace input
142} // namespace y2014_bot3
143
144int main() {
Brian Silverman5090c432016-01-02 14:44:26 -0800145 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800146 ::aos::ShmEventLoop event_loop;
147 ::y2014_bot3::input::joysticks::Reader reader(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000148 reader.Run();
149 ::aos::Cleanup();
150}