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 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 11 | #include "frc971/queues/GyroAngle.q.h" |
| 12 | #include "frc971/queues/Piston.q.h" |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 14 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 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; |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 26 | using ::frc971::control_loops::hangers; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | |
| 28 | namespace frc971 { |
| 29 | |
| 30 | class JoystickReader : public aos::JoystickInput { |
| 31 | public: |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 32 | static const bool kWristAlwaysDown = false; |
| 33 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 34 | JoystickReader() : aos::JoystickInput() { |
| 35 | shifters.MakeWithBuilder().set(true).Send(); |
| 36 | } |
| 37 | |
| 38 | virtual void RunIteration() { |
| 39 | static bool is_high_gear = false; |
| 40 | |
| 41 | if (Pressed(0, AUTONOMOUS)) { |
| 42 | if (PosEdge(0, ENABLED)){ |
| 43 | LOG(INFO, "Starting auto mode\n"); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 44 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 45 | } |
| 46 | if (NegEdge(0, ENABLED)) { |
| 47 | LOG(INFO, "Stopping auto mode\n"); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 48 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 49 | } |
| 50 | } else { // teleop |
| 51 | bool is_control_loop_driving = false; |
| 52 | double left_goal = 0.0; |
| 53 | double right_goal = 0.0; |
| 54 | const double wheel = control_data_.stick0Axis1 / 127.0; |
| 55 | const double throttle = -control_data_.stick1Axis2 / 127.0; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame] | 56 | LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 57 | const double kThrottleGain = 1.0 / 2.5; |
| 58 | if (Pressed(0, 7) || Pressed(0, 11)) { |
| 59 | static double distance = 0.0; |
| 60 | static double angle = 0.0; |
| 61 | static double filtered_goal_distance = 0.0; |
| 62 | if (PosEdge(0, 7) || PosEdge(0, 11)) { |
| 63 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 64 | distance = (drivetrain.position->left_encoder + |
| 65 | drivetrain.position->right_encoder) / 2.0 |
| 66 | - throttle * kThrottleGain / 2.0; |
| 67 | angle = gyro->angle; |
| 68 | filtered_goal_distance = distance; |
| 69 | } |
| 70 | } |
| 71 | is_control_loop_driving = true; |
| 72 | |
| 73 | //const double gyro_angle = Gyro.View().angle; |
| 74 | const double goal_theta = angle - wheel * 0.27; |
| 75 | const double goal_distance = distance + throttle * kThrottleGain; |
| 76 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 77 | const double kMaxVelocity = 0.6; |
| 78 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 79 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 80 | } else if (goal_distance < -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 81 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 82 | } else { |
| 83 | filtered_goal_distance = goal_distance; |
| 84 | } |
| 85 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 86 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 87 | is_high_gear = false; |
| 88 | |
| 89 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 90 | } |
| 91 | if (!(drivetrain.goal.MakeWithBuilder() |
| 92 | .steering(wheel) |
| 93 | .throttle(throttle) |
| 94 | .highgear(is_high_gear).quickturn(Pressed(0, 5)) |
| 95 | .control_loop_driving(is_control_loop_driving) |
| 96 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 97 | LOG(WARNING, "sending stick values failed\n"); |
| 98 | } |
| 99 | |
| 100 | if (PosEdge(1, 1)) { |
| 101 | is_high_gear = false; |
| 102 | } |
| 103 | if (PosEdge(1, 3)) { |
| 104 | is_high_gear = true; |
| 105 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 106 | |
Brian Silverman | 1c0cb8b | 2013-03-15 23:19:59 -0700 | [diff] [blame] | 107 | // Where the wrist should be to pick up a frisbee. |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 108 | // TODO(brians): Make these globally accessible and clean up auto. |
Brian Silverman | bcaf384 | 2013-03-16 23:49:35 -0700 | [diff] [blame] | 109 | static const double kWristPickup = -0.633; |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 110 | static const double kWristNearGround = -0.4; |
Brian Silverman | 1c0cb8b | 2013-03-15 23:19:59 -0700 | [diff] [blame] | 111 | // Where the wrist gets stored when up. |
| 112 | // All the way up is 1.5. |
| 113 | static const double kWristUp = 1.43; |
| 114 | static double wrist_down_position = kWristPickup; |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 115 | double wrist_up_position = kWristUp; |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 116 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 117 | ::aos::ScopedMessagePtr<control_loops::ShooterLoop::Goal> shooter_goal = |
| 118 | shooter.goal.MakeMessage(); |
| 119 | shooter_goal->velocity = 0; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame] | 120 | static double angle_adjust_goal = 0.42; |
| 121 | if (Pressed(2, 5)) { |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 122 | // long shot |
Brian Silverman | c527754 | 2013-03-22 13:33:07 -0700 | [diff] [blame] | 123 | shooter_goal->velocity = 131; |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 124 | angle_adjust_goal = 0.70; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame] | 125 | } else if (Pressed(2, 3)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 126 | // medium shot |
Brian Silverman | 947735d | 2013-03-22 15:15:58 -0700 | [diff] [blame^] | 127 | #if 0 |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 128 | shooter_goal->velocity = 375; |
| 129 | wrist_up_position = 0.70; |
| 130 | angle_adjust_goal = 0.564; |
Brian Silverman | 947735d | 2013-03-22 15:15:58 -0700 | [diff] [blame^] | 131 | #endif |
| 132 | // middle wheel on the back line (same as auto) |
| 133 | shooter_goal->velocity = 410; |
| 134 | wrist_up_position = 1.23 - 0.4; |
| 135 | angle_adjust_goal = 0.5101; |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 136 | } else if (Pressed(2, 6)) { |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 137 | // short shot |
| 138 | shooter_goal->velocity = 375; |
Brian Silverman | 947735d | 2013-03-22 15:15:58 -0700 | [diff] [blame^] | 139 | angle_adjust_goal = 0.7267; |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 140 | } |
| 141 | angle_adjust.goal.MakeWithBuilder().goal(angle_adjust_goal).Send(); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 142 | |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 143 | double wrist_pickup_position = Pressed(2, 10) /*intake*/ ? |
| 144 | kWristPickup : kWristNearGround; |
| 145 | index_loop.status.FetchLatest(); |
| 146 | if (index_loop.status.get()) { |
| 147 | if (index_loop.status->hopper_disc_count >= 4) { |
| 148 | wrist_down_position = kWristNearGround; |
| 149 | } else { |
| 150 | wrist_down_position = wrist_pickup_position; |
| 151 | } |
| 152 | } |
| 153 | wrist.goal.MakeWithBuilder() |
| 154 | .goal(Pressed(2, 8) ? wrist_down_position : wrist_up_position).Send(); |
| 155 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 156 | ::aos::ScopedMessagePtr<control_loops::IndexLoop::Goal> index_goal = |
| 157 | index_loop.goal.MakeMessage(); |
| 158 | // TODO(brians): replace these with the enum values |
| 159 | if (Pressed(2, 11)) { |
| 160 | // FIRE |
| 161 | index_goal->goal_state = 4; |
| 162 | } else if (shooter_goal->velocity != 0) { |
| 163 | // get ready to shoot |
| 164 | index_goal->goal_state = 3; |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame] | 165 | } else if (Pressed(2, 10)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 166 | // intake |
| 167 | index_goal->goal_state = 2; |
| 168 | } else { |
| 169 | // get ready to intake |
| 170 | index_goal->goal_state = 1; |
| 171 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 172 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 173 | index_goal.Send(); |
| 174 | shooter_goal.Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 175 | } |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 176 | |
| 177 | static int hanger_cycles = 0; |
Brian Silverman | d3f92f1 | 2013-03-20 23:01:33 -0700 | [diff] [blame] | 178 | if (Pressed(2, 1)) { |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 179 | ++hanger_cycles; |
| 180 | } else { |
| 181 | hanger_cycles = 0; |
| 182 | } |
| 183 | hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | |
| 187 | } // namespace frc971 |
| 188 | |
| 189 | AOS_RUN(frc971::JoystickReader) |