2025-01-28 19:00:44 -07:00
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc4388.robot.commands ;
2025-01-29 17:06:55 -07:00
import edu.wpi.first.math.geometry.Rotation2d ;
import edu.wpi.first.math.geometry.Translation2d ;
2025-02-01 15:52:06 -07:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard ;
2025-01-28 19:00:44 -07:00
import edu.wpi.first.wpilibj2.command.Command ;
import frc4388.robot.subsystems.Lidar ;
import frc4388.robot.subsystems.SwerveDrive ;
/* You should consider using the more terse Command factories API instead https://docs.wpilib.org/en/stable/docs/software/commandbased/organizing-command-based.html#defining-commands */
public class LidarAlign extends Command {
private SwerveDrive swerveDrive ;
private Lidar lidar ;
private int currentFinderTick ;
// private int tickFoundPipe;
private boolean foundReef ;
private boolean headedRight ;
2025-01-29 17:06:55 -07:00
private double speed ;
2025-01-28 19:00:44 -07:00
private final boolean constructedHeadedRight ;
/** Creates a new LidarAlign. */
public LidarAlign ( SwerveDrive swerveDrive , Lidar lidar , boolean headedRight ) {
// Use addRequirements() here to declare subsystem dependencies.
constructedHeadedRight = headedRight ;
this . swerveDrive = swerveDrive ;
this . lidar = lidar ;
2025-01-29 17:06:55 -07:00
addRequirements ( swerveDrive , lidar ) ;
2025-01-28 19:00:44 -07:00
}
// Called when the command is initially scheduled.
@Override
public void initialize ( ) {
this . currentFinderTick = 0 ;
2025-01-30 19:28:46 -07:00
this . speed = 0 . 4 ; // TODO: find good speed for this
2025-01-28 19:00:44 -07:00
this . foundReef = false ;
this . headedRight = constructedHeadedRight ;
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute ( ) {
2025-01-29 17:06:55 -07:00
if ( lidar . withinDistance ( ) ) {
swerveDrive . softStop ( ) ;
foundReef = true ;
return ;
}
if ( currentFinderTick > 100 ) { //arbutrary threshhold for now.
headedRight = ! headedRight ;
currentFinderTick * = - 1 ;
}
2025-02-01 15:52:06 -07:00
double currentHeading = ( swerveDrive . getGyroAngle ( ) * 180 ) / Math . PI ;
double relAngle = ( Math . round ( currentHeading / 60 . d ) * 60 ) ; // Relative driving to the side of the reef
SmartDashboard . putNumber ( " Rel Angle " , relAngle ) ;
SmartDashboard . putNumber ( " heading " , currentHeading ) ;
2025-01-29 17:06:55 -07:00
if ( ! headedRight ) {
swerveDrive . driveRelativeLockedAngle ( new Translation2d ( 0 , - speed ) , Rotation2d . fromDegrees ( relAngle ) ) ;
2025-01-30 19:28:46 -07:00
} else {
swerveDrive . driveRelativeLockedAngle ( new Translation2d ( 0 , speed ) , Rotation2d . fromDegrees ( relAngle ) ) ;
2025-01-29 17:06:55 -07:00
}
currentFinderTick + + ;
2025-01-28 19:00:44 -07:00
}
// Called once the command ends or is interrupted.
@Override
2025-01-29 17:06:55 -07:00
public void end ( boolean interrupted ) {
}
2025-01-28 19:00:44 -07:00
// Returns true when the command should end.
@Override
public boolean isFinished ( ) {
2025-01-30 16:32:45 -07:00
if ( foundReef & & lidar . withinDistance ( ) ) { // spot on
2025-01-29 17:06:55 -07:00
swerveDrive . stopModules ( ) ;
return true ;
2025-01-30 16:32:45 -07:00
} else if ( foundReef & & ! lidar . withinDistance ( ) ) { // over shot
2025-01-29 17:06:55 -07:00
speed = speed / 2 ;
headedRight = ! headedRight ;
currentFinderTick = 0 ;
foundReef = false ;
return false ;
} else {
return false ;
}
2025-01-28 19:00:44 -07:00
}
}