brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "aos/aos_core.h" |
| 7 | #include "aos/atom_code/input/FRCComm.h" |
| 8 | #include "aos/atom_code/input/JoystickInput.h" |
| 9 | |
| 10 | #include "frc971/input/AutoMode.q.h" |
| 11 | #include "frc971/control_loops/DriveTrain.q.h" |
| 12 | #include "frc971/queues/GyroAngle.q.h" |
| 13 | #include "frc971/queues/Piston.q.h" |
| 14 | |
| 15 | using ::frc971::control_loops::drivetrain; |
| 16 | using ::frc971::control_loops::shifters; |
| 17 | using ::frc971::sensors::gyro; |
| 18 | |
| 19 | namespace frc971 { |
| 20 | |
| 21 | class JoystickReader : public aos::JoystickInput { |
| 22 | public: |
| 23 | JoystickReader() : aos::JoystickInput() { |
| 24 | shifters.MakeWithBuilder().set(true).Send(); |
| 25 | } |
| 26 | |
| 27 | virtual void RunIteration() { |
| 28 | static bool is_high_gear = false; |
| 29 | |
| 30 | if (Pressed(0, AUTONOMOUS)) { |
| 31 | if (PosEdge(0, ENABLED)){ |
| 32 | LOG(INFO, "Starting auto mode\n"); |
| 33 | AutoMode.Start(); |
| 34 | } |
| 35 | if (NegEdge(0, ENABLED)) { |
| 36 | LOG(INFO, "Stopping auto mode\n"); |
| 37 | AutoMode.Stop(); |
| 38 | } |
| 39 | } else { // teleop |
| 40 | bool is_control_loop_driving = false; |
| 41 | double left_goal = 0.0; |
| 42 | double right_goal = 0.0; |
| 43 | const double wheel = control_data_.stick0Axis1 / 127.0; |
| 44 | const double throttle = -control_data_.stick1Axis2 / 127.0; |
| 45 | const double kThrottleGain = 1.0 / 2.5; |
| 46 | if (Pressed(0, 7) || Pressed(0, 11)) { |
| 47 | static double distance = 0.0; |
| 48 | static double angle = 0.0; |
| 49 | static double filtered_goal_distance = 0.0; |
| 50 | if (PosEdge(0, 7) || PosEdge(0, 11)) { |
| 51 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 52 | distance = (drivetrain.position->left_encoder + |
| 53 | drivetrain.position->right_encoder) / 2.0 |
| 54 | - throttle * kThrottleGain / 2.0; |
| 55 | angle = gyro->angle; |
| 56 | filtered_goal_distance = distance; |
| 57 | } |
| 58 | } |
| 59 | is_control_loop_driving = true; |
| 60 | |
| 61 | //const double gyro_angle = Gyro.View().angle; |
| 62 | const double goal_theta = angle - wheel * 0.27; |
| 63 | const double goal_distance = distance + throttle * kThrottleGain; |
| 64 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 65 | const double kMaxVelocity = 0.6; |
| 66 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 67 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 68 | } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 69 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 70 | } else { |
| 71 | filtered_goal_distance = goal_distance; |
| 72 | } |
| 73 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 74 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 75 | is_high_gear = false; |
| 76 | |
| 77 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 78 | } |
| 79 | if (!(drivetrain.goal.MakeWithBuilder() |
| 80 | .steering(wheel) |
| 81 | .throttle(throttle) |
| 82 | .highgear(is_high_gear).quickturn(Pressed(0, 5)) |
| 83 | .control_loop_driving(is_control_loop_driving) |
| 84 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 85 | LOG(WARNING, "sending stick values failed\n"); |
| 86 | } |
| 87 | |
| 88 | if (PosEdge(1, 1)) { |
| 89 | is_high_gear = false; |
| 90 | } |
| 91 | if (PosEdge(1, 3)) { |
| 92 | is_high_gear = true; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // namespace frc971 |
| 99 | |
| 100 | AOS_RUN(frc971::JoystickReader) |