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