-
Notifications
You must be signed in to change notification settings - Fork 0
Limelight #110
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
base: dev
Are you sure you want to change the base?
Limelight #110
Changes from 6 commits
b4b4578
ba673dd
37e3dbf
98c1bba
479eaf7
8dc4594
c014588
5ae706b
a308866
821e31a
6a240e8
e92ddd8
da45242
29782d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,33 @@ | ||
| /*----------------------------------------------------------------------------*/ | ||
| /* Copyright (c) 2018 FIRST. All Rights Reserved. */ | ||
| /* Copyright (c) 2017-2018 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 frc.robot.commands; | ||
|
|
||
| import frc.robot.lib.Limelight; | ||
| import frc.robot.subsystems.Drivetrain; | ||
| import edu.wpi.first.wpilibj.Joystick; | ||
| import edu.wpi.first.wpilibj.command.Command; | ||
| import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
| import frc.robot.subsystems.Drivetrain; | ||
|
|
||
| public class TeleopDrive extends Command { | ||
| Drivetrain dt; | ||
| Joystick leftJoy, rightJoy; | ||
|
|
||
| double prevSpeed = 0, prevLeft = 0, prevRight = 0; | ||
|
|
||
| double outreachSpeed = 0.3; | ||
|
|
||
| /** | ||
| * Handles all the teleoperated driving functionality | ||
| * | ||
| * @param dt the Drivetrain object to use, passing it in is useful for testing | ||
| * purposes | ||
| */ | ||
| public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) { | ||
| public class Drive extends Command { | ||
| private Drivetrain dt; | ||
| private Joystick leftJoy, rightJoy; | ||
| private Limelight lime; | ||
| private Limelight.Mode limelightMode = Limelight.Mode.TARGET; | ||
| private double adjustment = 0; | ||
| private double minError = 0.05; // TODO: Test minimum values | ||
| private double prevSpeed = 0, prevLeft = 0, prevRight = 0; | ||
| private double outreachSpeed = 0.3; | ||
|
|
||
| public Drive(Drivetrain dt, Limelight lime, Joystick leftJoy, Joystick rightJoy) { | ||
| requires(dt); | ||
| this.dt = dt; | ||
| this.lime = lime; | ||
| this.dt = dt; | ||
| this.leftJoy = leftJoy; | ||
| this.rightJoy = rightJoy; | ||
|
|
||
|
|
@@ -45,12 +44,22 @@ public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) { | |
| } | ||
| } | ||
|
|
||
| // Called just before this Command runs the first time | ||
| @Override | ||
| protected void initialize() { | ||
| } | ||
|
|
||
| // Called repeatedly when this Command is scheduled to run | ||
| @Override | ||
| protected void execute() { | ||
| if (SmartDashboard.getBoolean("Arcade Drive", true)) { | ||
| arcadeDrive(); | ||
| if (SmartDashboard.getBoolean("Using Limelight", false)) { | ||
| autoAlign(); | ||
| } else { | ||
| tankDrive(); | ||
| if (SmartDashboard.getBoolean("Arcade Drive", true)) { | ||
| arcadeDrive(); | ||
| } else { | ||
| tankDrive(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -230,16 +239,49 @@ private void charDrive(double left, double right) { | |
| dt.disableVoltageCompensation(); | ||
| } | ||
|
|
||
| private void autoAlign() { | ||
| if (limelightMode == Limelight.Mode.DIST) { | ||
| adjustment = lime.distanceAssist(); | ||
| dt.drive(adjustment, adjustment); | ||
| if (Math.abs(adjustment) < minError) { | ||
| SmartDashboard.putBoolean("Finished Aligning", true); | ||
| } | ||
| } | ||
| else if (limelightMode == Limelight.Mode.STEER) { | ||
| adjustment = lime.distanceAssist(); | ||
| dt.drive(adjustment, -adjustment); | ||
| if (Math.abs(adjustment) < minError) { | ||
| SmartDashboard.putBoolean("Finished Aligning", true); | ||
| } | ||
| } else { | ||
| double[] params = lime.autoTarget(); | ||
| dt.drive(params[0], params[1]); | ||
| double maxInput = Math.max(Math.abs(params[0]), Math.abs(params[1])); | ||
| if (maxInput < minError) { | ||
| SmartDashboard.putBoolean("Finished Aligning", true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Make this return true when this Command no longer needs to run execute() | ||
| @Override | ||
| protected boolean isFinished() { | ||
| return false; | ||
| if (!SmartDashboard.getBoolean("Use Limelight", false) || SmartDashboard.getBoolean("Finished Aligning", false)) { | ||
| SmartDashboard.putBoolean("Use Limelight", false); | ||
| SmartDashboard.putBoolean("Finished Aligning", false); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
|
||
| } | ||
|
|
||
| // Called once after isFinished returns true | ||
| @Override | ||
| protected void end() { | ||
| dt.stop(); | ||
| } | ||
|
|
||
| // Called when another command which requires one or more of the same | ||
| // subsystems is scheduled to run | ||
| @Override | ||
| protected void interrupted() { | ||
| end(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,4 +28,4 @@ protected void initialize() { | |
| lights.setLights(state); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /*----------------------------------------------------------------------------*/ | ||
| /* Copyright (c) 2018 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 frc.robot.commands; | ||
|
|
||
| import edu.wpi.first.wpilibj.command.InstantCommand; | ||
| import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
|
||
| public class ToggleLimelight extends InstantCommand { | ||
| public ToggleLimelight() { | ||
| } | ||
|
|
||
| @Override | ||
| protected void initialize() { | ||
| if (SmartDashboard.getBoolean("Using Limelight", false)) { | ||
| SmartDashboard.putBoolean("Using Limelight", true); | ||
| } else { | ||
| SmartDashboard.putBoolean("Using Limelight", false); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /*----------------------------------------------------------------------------*/ | ||
| /* Copyright (c) 2017-2018 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 frc.robot.lib; | ||
|
|
||
| import edu.wpi.first.networktables.NetworkTable; | ||
| import edu.wpi.first.networktables.NetworkTableInstance; | ||
| import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
|
||
| public class Limelight { | ||
| public enum Mode { | ||
| DIST, STEER, TARGET | ||
| } | ||
|
|
||
| private NetworkTableInstance inst; | ||
| private NetworkTable table; | ||
| private double tv, tx, ty, ta; | ||
|
|
||
| public Limelight() { | ||
| inst = NetworkTableInstance.getDefault(); | ||
| table = inst.getTable("limelight"); | ||
| } | ||
|
|
||
| // Adjusts the distance between a vision target and the robot. Uses basic PID with the ty value from the network table. | ||
| public double distanceAssist() { | ||
| tv = table.getEntry("tv").getDouble(0.0); | ||
| ty = table.getEntry("ty").getDouble(0.0); | ||
|
||
| SmartDashboard.putNumber("Crosshair Vertical Offset", ty); | ||
| ta = table.getEntry("ta").getDouble(0.0); | ||
| double adjustment = 0.0; | ||
| double area_threshold = 0.5; // TODO: Set the desired area. | ||
| double Kp = 0.2; // TODO: Set PID K value. | ||
|
|
||
| if (tv == 1.0) { | ||
| if (ta < area_threshold) { | ||
| adjustment += Kp * ty; | ||
| } | ||
|
||
| } | ||
| return adjustment; | ||
| } | ||
|
|
||
| // Adjusts the angle facing a vision target. Uses basic PID with the tx value from the network table. | ||
| public double steeringAssist() { | ||
| tv = table.getEntry("tv").getDouble(0.0); | ||
| tx = -table.getEntry("tx").getDouble(0.0); | ||
| SmartDashboard.putBoolean("Found Vision Target", tv == 1.0); | ||
| SmartDashboard.putNumber("Crosshair Horizontal Offset", tx); | ||
| double adjustment = 0.0; | ||
| double steering_factor = 0.25; | ||
| double Kp = 0.2; // TODO: Set PID K value. | ||
|
|
||
| if (tv == 1.0) { | ||
| adjustment += Kp * tx; | ||
| } else { | ||
| adjustment += steering_factor; | ||
| } | ||
| return adjustment; | ||
| } | ||
|
|
||
| // Combination of distance assist and steering assist | ||
| public double[] autoTarget() { | ||
| double dist_assist = distanceAssist(); | ||
| double steer_assist = steeringAssist(); | ||
| double[] params = {dist_assist + steer_assist, dist_assist - steer_assist}; | ||
| return params; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want lime.steerAssist() here?