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