blob: 09d815c8b1748ea70f1e936a4a2ce6b51b272d0f [file] [log] [blame]
Brian Silverman20141f92015-01-05 17:39:01 -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/prime/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"
Ben Fredricksond69f38b2015-01-28 20:06:15 -080012#include "aos/common/actions/actions.h"
Brian Silverman20141f92015-01-05 17:39:01 -080013
Daniel Petti61896522015-02-15 18:01:43 -080014#include "frc971/control_loops/claw/claw.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080015#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Daniel Petti61896522015-02-15 18:01:43 -080016#include "frc971/control_loops/fridge/fridge.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080017#include "frc971/constants.h"
18#include "frc971/queues/gyro.q.h"
19#include "frc971/autonomous/auto.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080020
Daniel Petti61896522015-02-15 18:01:43 -080021using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050022using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080023using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080024using ::frc971::sensors::gyro_reading;
25
26using ::aos::input::driver_station::ButtonLocation;
27using ::aos::input::driver_station::JoystickAxis;
28using ::aos::input::driver_station::ControlBit;
29
30namespace frc971 {
31namespace input {
32namespace joysticks {
33
Brian Silverman20141f92015-01-05 17:39:01 -080034const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
35const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
36const ButtonLocation kQuickTurn(1, 5);
37
Daniel Petti61896522015-02-15 18:01:43 -080038// TODO(danielp): Real buttons for all of these.
Austin Schuh8a436e82015-02-16 23:31:28 -080039const ButtonLocation kElevatorUp(3, 10);
40const ButtonLocation kElevatorDown(2, 6);
41const ButtonLocation kArmUp(3, 8);
42const ButtonLocation kArmDown(3, 3);
43const ButtonLocation kClawUp(3, 7);
44const ButtonLocation kClawDown(3, 6);
Daniel Petti61896522015-02-15 18:01:43 -080045
46// Testing mode.
Austin Schuh8a436e82015-02-16 23:31:28 -080047const double kElevatorVelocity = 0.5;
48const double kArmVelocity = 0.5;
49const double kClawVelocity = 2.0;
Daniel Petti61896522015-02-15 18:01:43 -080050// TODO(danielp): Verify.
51const double kJoystickDt = 0.01;
Brian Silverman20141f92015-01-05 17:39:01 -080052
53class Reader : public ::aos::input::JoystickInput {
54 public:
Austin Schuh331e13d2015-02-15 00:16:51 -080055 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -080056
57 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080058 bool last_auto_running = auto_running_;
59 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050060 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080061 if (auto_running_ != last_auto_running) {
62 if (auto_running_) {
63 StartAuto();
64 } else {
65 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080066 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080067 }
68
69 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -080070 HandleDrivetrain(data);
71 if (data.GetControlBit(ControlBit::kTestMode)) {
72 HandleTest(data);
73 } else {
74 HandleTeleop(data);
75 }
Brian Silverman20141f92015-01-05 17:39:01 -080076 }
77 }
78
79 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -080080 const double wheel = -data.GetAxis(kSteeringWheel);
81 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -080082
Brian Silvermanada5f2c2015-02-01 02:41:14 -050083 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -080084 .steering(wheel)
85 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -080086 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -080087 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -080088 .Send()) {
89 LOG(WARNING, "sending stick values failed\n");
90 }
Brian Silverman20141f92015-01-05 17:39:01 -080091 }
92
93 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -080094 if (!data.GetControlBit(ControlBit::kEnabled)) {
95 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -080096 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -080097 }
98
99 action_queue_.Tick();
100 was_running_ = action_queue_.Running();
101 }
102
Daniel Petti61896522015-02-15 18:01:43 -0800103 void HandleTest(const ::aos::input::driver_station::Data &data) {
104 if (action_queue_.Running()) {
105 // We don't really want any actions running.
106 LOG(DEBUG, "Cancelling actions for test mode.\n");
107 action_queue_.CancelAllActions();
108 }
109
110 if (data.GetControlBit(ControlBit::kEnabled)) {
111 if (data.PosEdge(ControlBit::kEnabled)) {
112 // If we got enabled, wait for everything to zero.
113 LOG(INFO, "Waiting for zero.\n");
114 waiting_for_zero_ = true;
115 }
116 if (waiting_for_zero_) {
117 claw_queue.status.FetchLatest();
118 fridge_queue.status.FetchLatest();
119 if (!claw_queue.status.get()) {
120 LOG(ERROR, "Got no claw status packet.\n");
121 // Not safe to continue.
122 return;
123 }
124 if (!fridge_queue.status.get()) {
125 LOG(ERROR, "Got no fridge status packet.\n");
126 return;
127 }
128
129 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
130 LOG(INFO, "Zeroed! Starting test mode.\n");
131 waiting_for_zero_ = false;
132
133 // Set the initial goals to where we are now.
134 elevator_goal_ = fridge_queue.status->height;
135 arm_goal_ = fridge_queue.status->angle;
136 claw_goal_ = claw_queue.status->angle;
137 } else {
138 return;
139 }
140 }
141
142 // These buttons move a subsystem up or down for as long as they are
143 // pressed, at low velocity.
144 if (data.IsPressed(kElevatorUp)) {
145 elevator_goal_ += kElevatorVelocity * kJoystickDt;
146 }
147 if (data.IsPressed(kElevatorDown)) {
148 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
149 }
150 if (data.IsPressed(kArmUp)) {
151 arm_goal_ += kArmVelocity * kJoystickDt;
152 }
153 if (data.IsPressed(kArmDown)) {
154 arm_goal_ -= kArmVelocity * kJoystickDt;
155 }
156 if (data.IsPressed(kClawUp)) {
157 claw_goal_ += kClawVelocity * kJoystickDt;
158 }
159 if (data.IsPressed(kClawDown)) {
160 claw_goal_ -= kClawVelocity * kJoystickDt;
161 }
162
163 if (!fridge_queue.goal.MakeWithBuilder()
164 .height(elevator_goal_)
165 .angle(arm_goal_)
166 .Send()) {
167 LOG(ERROR, "Sending fridge goal failed.\n");
168 }
169 if (!claw_queue.goal.MakeWithBuilder()
170 .angle(claw_goal_)
171 .Send()) {
172 LOG(ERROR, "Sending claw goal failed.\n");
173 }
174 }
175 }
176
Brian Silverman20141f92015-01-05 17:39:01 -0800177 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800178 void StartAuto() {
179 LOG(INFO, "Starting auto mode\n");
180 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
181 }
182
183 void StopAuto() {
184 LOG(INFO, "Stopping auto mode\n");
185 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
186 }
187
Brian Silverman20141f92015-01-05 17:39:01 -0800188 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800189
190 // Previous goals for systems.
191 double elevator_goal_ = 0.0;
192 double arm_goal_ = 0.0;
193 double claw_goal_ = 0.0;
194
195 // If we're waiting for the subsystems to zero.
196 bool waiting_for_zero_ = false;
197
Austin Schuh6182f8d2015-02-14 22:15:04 -0800198 bool auto_running_ = false;
199
Austin Schuh331e13d2015-02-15 00:16:51 -0800200 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800201
202 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
203 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
204 "no drivetrain status");
205};
206
207} // namespace joysticks
208} // namespace input
209} // namespace frc971
210
211int main() {
212 ::aos::Init();
213 ::frc971::input::joysticks::Reader reader;
214 reader.Run();
215 ::aos::Cleanup();
216}