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" |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include "frc971/queues/GyroAngle.q.h" |
| 13 | #include "frc971/queues/Piston.q.h" |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame^] | 14 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
| 15 | #include "frc971/control_loops/index/index_motor.q.h" |
| 16 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
| 17 | #include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | |
| 19 | using ::frc971::control_loops::drivetrain; |
| 20 | using ::frc971::control_loops::shifters; |
| 21 | using ::frc971::sensors::gyro; |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame^] | 22 | using ::frc971::control_loops::wrist; |
| 23 | using ::frc971::control_loops::index_loop; |
| 24 | using ::frc971::control_loops::shooter; |
| 25 | using ::frc971::control_loops::angle_adjust; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | |
| 27 | namespace frc971 { |
| 28 | |
| 29 | class JoystickReader : public aos::JoystickInput { |
| 30 | public: |
| 31 | JoystickReader() : aos::JoystickInput() { |
| 32 | shifters.MakeWithBuilder().set(true).Send(); |
| 33 | } |
| 34 | |
| 35 | virtual void RunIteration() { |
| 36 | static bool is_high_gear = false; |
| 37 | |
| 38 | if (Pressed(0, AUTONOMOUS)) { |
| 39 | if (PosEdge(0, ENABLED)){ |
| 40 | LOG(INFO, "Starting auto mode\n"); |
| 41 | AutoMode.Start(); |
| 42 | } |
| 43 | if (NegEdge(0, ENABLED)) { |
| 44 | LOG(INFO, "Stopping auto mode\n"); |
| 45 | AutoMode.Stop(); |
| 46 | } |
| 47 | } else { // teleop |
| 48 | bool is_control_loop_driving = false; |
| 49 | double left_goal = 0.0; |
| 50 | double right_goal = 0.0; |
| 51 | const double wheel = control_data_.stick0Axis1 / 127.0; |
| 52 | const double throttle = -control_data_.stick1Axis2 / 127.0; |
| 53 | const double kThrottleGain = 1.0 / 2.5; |
| 54 | if (Pressed(0, 7) || Pressed(0, 11)) { |
| 55 | static double distance = 0.0; |
| 56 | static double angle = 0.0; |
| 57 | static double filtered_goal_distance = 0.0; |
| 58 | if (PosEdge(0, 7) || PosEdge(0, 11)) { |
| 59 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 60 | distance = (drivetrain.position->left_encoder + |
| 61 | drivetrain.position->right_encoder) / 2.0 |
| 62 | - throttle * kThrottleGain / 2.0; |
| 63 | angle = gyro->angle; |
| 64 | filtered_goal_distance = distance; |
| 65 | } |
| 66 | } |
| 67 | is_control_loop_driving = true; |
| 68 | |
| 69 | //const double gyro_angle = Gyro.View().angle; |
| 70 | const double goal_theta = angle - wheel * 0.27; |
| 71 | const double goal_distance = distance + throttle * kThrottleGain; |
| 72 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 73 | const double kMaxVelocity = 0.6; |
| 74 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 75 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 76 | } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 77 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 78 | } else { |
| 79 | filtered_goal_distance = goal_distance; |
| 80 | } |
| 81 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 82 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 83 | is_high_gear = false; |
| 84 | |
| 85 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 86 | } |
| 87 | if (!(drivetrain.goal.MakeWithBuilder() |
| 88 | .steering(wheel) |
| 89 | .throttle(throttle) |
| 90 | .highgear(is_high_gear).quickturn(Pressed(0, 5)) |
| 91 | .control_loop_driving(is_control_loop_driving) |
| 92 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 93 | LOG(WARNING, "sending stick values failed\n"); |
| 94 | } |
| 95 | |
| 96 | if (PosEdge(1, 1)) { |
| 97 | is_high_gear = false; |
| 98 | } |
| 99 | if (PosEdge(1, 3)) { |
| 100 | is_high_gear = true; |
| 101 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame^] | 102 | |
| 103 | // frisbee pickup is -0.634 |
| 104 | wrist.goal.MakeWithBuilder().goal(-0.634).Send(); |
| 105 | |
| 106 | index_loop.goal.MakeWithBuilder() |
| 107 | .goal_state(Pressed(1, 4) ? 2 : |
| 108 | Pressed(1, 5) ? 3 : |
| 109 | Pressed(1, 10) ? 4 : 1).Send(); |
| 110 | |
| 111 | angle_adjust.goal.MakeWithBuilder() |
| 112 | .goal(Pressed(3, 1) ? 0.6 : 0.35).Send(); |
| 113 | |
| 114 | shooter.goal.MakeWithBuilder() |
| 115 | .velocity(Pressed(2, 9) ? 325.0 : 0.0).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | } // namespace frc971 |
| 121 | |
| 122 | AOS_RUN(frc971::JoystickReader) |