Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "aos/linux_code/init.h" |
| 7 | #include "aos/prime/input/joystick_input.h" |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 8 | #include "aos/common/input/driver_station_data.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/queues/gyro_angle.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 13 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/claw/claw.q.h" |
| 15 | #include "frc971/control_loops/shooter/shooter.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 16 | |
| 17 | using ::frc971::control_loops::drivetrain; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 18 | using ::frc971::sensors::gyro; |
| 19 | |
| 20 | using ::aos::input::driver_station::ButtonLocation; |
| 21 | using ::aos::input::driver_station::JoystickAxis; |
| 22 | using ::aos::input::driver_station::ControlBit; |
| 23 | |
| 24 | namespace frc971 { |
| 25 | namespace input { |
| 26 | namespace joysticks { |
| 27 | |
| 28 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 29 | kDriveControlLoopEnable2(1, 11); |
| 30 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 31 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 32 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 34 | const ButtonLocation kFire(3, 11); |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 35 | const ButtonLocation kUnload(2, 11); |
| 36 | const ButtonLocation kReload(2, 6); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 37 | |
| 38 | const ButtonLocation kRollersOut(3, 12); |
| 39 | const ButtonLocation kRollersIn(3, 10); |
| 40 | |
| 41 | const ButtonLocation kTuck(3, 8); |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 42 | const ButtonLocation kIntakeOpenPosition(3, 9); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 43 | const ButtonLocation kIntakePosition(3, 7); |
| 44 | |
| 45 | const ButtonLocation kLongShot(3, 5); |
| 46 | const ButtonLocation kMediumShot(3, 3); |
| 47 | const ButtonLocation kShortShot(3, 6); |
| 48 | |
| 49 | struct ClawGoal { |
| 50 | double angle; |
| 51 | double separation; |
| 52 | }; |
| 53 | |
| 54 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
| 55 | const ClawGoal kIntakeGoal = {-2.273474, 0.0}; |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 56 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.2}; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 57 | |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 58 | const ClawGoal kLongShotGoal = {-M_PI / 2.0 + 0.43, 0.10}; |
| 59 | const ClawGoal kMediumShotGoal = {-0.9, 0.10}; |
| 60 | const ClawGoal kShortShotGoal = {-0.04, 0.11}; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 61 | |
| 62 | class Reader : public ::aos::input::JoystickInput { |
| 63 | public: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 64 | Reader() |
| 65 | : is_high_gear_(false), |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 66 | shot_power_(80.0), |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 67 | goal_angle_(0.0), |
| 68 | separation_angle_(0.0) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 69 | |
| 70 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 71 | |
| 72 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 73 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 74 | LOG(INFO, "Starting auto mode\n"); |
| 75 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 76 | .run_auto(true).Send(); |
| 77 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 78 | LOG(INFO, "Stopping auto mode\n"); |
| 79 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 80 | .run_auto(false).Send(); |
| 81 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 82 | } else { |
| 83 | HandleTeleop(data); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 86 | |
| 87 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 88 | bool is_control_loop_driving = false; |
| 89 | double left_goal = 0.0; |
| 90 | double right_goal = 0.0; |
| 91 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 92 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 93 | const double kThrottleGain = 1.0 / 2.5; |
| 94 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 95 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 96 | // TODO(austin): Static sucks! |
| 97 | static double distance = 0.0; |
| 98 | static double angle = 0.0; |
| 99 | static double filtered_goal_distance = 0.0; |
| 100 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 101 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 102 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 103 | distance = (drivetrain.position->left_encoder + |
| 104 | drivetrain.position->right_encoder) / |
| 105 | 2.0 - |
| 106 | throttle * kThrottleGain / 2.0; |
| 107 | angle = gyro->angle; |
| 108 | filtered_goal_distance = distance; |
| 109 | } |
| 110 | } |
| 111 | is_control_loop_driving = true; |
| 112 | |
| 113 | // const double gyro_angle = Gyro.View().angle; |
| 114 | const double goal_theta = angle - wheel * 0.27; |
| 115 | const double goal_distance = distance + throttle * kThrottleGain; |
| 116 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 117 | const double kMaxVelocity = 0.6; |
| 118 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 119 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 120 | } else if (goal_distance < |
| 121 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 122 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 123 | } else { |
| 124 | filtered_goal_distance = goal_distance; |
| 125 | } |
| 126 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 127 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 128 | is_high_gear_ = false; |
| 129 | |
| 130 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 131 | } |
| 132 | if (!drivetrain.goal.MakeWithBuilder() |
| 133 | .steering(wheel) |
| 134 | .throttle(throttle) |
| 135 | .highgear(is_high_gear_) |
| 136 | .quickturn(data.IsPressed(kQuickTurn)) |
| 137 | .control_loop_driving(is_control_loop_driving) |
| 138 | .left_goal(left_goal) |
| 139 | .right_goal(right_goal) |
| 140 | .Send()) { |
| 141 | LOG(WARNING, "sending stick values failed\n"); |
| 142 | } |
| 143 | if (data.PosEdge(kShiftHigh)) { |
| 144 | is_high_gear_ = false; |
| 145 | } |
| 146 | if (data.PosEdge(kShiftLow)) { |
| 147 | is_high_gear_ = true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void SetGoal(ClawGoal goal) { |
| 152 | goal_angle_ = goal.angle; |
| 153 | separation_angle_ = goal.separation; |
| 154 | } |
| 155 | |
| 156 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 157 | HandleDrivetrain(data); |
| 158 | |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 159 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 160 | SetGoal(kIntakeOpenGoal); |
| 161 | } else if (data.IsPressed(kIntakePosition)) { |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 162 | SetGoal(kIntakeGoal); |
| 163 | } else if (data.IsPressed(kTuck)) { |
| 164 | SetGoal(kTuckGoal); |
| 165 | } |
| 166 | |
| 167 | // TODO(austin): Wait for the claw to go to position before shooting, and |
| 168 | // open the claw as part of the actual fire step. |
| 169 | if (data.IsPressed(kLongShot)) { |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 170 | shot_power_ = 160.0; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 171 | SetGoal(kLongShotGoal); |
| 172 | } else if (data.IsPressed(kMediumShot)) { |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 173 | shot_power_ = 100.0; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 174 | SetGoal(kMediumShotGoal); |
| 175 | } else if (data.IsPressed(kShortShot)) { |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 176 | shot_power_ = 70.0; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 177 | SetGoal(kShortShotGoal); |
| 178 | } |
| 179 | |
| 180 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 181 | .bottom_angle(goal_angle_) |
| 182 | .separation_angle(separation_angle_) |
| 183 | .intake(data.IsPressed(kRollersIn) |
| 184 | ? 12.0 |
| 185 | : (data.IsPressed(kRollersOut) ? -12.0 : 0.0)) |
| 186 | .centering(data.IsPressed(kRollersIn) ? 12.0 : 0.0) |
| 187 | .Send()) { |
| 188 | LOG(WARNING, "sending claw goal failed\n"); |
| 189 | } |
| 190 | |
| 191 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 192 | .shot_power(shot_power_) |
| 193 | .shot_requested(data.IsPressed(kFire)) |
| 194 | .unload_requested(data.IsPressed(kUnload)) |
| 195 | .load_requested(data.IsPressed(kReload)) |
| 196 | .Send()) { |
| 197 | LOG(WARNING, "sending shooter goal failed\n"); |
| 198 | } |
| 199 | } |
| 200 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 201 | private: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 202 | bool is_high_gear_; |
| 203 | double shot_power_; |
| 204 | double goal_angle_; |
| 205 | double separation_angle_; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | } // namespace joysticks |
| 209 | } // namespace input |
| 210 | } // namespace frc971 |
| 211 | |
| 212 | int main() { |
| 213 | ::aos::Init(); |
| 214 | ::frc971::input::joysticks::Reader reader; |
| 215 | reader.Run(); |
| 216 | ::aos::Cleanup(); |
| 217 | } |