Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Automatic Aiming #15

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7d7b2cf
Add aiming to x coordinate
sreeves750 Jan 25, 2020
0df298f
Remove P variable
sreeves750 Jan 25, 2020
2a9dc1f
Add angle aiming
sreeves750 Jan 25, 2020
24b30ea
Add documentation
sreeves750 Jan 27, 2020
f38c0c0
Fix dimensional analysis
sreeves750 Jan 28, 2020
c2112ed
Fix aimX negative error
sreeves750 Feb 1, 2020
63591ce
Merge branch 'master' into auto-aim
sreeves750 Feb 7, 2020
f48b264
Merge branch 'master' into auto-aim
sreeves750 Feb 15, 2020
cbef085
Merge branch 'shooter-angle' into auto-aim
sreeves750 Feb 15, 2020
5735475
Merge branch 'master' into auto-aim
sreeves750 Feb 17, 2020
d2f018b
Organize states into methods
sreeves750 Feb 19, 2020
2bdd352
Move state transitions into methods
sreeves750 Feb 19, 2020
c575583
Add vertical distance of limelight target to degree calculations
sreeves750 Feb 19, 2020
8eba47f
Use gyro angle to aim robot x-coordinate
sreeves750 Feb 19, 2020
ba08811
Add auto aiming to robot
sreeves750 Feb 21, 2020
0235f8f
Add driver controls
sreeves750 Feb 21, 2020
ddc1dfb
Add additional controls
sreeves750 Feb 21, 2020
91c6b79
Add angle calculations
sreeves750 Feb 22, 2020
fbd04f8
Fix camera variables
sreeves750 Feb 22, 2020
a4b7a9d
Add correct camera variables
sreeves750 Feb 22, 2020
36e9d59
Convert camera variables to doubles
sreeves750 Feb 22, 2020
64f2b8e
Fix AimX error variable
sreeves750 Feb 22, 2020
e6a2eb2
Merge branch 'master' into auto-aim
sreeves750 Feb 22, 2020
b8a169d
Fix toggles
sreeves750 Feb 22, 2020
5f1a646
Add state machine resets
sreeves750 Feb 25, 2020
939ea48
Add drivetrain sweeping
sreeves750 Feb 28, 2020
9ed3479
Add remote state control
sreeves750 Feb 28, 2020
f8cfb3a
Merge branch 'drivetrain-sweep' into auto-aim
sreeves750 Feb 28, 2020
ba8570b
Add sweeping if target is not visible
sreeves750 Feb 29, 2020
87327f7
Fix driver controlled autoaiming
sreeves750 Mar 4, 2020
891bfc2
Turn off limelight light when not aiming
sreeves750 Mar 5, 2020
5938e09
Merge remote-tracking branch 'origin/master' into auto-aim
rar1741programmer Mar 6, 2020
97c2b62
Start testing
rar1741programmer Mar 6, 2020
c46a4cf
Limelight tuning
rar1741programmer Mar 6, 2020
85f52f7
Change robot is aim X and Angle at the same time
sreeves750 Mar 8, 2020
6fb5f4c
Update current auto-aim code
sreeves750 Mar 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/main/java/frc/robot/AutoAim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package frc.robot;

import com.kauailabs.navx.frc.AHRS;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class AutoAim {

private double error;
private double motorPower;
private double angleDegrees;
private double gyroDegrees;

private boolean sweeping;

public enum AutoAimState {
SET_ANGLES,
AIM,
IDLE;
}
private AutoAimState state;
private Drivetrain drive;
private Limelight limelight;
private Shooter shooter;
private AHRS gyro;

/**
* Constructor
*
* @param drive drive train object.
* @param limelight limelight object.
* @param shooter shooter object.
* @param gyro gyro object.
*/
public AutoAim(Drivetrain drive, Limelight limelight, Shooter shooter, AHRS gyro) {
state = AutoAimState.IDLE;
this.drive = drive;
this.limelight = limelight;
this.shooter = shooter;
this.gyro = gyro;
}

/**
* Runs Automatic Aiming state machine.
*/
public void run() {
switch (state) {

case SET_ANGLES:
SetAngles();
break;

case AIM:
AimX();
AimAngle();
break;

case IDLE:
break;
}

SmartDashboard.putString("AutoAimState", state.toString());
SmartDashboard.putNumber("MotorPower", motorPower);
SmartDashboard.putNumber("gyroDegrees", gyroDegrees);
}

/**
* Sets degrees to turn the robot to.
*/
private void SetAngles() {
if (limelight.isTargetVisible()) {
sweeping = false;
drive.stopSweep();
gyroDegrees = gyro.getYaw() + limelight.getTargetXDegrees();
state = AutoAimState.AIM;
} else {
if (!sweeping) {
drive.resetState();
}
sweeping = true;
drive.sweep();
}
}

/**
* Aims to robot to the X-coordinate of the power port.
*/
private void AimX() {
// error = (gyroDegrees - gyro.getYaw()) / 29.8;
error = (gyroDegrees - gyro.getYaw());
// motorPower = -.5 * error;
motorPower = -error * 1;
drive.driveLeft(motorPower);
drive.driveRight(-motorPower);
}

/**
* Aims the shooter angle to the appropriate angle, based on distance, to the
* power port.
*/
private void AimAngle() {
angleDegrees = 1 * limelight.getTargetVertical(); // TODO: Use a table from testing to create an equation to
// plug this into
shooter.setAngle(angleDegrees);
}

/**
* Gets if done aiming.
*
* @return true if robot is done aiming, false if not done aiming.
*/
private boolean getDoneAiming() {
if (getWithinTolerance(angleDegrees, shooter.getAngleInDegrees(), 1) && getWithinTolerance(gyroDegrees, gyro.getYaw(), 1)) {
return true;
} else {
return false;
}
}

/**
* Gets if input is within tolerance of its goal.
*
* @param goal desired value of input.
* @param current current value of input.
* @param tolerance tolerance of current input to desired input.
* @return true if within tolerance, false if not within tolerance.
*/
private boolean getWithinTolerance(double goal, double current, double tolerance) {
return Math.abs(goal - current) < tolerance;
}

/**
* Resets the state machine.
*/
public void resetState() {
state = AutoAimState.SET_ANGLES;
}

/**
* Stops the state machine
*/
public void stopState() {
state = AutoAimState.IDLE;
}

/**
* Gets the aiming state.
*/
public AutoAimState getState() {
return state;
}
}
88 changes: 87 additions & 1 deletion src/main/java/frc/robot/Drivetrain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package frc.robot;

import com.kauailabs.navx.frc.AHRS;

public class Drivetrain {

/**
Expand All @@ -8,23 +10,37 @@ public class Drivetrain {
*/
private static final double DEADBAND_LIMIT = 0.02;

private enum SweepState {
SET_ANGLE, SWEEP_LEFT, SWEEP_RIGHT, IDLE;
}

private SweepState sweepState;

private DriveModule left;
private DriveModule right;
private PowercellDetection detector;
private Limelight limelight;
private AHRS gyro;

private double gyroAngle;

/**
* Constructor
*
* @param left The left drive module
* @param right The right drive module
* @param detector The powercell detection object.
* @param gyro The gyro object.
*/
Drivetrain(DriveModule left, DriveModule right, PowercellDetection detector) {
Drivetrain(DriveModule left, DriveModule right, PowercellDetection detector, AHRS gyro) {
this.left = left;
this.right = right;
right.setInverted(true);

this.detector = detector;
this.gyro = gyro;

sweepState = SweepState.IDLE;
}

/**
Expand Down Expand Up @@ -90,6 +106,76 @@ public double deadband(double in) {
return Math.abs(in) > DEADBAND_LIMIT ? in : 0.0;
}

/**
* Runs the sweeping procedure.
*/
public void sweep() {
switch (sweepState) {

case SET_ANGLE:
sweepAngle();
break;

case SWEEP_LEFT:
sweepLeft();
break;

case SWEEP_RIGHT:
sweepRight();
break;

case IDLE:
break;
}
}

/**
* Sets home gyro angle.
*/
private void sweepAngle() {
gyroAngle = gyro.getAngle();
sweepState = SweepState.SWEEP_LEFT;
}

/**
* Sweeps to the left 80 degrees.
*/
private void sweepLeft() {
if (gyro.getAngle() < gyroAngle + 80) {
driveLeft(-0.25);
driveRight(0.25);
} else {
sweepState = SweepState.SWEEP_RIGHT;
}
}

/**
* Sweeps to the right 80 degrees.
*
*/
private void sweepRight() {
if (gyro.getAngle() > gyroAngle - 80) {
driveLeft(0.25);
driveRight(-0.25);
} else {
sweepState = SweepState.SWEEP_LEFT;
}
}

/**
* Resets to the state to setting the angle.
*/
public void resetState() {
sweepState = SweepState.SET_ANGLE;
}

/**
* Sets the state to idle.
*/
public void stopSweep() {
sweepState = SweepState.IDLE;
}

/**
* Moves the robot to intercept powercells
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/frc/robot/Limelight.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

public class Limelight {

// TODO: Get proper dimension varibles.
private final double IMG_WIDTH = 320;
private final double CAM_FOV = 59.6;

private double tv;
private double tx;
private double ty;
Expand Down Expand Up @@ -54,6 +58,8 @@ public double getTargetX() {
return this.tx;
}



/**
* @return current target y-coordinate.
*/
Expand All @@ -75,6 +81,13 @@ public double getTargetVertical() {
return this.tvert;
}

/**
* @return current target x-coordinate in degrees.
*/
public double getTargetXDegrees() {
return this.tx*(CAM_FOV/IMG_WIDTH);
}

public void setLightEnabled(boolean enabled) {
limelightTable.getEntry("ledMode").forceSetNumber(enabled ? 3: 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Manipulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void setIntakeSpin(boolean spin) {
* @param extend true if it should extend, false if not.
*/
public void setIntakeExtend(boolean extend) {
// intakePneumatics.set(extend ? Value.kForward : Value.kReverse);
intakePneumatics.set(extend ? Value.kForward : Value.kReverse);
}

/**
Expand Down
Loading