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