Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cmath> |
| 4 | #include <cstdio> |
| 5 | #include <cstring> |
| 6 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 7 | #include "aos/actions/actions.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 8 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/time/time.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 11 | #include "aos/util/log_interval.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 13 | #include "frc971/input/action_joystick_input.h" |
| 14 | #include "frc971/input/driver_station_data.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 15 | #include "y2014/actors/shoot_actor.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 16 | #include "y2014/constants.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "y2014/control_loops/claw/claw_goal_generated.h" |
| 18 | #include "y2014/control_loops/claw/claw_status_generated.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 19 | #include "y2014/control_loops/drivetrain/drivetrain_base.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | #include "y2014/control_loops/shooter/shooter_goal_generated.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 21 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 22 | using ::frc971::input::driver_station::ButtonLocation; |
| 23 | using ::frc971::input::driver_station::ControlBit; |
| 24 | using ::frc971::input::driver_station::JoystickAxis; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 25 | |
| 26 | #define OLD_DS 0 |
| 27 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 28 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 29 | namespace input { |
| 30 | namespace joysticks { |
| 31 | |
| 32 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 33 | kDriveControlLoopEnable2(1, 11); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 34 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 35 | const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 36 | const ButtonLocation kQuickTurn(1, 5); |
| 37 | |
| 38 | const ButtonLocation kCatch(3, 10); |
| 39 | |
| 40 | #if OLD_DS |
| 41 | const ButtonLocation kFire(3, 11); |
| 42 | const ButtonLocation kUnload(1, 4); |
| 43 | const ButtonLocation kReload(1, 2); |
| 44 | |
| 45 | const ButtonLocation kRollersOut(3, 12); |
| 46 | const ButtonLocation kRollersIn(3, 7); |
| 47 | |
| 48 | const ButtonLocation kTuck(3, 9); |
| 49 | const ButtonLocation kIntakePosition(3, 8); |
| 50 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 51 | const ButtonLocation kVerticalTuck(3, 1); |
| 52 | const JoystickAxis kFlipRobot(3, 3); |
| 53 | |
| 54 | const ButtonLocation kLongShot(3, 5); |
| 55 | const ButtonLocation kCloseShot(3, 2); |
| 56 | const ButtonLocation kFenderShot(3, 3); |
| 57 | const ButtonLocation kTrussShot(2, 11); |
| 58 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 59 | #else |
| 60 | const ButtonLocation kFire(3, 9); |
| 61 | const ButtonLocation kUnload(1, 4); |
| 62 | const ButtonLocation kReload(1, 2); |
| 63 | |
| 64 | const ButtonLocation kRollersOut(3, 8); |
| 65 | const ButtonLocation kRollersIn(3, 3); |
| 66 | |
| 67 | const ButtonLocation kTuck(3, 4); |
| 68 | const ButtonLocation kIntakePosition(3, 5); |
| 69 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 70 | const ButtonLocation kVerticalTuck(2, 6); |
| 71 | const JoystickAxis kFlipRobot(3, 3); |
| 72 | |
| 73 | const ButtonLocation kLongShot(3, 7); |
| 74 | const ButtonLocation kCloseShot(3, 6); |
| 75 | const ButtonLocation kFenderShot(3, 2); |
| 76 | const ButtonLocation kTrussShot(2, 11); |
| 77 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 78 | #endif |
| 79 | |
| 80 | const ButtonLocation kUserLeft(2, 7); |
| 81 | const ButtonLocation kUserRight(2, 10); |
| 82 | |
| 83 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 84 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 85 | |
| 86 | struct ClawGoal { |
| 87 | double angle; |
| 88 | double separation; |
| 89 | }; |
| 90 | |
| 91 | struct ShotGoal { |
| 92 | ClawGoal claw; |
| 93 | double shot_power; |
| 94 | double velocity_compensation; |
| 95 | double intake_power; |
| 96 | }; |
| 97 | |
| 98 | const double kIntakePower = 4.0; |
| 99 | // In case we have to quickly adjust it. |
| 100 | const double kGrabSeparation = 0; |
| 101 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 102 | |
| 103 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
| 104 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
| 105 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 106 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
| 107 | |
| 108 | // TODO(austin): Tune these by hand... |
| 109 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
| 110 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
| 111 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 112 | |
| 113 | // 34" between near edge of colored line and rear edge of bumper. |
| 114 | // Only works running? |
| 115 | const ShotGoal kLongShotGoal = { |
| 116 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
| 117 | // old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 118 | const ShotGoal kFlippedLongShotGoal = { |
| 119 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
| 120 | // old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
| 121 | |
| 122 | // 78" between near edge of colored line and rear edge of bumper. |
| 123 | const ShotGoal kCloseShotGoal = { |
| 124 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 125 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 126 | const ShotGoal kFlippedMediumShotGoal = { |
| 127 | {0.865, kShootSeparation}, 120, 0.2, kIntakePower}; |
| 128 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 129 | |
| 130 | // Shot from the fender. |
| 131 | const ShotGoal kFenderShotGoal = { |
| 132 | {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 133 | const ShotGoal kFlippedShortShotGoal = { |
| 134 | {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 135 | |
| 136 | const ShotGoal kHumanShotGoal = { |
| 137 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 138 | const ShotGoal kFlippedHumanShotGoal = { |
| 139 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
| 140 | const ShotGoal kTrussShotGoal = { |
| 141 | {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower}; |
| 142 | const ShotGoal kFlippedTrussShotGoal = { |
| 143 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 144 | |
| 145 | const ShotGoal kFlippedDemoShotGoal = { |
| 146 | {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower}; |
| 147 | const ShotGoal kDemoShotGoal = { |
| 148 | {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower}; |
| 149 | |
| 150 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 151 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
| 152 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 153 | class Reader : public ::frc971::input::ActionJoystickInput { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 154 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 155 | Reader(::aos::EventLoop *event_loop) |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 156 | : ::frc971::input::ActionJoystickInput( |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 157 | event_loop, control_loops::GetDrivetrainConfig(), |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 158 | ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel, |
| 159 | {}), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 160 | claw_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 161 | event_loop->MakeFetcher<::y2014::control_loops::claw::Status>( |
| 162 | "/claw")), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 163 | claw_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | event_loop->MakeSender<::y2014::control_loops::claw::Goal>( |
| 165 | "/claw")), |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 166 | shooter_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 167 | event_loop->MakeSender<::y2014::control_loops::shooter::Goal>( |
| 168 | "/shooter")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 169 | drivetrain_status_fetcher_( |
| 170 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | ->MakeFetcher<::frc971::control_loops::drivetrain::Status>( |
| 172 | "/drivetrain")), |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 173 | shot_power_(80.0), |
| 174 | goal_angle_(0.0), |
| 175 | separation_angle_(kGrabSeparation), |
| 176 | velocity_compensation_(0.0), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 177 | intake_power_(0.0), |
| 178 | shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {} |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 179 | |
| 180 | void SetGoal(ClawGoal goal) { |
| 181 | goal_angle_ = goal.angle; |
| 182 | separation_angle_ = goal.separation; |
| 183 | moving_for_shot_ = false; |
| 184 | velocity_compensation_ = 0.0; |
| 185 | intake_power_ = 0.0; |
| 186 | } |
| 187 | |
| 188 | void SetGoal(ShotGoal goal) { |
| 189 | goal_angle_ = goal.claw.angle; |
| 190 | shot_separation_angle_ = goal.claw.separation; |
| 191 | separation_angle_ = kGrabSeparation; |
| 192 | moving_for_shot_ = true; |
| 193 | shot_power_ = goal.shot_power; |
| 194 | velocity_compensation_ = goal.velocity_compensation; |
| 195 | intake_power_ = goal.intake_power; |
| 196 | } |
| 197 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 198 | void HandleTeleop( |
| 199 | const ::frc971::input::driver_station::Data &data) override { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 200 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 201 | intake_power_ = 0.0; |
| 202 | separation_angle_ = kGrabSeparation; |
| 203 | moving_for_shot_ = false; |
| 204 | } |
| 205 | |
| 206 | static const double kAdjustClawGoalDeadband = 0.08; |
| 207 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 208 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 209 | claw_goal_adjust = 0; |
| 210 | } else { |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 211 | claw_goal_adjust = (claw_goal_adjust - ((claw_goal_adjust < 0) |
| 212 | ? -kAdjustClawGoalDeadband |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 213 | : kAdjustClawGoalDeadband)) * |
| 214 | 0.035; |
| 215 | } |
| 216 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 217 | if (OLD_DS || |
| 218 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 219 | claw_separation_adjust = 0; |
| 220 | } else { |
| 221 | claw_separation_adjust = |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 222 | (claw_separation_adjust - ((claw_separation_adjust < 0) |
| 223 | ? -kAdjustClawGoalDeadband |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 224 | : kAdjustClawGoalDeadband)) * |
| 225 | -0.035; |
| 226 | } |
| 227 | |
| 228 | #if OLD_DS |
| 229 | if (data.IsPressed(kFenderShot)) { |
| 230 | #else |
| 231 | if (data.GetAxis(kFlipRobot) > 0.9) { |
| 232 | #endif |
| 233 | claw_goal_adjust += claw_separation_adjust; |
| 234 | claw_goal_adjust *= -1; |
| 235 | |
| 236 | if (data.IsPressed(kIntakeOpenPosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 237 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 238 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 239 | SetGoal(kFlippedIntakeOpenGoal); |
| 240 | } else if (data.IsPressed(kIntakePosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 241 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 242 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 243 | SetGoal(kFlippedIntakeGoal); |
| 244 | } else if (data.IsPressed(kVerticalTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 245 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 246 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 247 | SetGoal(kVerticalTuckGoal); |
| 248 | } else if (data.IsPressed(kTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 249 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 250 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 251 | SetGoal(kFlippedTuckGoal); |
| 252 | } else if (data.PosEdge(kLongShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 253 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 254 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 255 | SetGoal(kFlippedLongShotGoal); |
| 256 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 257 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 258 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 259 | SetGoal(kFlippedMediumShotGoal); |
| 260 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 261 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 262 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 263 | SetGoal(kFlippedShortShotGoal); |
| 264 | } else if (data.PosEdge(kHumanPlayerShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 265 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 266 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 267 | SetGoal(kFlippedHumanShotGoal); |
| 268 | } else if (data.PosEdge(kUserLeft)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 269 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 270 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 271 | SetGoal(kFlipped254PassGoal); |
| 272 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 273 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 274 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 275 | SetGoal(kFlippedDemoShotGoal); |
| 276 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 277 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 278 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 279 | SetGoal(kFlippedTrussShotGoal); |
| 280 | } |
| 281 | } else { |
| 282 | if (data.IsPressed(kIntakeOpenPosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 283 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 284 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 285 | SetGoal(kIntakeOpenGoal); |
| 286 | } else if (data.IsPressed(kIntakePosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 287 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 288 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 289 | SetGoal(kIntakeGoal); |
| 290 | } else if (data.IsPressed(kVerticalTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 291 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 292 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 293 | SetGoal(kVerticalTuckGoal); |
| 294 | } else if (data.IsPressed(kTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 295 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 296 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 297 | SetGoal(kTuckGoal); |
| 298 | } else if (data.PosEdge(kLongShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 299 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 300 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 301 | SetGoal(kLongShotGoal); |
| 302 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 303 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 304 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 305 | SetGoal(kCloseShotGoal); |
| 306 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 307 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 308 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 309 | SetGoal(kFenderShotGoal); |
| 310 | } else if (data.PosEdge(kHumanPlayerShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 311 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 312 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 313 | SetGoal(kHumanShotGoal); |
| 314 | } else if (data.PosEdge(kUserLeft)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 315 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 316 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 317 | SetGoal(k254PassGoal); |
| 318 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 319 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 320 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 321 | SetGoal(kDemoShotGoal); |
| 322 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 323 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 324 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 325 | SetGoal(kTrussShotGoal); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (data.PosEdge(kFire)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 330 | aos::common::actions::DoubleParamT param; |
| 331 | EnqueueAction(shoot_action_factory_.Make(param)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 332 | } else if (data.NegEdge(kFire)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 333 | CancelCurrentAction(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 336 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 337 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 338 | AOS_LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 339 | intake_power_ = 0.0; |
| 340 | velocity_compensation_ = 0.0; |
| 341 | } |
| 342 | |
| 343 | // Send out the claw and shooter goals if no actions are running. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 344 | if (!ActionRunning()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 345 | goal_angle_ += claw_goal_adjust; |
| 346 | separation_angle_ += claw_separation_adjust; |
| 347 | |
| 348 | // If the action just ended, turn the intake off and stop velocity |
| 349 | // compensating. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 350 | if (was_running_action()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 351 | intake_power_ = 0.0; |
| 352 | velocity_compensation_ = 0.0; |
| 353 | } |
| 354 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 355 | drivetrain_status_fetcher_.Fetch(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 356 | double goal_angle = goal_angle_; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 357 | if (drivetrain_status_fetcher_.get()) { |
| 358 | goal_angle += |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 359 | SpeedToAngleOffset(drivetrain_status_fetcher_->robot_speed()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 360 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 361 | AOS_LOG_INTERVAL(no_drivetrain_status_); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (moving_for_shot_) { |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 365 | claw_status_fetcher_.Fetch(); |
| 366 | if (claw_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | if (::std::abs(claw_status_fetcher_->bottom() - goal_angle) < 0.2) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 368 | moving_for_shot_ = false; |
| 369 | separation_angle_ = shot_separation_angle_; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | double separation_angle = separation_angle_; |
| 375 | |
| 376 | if (data.IsPressed(kCatch)) { |
| 377 | const double kCatchSeparation = 1.0; |
| 378 | goal_angle -= kCatchSeparation / 2.0; |
| 379 | separation_angle = kCatchSeparation; |
| 380 | } |
| 381 | |
| 382 | bool intaking = |
| 383 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 384 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 385 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 386 | auto builder = claw_goal_sender_.MakeBuilder(); |
| 387 | control_loops::claw::Goal::Builder goal_builder = |
| 388 | builder.MakeBuilder<control_loops::claw::Goal>(); |
| 389 | goal_builder.add_bottom_angle(goal_angle); |
| 390 | goal_builder.add_separation_angle(separation_angle); |
| 391 | goal_builder.add_intake( |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 392 | intaking ? 12.0 |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 393 | : (data.IsPressed(kRollersOut) ? -12.0 : intake_power_)); |
| 394 | goal_builder.add_centering(intaking ? 12.0 : 0.0); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 395 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 396 | if (builder.Send(goal_builder.Finish()) != |
| 397 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 398 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 399 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 402 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 403 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 404 | control_loops::shooter::Goal::Builder goal_builder = |
| 405 | builder.MakeBuilder<control_loops::shooter::Goal>(); |
| 406 | goal_builder.add_shot_power(shot_power_); |
| 407 | goal_builder.add_shot_requested(data.IsPressed(kFire)); |
| 408 | goal_builder.add_unload_requested(data.IsPressed(kUnload)); |
| 409 | goal_builder.add_load_requested(data.IsPressed(kReload)); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 410 | if (builder.Send(goal_builder.Finish()) != |
| 411 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 412 | AOS_LOG(WARNING, "sending shooter goal failed\n"); |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame] | 413 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | double SpeedToAngleOffset(double speed) { |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 419 | const ::y2014::constants::Values &values = ::y2014::constants::GetValues(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 420 | // scale speed to a [0.0-1.0] on something close to the max |
| 421 | // TODO(austin): Change the scale factor for different shots. |
| 422 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
| 423 | } |
| 424 | |
| 425 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 426 | ::aos::Fetcher<::y2014::control_loops::claw::Status> claw_status_fetcher_; |
| 427 | ::aos::Sender<::y2014::control_loops::claw::Goal> claw_goal_sender_; |
| 428 | ::aos::Sender<::y2014::control_loops::shooter::Goal> shooter_goal_sender_; |
| 429 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Status> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 430 | drivetrain_status_fetcher_; |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 431 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 432 | double shot_power_; |
| 433 | double goal_angle_; |
| 434 | double separation_angle_, shot_separation_angle_; |
| 435 | double velocity_compensation_; |
| 436 | double intake_power_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 437 | bool moving_for_shot_ = false; |
| 438 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 439 | actors::ShootActor::Factory shoot_action_factory_; |
| 440 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 441 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 442 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 443 | "no drivetrain status"); |
| 444 | }; |
| 445 | |
| 446 | } // namespace joysticks |
| 447 | } // namespace input |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 448 | } // namespace y2014 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 449 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 450 | int main(int argc, char **argv) { |
| 451 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 452 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 454 | aos::configuration::ReadConfig("config.json"); |
| 455 | |
| 456 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 457 | ::y2014::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 458 | |
| 459 | event_loop.Run(); |
| 460 | |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 461 | return 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 462 | } |