blob: ba25e4b3ff066f27f225a05d9ca4f229f8882c3c [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"
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080020#include "frc971/actors/fridge_profile_action.q.h"
21#include "frc971/actors/fridge_profile_actor.h"
Brian Silverman20141f92015-01-05 17:39:01 -080022
Daniel Petti61896522015-02-15 18:01:43 -080023using ::frc971::control_loops::claw_queue;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050024using ::frc971::control_loops::drivetrain_queue;
Daniel Petti61896522015-02-15 18:01:43 -080025using ::frc971::control_loops::fridge_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080026using ::frc971::sensors::gyro_reading;
27
28using ::aos::input::driver_station::ButtonLocation;
29using ::aos::input::driver_station::JoystickAxis;
30using ::aos::input::driver_station::ControlBit;
31
32namespace frc971 {
33namespace input {
34namespace joysticks {
35
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080036// preset motion limits
37static const double kArmDebugVelocity = 0.17;
38static const double kArmDebugAcceleration = 0.8;
39static const double kElevatorDebugVelocity = 0.2;
40static const double kElevatorDebugAcceleration = 2.2;
41
Brian Silverman20141f92015-01-05 17:39:01 -080042const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
43const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
44const ButtonLocation kQuickTurn(1, 5);
45
Daniel Petti61896522015-02-15 18:01:43 -080046// TODO(danielp): Real buttons for all of these.
Austin Schuh8a436e82015-02-16 23:31:28 -080047const ButtonLocation kElevatorUp(3, 10);
48const ButtonLocation kElevatorDown(2, 6);
49const ButtonLocation kArmUp(3, 8);
50const ButtonLocation kArmDown(3, 3);
51const ButtonLocation kClawUp(3, 7);
52const ButtonLocation kClawDown(3, 6);
Daniel Petti61896522015-02-15 18:01:43 -080053
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -080054// TODO(ben): Real buttons for all of these.
55const ButtonLocation kArmPresetOne(99, 99);
56const ButtonLocation kArmPresetTwo(99, 99);
57const ButtonLocation kElevatorPresetOne(99, 99);
58const ButtonLocation kElevatorPresetTwo(99, 99);
59
Daniel Petti61896522015-02-15 18:01:43 -080060// Testing mode.
Austin Schuh8a436e82015-02-16 23:31:28 -080061const double kElevatorVelocity = 0.5;
62const double kArmVelocity = 0.5;
63const double kClawVelocity = 2.0;
Daniel Petti61896522015-02-15 18:01:43 -080064// TODO(danielp): Verify.
65const double kJoystickDt = 0.01;
Brian Silverman20141f92015-01-05 17:39:01 -080066
67class Reader : public ::aos::input::JoystickInput {
68 public:
Austin Schuh331e13d2015-02-15 00:16:51 -080069 Reader() : was_running_(false) {}
Brian Silverman20141f92015-01-05 17:39:01 -080070
71 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080072 bool last_auto_running = auto_running_;
73 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050074 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080075 if (auto_running_ != last_auto_running) {
76 if (auto_running_) {
77 StartAuto();
78 } else {
79 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080080 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080081 }
82
83 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Daniel Petti61896522015-02-15 18:01:43 -080084 HandleDrivetrain(data);
85 if (data.GetControlBit(ControlBit::kTestMode)) {
86 HandleTest(data);
87 } else {
88 HandleTeleop(data);
89 }
Brian Silverman20141f92015-01-05 17:39:01 -080090 }
91 }
92
93 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -080094 const double wheel = -data.GetAxis(kSteeringWheel);
95 const double throttle = -data.GetAxis(kDriveThrottle);
Brian Silverman20141f92015-01-05 17:39:01 -080096
Brian Silvermanada5f2c2015-02-01 02:41:14 -050097 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -080098 .steering(wheel)
99 .throttle(throttle)
Brian Silverman20141f92015-01-05 17:39:01 -0800100 .quickturn(data.IsPressed(kQuickTurn))
Austin Schuh331e13d2015-02-15 00:16:51 -0800101 .control_loop_driving(false)
Brian Silverman20141f92015-01-05 17:39:01 -0800102 .Send()) {
103 LOG(WARNING, "sending stick values failed\n");
104 }
Brian Silverman20141f92015-01-05 17:39:01 -0800105 }
106
107 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Brian Silverman20141f92015-01-05 17:39:01 -0800108 if (!data.GetControlBit(ControlBit::kEnabled)) {
109 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800110 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800111 }
112
113 action_queue_.Tick();
114 was_running_ = action_queue_.Running();
115 }
116
Daniel Petti61896522015-02-15 18:01:43 -0800117 void HandleTest(const ::aos::input::driver_station::Data &data) {
118 if (action_queue_.Running()) {
119 // We don't really want any actions running.
120 LOG(DEBUG, "Cancelling actions for test mode.\n");
121 action_queue_.CancelAllActions();
122 }
123
124 if (data.GetControlBit(ControlBit::kEnabled)) {
125 if (data.PosEdge(ControlBit::kEnabled)) {
126 // If we got enabled, wait for everything to zero.
127 LOG(INFO, "Waiting for zero.\n");
128 waiting_for_zero_ = true;
129 }
130 if (waiting_for_zero_) {
131 claw_queue.status.FetchLatest();
132 fridge_queue.status.FetchLatest();
133 if (!claw_queue.status.get()) {
134 LOG(ERROR, "Got no claw status packet.\n");
135 // Not safe to continue.
136 return;
137 }
138 if (!fridge_queue.status.get()) {
139 LOG(ERROR, "Got no fridge status packet.\n");
140 return;
141 }
142
143 if (claw_queue.status->zeroed && fridge_queue.status->zeroed) {
144 LOG(INFO, "Zeroed! Starting test mode.\n");
145 waiting_for_zero_ = false;
146
147 // Set the initial goals to where we are now.
148 elevator_goal_ = fridge_queue.status->height;
149 arm_goal_ = fridge_queue.status->angle;
150 claw_goal_ = claw_queue.status->angle;
151 } else {
152 return;
153 }
154 }
155
156 // These buttons move a subsystem up or down for as long as they are
157 // pressed, at low velocity.
158 if (data.IsPressed(kElevatorUp)) {
159 elevator_goal_ += kElevatorVelocity * kJoystickDt;
160 }
161 if (data.IsPressed(kElevatorDown)) {
162 elevator_goal_ -= kElevatorVelocity * kJoystickDt;
163 }
164 if (data.IsPressed(kArmUp)) {
165 arm_goal_ += kArmVelocity * kJoystickDt;
166 }
167 if (data.IsPressed(kArmDown)) {
168 arm_goal_ -= kArmVelocity * kJoystickDt;
169 }
170 if (data.IsPressed(kClawUp)) {
171 claw_goal_ += kClawVelocity * kJoystickDt;
172 }
173 if (data.IsPressed(kClawDown)) {
174 claw_goal_ -= kClawVelocity * kJoystickDt;
175 }
176
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800177 if (!action_queue_.Running()) {
178 if (!fridge_queue.goal.MakeWithBuilder()
179 .height(elevator_goal_)
180 .angle(arm_goal_)
181 .Send()) {
182 LOG(ERROR, "Sending fridge goal failed.\n");
183 }
184 if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) {
185 LOG(ERROR, "Sending claw goal failed.\n");
186 }
Daniel Petti61896522015-02-15 18:01:43 -0800187 }
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800188 if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) {
189 actors::FridgeProfileParams fridge_params;
190 fridge_params.arm_max_velocity = kArmDebugVelocity;
191 fridge_params.arm_max_acceleration = kArmDebugAcceleration;
192 if (data.IsPressed(kArmPresetOne)) {
193 LOG(INFO, "Preset asked for test arm position one position.\n");
194 fridge_params.arm_angle = M_PI / 4.0;
195 fridge_params.top_front_grabber = false;
196 fridge_params.top_back_grabber = false;
197 fridge_params.bottom_front_grabber = false;
198 fridge_params.bottom_back_grabber = false;
199 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
200 } else if (data.IsPressed(kArmPresetTwo)) {
201 LOG(INFO, "Preset asked for test arm position two position.\n");
202 fridge_params.arm_angle = -M_PI / 4.0;
203 fridge_params.top_front_grabber = true;
204 fridge_params.top_back_grabber = true;
205 fridge_params.bottom_front_grabber = true;
206 fridge_params.bottom_back_grabber = true;
207 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
208 }
209 } else if (data.IsPressed(kElevatorPresetOne) ||
210 data.IsPressed(kElevatorPresetOne)) {
211 actors::FridgeProfileParams fridge_params;
212 fridge_params.elevator_max_velocity = kElevatorDebugVelocity;
213 fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration;
214 if (data.IsPressed(kElevatorPresetOne)) {
215 LOG(INFO, "Preset asked for test elevator position one position.\n");
216 fridge_params.elevator_height = 0.5;
217 fridge_params.top_front_grabber = false;
218 fridge_params.top_back_grabber = false;
219 fridge_params.bottom_front_grabber = false;
220 fridge_params.bottom_back_grabber = false;
221 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
222 } else if (data.IsPressed(kElevatorPresetTwo)) {
223 LOG(INFO, "Preset asked for test elevator position two position.\n");
224 fridge_params.elevator_height = 1.2;
225 fridge_params.top_front_grabber = true;
226 fridge_params.top_back_grabber = true;
227 fridge_params.bottom_front_grabber = true;
228 fridge_params.bottom_back_grabber = true;
229 action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params));
230 }
Daniel Petti61896522015-02-15 18:01:43 -0800231 }
232 }
233 }
234
Brian Silverman20141f92015-01-05 17:39:01 -0800235 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800236 void StartAuto() {
237 LOG(INFO, "Starting auto mode\n");
238 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
239 }
240
241 void StopAuto() {
242 LOG(INFO, "Stopping auto mode\n");
243 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
244 }
245
Brian Silverman20141f92015-01-05 17:39:01 -0800246 bool was_running_;
Daniel Petti61896522015-02-15 18:01:43 -0800247
248 // Previous goals for systems.
249 double elevator_goal_ = 0.0;
250 double arm_goal_ = 0.0;
251 double claw_goal_ = 0.0;
252
253 // If we're waiting for the subsystems to zero.
254 bool waiting_for_zero_ = false;
255
Austin Schuh6182f8d2015-02-14 22:15:04 -0800256 bool auto_running_ = false;
257
Austin Schuh331e13d2015-02-15 00:16:51 -0800258 ::aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800259
260 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
261 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
262 "no drivetrain status");
263};
264
265} // namespace joysticks
266} // namespace input
267} // namespace frc971
268
269int main() {
270 ::aos::Init();
271 ::frc971::input::joysticks::Reader reader;
272 reader.Run();
273 ::aos::Cleanup();
274}