Skip to content

Commit

Permalink
Merge pull request #174 from FRCteam4909/DCMP-Changes
Browse files Browse the repository at this point in the history
Dcmp changes
  • Loading branch information
ashwinc12 authored Apr 14, 2019
2 parents 25bad7c + 386db7a commit 104c945
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 14 deletions.
17 changes: 16 additions & 1 deletion src/main/java/frc/team4909/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import frc.team4909.robot.subsystems.elevatorarm.commands.*;
import frc.team4909.robot.subsystems.intake.IntakeSubsystem;
import frc.team4909.robot.subsystems.intake.commands.*;
import frc.team4909.robot.SetLights;


/*
Expand Down Expand Up @@ -78,6 +79,7 @@ public class Robot extends TimedRobot {

// Sensors
public static LidarLitePWM lidar;
public static Vision vision;

/**
* map a number from one range to another
Expand Down Expand Up @@ -111,6 +113,8 @@ public void robotInit() {
c = new Compressor(0); // Initialize Compressor
c.setClosedLoopControl(true); // Start Compressor in Closed Loop Control

// lights

// Subsystems
powerDistributionPanel = new PowerDistributionPanel();
drivetrainSubsystem = new DriveTrainSubsystem();
Expand All @@ -123,6 +127,10 @@ public void robotInit() {
// Sensors
lidar = new LidarLitePWM(RobotMap.lidarPort);

//Limelight
vision = new Vision();
vision.setLights(1);

// Operator Input
driverGamepad = new BionicF310(RobotMap.driverGamepadPort, // Port
RobotConstants.driverGamepadDeadzone, // Deadzone
Expand Down Expand Up @@ -190,6 +198,7 @@ public void robotInit() {
driverGamepad.buttonPressed(BionicF310.RB, new InvertDriveDirection());
driverGamepad.buttonPressed(BionicF310.X, new TogglePreciseMode());
driverGamepad.buttonHeld(BionicF310.A, new StayOnHab());
driverGamepad.buttonToggled(BionicF310.B, new SetLights());

/* Wrist Setpoints */
manipulatorGamepad.buttonPressed(BionicF310.A, new SetWristAngle(RobotConstants.wristSetpointCargoIn));
Expand Down Expand Up @@ -218,6 +227,8 @@ public void robotPeriodic() {
// SmartDashboard.putNumber("RT Climber Sink", climberGamepad.getThresholdAxis(BionicF310.RT));
// SmartDashboard.putNumber("LY Elevator", manipulatorGamepad.getThresholdAxis(BionicF310.LY));
SmartDashboard.putNumber("Loop Period", getPeriod());
SmartDashboard.putNumber("Lidar distance", lidar.getDistance());


// process();
Scheduler.getInstance().run();
Expand Down Expand Up @@ -252,9 +263,13 @@ public void disabledPeriodic() {
Robot.elevatorSubsystem.updateHoldingPos();
Robot.elevatorArmSubsystem.holdingPosition = Robot.elevatorArmSubsystem.getPosition();
Robot.climberSubsystem.updateHoldingPos();


}

public void teleopPeriodic() {
public void teleopPeriodic() {


}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/frc/team4909/robot/RobotConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class RobotConstants {
public static final double irSensorThreshold = 2.25;

/* Elevator */
public static final double elevatorSpeedMultiplier = 0.85; // Multiplier for elevator speed
public static final double elevatorSpeedMultiplier = 0.9; // Multiplier for elevator speed
public static final double elevatorSpeedClimbMultiplier = 0.4; // multiplier for elevator when climbing


Expand All @@ -80,8 +80,8 @@ public class RobotConstants {
public static final int wristSetpointUpright = 3713; // makes arm upright
public static final int wristSetpointCargoScore = wristSetpointUpright - 393 ; // 45 degrees up
public static final int wristSetpointHatch = wristSetpointUpright - 981; // horizontal arm
public static final int wristSetpointCargoIn = wristSetpointUpright - (1205 - 120); // 45 degrees down
public static final int wristSetpointCargoIn = wristSetpointUpright - 1222; // 45 degrees down

/* Linefollow */
public static final double fastVelocity = 0.7;
public static final double mediumVelocity = 0.5; // constant velocity given to both motors
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/team4909/robot/RobotMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class RobotMap {
public static final int climberHookSPXID = 14;

/* Sensors */
public static final int lidarPort = 4;
public static final int lidarPort = 9;
public static final int intakePhotoElectric = 0;


Expand Down
26 changes: 26 additions & 0 deletions src/main/java/frc/team4909/robot/SetLights.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package frc.team4909.robot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.InstantCommand;

public class SetLights extends Command {

public SetLights(){
// requires(Robot.vision);
}

@Override
protected void initialize() {
Robot.vision.setLights(3);
}

@Override
protected void end() {
Robot.vision.setLights(1);
}
@Override
protected boolean isFinished() {
return false;
}


}
228 changes: 228 additions & 0 deletions src/main/java/frc/team4909/robot/Vision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package frc.team4909.robot;

import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class Vision extends Subsystem {
private final double slope = -56.4;
private final double yintercept = 105.95;
private final double lightOn = 3.0;
private final double lightFlash= 2.0;
private final double lightOff = 1.0;
private final double blink = 2.0;
private NetworkTable frontCamFeed;
private NetworkTable rearCamFeed;
private NetworkTable activeCamFeed;

private double tv;
private double tx;
private double ty;
private double ta;
private double ts;
private double tl;
//Camera Dep
private double targetHeight = 29;
private double cameraHeight = 5;
private double cameraAngle = 0;
//Target Dependent
private double angleToTarget;
private double distanceToTarget;
private boolean porthacth = false;
private boolean usingFrontCam = false;

private double frontLight;
private double backLight;
private String camName;

/**
* Gets the X offset in degrees.
*
* @param targetHeight is used for the distance formulas.
* @param cameraHeight is used for the distance formulas.
* @param cameraAngle is used for the distance formulas
*/
public Vision() {
this.frontCamFeed = NetworkTableInstance.getDefault().getTable("limelight");
this.setFrontCamFeed();
this.updateTableVariables();
}



double ledState = 1;

@Override
public void periodic() {
frontCamFeed.getEntry("ledMode").setNumber(ledState);
}

/**
* Updates network frontCamFeed variables.
*/
private void updateTableVariables() {
tv = activeCamFeed.getEntry("tv").getDouble(0.0);
tx = activeCamFeed.getEntry("tx").getDouble(0.0);
ty = activeCamFeed.getEntry("ty").getDouble(0.0);
ta = activeCamFeed.getEntry("ta").getDouble(0.0);
ts = activeCamFeed.getEntry("ts").getDouble(0.0);
tl = activeCamFeed.getEntry("tl").getDouble(0.0);
}

@Override
public void initDefaultCommand() {
}

public void setLights(double ledState){
this.ledState = ledState;
}
/**
* Gets the X offset in degrees.
*
* @return The X offset in degrees.
*/
public double getXOffset() {
return tx;
}

/**
* Gets the Y offset in radians.
*
* @return The Y offset in radians.
*/
public double getYOffset() {
return Math.toRadians(ty);
}

// public double getYOffsetWithMin() {
// if (Math.toRadians(ty) < -RobotMap.CAMERA_ANGLE){
// return -RobotMap.CAMERA_ANGLE;
// }
// else {
// return Math.toRadians(ty);
// }
// }

/**
* Gets the target area.
*
* @return The target area.
*/
public double getTargetArea() {
return ta;
}

/**
* Gets the target skew.
*
* @return Target Skew.
*/
public double getTargetSkew() {
return ts;
}

/**
* Gets the X limelight's latency.
*
* @return Latency
*/
public double getLatency() {
return tl;
}

/**
* Returns 1 if a target is visible, returns 0 if there's no target visible.
*
* @return 1 or 0
*/
public double isTargetVisibleDouble() {
return tv;
}

/**
* Gets the distance of the target using the formula from the limelight's documentation.
*
* @return The distance to the target using a formula.
*/
public double calculateDistanceFromCameraHeight(double targetHeight, double cameraHeight, double cameraAngle) {
if (porthacth) {
// this.targetHeight = RobotMap.ROCKET_PORT_HEIGHT;
}
double methodDistance = (targetHeight - cameraHeight) / Math.tan(cameraAngle + getYOffset());
return methodDistance;
}

// public double calculateDistanceFromCameraHeightWithMin(double targetHeight, double cameraHeight, double cameraAngle) {
// if (porthacth) {
// // this.targetHeight = RobotMap.ROCKET_PORT_HEIGHT;
// }
// double methodDistance = (targetHeight - cameraHeight) / Math.tan(cameraAngle + getYOffsetWithMin());
// return methodDistance;
// }

/**
* Gets the distance to the target based of off its area using a slope-intercept formula.
*
* @return The distance based off of the area of the target.
*/
public double calculateDistanceArea() {
return slope * ta + yintercept;
}



/**
* Sets the camera feed to stream from the front facing camera.
*/
public void setFrontCamFeed() {
this.activeCamFeed = this.frontCamFeed;
this.usingFrontCam = true;
this.frontLight = this.lightOn;
this.backLight = this.lightOff;
this.cameraAngle = 0;
}



public void flash() {
this.frontLight = this.lightFlash;
this.backLight = this.lightFlash;
}

public boolean isUsingFrontCam() {
return this.usingFrontCam;
}

public boolean targetAcquired(){
if(this.tv == 1){
return true;
}
else{
return false;
}
}

/**
* Updates the values of the Vision class.
*/
public void updateVisionDashboard() {
updateTableVariables();
SmartDashboard.putNumber("X Offset: ", getXOffset());
SmartDashboard.putNumber("Y Offset: ", getYOffset());
SmartDashboard.putNumber("Distance to target(Formula): ",
calculateDistanceFromCameraHeight(this.targetHeight,
this.cameraHeight,
this.cameraAngle));
SmartDashboard.putNumber("Distance to target(Area): ", calculateDistanceArea());
SmartDashboard.putNumber("Target Area: ", getTargetArea());
SmartDashboard.putNumber("Target Skew: ", getTargetSkew());
SmartDashboard.putNumber("LimeLight Latency: ", getLatency());
SmartDashboard.putNumber("Target Found (double): ", isTargetVisibleDouble());

SmartDashboard.putBoolean("Front Camera: ", this.usingFrontCam);

SmartDashboard.putBoolean("Is Target Acquired?", this.targetAcquired());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void buttonHeld(BionicAxis axis, double threshold, Command command) {
* @param button Button to Create Handler For
* @param commandable Returns a Commandable that can be used by the operator and autonomous CommandGroups
*/
public void buttonToggled(BionicButton button, InstantCommand command) {
public void buttonToggled(BionicButton button, Command command) {
JoystickButton newButton = new JoystickButton(this, button.getNumber());

newButton.toggleWhenPressed(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public void arcadeDrive(double leftSpeed, double rightSpeed) {
turnMultiplier = RobotConstants.speedTurnMultiplier;
speedMultiplier = RobotConstants.speedMultiplier;
}
if (inverted) { //inverts arcadeDrive
speedOutput = -rightSpeed;
turnOutput = -leftSpeed;
}

speedOutput = speedOutput * speedMultiplier;
turnOutput = turnOutput * turnMultiplier;

bionicDrive.arcadeDrive(speedOutput, turnOutput);
}

public void curvatureDrive(double xSpeed, double zRotation, boolean isQuickTurn){
bionicDrive.curvatureDrive(xSpeed, zRotation, isQuickTurn);
}

public void invertDriveDirection() {
inverted = !inverted;
}
Expand All @@ -91,6 +91,10 @@ public void togglePreciseMode() {
preciseMode = !preciseMode;
}

public void preciseModeTrue(){
preciseMode = true;
}

protected void initDefaultCommand() {
setDefaultCommand(new Drive());
}
Expand Down
Loading

0 comments on commit 104c945

Please sign in to comment.