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