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; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame^] | 55 | LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | const double kThrottleGain = 1.0 / 2.5; |
| 57 | if (Pressed(0, 7) || Pressed(0, 11)) { |
| 58 | static double distance = 0.0; |
| 59 | static double angle = 0.0; |
| 60 | static double filtered_goal_distance = 0.0; |
| 61 | if (PosEdge(0, 7) || PosEdge(0, 11)) { |
| 62 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 63 | distance = (drivetrain.position->left_encoder + |
| 64 | drivetrain.position->right_encoder) / 2.0 |
| 65 | - throttle * kThrottleGain / 2.0; |
| 66 | angle = gyro->angle; |
| 67 | filtered_goal_distance = distance; |
| 68 | } |
| 69 | } |
| 70 | is_control_loop_driving = true; |
| 71 | |
| 72 | //const double gyro_angle = Gyro.View().angle; |
| 73 | const double goal_theta = angle - wheel * 0.27; |
| 74 | const double goal_distance = distance + throttle * kThrottleGain; |
| 75 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 76 | const double kMaxVelocity = 0.6; |
| 77 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 78 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 79 | } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 80 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 81 | } else { |
| 82 | filtered_goal_distance = goal_distance; |
| 83 | } |
| 84 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 85 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 86 | is_high_gear = false; |
| 87 | |
| 88 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 89 | } |
| 90 | if (!(drivetrain.goal.MakeWithBuilder() |
| 91 | .steering(wheel) |
| 92 | .throttle(throttle) |
| 93 | .highgear(is_high_gear).quickturn(Pressed(0, 5)) |
| 94 | .control_loop_driving(is_control_loop_driving) |
| 95 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 96 | LOG(WARNING, "sending stick values failed\n"); |
| 97 | } |
| 98 | |
| 99 | if (PosEdge(1, 1)) { |
| 100 | is_high_gear = false; |
| 101 | } |
| 102 | if (PosEdge(1, 3)) { |
| 103 | is_high_gear = true; |
| 104 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 105 | |
Brian Silverman | 1c0cb8b | 2013-03-15 23:19:59 -0700 | [diff] [blame] | 106 | // Where the wrist should be to pick up a frisbee. |
Brian Silverman | 53b6640 | 2013-03-16 12:20:10 -0700 | [diff] [blame] | 107 | static const double kWristPickup = -0.655; |
Brian Silverman | 1c0cb8b | 2013-03-15 23:19:59 -0700 | [diff] [blame] | 108 | // Where the wrist gets stored when up. |
| 109 | // All the way up is 1.5. |
| 110 | static const double kWristUp = 1.43; |
| 111 | static double wrist_down_position = kWristPickup; |
| 112 | index_loop.status.FetchLatest(); |
| 113 | if (index_loop.status.get()) { |
| 114 | if (index_loop.status->hopper_disc_count >= 4) { |
| 115 | wrist_down_position = -0.4; |
| 116 | } else { |
| 117 | wrist_down_position = kWristPickup; |
| 118 | } |
| 119 | } |
| 120 | wrist.goal.MakeWithBuilder() |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame^] | 121 | .goal(Pressed(2, 8) ? wrist_down_position : kWristUp).Send(); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 122 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 123 | ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal = |
| 124 | shooter.goal.MakeMessage(); |
| 125 | shooter_goal->velocity = 0; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame^] | 126 | static double angle_adjust_goal = 0.42; |
| 127 | if (Pressed(2, 5)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 128 | // short shot |
| 129 | shooter_goal->velocity = 200; |
Brian Silverman | 8376bd2 | 2013-03-15 21:50:11 -0700 | [diff] [blame] | 130 | angle_adjust_goal = 0.42; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame^] | 131 | } else if (Pressed(2, 3)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 132 | // medium shot |
| 133 | shooter_goal->velocity = 220; |
| 134 | angle_adjust_goal = 0.45; |
| 135 | } else if (Pressed(2, 6)) { |
| 136 | // long shot |
| 137 | shooter_goal->velocity = 240; |
| 138 | angle_adjust_goal = 0.55; |
| 139 | } |
| 140 | angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send(); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 141 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 142 | ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal = |
| 143 | index_loop.goal.MakeMessage(); |
| 144 | // TODO(brians): replace these with the enum values |
| 145 | if (Pressed(2, 11)) { |
| 146 | // FIRE |
| 147 | index_goal->goal_state = 4; |
| 148 | } else if (shooter_goal->velocity != 0) { |
| 149 | // get ready to shoot |
| 150 | index_goal->goal_state = 3; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame^] | 151 | } else if (Pressed(2, 10)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 152 | // intake |
| 153 | index_goal->goal_state = 2; |
| 154 | } else { |
| 155 | // get ready to intake |
| 156 | index_goal->goal_state = 1; |
| 157 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 158 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 159 | index_goal.Send(); |
| 160 | shooter_goal.Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | } // namespace frc971 |
| 166 | |
| 167 | AOS_RUN(frc971::JoystickReader) |