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 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 6 | #include "aos/atom_code/init.h" |
| 7 | #include "aos/atom_code/input/joystick_input.h" |
| 8 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 13 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/index/index_motor.q.h" |
| 15 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
Brian Silverman | 32d6914 | 2013-03-30 00:02:06 -0700 | [diff] [blame] | 16 | #include "frc971/queues/CameraTarget.q.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
| 18 | using ::frc971::control_loops::drivetrain; |
| 19 | using ::frc971::control_loops::shifters; |
| 20 | using ::frc971::sensors::gyro; |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 21 | using ::frc971::control_loops::index_loop; |
| 22 | using ::frc971::control_loops::shooter; |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 23 | using ::frc971::control_loops::hangers; |
Brian Silverman | 32d6914 | 2013-03-30 00:02:06 -0700 | [diff] [blame] | 24 | using ::frc971::vision::target_angle; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 26 | using ::aos::input::driver_station::ButtonLocation; |
| 27 | using ::aos::input::driver_station::JoystickAxis; |
| 28 | using ::aos::input::driver_station::ControlBit; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 30 | namespace bot3 { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 31 | namespace input { |
| 32 | namespace joysticks { |
| 33 | |
| 34 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 35 | kDriveControlLoopEnable2(1, 11); |
| 36 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 37 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 38 | const ButtonLocation kQuickTurn(1, 5); |
| 39 | |
| 40 | const ButtonLocation kLongShot(3, 5); |
| 41 | const ButtonLocation kMediumShot(3, 3); |
| 42 | const ButtonLocation kShortShot(3, 6); |
| 43 | const ButtonLocation kPitShot1(2, 7), kPitShot2(2, 10); |
| 44 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 45 | const ButtonLocation kFire(3, 11); |
| 46 | const ButtonLocation kIntake(3, 10); |
| 47 | const ButtonLocation kForceFire(3, 12); |
| 48 | const ButtonLocation kForceIndexUp(3, 9), kForceIndexDown(3, 7); |
| 49 | |
| 50 | const ButtonLocation kDeployHangers(3, 1); |
| 51 | |
| 52 | class Reader : public ::aos::input::JoystickInput { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | public: |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 54 | static const bool kWristAlwaysDown = false; |
| 55 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 56 | Reader() { |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 57 | printf("\nRunning Bot3 JoystickReader!\n"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | shifters.MakeWithBuilder().set(true).Send(); |
| 59 | } |
| 60 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 61 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 62 | static bool is_high_gear = false; |
| 63 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 64 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 65 | if (data.PosEdge(ControlBit::kEnabled)){ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | LOG(INFO, "Starting auto mode\n"); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 67 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 68 | .run_auto(true).Send(); |
| 69 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 70 | LOG(INFO, "Stopping auto mode\n"); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 71 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 72 | .run_auto(false).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | } |
| 74 | } else { // teleop |
| 75 | bool is_control_loop_driving = false; |
| 76 | double left_goal = 0.0; |
| 77 | double right_goal = 0.0; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 78 | const double wheel = data.GetAxis(kSteeringWheel); |
Brian Silverman | c6064c1 | 2013-08-31 10:58:54 -0700 | [diff] [blame] | 79 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 834a0da | 2013-03-16 23:49:27 -0700 | [diff] [blame] | 80 | LOG(DEBUG, "wheel %f throttle %f\n", wheel, throttle); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 81 | const double kThrottleGain = 1.0 / 2.5; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 82 | if (data.IsPressed(kDriveControlLoopEnable1) || |
| 83 | data.IsPressed(kDriveControlLoopEnable2)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | static double distance = 0.0; |
| 85 | static double angle = 0.0; |
| 86 | static double filtered_goal_distance = 0.0; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 87 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 88 | data.PosEdge(kDriveControlLoopEnable2)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 90 | distance = (drivetrain.position->left_encoder + |
| 91 | drivetrain.position->right_encoder) / 2.0 |
| 92 | - throttle * kThrottleGain / 2.0; |
| 93 | angle = gyro->angle; |
| 94 | filtered_goal_distance = distance; |
| 95 | } |
| 96 | } |
| 97 | is_control_loop_driving = true; |
| 98 | |
| 99 | //const double gyro_angle = Gyro.View().angle; |
| 100 | const double goal_theta = angle - wheel * 0.27; |
| 101 | const double goal_distance = distance + throttle * kThrottleGain; |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 102 | //TODO(danielp) Change this after a look in the CAD. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 104 | const double kMaxVelocity = 0.6; |
| 105 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 106 | filtered_goal_distance += kMaxVelocity * 0.02; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 107 | } else if (goal_distance < -kMaxVelocity * 0.02 + |
| 108 | filtered_goal_distance) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 110 | } else { |
| 111 | filtered_goal_distance = goal_distance; |
| 112 | } |
| 113 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 114 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 115 | is_high_gear = false; |
| 116 | |
| 117 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 118 | } |
| 119 | if (!(drivetrain.goal.MakeWithBuilder() |
| 120 | .steering(wheel) |
| 121 | .throttle(throttle) |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 122 | .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 123 | .control_loop_driving(is_control_loop_driving) |
| 124 | .left_goal(left_goal).right_goal(right_goal).Send())) { |
| 125 | LOG(WARNING, "sending stick values failed\n"); |
| 126 | } |
| 127 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 128 | if (data.PosEdge(kShiftHigh)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 129 | is_high_gear = false; |
| 130 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 131 | if (data.PosEdge(kShiftLow)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | is_high_gear = true; |
| 133 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 134 | |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 135 | ::aos::ScopedMessagePtr<frc971::control_loops::ShooterLoop::Goal> shooter_goal = |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 136 | shooter.goal.MakeMessage(); |
| 137 | shooter_goal->velocity = 0; |
Brian Silverman | f34bbe0 | 2013-09-01 09:08:32 -0700 | [diff] [blame] | 138 | if (data.IsPressed(kPitShot1) && data.IsPressed(kPitShot2)) { |
| 139 | shooter_goal->velocity = 131; |
Brian Silverman | f34bbe0 | 2013-09-01 09:08:32 -0700 | [diff] [blame] | 140 | } else if (data.IsPressed(kLongShot)) { |
Brian Silverman | 304b2bf | 2013-04-04 17:54:41 -0700 | [diff] [blame] | 141 | #if 0 |
Brian Silverman | 32d6914 | 2013-03-30 00:02:06 -0700 | [diff] [blame] | 142 | target_angle.FetchLatest(); |
| 143 | if (target_angle.IsNewerThanMS(500)) { |
| 144 | shooter_goal->velocity = target_angle->shooter_speed; |
| 145 | angle_adjust_goal = target_angle->shooter_angle; |
| 146 | // TODO(brians): do the math right here |
| 147 | wrist_up_position = 0.70; |
| 148 | } else { |
| 149 | LOG(WARNING, "camera frame too old\n"); |
| 150 | // pretend like no button is pressed |
| 151 | } |
Brian Silverman | 304b2bf | 2013-04-04 17:54:41 -0700 | [diff] [blame] | 152 | #endif |
Brian Silverman | e296ebd | 2013-04-05 13:51:13 -0700 | [diff] [blame] | 153 | shooter_goal->velocity = 360; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 154 | } else if (data.IsPressed(kMediumShot)) { |
Brian Silverman | 947735d | 2013-03-22 15:15:58 -0700 | [diff] [blame] | 155 | #if 0 |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 156 | shooter_goal->velocity = 375; |
| 157 | wrist_up_position = 0.70; |
| 158 | angle_adjust_goal = 0.564; |
Brian Silverman | 947735d | 2013-03-22 15:15:58 -0700 | [diff] [blame] | 159 | #endif |
| 160 | // middle wheel on the back line (same as auto) |
Brian Silverman | e296ebd | 2013-04-05 13:51:13 -0700 | [diff] [blame] | 161 | shooter_goal->velocity = 395; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 162 | } else if (data.IsPressed(kShortShot)) { |
Brian Silverman | 906aef5 | 2013-03-17 23:37:41 -0700 | [diff] [blame] | 163 | shooter_goal->velocity = 375; |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 164 | } |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 165 | |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 166 | //TODO (daniel) Modify this for hopper. |
| 167 | ::aos::ScopedMessagePtr<frc971::control_loops::IndexLoop::Goal> index_goal = |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 168 | index_loop.goal.MakeMessage(); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 169 | if (data.IsPressed(kFire)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 170 | // FIRE |
| 171 | index_goal->goal_state = 4; |
| 172 | } else if (shooter_goal->velocity != 0) { |
| 173 | // get ready to shoot |
| 174 | index_goal->goal_state = 3; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 175 | } else if (data.IsPressed(kIntake)) { |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 176 | // intake |
| 177 | index_goal->goal_state = 2; |
| 178 | } else { |
| 179 | // get ready to intake |
| 180 | index_goal->goal_state = 1; |
| 181 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 182 | index_goal->force_fire = data.IsPressed(kForceFire); |
Brian Silverman | 687f524 | 2013-03-16 13:57:59 -0700 | [diff] [blame] | 183 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 184 | const bool index_up = data.IsPressed(kForceIndexUp); |
| 185 | const bool index_down = data.IsPressed(kForceIndexDown); |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 186 | index_goal->override_index = index_up || index_down; |
| 187 | if (index_up && index_down) { |
| 188 | index_goal->index_voltage = 0.0; |
| 189 | } else if (index_up) { |
| 190 | index_goal->index_voltage = 12.0; |
| 191 | } else if (index_down) { |
| 192 | index_goal->index_voltage = -12.0; |
| 193 | } |
| 194 | |
Brian Silverman | 8a82f38 | 2013-03-16 14:12:01 -0700 | [diff] [blame] | 195 | index_goal.Send(); |
| 196 | shooter_goal.Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 197 | } |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 198 | |
| 199 | static int hanger_cycles = 0; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 200 | if (data.IsPressed(kDeployHangers)) { |
Brian Silverman | 513ad4e | 2013-03-20 19:59:50 -0700 | [diff] [blame] | 201 | ++hanger_cycles; |
| 202 | } else { |
| 203 | hanger_cycles = 0; |
| 204 | } |
| 205 | hangers.MakeWithBuilder().set(hanger_cycles >= 10).Send(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 209 | } // namespace joysticks |
| 210 | } // namespace input |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 211 | } // namespace bot3 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 212 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 213 | int main() { |
| 214 | ::aos::Init(); |
Daniel Petti | 3fe3654 | 2013-09-25 04:18:24 +0000 | [diff] [blame^] | 215 | ::bot3::input::joysticks::Reader reader; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 216 | reader.Run(); |
| 217 | ::aos::Cleanup(); |
| 218 | } |