From 4c257acfc11cbb2b0d2e761fb608f0ab1244508f Mon Sep 17 00:00:00 2001 From: ryan123rudder <42309874+ryan123rudder@users.noreply.github.com> Date: Thu, 13 Feb 2020 16:42:54 -0700 Subject: [PATCH] Add Vision (Untested) --- .../java/frc4388/robot/RobotContainer.java | 9 ++ .../frc4388/robot/commands/TrackTarget.java | 84 +++++++++++++++++++ .../java/frc4388/robot/subsystems/Camera.java | 47 +++++++++++ vendordeps/Phoenix.json | 30 +++---- 4 files changed, 155 insertions(+), 15 deletions(-) create mode 100644 src/main/java/frc4388/robot/commands/TrackTarget.java create mode 100644 src/main/java/frc4388/robot/subsystems/Camera.java diff --git a/src/main/java/frc4388/robot/RobotContainer.java b/src/main/java/frc4388/robot/RobotContainer.java index 285d467..bb2c59d 100644 --- a/src/main/java/frc4388/robot/RobotContainer.java +++ b/src/main/java/frc4388/robot/RobotContainer.java @@ -30,6 +30,8 @@ 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 +55,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); @@ -102,6 +108,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_robotDrive, m_driverXbox)); 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..73f091c --- /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.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; + Drive m_drive; + IHandController m_driverController; + //Aiming + double turnAmount = 0; + double xAngle = 0; + double yAngle = 0; + double target = 0; + double distance; + + /** + * Uses the Limelight to track the target + */ + public TrackTarget(Drive driveSubsystem, IHandController driverController) { + m_drive = driveSubsystem; + addRequirements(m_drive); + m_driverController = driverController; + 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_drive.driveWithInput(m_driverController.getLeftYAxis(), 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; + } +} \ No newline at end of file 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/vendordeps/Phoenix.json b/vendordeps/Phoenix.json index a633555..c6ec878 100644 --- a/vendordeps/Phoenix.json +++ b/vendordeps/Phoenix.json @@ -1,7 +1,7 @@ { "fileName": "Phoenix.json", "name": "CTRE-Phoenix", - "version": "5.17.4", + "version": "5.18.1", "uuid": "ab676553-b602-441f-a38d-f1296eff6537", "mavenUrls": [ "http://devsite.ctr-electronics.com/maven/release/" @@ -11,19 +11,19 @@ { "groupId": "com.ctre.phoenix", "artifactId": "api-java", - "version": "5.17.4" + "version": "5.18.1" }, { "groupId": "com.ctre.phoenix", "artifactId": "wpiapi-java", - "version": "5.17.4" + "version": "5.18.1" } ], "jniDependencies": [ { "groupId": "com.ctre.phoenix", "artifactId": "cci", - "version": "5.17.4", + "version": "5.18.1", "isJar": false, "skipInvalidPlatforms": true, "validPlatforms": [ @@ -35,7 +35,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "diagnostics", - "version": "5.17.4", + "version": "5.18.1", "isJar": false, "skipInvalidPlatforms": true, "validPlatforms": [ @@ -47,7 +47,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "canutils", - "version": "5.17.4", + "version": "5.18.1", "isJar": false, "skipInvalidPlatforms": true, "validPlatforms": [ @@ -58,7 +58,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "platform-stub", - "version": "5.17.4", + "version": "5.18.1", "isJar": false, "skipInvalidPlatforms": true, "validPlatforms": [ @@ -69,7 +69,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "core", - "version": "5.17.4", + "version": "5.18.1", "isJar": false, "skipInvalidPlatforms": true, "validPlatforms": [ @@ -83,7 +83,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "wpiapi-cpp", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_Phoenix_WPI", "headerClassifier": "headers", "sharedLibrary": false, @@ -97,7 +97,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "api-cpp", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_Phoenix", "headerClassifier": "headers", "sharedLibrary": false, @@ -111,7 +111,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "cci", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_PhoenixCCI", "headerClassifier": "headers", "sharedLibrary": false, @@ -125,7 +125,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "diagnostics", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_PhoenixDiagnostics", "headerClassifier": "headers", "sharedLibrary": false, @@ -139,7 +139,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "canutils", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_PhoenixCanutils", "headerClassifier": "headers", "sharedLibrary": false, @@ -152,7 +152,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "platform-stub", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_PhoenixPlatform", "headerClassifier": "headers", "sharedLibrary": false, @@ -165,7 +165,7 @@ { "groupId": "com.ctre.phoenix", "artifactId": "core", - "version": "5.17.4", + "version": "5.18.1", "libName": "CTRE_PhoenixCore", "headerClassifier": "headers", "sharedLibrary": false,