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: |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame^] | 31 | static const bool kWristAlwaysDown = false; |
| 32 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | JoystickReader() : aos::JoystickInput() { |
| 34 | shifters.MakeWithBuilder().set(true).Send(); |
| 35 | } |
| 36 | |
| 37 | virtual void RunIteration() { |
| 38 | static bool is_high_gear = false; |
| 39 | |
| 40 | if (Pressed(0, AUTONOMOUS)) { |
| 41 | if (PosEdge(0, ENABLED)){ |
| 42 | LOG(INFO, "Starting auto mode\n"); |
| 43 | AutoMode.Start(); |
| 44 | } |
| 45 | if (NegEdge(0, ENABLED)) { |
| 46 | LOG(INFO, "Stopping auto mode\n"); |
| 47 | AutoMode.Stop(); |
| 48 | } |
| 49 | } else { // teleop |
| 50 | bool is_control_loop_driving = false; |
| 51 | double left_goal = 0.0; |
| 52 | double right_goal = 0.0; |
| 53 | const double wheel = control_data_.stick0Axis1 / 127.0; |
| 54 | const double throttle = -control_data_.stick1Axis2 / 127.0; |
| 55 | const double kThrottleGain = 1.0 / 2.5; |
| 56 | if (Pressed(0, 7) || Pressed(0, 11)) { |
| 57 | static double distance = 0.0; |
| 58 | static double angle = 0.0; |
| 59 | static double filtered_goal_distance = 0.0; |
| 60 | if (PosEdge(0, 7) || PosEdge(0, 11)) { |
| 61 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 62 | distance = (drivetrain.position->left_encoder + |
| 63 | drivetrain.position->right_encoder) / 2.0 |
| 64 | - throttle * kThrottleGain / 2.0; |
| 65 | angle = gyro->angle; |
| 66 | filtered_goal_distance = distance; |
| 67 | } |
| 68 | } |
| 69 | is_control_loop_driving = true; |
| 70 | |
| 71 | //const double gyro_angle = Gyro.View().angle; |
| 72 | const double goal_theta = angle - wheel * 0.27; |
| 73 | const double goal_distance = distance + throttle * kThrottleGain; |
| 74 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 75 | const double kMaxVelocity = 0.6; |
| 76 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 77 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 78 | } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 79 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 80 | } else { |
| 81 | filtered_goal_distance = goal_distance; |
| 82 | } |
| 83 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 84 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 85 | is_high_gear = false; |
| 86 | |
| 87 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 88 | } |
| 89 | if (!(drivetrain.goal.MakeWithBuilder() |
| 90 | .steering(wheel) |
| 91 | .throttle(throttle) |
| 92 | .highgear(is_high_gear).quickturn(Pressed(0, 5)) |
| 93 | .control_loop_driving(is_control_loop_driving) |
| 94 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 95 | LOG(WARNING, "sending stick values failed\n"); |
| 96 | } |
| 97 | |
| 98 | if (PosEdge(1, 1)) { |
| 99 | is_high_gear = false; |
| 100 | } |
| 101 | if (PosEdge(1, 3)) { |
| 102 | is_high_gear = true; |
| 103 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 104 | |
| 105 | // frisbee pickup is -0.634 |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame^] | 106 | wrist.goal.MakeWithBuilder().goal((Pressed(2, 10) || kWristAlwaysDown) ? |
| 107 | -0.634 : 1.5).Send(); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 108 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame^] | 109 | ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal = |
| 110 | shooter.goal.MakeMessage(); |
| 111 | shooter_goal->velocity = 0; |
| 112 | static double angle_adjust_goal = 0; |
| 113 | if (Pressed(2, 3)) { |
| 114 | // short shot |
| 115 | shooter_goal->velocity = 200; |
| 116 | angle_adjust_goal = 0.435; |
| 117 | } else if (Pressed(2, 5)) { |
| 118 | // medium shot |
| 119 | shooter_goal->velocity = 220; |
| 120 | angle_adjust_goal = 0.45; |
| 121 | } else if (Pressed(2, 6)) { |
| 122 | // long shot |
| 123 | shooter_goal->velocity = 240; |
| 124 | angle_adjust_goal = 0.55; |
| 125 | } |
| 126 | angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send(); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 127 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame^] | 128 | ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal = |
| 129 | index_loop.goal.MakeMessage(); |
| 130 | // TODO(brians): replace these with the enum values |
| 131 | if (Pressed(2, 11)) { |
| 132 | // FIRE |
| 133 | index_goal->goal_state = 4; |
| 134 | } else if (shooter_goal->velocity != 0) { |
| 135 | // get ready to shoot |
| 136 | index_goal->goal_state = 3; |
| 137 | } else if (Pressed(2, 9)) { |
| 138 | // intake |
| 139 | index_goal->goal_state = 2; |
| 140 | } else { |
| 141 | // get ready to intake |
| 142 | index_goal->goal_state = 1; |
| 143 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 144 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame^] | 145 | index_goal.Send(); |
| 146 | shooter_goal.Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | } // namespace frc971 |
| 152 | |
| 153 | AOS_RUN(frc971::JoystickReader) |