blob: 40a8d12335d573b7913badc3ae3c0a536eb31baf [file] [log] [blame]
Sabina Davis92d2efa2017-11-04 22:35:25 -07001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "aos/common/actions/actions.h"
7#include "aos/common/input/driver_station_data.h"
8#include "aos/common/logging/logging.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/linux_code/init.h"
14#include "frc971/autonomous/auto.q.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
16#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070017#include "y2017_bot3/control_loops/superstructure/superstructure.q.h"
18
19using ::frc971::control_loops::drivetrain_queue;
20using ::y2017_bot3::control_loops::superstructure_queue;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::ControlBit;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::POVLocation;
26using ::aos::input::DrivetrainInputReader;
27
28namespace y2017_bot3 {
29namespace input {
30namespace joysticks {
31
32const ButtonLocation kHangerOn(3, 1);
33const ButtonLocation kRollersOn(3, 2);
34const ButtonLocation kGearOut(3, 3);
35
36const ButtonLocation kRollerOn(3, 4);
37
38std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
39
40class Reader : public ::aos::input::JoystickInput {
41 public:
42 Reader() {
43 drivetrain_input_reader_ = DrivetrainInputReader::Make(
44 DrivetrainInputReader::InputType::kSteeringWheel);
45 }
46
47 void RunIteration(const ::aos::input::driver_station::Data &data) override {
Sabina Davisff3e51d2017-10-26 21:40:22 -070048 if (!data.GetControlBit(ControlBit::kEnabled)) {
49 action_queue_.CancelAllActions();
50 LOG(DEBUG, "Canceling\n");
51 }
52
53 const bool last_auto_running = auto_running_;
Sabina Davis92d2efa2017-11-04 22:35:25 -070054 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
55 data.GetControlBit(ControlBit::kEnabled);
56 if (auto_running_ != last_auto_running) {
57 if (auto_running_) {
58 StartAuto();
59 } else {
60 StopAuto();
61 }
62 }
63
64 if (!auto_running_) {
65 HandleDrivetrain(data);
66 HandleTeleop(data);
67 }
68
69 // Process pending actions.
70 action_queue_.Tick();
71 was_running_ = action_queue_.Running();
72 }
73
Sabina Davis92d2efa2017-11-04 22:35:25 -070074 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
75 drivetrain_input_reader_->HandleDrivetrain(data);
76 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
77 }
78
79 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
80 superstructure_queue.status.FetchLatest();
81 if (!superstructure_queue.status.get()) {
82 LOG(ERROR, "Got no superstructure status packet.\n");
83 return;
84 }
85
86 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
87 new_superstructure_goal->voltage_rollers = 0.0;
88
89 if (data.IsPressed(kRollerOn)) {
90 new_superstructure_goal->voltage_rollers = 12.0;
91 }
92
93 if (data.IsPressed(kHangerOn)) {
94 new_superstructure_goal->hanger_voltage = 12.0;
95 }
96
97
98 if (data.IsPressed(kGearOut)) {
99 new_superstructure_goal->fingers_out = true;
100 } else {
101 new_superstructure_goal->fingers_out = false;
102 }
103
104 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
105 if (!new_superstructure_goal.Send()) {
106 LOG(ERROR, "Sending superstructure goal failed.\n");
107 }
108 }
109
110 private:
111 void StartAuto() {
112 LOG(INFO, "Starting auto mode\n");
113 ::frc971::autonomous::AutonomousActionParams params;
114 ::frc971::autonomous::auto_mode.FetchLatest();
115 if (::frc971::autonomous::auto_mode.get() != nullptr) {
116 params.mode = ::frc971::autonomous::auto_mode->mode;
117 } else {
118 LOG(WARNING, "no auto mode values\n");
119 params.mode = 0;
120 }
121 action_queue_.EnqueueAction(
122 ::frc971::autonomous::MakeAutonomousAction(params));
123 }
124
125 void StopAuto() {
126 LOG(INFO, "Stopping auto mode\n");
127 action_queue_.CancelAllActions();
128 }
129 double robot_velocity_ = 0.0;
130 ::aos::common::actions::ActionQueue action_queue_;
131
132 bool was_running_ = false;
133 bool auto_running_ = false;
134};
135
136} // namespace joysticks
137} // namespace input
138} // namespace y2017_bot3
139
140int main() {
141 ::aos::Init(-1);
142 ::y2017_bot3::input::joysticks::Reader reader;
143 reader.Run();
144 ::aos::Cleanup();
145}