blob: e306553fa2f0527b96d5cf74d7f0779422361630 [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
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
15#include "frc971/constants.h"
16#include "frc971/queues/gyro.q.h"
17#include "frc971/autonomous/auto.q.h"
Brian Silverman20141f92015-01-05 17:39:01 -080018
Brian Silvermanada5f2c2015-02-01 02:41:14 -050019using ::frc971::control_loops::drivetrain_queue;
Brian Silverman20141f92015-01-05 17:39:01 -080020using ::frc971::sensors::gyro_reading;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::ControlBit;
25
26namespace frc971 {
27namespace input {
28namespace joysticks {
29
30const ButtonLocation kDriveControlLoopEnable1(1, 7),
31 kDriveControlLoopEnable2(1, 11);
32const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
33const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3);
34const ButtonLocation kQuickTurn(1, 5);
35
Brian Silverman20141f92015-01-05 17:39:01 -080036
37class Reader : public ::aos::input::JoystickInput {
38 public:
39 Reader()
40 : is_high_gear_(false),
41 was_running_(false) {}
42
43 virtual void RunIteration(const ::aos::input::driver_station::Data &data) {
Austin Schuh6182f8d2015-02-14 22:15:04 -080044 bool last_auto_running = auto_running_;
45 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
Brian Silvermane1103e62015-02-15 02:03:38 -050046 data.GetControlBit(ControlBit::kEnabled);
Austin Schuh6182f8d2015-02-14 22:15:04 -080047 if (auto_running_ != last_auto_running) {
48 if (auto_running_) {
49 StartAuto();
50 } else {
51 StopAuto();
Brian Silverman20141f92015-01-05 17:39:01 -080052 }
Austin Schuh6182f8d2015-02-14 22:15:04 -080053 }
54
55 if (!data.GetControlBit(ControlBit::kAutonomous)) {
Brian Silverman20141f92015-01-05 17:39:01 -080056 HandleTeleop(data);
57 }
58 }
59
60 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
61 bool is_control_loop_driving = false;
62 double left_goal = 0.0;
63 double right_goal = 0.0;
64 const double wheel = -data.GetAxis(kSteeringWheel);
65 const double throttle = -data.GetAxis(kDriveThrottle);
66 const double kThrottleGain = 1.0 / 2.5;
67 if (false && (data.IsPressed(kDriveControlLoopEnable1) ||
68 data.IsPressed(kDriveControlLoopEnable2))) {
69 // TODO(austin): Static sucks!
70 static double distance = 0.0;
71 static double angle = 0.0;
72 static double filtered_goal_distance = 0.0;
73 if (data.PosEdge(kDriveControlLoopEnable1) ||
74 data.PosEdge(kDriveControlLoopEnable2)) {
Brian Silvermanada5f2c2015-02-01 02:41:14 -050075 if (drivetrain_queue.position.FetchLatest() &&
76 gyro_reading.FetchLatest()) {
77 distance = (drivetrain_queue.position->left_encoder +
78 drivetrain_queue.position->right_encoder) /
Brian Silverman20141f92015-01-05 17:39:01 -080079 2.0 -
80 throttle * kThrottleGain / 2.0;
81 angle = gyro_reading->angle;
82 filtered_goal_distance = distance;
83 }
84 }
85 is_control_loop_driving = true;
86
87 // const double gyro_angle = Gyro.View().angle;
88 const double goal_theta = angle - wheel * 0.27;
89 const double goal_distance = distance + throttle * kThrottleGain;
90 const double robot_width = 22.0 / 100.0 * 2.54;
91 const double kMaxVelocity = 0.6;
92 if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) {
93 filtered_goal_distance += kMaxVelocity * 0.02;
94 } else if (goal_distance <
95 -kMaxVelocity * 0.02 + filtered_goal_distance) {
96 filtered_goal_distance -= kMaxVelocity * 0.02;
97 } else {
98 filtered_goal_distance = goal_distance;
99 }
100 left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0;
101 right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0;
102 is_high_gear_ = false;
103
104 LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal);
105 }
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500106 if (!drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman20141f92015-01-05 17:39:01 -0800107 .steering(wheel)
108 .throttle(throttle)
Brian Silverman93335ae2015-01-26 20:43:39 -0500109 //.highgear(is_high_gear_)
Brian Silverman20141f92015-01-05 17:39:01 -0800110 .quickturn(data.IsPressed(kQuickTurn))
111 .control_loop_driving(is_control_loop_driving)
112 .left_goal(left_goal)
113 .right_goal(right_goal)
114 .left_velocity_goal(0)
115 .right_velocity_goal(0)
116 .Send()) {
117 LOG(WARNING, "sending stick values failed\n");
118 }
119 if (data.PosEdge(kShiftHigh)) {
120 is_high_gear_ = false;
121 }
122 if (data.PosEdge(kShiftLow)) {
123 is_high_gear_ = true;
124 }
125 }
126
127 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
128 HandleDrivetrain(data);
129 if (!data.GetControlBit(ControlBit::kEnabled)) {
130 action_queue_.CancelAllActions();
Austin Schuh6182f8d2015-02-14 22:15:04 -0800131 LOG(DEBUG, "Canceling\n");
Brian Silverman20141f92015-01-05 17:39:01 -0800132 }
133
134 action_queue_.Tick();
135 was_running_ = action_queue_.Running();
136 }
137
138 private:
Austin Schuh6182f8d2015-02-14 22:15:04 -0800139 void StartAuto() {
140 LOG(INFO, "Starting auto mode\n");
141 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send();
142 }
143
144 void StopAuto() {
145 LOG(INFO, "Stopping auto mode\n");
146 ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send();
147 }
148
Brian Silverman20141f92015-01-05 17:39:01 -0800149 bool is_high_gear_;
150 bool was_running_;
151
Austin Schuh6182f8d2015-02-14 22:15:04 -0800152 bool auto_running_ = false;
153
Ben Fredricksond69f38b2015-01-28 20:06:15 -0800154 aos::common::actions::ActionQueue action_queue_;
Brian Silverman20141f92015-01-05 17:39:01 -0800155
156 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
157 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING,
158 "no drivetrain status");
159};
160
161} // namespace joysticks
162} // namespace input
163} // namespace frc971
164
165int main() {
166 ::aos::Init();
167 ::frc971::input::joysticks::Reader reader;
168 reader.Run();
169 ::aos::Cleanup();
170}