2022-Robot
Robot code for 2022 FRC Season by Argos, FRC team #1756
Loading...
Searching...
No Matches
shooter_subsystem.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <frc2/command/SubsystemBase.h>
8
9#include "Constants.h"
14#include "ctre/Phoenix.h"
15#include "networktables/NetworkTable.h"
16#include "networktables/NetworkTableEntry.h"
17#include "networktables/NetworkTableInstance.h"
18#include "networktables/NetworkTableValue.h"
19#include "photonlib/PhotonCamera.h"
21#include "units/angle.h"
22#include "units/angular_velocity.h"
23#include "units/length.h"
25
31 private:
32 units::degree_t m_pitch;
33 units::degree_t m_yaw;
34 double m_bboxHor;
35 double m_bboxVer;
36 units::degree_t m_skew;
38 units::millisecond_t m_pipelineLatency;
39 constexpr static units::millisecond_t m_miscLatency{11_ms};
40
41 public:
42 LimelightTarget() = default;
43
48 struct tValues {
49 units::degree_t pitch;
50 units::degree_t yaw;
51 double bboxHor;
52 double bboxVer;
53 units::degree_t skew;
54 units::millisecond_t totalLatency;
55 };
56
63
70 bool HasTarget();
71
79 void adjustPerspective(units::degree_t& currentPitch, const units::degree_t& currentYaw);
80};
81
85struct AimValues {
86 units::degree_t turretTarget;
87 units::degree_t hoodTarget;
88 units::angular_velocity::revolutions_per_minute_t shooterTarget;
89};
90
96 public:
98
100
106 std::optional<LimelightTarget> GetHighestTarget();
107
113 units::angle::degree_t VerticalPixelToAngle(int pixels);
114
120 units::angle::degree_t HorizontalPixelToAngle(int pixels);
121
127 units::angle::degree_t GetNewPitch(
128 units::degree_t cx, units::degree_t cy, int bboxHorizontalPixels, int bboxVerticalPixels, units::degree_t skew);
129
135 void SetDriverMode(bool mode);
136};
137
138class ShooterSubsystem : public frc2::SubsystemBase {
139 public:
144 units::revolutions_per_minute_t shooterSpeed;
145 units::degree_t hoodAngle;
146 };
147
149 units::feet_per_second_t radialVelocity;
150 units::feet_per_second_t tangentialVelocity;
151 units::degrees_per_second_t chassisYawRate;
152 };
153
157 struct AimOffsets {
158 units::foot_t distanceOffset;
159 units::degree_t yawOffset;
160 };
161
163 SwerveDriveSubsystem* pDriveSubsystem,
164 argos_lib::SwappableControllersSubsystem* controllers = nullptr);
165
169 enum class FixedPosState {
170 Front,
171 Left,
172 Right,
173 Back
174 };
175
182 std::optional<units::degree_t> GetTurretTargetAngle(LimelightTarget::tValues target);
183
190
194 void Periodic() override;
195
202 bool AutoAim(bool drivingAdjustment = false);
203
209 void Shoot(double ballfiringspeed);
210
217 void ManualAim(double turnSpeed, double hoodSpeed);
218
224 void CloseLoopShoot(units::revolutions_per_minute_t ShooterWheelSpeed);
225
231 units::revolutions_per_minute_t GetShooterWheelSpeed();
232
238 void MoveHood(double hoodSpeed);
239
245 void MoveTurret(double turnSpeed);
246
252 void HoodSetPosition(units::degree_t angle);
253
259 units::degree_t GetHoodPosition();
260
265 void UpdateHoodHome();
266
272 bool IsHoodMoving();
273
279 bool IsHoodHomed();
280
284 void ClearHoodHome();
285
289 void UpdateTurretHome();
290
300
306 bool IsTurretHomed();
307
313 void TurretSetPosition(units::degree_t angle);
314
320 std::optional<units::degree_t> TurretGetPosition();
321
325 void SetTurretSoftLimits();
326
331
335 void SetHoodSoftLimits();
336
341
347 bool IsManualOverride();
348
352 void Disable();
353
360 units::inch_t GetTargetDistance(units::degree_t targetVerticalAngle);
361
368 units::inch_t GetPolynomialOffset(units::inch_t actualDistance);
369
376 ShooterDistanceSetpoints GetShooterDistanceSetpoints(units::inch_t distanceToTarget) const;
377
384 ShooterDistanceSetpoints SetShooterDistance(units::inch_t distanceToTarget);
385
391
397 units::angular_velocity::revolutions_per_minute_t GetShooterSpeed();
398
402 void ManualOverride();
403
409 void SetCameraDriverMode(bool driverMode);
410
418 static bool InAcceptableRanges(const AimValues targets, const AimValues real);
419
423 void StopFeedback() const;
424
428 void AimedFeedback() const;
429
430 private:
438 HubRelativeVelocities ChassisVelocitiesToHubVelocities(const frc::ChassisSpeeds robotChassisSpeed,
439 units::degree_t hubTurretAngle);
440
442 units::foot_t hubDistance,
443 units::degree_t hubAngle,
444 units::second_t targetStaleness);
445
450
451 // Components (e.g. motor controllers and sensors) should generally be
452 // declared private and exposed only through public methods.
453 WPI_TalonFX m_shooterWheelLeft;
455 WPI_TalonSRX m_hoodMotor;
456 WPI_TalonSRX m_turretMotor;
457
459
461
466
469 decltype(shooterRange::shooterSpeed.front().outVal)>
473 decltype(shooterRange::hoodAngle.front().outVal)>
477 decltype(shooterRange::lateralSpeed.front().outVal)>
479
483
488};
Provides methods for interacting with the camera on a high level.
Definition: shooter_subsystem.h:95
void SetDriverMode(bool mode)
Turns the camera's driver mode on and off.
Definition: shooter_subsystem.cpp:628
units::angle::degree_t GetNewPitch(units::degree_t cx, units::degree_t cy, int bboxHorizontalPixels, int bboxVerticalPixels, units::degree_t skew)
Gets the new pitch for the top of the target.
Definition: shooter_subsystem.cpp:644
CameraInterface()
Definition: shooter_subsystem.cpp:626
units::angle::degree_t HorizontalPixelToAngle(int pixels)
Converts horizontal pixels to degrees.
Definition: shooter_subsystem.cpp:636
units::angle::degree_t VerticalPixelToAngle(int pixels)
Converts vertical pixels to degrees.
Definition: shooter_subsystem.cpp:640
std::optional< LimelightTarget > GetHighestTarget()
Get the highest target the camera can see CAN RETURN NONE.
LimelightTarget m_target
object that holds the current target seen by the camera
Definition: shooter_subsystem.h:99
Saves and loads home positions from filesystem.
Definition: homing_storage_interface.h:45
Wraps a limelight target in a usable object.
Definition: shooter_subsystem.h:30
bool HasTarget()
Does the camera see a target?
Definition: shooter_subsystem.cpp:688
units::degree_t m_pitch
Pitch of target relative to camera -20.5 to 20.5 degrees.
Definition: shooter_subsystem.h:32
LimelightTarget()=default
double m_bboxVer
Shortest edge of bounding box (vertical in our case) in px.
Definition: shooter_subsystem.h:35
units::degree_t m_skew
The skew or rotation -90 to 0 degrees.
Definition: shooter_subsystem.h:36
void adjustPerspective(units::degree_t &currentPitch, const units::degree_t &currentYaw)
Adjusts pitch based on the current yaw to correct the camera perspective. Perspective distortion poly...
Definition: shooter_subsystem.cpp:692
units::degree_t m_yaw
Yaw of target relative to camera -27 to 27 degrees.
Definition: shooter_subsystem.h:33
bool m_hasTargets
True if the camera has a target it can read.
Definition: shooter_subsystem.h:37
units::millisecond_t m_pipelineLatency
Pipeline latency contribution.
Definition: shooter_subsystem.h:38
double m_bboxHor
Longest edge of bounding box (horizontal in our case) in px.
Definition: shooter_subsystem.h:34
static constexpr units::millisecond_t m_miscLatency
Any extra latency to account for.
Definition: shooter_subsystem.h:39
tValues GetTarget()
Get the values of the camera's current target.
Definition: shooter_subsystem.cpp:673
Definition: shooter_subsystem.h:138
const argos_lib::CANAddress m_hoodMotorAddr
Definition: shooter_subsystem.h:448
void StopFeedback() const
Stop vibration feedback.
Definition: shooter_subsystem.cpp:518
void HoodSetPosition(units::degree_t angle)
Closed loop go to position.
Definition: shooter_subsystem.cpp:278
bool AutoAim(bool drivingAdjustment=false)
Auto aims shooter to hub using vision.
Definition: shooter_subsystem.cpp:108
units::degree_t GetHoodPosition()
Get the hood position in physical units.
Definition: shooter_subsystem.cpp:286
argos_lib::InterpolationMap< decltype(shooterRange::lateralSpeed.front().inVal), shooterRange::lateralSpeed.size(), decltype(shooterRange::lateralSpeed.front().outVal)> m_lateralSpeedMap
Definition: shooter_subsystem.h:478
ShooterDistanceSetpoints GetShooterDistanceSetpoints(units::inch_t distanceToTarget) const
Get the shooter setpoints for a target distance.
Definition: shooter_subsystem.cpp:432
HubRelativeVelocities ChassisVelocitiesToHubVelocities(const frc::ChassisSpeeds robotChassisSpeed, units::degree_t hubTurretAngle)
Generate hub-relative velocities for driving shot.
Definition: shooter_subsystem.cpp:532
void UpdateTurretHome()
Update turret home position.
Definition: shooter_subsystem.cpp:314
LimelightTarget::tValues GetCameraTargetValues()
Get the pitch and yaw of target.
Definition: shooter_subsystem.cpp:477
bool m_useCalculatedPitch
True if we want to use the calculated pitch to the top of the hood.
Definition: shooter_subsystem.h:465
units::angular_velocity::revolutions_per_minute_t GetShooterSpeed()
Get the shooter speed in physical units.
Definition: shooter_subsystem.cpp:241
argos_lib::NTMotorPIDTuner m_hoodPIDTuner
Assists hood PID tuning.
Definition: shooter_subsystem.h:480
void ManualAim(double turnSpeed, double hoodSpeed)
manually aiming the turret
Definition: shooter_subsystem.cpp:259
void UpdateHoodHome()
Update hood home position.
Definition: shooter_subsystem.cpp:290
std::optional< units::degree_t > TurretGetPosition()
Get current angle of turret with 0 degrees at intake side and positive counterclockwise.
Definition: shooter_subsystem.cpp:357
bool IsHoodHomed()
Detect if hood homing is complete.
Definition: shooter_subsystem.cpp:302
bool IsTurretHomed()
Detect if turret homing is complete.
Definition: shooter_subsystem.cpp:344
void SetTurretSoftLimits()
Sets and enables soft angle limits for turret.
Definition: shooter_subsystem.cpp:371
WPI_TalonFX m_shooterWheelRight
Right shooter wheel drive motor.
Definition: shooter_subsystem.h:454
void AimedFeedback() const
Activate vibration feedback to indicate aimed.
Definition: shooter_subsystem.cpp:525
void MoveHood(double hoodSpeed)
Move hood at specified percent speed.
Definition: shooter_subsystem.cpp:270
CameraInterface m_cameraInterface
Interface to limelight camera.
Definition: shooter_subsystem.h:458
std::optional< units::degree_t > GetTurretTargetAngle(LimelightTarget::tValues target)
Get the angle to aim at a specified target. 0 Degrees is toward intake, positive CCW.
Definition: shooter_subsystem.cpp:445
FSHomingStorage< units::degree_t > m_turretHomingStorage
Filesystem storage for turret rotation.
Definition: shooter_subsystem.h:460
void Disable()
Runs on robot disable to reset manual control.
Definition: shooter_subsystem.cpp:411
argos_lib::InterpolationMap< decltype(shooterRange::hoodAngle.front().inVal), shooterRange::hoodAngle.size(), decltype(shooterRange::hoodAngle.front().outVal)> m_hoodAngleMap
Maps a distance to a hood angle.
Definition: shooter_subsystem.h:474
SwerveDriveSubsystem * m_pDriveSubsystem
Pointer to drivetrain for reading some odometry.
Definition: shooter_subsystem.h:486
units::inch_t GetPolynomialOffset(units::inch_t actualDistance)
Gets the offset for the distance of targets above 7.416667 ft (89 in)
Definition: shooter_subsystem.cpp:202
void TurretSetPosition(units::degree_t angle)
Closed-loop go to turret position.
Definition: shooter_subsystem.cpp:348
const argos_lib::CANAddress m_turretMotorAddr
Definition: shooter_subsystem.h:449
units::inch_t GetTargetDistance(units::degree_t targetVerticalAngle)
Gets from the target to the camera.
Definition: shooter_subsystem.cpp:420
const argos_lib::CANAddress m_shooterWheelLeftAddr
Definition: shooter_subsystem.h:446
ShooterDistanceSetpoints SetShooterDistance(units::inch_t distanceToTarget)
Setting the shooter speed and hood angle depeneding on how far away the target is.
Definition: shooter_subsystem.cpp:438
bool m_turretHomed
True when turret has known closed loop position.
Definition: shooter_subsystem.h:463
bool m_manualOverride
Definition: shooter_subsystem.h:464
const argos_lib::CANAddress m_shooterWheelRightAddr
Definition: shooter_subsystem.h:447
bool IsManualOverride()
Detect if manual control has been enabled.
Definition: shooter_subsystem.cpp:310
void ManualOverride()
Override automatic control.
Definition: shooter_subsystem.cpp:246
FixedPosState
Cardinal directions used for aiming without vision.
Definition: shooter_subsystem.h:169
@ Back
Opposite intake side.
@ Right
90 degrees CW from intake side
@ Left
90 degrees CCW from intake side
static bool InAcceptableRanges(const AimValues targets, const AimValues real)
Checks if all shooter values are close enough to target values.
Definition: shooter_subsystem.cpp:488
void CloseLoopShoot(units::revolutions_per_minute_t ShooterWheelSpeed)
Run shooter at desired speed.
Definition: shooter_subsystem.cpp:254
WPI_TalonFX m_shooterWheelLeft
Left shooter wheel drive motor.
Definition: shooter_subsystem.h:453
AimOffsets DrivingAimOffsets(const HubRelativeVelocities robotVelocity, units::foot_t hubDistance, units::degree_t hubAngle, units::second_t targetStaleness)
Definition: shooter_subsystem.cpp:562
void MoveTurret(double turnSpeed)
Move turret at specified percent speed.
Definition: shooter_subsystem.cpp:274
void DisableTurretSoftLimits()
Disables soft angle limits for turret.
Definition: shooter_subsystem.cpp:385
argos_lib::NTMotorPIDTuner m_shooterPIDTuner
Assists shooter PID tuning.
Definition: shooter_subsystem.h:481
argos_lib::InterpolationMap< decltype(shooterRange::shooterSpeed.front().inVal), shooterRange::shooterSpeed.size(), decltype(shooterRange::shooterSpeed.front().outVal)> m_shooterSpeedMap
Maps distance to a shooter wheel speed.
Definition: shooter_subsystem.h:470
argos_lib::SwappableControllersSubsystem * m_pControllers
Pointer to controller subsystem for vibration feedback.
Definition: shooter_subsystem.h:487
argos_lib::NTMotorPIDTuner m_turretPIDTuner
Assists turret PID tuning.
Definition: shooter_subsystem.h:482
void SetHoodSoftLimits()
Sets and enables soft angle limits for hood.
Definition: shooter_subsystem.cpp:390
void Periodic() override
Definition: shooter_subsystem.cpp:90
bool m_hoodHomed
True when hood has known closed loop position.
Definition: shooter_subsystem.h:462
void Shoot(double ballfiringspeed)
pressing a button to fire a ball
Definition: shooter_subsystem.cpp:250
units::revolutions_per_minute_t GetShooterWheelSpeed()
Get the speed of the shooter wheel in RPM.
void InitializeTurretHome()
Initialize turret position based on saved home.
Definition: shooter_subsystem.cpp:326
bool IsHoodMoving()
Detect if hood is moving.
Definition: shooter_subsystem.cpp:298
void DisableHoodSoftLimits()
Disables soft angle limits for hood.
Definition: shooter_subsystem.cpp:403
WPI_TalonSRX m_turretMotor
Turret rotation motor.
Definition: shooter_subsystem.h:456
argos_lib::RobotInstance m_instance
Contains either the competition bot or practice bot. Differentiates between the two.
Definition: shooter_subsystem.h:485
void ClearHoodHome()
Reset hood home status.
Definition: shooter_subsystem.cpp:306
void SetCameraDriverMode(bool driverMode)
Set camera mode to driver mode without LEDs or targetting mode with LEDs.
Definition: shooter_subsystem.cpp:481
void FixedShooterPosition(FixedPosState)
Handles fixed close shot shooting positions around hub.
Definition: shooter_subsystem.cpp:220
WPI_TalonSRX m_hoodMotor
Hood articulation drive motor.
Definition: shooter_subsystem.h:455
Subsystem for controlling the swerve drivetrain of the robot.
Definition: swerve_drive_subsystem.h:50
Performs linear interpolation of a value based on a set of input->output mapping points.
Definition: interpolation.h:46
Allows user to set PID parameters from network tables and update the motor configurations on updates....
Definition: nt_motor_pid_tuner.h:48
Allows two controllers to swap between "Driver" and "Operator" control schemes on the fly by swapping...
Definition: swappable_controllers_subsystem.h:18
RobotInstance
Differentiates between practice robot and competition robot.
Definition: config_types.h:13
constexpr std::array shooterSpeed
Definition: interpolation_maps.h:17
constexpr std::array hoodAngle
Definition: interpolation_maps.h:25
constexpr std::array lateralSpeed
Definition: interpolation_maps.h:33
Shooter aiming parameters.
Definition: shooter_subsystem.h:85
units::degree_t hoodTarget
Hood and target.
Definition: shooter_subsystem.h:87
units::angular_velocity::revolutions_per_minute_t shooterTarget
Shooter wheel velocity target.
Definition: shooter_subsystem.h:88
units::degree_t turretTarget
Shooter turret angle target.
Definition: shooter_subsystem.h:86
Wraps members of LimelightTarget for use elsewhere.
Definition: shooter_subsystem.h:48
units::degree_t yaw
See LimelightTarget::m_yaw.
Definition: shooter_subsystem.h:50
units::millisecond_t totalLatency
See LimelightTarget::m_pipelineLatency.
Definition: shooter_subsystem.h:54
units::degree_t skew
See LimelightTarget::m_skew.
Definition: shooter_subsystem.h:53
units::degree_t pitch
See LimelightTarget::m_pitch.
Definition: shooter_subsystem.h:49
double bboxHor
See LimelightTarget::m_bboxHor.
Definition: shooter_subsystem.h:51
double bboxVer
See LimelightTarget::m_m_bboxVer.
Definition: shooter_subsystem.h:52
Offsets useful for aiming while driving.
Definition: shooter_subsystem.h:157
units::degree_t yawOffset
Positive indicates aim left of hub to compensate for robot lateral motion.
Definition: shooter_subsystem.h:159
units::foot_t distanceOffset
Positive indicates aim farther to compensate for motion away from hub.
Definition: shooter_subsystem.h:158
Definition: shooter_subsystem.h:148
units::degrees_per_second_t chassisYawRate
Positive is robot rotating counterclockwise.
Definition: shooter_subsystem.h:151
units::feet_per_second_t tangentialVelocity
Positive is velocity clockwise around hub.
Definition: shooter_subsystem.h:150
units::feet_per_second_t radialVelocity
Positive is velocity toward center of hub.
Definition: shooter_subsystem.h:149
Hood and shooter setpoints for shooting at a known distance.
Definition: shooter_subsystem.h:143
units::degree_t hoodAngle
Definition: shooter_subsystem.h:145
units::revolutions_per_minute_t shooterSpeed
Definition: shooter_subsystem.h:144
Definition: config_types.h:25