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