diff --git a/src/main/java/frc4388/robot/Constants.java b/src/main/java/frc4388/robot/Constants.java index 4cb083f..1600ab1 100644 --- a/src/main/java/frc4388/robot/Constants.java +++ b/src/main/java/frc4388/robot/Constants.java @@ -127,6 +127,17 @@ public final class Constants { public static final int LED_SPARK_ID = 0; public static final LEDPatterns DEFAULT_PATTERN = LEDPatterns.FOREST_WAVES; } + + public static final class VisionConstants { + public static final double FOV = 29.8; //Field of view of limelight + public static final double TARGET_HEIGHT = 82.75; + public static final double LIME_ANGLE = 18.7366; + public static final double TURN_P_VALUE = 0.65; + public static final double X_ANGLE_ERROR = 1.3; + public static final double MOTOR_DEAD_ZONE = 0.3; + public static final double DISTANCE_ERROR_EQUATION_M = 1.1279; + public static final double DISTANCE_ERROR_EQUATION_B = -15.0684; + } public static final class OIConstants { public static final int XBOX_DRIVER_ID = 0; diff --git a/src/main/java/frc4388/robot/RobotContainer.java b/src/main/java/frc4388/robot/RobotContainer.java index 463468d..f62c005 100644 --- a/src/main/java/frc4388/robot/RobotContainer.java +++ b/src/main/java/frc4388/robot/RobotContainer.java @@ -30,6 +30,11 @@ import frc4388.robot.subsystems.LED; import frc4388.robot.subsystems.Shooter; import frc4388.robot.subsystems.Climber; import frc4388.robot.commands.RunLevelerWithJoystick; +import frc4388.robot.subsystems.Drive; +import frc4388.robot.subsystems.Intake; +import frc4388.robot.subsystems.LED; +import frc4388.robot.commands.TrackTarget; +import frc4388.robot.subsystems.Camera; import frc4388.robot.subsystems.Leveler; import frc4388.robot.subsystems.Storage; import frc4388.utility.LEDPatterns; @@ -53,6 +58,10 @@ public class RobotContainer { private final Leveler m_robotLeveler = new Leveler(); private final Storage m_robotStorage = new Storage(); + /* Cameras */ + private final Camera m_robotCameraFront = new Camera("front",0,160,120,40); + private final Camera m_robotCameraBack = new Camera("back",1,160,120,40); + /* Controllers */ private final XboxController m_driverXbox = new XboxController(OIConstants.XBOX_DRIVER_ID); private final XboxController m_operatorXbox = new XboxController(OIConstants.XBOX_OPERATOR_ID); @@ -75,7 +84,7 @@ public class RobotContainer { // runs the drum shooter in idle mode - m_robotShooter.setDefaultCommand(new RunCommand(() -> m_robotShooter.runShooterWithInput(m_operatorXbox), m_robotShooter)); + m_robotShooter.setDefaultCommand(new RunCommand(() -> m_robotShooter.runShooterWithInput(m_operatorXbox.getLeftXAxis()), m_robotShooter)); // drives the leveler with an axis input from the driver controller // m_robotLeveler.setDefaultCommand(new RunLevelerWithJoystick(m_robotLeveler, getDriverController())); // runs storage motor at 50 percent @@ -101,6 +110,9 @@ public class RobotContainer { new JoystickButton(getOperatorJoystick(), XboxController.X_BUTTON) .whileHeld(new ShooterVelocityControlPID(m_robotShooter, 4000)); + + new JoystickButton(getOperatorJoystick(), XboxController.Y_BUTTON) + .whileHeld(new TrackTarget(m_robotShooter)); new JoystickButton(getOperatorJoystick(), XboxController.LEFT_BUMPER_BUTTON) .whenPressed(new RunExtenderOutIn(m_robotIntake)); diff --git a/src/main/java/frc4388/robot/commands/TrackTarget.java b/src/main/java/frc4388/robot/commands/TrackTarget.java new file mode 100644 index 0000000..cbded76 --- /dev/null +++ b/src/main/java/frc4388/robot/commands/TrackTarget.java @@ -0,0 +1,84 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package frc4388.robot.commands; + +import frc4388.robot.Constants.VisionConstants; +import frc4388.robot.subsystems.Drive; +import frc4388.robot.subsystems.Shooter; +import frc4388.utility.controller.IHandController; +import edu.wpi.first.wpilibj2.command.CommandBase; +import edu.wpi.first.networktables.NetworkTable; +import edu.wpi.first.networktables.NetworkTableEntry; +import edu.wpi.first.networktables.NetworkTableInstance; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; + +public class TrackTarget extends CommandBase { + //Setup + NetworkTableEntry xEntry; + Shooter m_shooter; + IHandController m_driverController; + //Aiming + double turnAmount = 0; + double xAngle = 0; + double yAngle = 0; + double target = 0; + public double distance; + + /** + * Uses the Limelight to track the target + */ + public TrackTarget(Shooter shooterSubsystem) { + m_shooter = shooterSubsystem; + addRequirements(m_shooter); + NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(1); + } + + // Called when the command is initially scheduled. + @Override + public void initialize() { + //Vision Processing Mode + NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(0); + NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(3); + } + + // Called every time the scheduler runs while the command is scheduled. + @Override + public void execute() { + target = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tv").getDouble(0); + xAngle = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tx").getDouble(0); + yAngle = NetworkTableInstance.getDefault().getTable("limelight").getEntry("ty").getDouble(0); + + if (target == 1.0){ //If target in view + //Aiming Left/Right + turnAmount = (xAngle/VisionConstants.FOV)*VisionConstants.TURN_P_VALUE; + if (Math.abs(xAngle) < VisionConstants.X_ANGLE_ERROR){turnAmount = 0;} //Angle Error Zone + //Deadzones + else if(turnAmount > 0 && turnAmount < VisionConstants.MOTOR_DEAD_ZONE){turnAmount = VisionConstants.MOTOR_DEAD_ZONE;} + else if(turnAmount < 0 && turnAmount > -VisionConstants.MOTOR_DEAD_ZONE){turnAmount = -VisionConstants.MOTOR_DEAD_ZONE;} + m_shooter.runShooterWithInput(turnAmount); + + //Finding Distance + distance = VisionConstants.TARGET_HEIGHT/Math.tan((VisionConstants.LIME_ANGLE + yAngle)*(Math.PI/180)); + SmartDashboard.putNumber("Distance to Target", distance); + } + } + + // Called once the command ends or is interrupted. + @Override + public void end(boolean interrupted) { + //Drive Camera Mode + NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(1); + NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(0); + } + + // Returns true when the command should end. + @Override + public boolean isFinished() { + return false; + } +} diff --git a/src/main/java/frc4388/robot/subsystems/Camera.java b/src/main/java/frc4388/robot/subsystems/Camera.java new file mode 100644 index 0000000..e483009 --- /dev/null +++ b/src/main/java/frc4388/robot/subsystems/Camera.java @@ -0,0 +1,47 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package frc4388.robot.subsystems; + +import edu.wpi.cscore.MjpegServer; +import edu.wpi.cscore.UsbCamera; +import edu.wpi.cscore.VideoSource; +import edu.wpi.first.cameraserver.CameraServer; +import edu.wpi.first.wpilibj2.command.SubsystemBase; + + + +public class Camera extends SubsystemBase { + CameraServer camServ = CameraServer.getInstance(); + /** + * Creates a new Camera. + * Makes a Camera and sends the stream to a CameraServer, to be viewed in Shuffle Board. + * @param name Name of the Camera in Shuffle Board. + * @param id USB Id of the Camera. + * @param width Resolution width. + * @param height Resolution height. + * @param brightness Percent brightness of the stream. + */ + public Camera(String name, int id, int width, int height, int brightness) { + try{ + UsbCamera cam = new UsbCamera(name, id); + cam.setResolution(width, height); + cam.setBrightness(brightness); + cam.setFPS(10); + VideoSource camera = cam; + camServ.startAutomaticCapture(camera); + } + catch(Exception e){ + System.err.println("Camera broken, pls nerf"); + } + + } + + @Override + public void periodic() { + } +} \ No newline at end of file diff --git a/src/main/java/frc4388/robot/subsystems/Shooter.java b/src/main/java/frc4388/robot/subsystems/Shooter.java index c072a8f..a18aca5 100644 --- a/src/main/java/frc4388/robot/subsystems/Shooter.java +++ b/src/main/java/frc4388/robot/subsystems/Shooter.java @@ -117,10 +117,7 @@ public class Shooter extends SubsystemBase { } - public void runShooterWithInput(IHandController controller) { - m_controller = controller; - input = m_controller.getLeftXAxis(); - System.err.println(input); + public void runShooterWithInput(double input) { m_shooterRotateMotor.set(input); }