-
Notifications
You must be signed in to change notification settings - Fork 1
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
Wojtaszek - Autonomous and other stuff #6
Open
wojtase1
wants to merge
18
commits into
main
Choose a base branch
from
wojtaszek
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
95109b7
Added capability to switch how the robot is controlled when the 'A' b…
wojtase1 f569bac
Adding documentation to RobotContainer.java and DriveSybsystemXrp.java
wojtase1 accb41a
Adding logic to toggle between three driving modes, Arcade Left only,…
wojtase1 47a2aa4
Putting gradle back to default settings
wojtase1 650140b
cleaning up code in robotContainer.java
wojtase1 73e9280
documenation cleanup
wojtase1 d411605
update comments
wojtase1 744df25
added autonomous drive logic
wojtase1 d911a8a
Merge pull request #4 from wojtase1/main
wojtase1 96c828f
Added PID logic to DriveDistance.java
wojtase1 f86d925
Adding logic to drive using the wheel encoders.
wojtase1 1de758c
Merge pull request #5 from wojtase1/main
wojtase1 19329e6
updated logic for tank drive to use chassisppeed
wojtase1 9fc33d0
Cleaned up logic.
wojtase1 4397cc0
updating per pull request comments.
wojtase1 2d1e03b
Putting field back into DriveSubSystemXRP.java
wojtase1 9df19e5
adding more autonous stuff
wojtase1 3159d50
adding logic to get both servos to move at the same time:
wojtase1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 frc.robot.commands; | ||
|
||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup; | ||
import frc.robot.Constants.DriveConstants; | ||
import frc.robot.subsystems.drive.DriveSubsystemXrp; | ||
|
||
public class AutonomousCommand extends SequentialCommandGroup { | ||
/** | ||
* Creates a new Autonomous Drive based on distance. This will drive out for a specified distance, | ||
* turn around and drive back. | ||
* | ||
* @param drivetrain The drivetrain subsystem on which this command will run | ||
*/ | ||
public AutonomousCommand(DriveSubsystemXrp drivetrain) { | ||
|
||
// Creates a PID Controller for the wheels going in a straight line | ||
PIDController wheelPIDController = | ||
new PIDController(DriveConstants.speedPIDConstants.kP, | ||
DriveConstants.speedPIDConstants.kI, | ||
DriveConstants.speedPIDConstants.kD); | ||
|
||
PIDController zAxisPidController = | ||
new PIDController(DriveConstants.zRotationPIDConstants.kP, | ||
DriveConstants.zRotationPIDConstants.kI, | ||
DriveConstants.zRotationPIDConstants.kD); | ||
|
||
addCommands( | ||
new DriveDistance(1, 1, drivetrain, wheelPIDController, zAxisPidController), // Drive forward | ||
new DriveDistance(-1, 1, drivetrain, wheelPIDController, zAxisPidController)); // Drive backward | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// 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 frc.robot.commands; | ||
|
||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics; | ||
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.Constants.DriveConstants; | ||
import frc.robot.subsystems.drive.DriveSubsystemXrp; | ||
import org.littletonrobotics.junction.Logger; | ||
|
||
public class DriveDistance extends Command { | ||
private DriveSubsystemXrp drive; | ||
private double distance; | ||
private double speed; | ||
private PIDController distancePIDController; | ||
private PIDController zRotationPIDController; | ||
private DifferentialDriveKinematics kinematics; | ||
private double rightWheelStartPosition; | ||
private double leftWheelStartPosition; | ||
private double zRotationStartingPosition; | ||
|
||
/** | ||
* Creates a new DriveDistance. This command will drive your your robot for a desired distance at | ||
* a desired speed. | ||
* | ||
* @param speed The speed at which the robot will drive | ||
* @param meters The number of meters the robot will drive | ||
* @param drive The drivetrain subsystem on which this command will run | ||
*/ | ||
|
||
public DriveDistance( | ||
double driveSpeed, | ||
double meters, | ||
DriveSubsystemXrp driveSubsystem, | ||
PIDController dPIDController, | ||
PIDController zPIDController | ||
) | ||
{ | ||
|
||
distance = meters; | ||
speed = driveSpeed; | ||
drive = driveSubsystem; | ||
distancePIDController = dPIDController; | ||
zRotationPIDController = zPIDController; // Initialize the PID controller for Z rotation | ||
|
||
// Create a DifferentialDriveKinematics object with the track width | ||
kinematics = new DifferentialDriveKinematics(DriveConstants.trackWidthMeters); | ||
|
||
|
||
|
||
addRequirements(drive); | ||
} | ||
|
||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() { | ||
drive.resetEncoders(); | ||
drive.setChassisSpeeds(new ChassisSpeeds(0, 0, 0)); | ||
distancePIDController.reset(); | ||
rightWheelStartPosition = drive.getRightPositionMeters(); | ||
leftWheelStartPosition = drive.getLeftPositionMeters(); | ||
zRotationStartingPosition = drive.getAngleZ(); | ||
} | ||
|
||
// Called every time the scheduler runs while the command is scheduled. | ||
@Override | ||
public void execute() { | ||
double leftDistance = leftWheelStartPosition - drive.getLeftPositionMeters(); | ||
double rightDistance = rightWheelStartPosition - drive.getRightPositionMeters(); | ||
|
||
double distanceError = leftDistance - rightDistance; | ||
|
||
double distanceCorrection = distancePIDController.calculate(distanceError); | ||
|
||
double zRotationDrift = zRotationStartingPosition - drive.getAngleZ(); | ||
|
||
// Calculate the Z rotation correction using the PID controller | ||
double headingCorrection = zRotationPIDController.calculate(zRotationDrift); | ||
|
||
double leftSpeed = speed - distanceCorrection; | ||
double rightSpeed = speed + distanceCorrection; | ||
|
||
// Create a DifferentialDriveWheelSpeeds object with the wheel speeds | ||
DifferentialDriveWheelSpeeds wheelSpeeds = | ||
new DifferentialDriveWheelSpeeds(leftSpeed, rightSpeed); | ||
|
||
// // Convert the wheel speeds to chassis speeds | ||
ChassisSpeeds chassisSpeeds = kinematics.toChassisSpeeds(wheelSpeeds); | ||
// Log the new value | ||
Logger.recordOutput("leftWheelStartPosition", leftWheelStartPosition); | ||
Logger.recordOutput("rightWheelStartPosition", rightWheelStartPosition); | ||
Logger.recordOutput("LeftPositionMeters", drive.getLeftPositionMeters()); | ||
Logger.recordOutput("RightPositionMeters", drive.getLeftPositionMeters()); | ||
Logger.recordOutput("leftDistance", leftDistance); | ||
Logger.recordOutput("rightDistance", leftDistance); | ||
Logger.recordOutput("distanceError", distanceError); | ||
Logger.recordOutput("distanceCorrection", distanceCorrection); | ||
Logger.recordOutput("zRotationDrift", zRotationDrift); | ||
Logger.recordOutput("headingCorrection", headingCorrection); | ||
Logger.recordOutput("leftSpeed", leftSpeed); | ||
Logger.recordOutput("rightSpeed", rightSpeed); | ||
Logger.recordOutput("chassisSpeeds", chassisSpeeds); | ||
Logger.recordOutput("zRotationStartingPosition", zRotationStartingPosition); | ||
|
||
drive.setChassisSpeeds(chassisSpeeds); | ||
|
||
} | ||
|
||
// Called once the command ends or is interrupted. | ||
@Override | ||
public void end(boolean interrupted) { | ||
drive.setChassisSpeeds(new ChassisSpeeds(0, 0, 0)); | ||
} | ||
|
||
// Returns true when the command should end. | ||
@Override | ||
public boolean isFinished() { | ||
double leftDistanceTraveled = leftWheelStartPosition - drive.getLeftPositionMeters(); | ||
double rightDistanceTraveled = rightWheelStartPosition - drive.getRightPositionMeters(); | ||
|
||
double distanceTraveled = (leftDistanceTraveled + rightDistanceTraveled) / 2.0; | ||
return (Math.abs(distanceTraveled) >= distance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 frc.robot.commands; | ||
|
||
import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics; | ||
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Subsystem; | ||
import frc.robot.Constants.DriveConstants; | ||
import frc.robot.subsystems.drive.Drive; | ||
import java.util.function.Supplier; | ||
|
||
public class TankDrive extends Command { | ||
private final Drive drive; | ||
private final Supplier<Double> rightWheelSpeedSupplier; | ||
private final Supplier<Double> leftWheelSpeedSupplier; | ||
|
||
// Create a DifferentialDriveKinematics object with the track width | ||
private DifferentialDriveKinematics kinematics = | ||
new DifferentialDriveKinematics(DriveConstants.trackWidthMeters); | ||
|
||
/** | ||
* Creates a new TankDrive. This command will drive your robot according to the speed supplier | ||
* lambdas. This command does not terminate. | ||
* | ||
* @param drive The drivetrain subsystem on which this command will run | ||
* @param leftWheelSpeedSupplier Lambda supplier of forward/backward speed | ||
* @param rightWheelSpeedSupplier Lambda supplier of rotational speed | ||
*/ | ||
public TankDrive( | ||
Drive drive, | ||
Supplier<Double> leftWheelSpeedSupplier, | ||
Supplier<Double> rightWheelSpeedSupplier) { | ||
this.drive = drive; | ||
this.rightWheelSpeedSupplier = leftWheelSpeedSupplier; | ||
this.leftWheelSpeedSupplier = rightWheelSpeedSupplier; | ||
addRequirements((Subsystem) drive); | ||
} | ||
|
||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() {} | ||
|
||
// Called every time the scheduler runs while the command is scheduled. | ||
@Override | ||
public void execute() { | ||
|
||
double rightWheelSpeed = | ||
rightWheelSpeedSupplier.get() * DriveConstants.maxLinearVelocityMetersPerSec; | ||
double leftWheelSpeed = | ||
leftWheelSpeedSupplier.get() * DriveConstants.maxLinearVelocityMetersPerSec; | ||
|
||
// Create a DifferentialDriveWheelSpeeds object with the wheel speeds | ||
DifferentialDriveWheelSpeeds wheelSpeeds = | ||
new DifferentialDriveWheelSpeeds(leftWheelSpeed, rightWheelSpeed); | ||
|
||
// Convert the wheel speeds to chassis speeds | ||
ChassisSpeeds chassisSpeeds = kinematics.toChassisSpeeds(wheelSpeeds); | ||
|
||
// Pass the wheel speeds to the drive subsystem | ||
drive.setChassisSpeeds(chassisSpeeds); | ||
} | ||
|
||
// Called once the command ends or is interrupted. | ||
@Override | ||
public void end(boolean interrupted) {} | ||
|
||
// Returns true when the command should end. | ||
@Override | ||
public boolean isFinished() { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you change DriveSubsystemXrp --> Drive, then this needs to be cast to a Subsystem:
addRequirements((Subsystem) drive);