limelight

This commit is contained in:
2026-03-30 05:04:22 +02:00
parent 5c626f33e3
commit f7e88619e9
3 changed files with 64 additions and 32 deletions

View File

@@ -50,21 +50,32 @@ public class Limelighter extends Command {
double[] BotPose = new double[6]; double[] BotPose = new double[6];
System.out.println("e"); System.out.println("e");
if (limelight3g.getV()) { if (limelight3g.getV()) {
if(!alliance.isPresent()){return;}
if(alliance.get() == Alliance.Blue){
BotPose = limelight3g.getBotPoseBlue(); BotPose = limelight3g.getBotPoseBlue();
}
else{
BotPose = limelight3g.getBotPoseRed();
}
botx = BotPose[1]; botx = BotPose[1];
boty = BotPose[0]; boty = BotPose[0];
angle = drivetrain.getPigeon2().getYaw().getValueAsDouble(); angle = drivetrain.getPigeon2().getYaw().getValueAsDouble();
if(angle >180){ calcul = limelight3g.Calcule(botx, 4, boty, 4.6, angle);
angle -= 360; if(calcul < -5 && calcul > -180){
drivetrain.setControl(
drive.withRotationalRate(0.5*180/Math.PI));
} }
calcul = limelight3g.Calcule(botx, 4, boty, 4.6, angle) / 10; else if(calcul > 5 && calcul < 180){
drivetrain.setControl(
drive.withRotationalRate(-0.5*180/Math.PI));
}
else if(calcul >= 180){
drivetrain.setControl(
drive.withRotationalRate(-0.5*180/Math.PI));
}
else if(calcul <= -180){
drivetrain.setControl(
drive.withRotationalRate(0.5*180/Math.PI));
}
else{
drivetrain.setControl(
drive.withRotationalRate(0));
}
drivetrain.setControl( drivetrain.setControl(
drive.withVelocityX(0).withVelocityY(0).withRotationalRate(calcul)); drive.withVelocityX(0).withVelocityY(0).withRotationalRate(calcul));
System.out.println(angle); System.out.println(angle);

View File

@@ -13,10 +13,7 @@ import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.generated.TunerConstants; import frc.robot.generated.TunerConstants;
import frc.robot.subsystems.CommandSwerveDrivetrain; import frc.robot.subsystems.CommandSwerveDrivetrain;
import frc.robot.subsystems.Limelight3G; import frc.robot.subsystems.Limelight3G;
import static edu.wpi.first.units.Units.*;
import static edu.wpi.first.units.Units.MetersPerSecond;
import static edu.wpi.first.units.Units.RadiansPerSecond;
import static edu.wpi.first.units.Units.RotationsPerSecond;
import java.util.Optional; import java.util.Optional;
@@ -30,7 +27,7 @@ public class LimelighterAuto extends Command {
double boty; double boty;
double angle; double angle;
double calcul; double calcul;
Optional<Alliance> alliance = DriverStation.getAlliance(); Optional<Alliance> alliance = DriverStation.getAlliance();
private final SwerveRequest.FieldCentric drive = new SwerveRequest.FieldCentric() private final SwerveRequest.FieldCentric drive = new SwerveRequest.FieldCentric()
.withDeadband(MaxSpeed * 0.1).withRotationalDeadband(MaxAngularRate * 0.1) .withDeadband(MaxSpeed * 0.1).withRotationalDeadband(MaxAngularRate * 0.1)
.withDriveRequestType(DriveRequestType.OpenLoopVoltage); .withDriveRequestType(DriveRequestType.OpenLoopVoltage);
@@ -45,35 +42,44 @@ public class LimelighterAuto extends Command {
// Called when the command is initially scheduled. // Called when the command is initially scheduled.
@Override @Override
public void initialize() { public void initialize() {}
}
// Called every time the scheduler runs while the command is scheduled. // Called every time the scheduler runs while the command is scheduled.
@Override @Override
public void execute() { public void execute() {
double[] BotPose = new double[6]; double[] BotPose = new double[6];
System.out.println("e");
if (limelight3g.getV()) { if (limelight3g.getV()) {
if(!alliance.isPresent()){return;}
if(alliance.get() == Alliance.Blue){
BotPose = limelight3g.getBotPoseBlue(); BotPose = limelight3g.getBotPoseBlue();
}
else{
BotPose = limelight3g.getBotPoseRed();
}
botx = BotPose[1]; botx = BotPose[1];
boty = BotPose[0]; boty = BotPose[0];
angle = drivetrain.getPigeon2().getYaw().getValueAsDouble(); angle = drivetrain.getPigeon2().getYaw().getValueAsDouble();
if(angle >180){ calcul = limelight3g.Calcule(botx, 4, boty, 4.6, angle);
angle -= 360; if(calcul < -5 && calcul > -180){
drivetrain.setControl(
drive.withRotationalRate(0.5*180/Math.PI));
} }
calcul = limelight3g.Calcule(botx, 4, boty, 4.6, angle) / 5; else if(calcul > 5 && calcul < 180){
drivetrain.setControl(
drive.withRotationalRate(-0.5*180/Math.PI));
}
else if(calcul >= 180){
drivetrain.setControl(
drive.withRotationalRate(-0.5*180/Math.PI));
}
else if(calcul <= -180){
drivetrain.setControl(
drive.withRotationalRate(0.5*180/Math.PI));
}
else{
drivetrain.setControl(
drive.withRotationalRate(0));
}
drivetrain.setControl( drivetrain.setControl(
drive.withVelocityX(0).withVelocityY(0).withRotationalRate(calcul)); drive.withVelocityX(0).withVelocityY(0).withRotationalRate(calcul));
System.out.println("Limelighter"); System.out.println(angle);
System.out.println(calcul); if (calcul < 0.2 && calcul > -0.2) {
if (calcul < 0.3 && calcul > -0.3) {
drivetrain.setControl(drive.withRotationalRate(0)); drivetrain.setControl(drive.withRotationalRate(0));
} }
} }
@@ -92,7 +98,7 @@ public class LimelighterAuto extends Command {
// Returns true when the command should end. // Returns true when the command should end.
@Override @Override
public boolean isFinished() { public boolean isFinished() {
return calcul < 0.3 && calcul > -0.3; return calcul < 5 && calcul > -5;
} }
} }

View File

@@ -52,7 +52,22 @@ NetworkTable table = NetworkTableInstance.getDefault().getTable("limelight-tag")
} }
public double Calcule(double x1, double x2, double y1, double y2, double angle) public double Calcule(double x1, double x2, double y1, double y2, double angle)
{ {
return Math.atan((x2 - x1) / (y2 - y1)) * (180 / Math.PI) - angle; if(x1 > x2){
if(y1 > y2){
return Math.atan(90-((x2 - x1) / (y2 - y1))) * (180 / Math.PI) - angle;
}
else{
return Math.atan((x2 - x1) / (y2 - y1)) * (180 / Math.PI)+90 - angle;
}
}
else{
if(y1 > y2){
return Math.atan(90-((x2 - x1) / (y2 - y1)))* (180 / Math.PI)+270 - angle;
}
else{
return Math.atan((x2 - x1) / (y2 - y1)) * (180 / Math.PI)+180 - angle;
}
}
} }
@Override @Override
public void periodic() { public void periodic() {