Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 1 | #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/logging/logging.h" |
| 9 | |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 11 | #include "frc971/queues/gyro_angle.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 12 | #include "frc971/autonomous/auto.q.h" |
| 13 | |
| 14 | using ::frc971::control_loops::drivetrain; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 15 | using ::frc971::sensors::gyro; |
| 16 | |
| 17 | using ::aos::input::driver_station::ButtonLocation; |
| 18 | using ::aos::input::driver_station::JoystickAxis; |
| 19 | using ::aos::input::driver_station::ControlBit; |
| 20 | |
| 21 | namespace frc971 { |
| 22 | namespace input { |
| 23 | namespace joysticks { |
| 24 | |
| 25 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 26 | kDriveControlLoopEnable2(1, 11); |
| 27 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 28 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 29 | const ButtonLocation kQuickTurn(1, 5); |
| 30 | |
| 31 | class Reader : public ::aos::input::JoystickInput { |
| 32 | public: |
Brian Silverman | 61e41fd | 2014-02-16 19:08:50 -0800 | [diff] [blame^] | 33 | Reader() {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 34 | |
| 35 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 36 | static bool is_high_gear = false; |
| 37 | |
| 38 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 39 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 40 | LOG(INFO, "Starting auto mode\n"); |
| 41 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 42 | .run_auto(true).Send(); |
| 43 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 44 | LOG(INFO, "Stopping auto mode\n"); |
| 45 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 46 | .run_auto(false).Send(); |
| 47 | } |
| 48 | } else { // teleop |
| 49 | bool is_control_loop_driving = false; |
| 50 | double left_goal = 0.0; |
| 51 | double right_goal = 0.0; |
| 52 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 53 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 54 | LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle); |
| 55 | const double kThrottleGain = 1.0 / 2.5; |
| 56 | if (data.IsPressed(kDriveControlLoopEnable1) || |
| 57 | data.IsPressed(kDriveControlLoopEnable2)) { |
| 58 | static double distance = 0.0; |
| 59 | static double angle = 0.0; |
| 60 | static double filtered_goal_distance = 0.0; |
| 61 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 62 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 63 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 64 | distance = (drivetrain.position->left_encoder + |
| 65 | drivetrain.position->right_encoder) / 2.0 |
| 66 | - throttle * kThrottleGain / 2.0; |
| 67 | angle = gyro->angle; |
| 68 | filtered_goal_distance = distance; |
| 69 | } |
| 70 | } |
| 71 | is_control_loop_driving = true; |
| 72 | |
| 73 | //const double gyro_angle = Gyro.View().angle; |
| 74 | const double goal_theta = angle - wheel * 0.27; |
| 75 | const double goal_distance = distance + throttle * kThrottleGain; |
| 76 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 77 | const double kMaxVelocity = 0.6; |
| 78 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 79 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 80 | } else if (goal_distance < -kMaxVelocity * 0.02 + |
| 81 | filtered_goal_distance) { |
| 82 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 83 | } else { |
| 84 | filtered_goal_distance = goal_distance; |
| 85 | } |
| 86 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 87 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 88 | is_high_gear = false; |
| 89 | |
| 90 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 91 | } |
| 92 | if (!(drivetrain.goal.MakeWithBuilder() |
| 93 | .steering(wheel) |
| 94 | .throttle(throttle) |
| 95 | .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn)) |
| 96 | .control_loop_driving(is_control_loop_driving) |
| 97 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 98 | LOG(WARNING, "sending stick values failed\n"); |
| 99 | } |
| 100 | |
| 101 | if (data.PosEdge(kShiftHigh)) { |
| 102 | is_high_gear = false; |
| 103 | } |
| 104 | if (data.PosEdge(kShiftLow)) { |
| 105 | is_high_gear = true; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | } // namespace joysticks |
| 112 | } // namespace input |
| 113 | } // namespace frc971 |
| 114 | |
| 115 | int main() { |
| 116 | ::aos::Init(); |
| 117 | ::frc971::input::joysticks::Reader reader; |
| 118 | reader.Run(); |
| 119 | ::aos::Cleanup(); |
| 120 | } |