diff --git a/ArmBot/subsystems/drivesubsystem.py b/ArmBot/subsystems/drivesubsystem.py index e704dc62..201943db 100644 --- a/ArmBot/subsystems/drivesubsystem.py +++ b/ArmBot/subsystems/drivesubsystem.py @@ -4,8 +4,8 @@ # the WPILib BSD license file in the root directory of this project. # -import wpilib -import wpilib.drive +from wpilib import PWMSparkMax, Encoder +from wpilib.drive import DifferentialDrive import commands2 import constants @@ -16,30 +16,29 @@ class DriveSubsystem(commands2.Subsystem): def __init__(self) -> None: super().__init__() - # The motors on the left side of the drive - self.left_motors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor2Port), - ) + # The motors on the left side of the drive. + self.left1 = PWMSparkMax(constants.DriveConstants.kLeftMotor1Port) + self.left2 = PWMSparkMax(constants.DriveConstants.kLeftMotor2Port) - # The motors on the right side of the drive - self.right_motors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor2Port), - ) + # The motors on the right side of the drive. + self.right1 = PWMSparkMax(constants.DriveConstants.kRightMotor1Port) + self.right2 = PWMSparkMax(constants.DriveConstants.kRightMotor2Port) + + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) # The robot's drive - self.drive = wpilib.drive.DifferentialDrive(self.left_motors, self.right_motors) + self.drive = DifferentialDrive(self.left1, self.right1) # The left-side drive encoder - self.left_encoder = wpilib.Encoder( + self.left_encoder = Encoder( constants.DriveConstants.kLeftEncoderPorts[0], constants.DriveConstants.kLeftEncoderPorts[1], constants.DriveConstants.kLeftEncoderReversed, ) # The right-side drive encoder - self.right_encoder = wpilib.Encoder( + self.right_encoder = Encoder( constants.DriveConstants.kRightEncoderPorts[0], constants.DriveConstants.kRightEncoderPorts[1], constants.DriveConstants.kRightEncoderReversed, @@ -56,7 +55,7 @@ def __init__(self) -> None: # We need to invert one side of the drivetrain so that positive voltages # result in both sides moving forward. Depending on how your robot's # gearbox is constructed, you might have to invert the left side instead. - self.right_motors.setInverted(True) + self.right1.setInverted(True) def arcadeDrive(self, fwd: float, rot: float) -> None: """Drives the robot using arcade controls. @@ -80,14 +79,14 @@ def getAverageEncoderDistance(self) -> float: self.left_encoder.getDistance() + self.right_encoder.getDistance() ) / 2.0 - def getLeftEncoder(self) -> wpilib.Encoder: + def getLeftEncoder(self) -> Encoder: """Gets the left drive encoder. :returns: the left drive encoder """ return self.left_encoder - def getRightEncoder(self) -> wpilib.Encoder: + def getRightEncoder(self) -> Encoder: """Gets the right drive encoder. :returns: the right drive encoder diff --git a/ArmBotOffboard/subsystems/drivesubsystem.py b/ArmBotOffboard/subsystems/drivesubsystem.py index 92c9eff0..4dcfef62 100644 --- a/ArmBotOffboard/subsystems/drivesubsystem.py +++ b/ArmBotOffboard/subsystems/drivesubsystem.py @@ -4,12 +4,14 @@ # the WPILib BSD license file in the root directory of this project. # +import typing + import commands2 import commands2.cmd +from wpilib import PWMSparkMax, Encoder +from wpilib.drive import DifferentialDrive + import constants -import wpilib -import wpilib.drive -import typing class DriveSubsystem(commands2.Subsystem): @@ -17,29 +19,28 @@ def __init__(self) -> None: super().__init__() # The motors on the left side of the drive. - self.left = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.kLeftMotor1Port), - wpilib.PWMSparkMax(constants.kLeftMotor2Port), - ) + self.left1 = PWMSparkMax(constants.kLeftMotor1Port) + self.left2 = PWMSparkMax(constants.kLeftMotor2Port) # The motors on the right side of the drive. - self.right = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.kRightMotor1Port), - wpilib.PWMSparkMax(constants.kRightMotor2Port), - ) + self.right1 = PWMSparkMax(constants.kRightMotor1Port) + self.right2 = PWMSparkMax(constants.kRightMotor2Port) + + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) # The robot's drive - self.drive = wpilib.drive.DifferentialDrive(self.left, self.right) + self.drive = DifferentialDrive(self.left1, self.right1) # The left-side drive encoder - self.left_encoder = wpilib.Encoder( + self.left_encoder = Encoder( constants.kLeftEncoderPorts[0], constants.kLeftEncoderPorts[1], constants.kLeftEncoderReversed, ) # The right-side drive encoder - self.right_encoder = wpilib.Encoder( + self.right_encoder = Encoder( constants.kRightEncoderPorts[0], constants.kRightEncoderPorts[1], constants.kRightEncoderReversed, @@ -52,7 +53,7 @@ def __init__(self) -> None: # We need to invert one side of the drivetrain so that positive voltages # result in both sides moving forward. Depending on how your robot's # gearbox is constructed, you might have to invert the left side instead. - self.right.setInverted(True) + self.right1.setInverted(True) def arcadeDriveCommand( self, fwd: typing.Callable[[], float], rot: typing.Callable[[], float] @@ -71,8 +72,8 @@ def arcadeDriveCommand( def resetEncoders(self) -> None: """Resets the drive encoders to currently read a position of 0.""" - self.left.reset() - self.right.reset() + self.left_encoder.reset() + self.right_encoder.reset() def getAverageEncoderDistance(self) -> float: """Gets the average distance of the two encoders. @@ -82,7 +83,7 @@ def getAverageEncoderDistance(self) -> float: """ return (self.left_encoder.getDistance() + self.right_encoder.getDistance()) / 2 - def getLeftEncoder(self) -> wpilib.Encoder: + def getLeftEncoder(self) -> Encoder: """Gets the left drive encoder. Returns: @@ -90,7 +91,7 @@ def getLeftEncoder(self) -> wpilib.Encoder: """ return self.left_encoder - def getRightEncoder(self) -> wpilib.Encoder: + def getRightEncoder(self) -> Encoder: """Gets the right drive encoder. Returns: diff --git a/DifferentialDriveBot/drivetrain.py b/DifferentialDriveBot/drivetrain.py index 6f97d873..f9018799 100755 --- a/DifferentialDriveBot/drivetrain.py +++ b/DifferentialDriveBot/drivetrain.py @@ -23,17 +23,23 @@ class Drivetrain: ENCODER_RESOLUTION = 4096 # counts per revolution def __init__(self): - leftLeader = wpilib.PWMSparkMax(1) - leftFollower = wpilib.PWMSparkMax(2) - rightLeader = wpilib.PWMSparkMax(3) - rightFollower = wpilib.PWMSparkMax(4) + self.leftLeader = wpilib.PWMSparkMax(1) + self.leftFollower = wpilib.PWMSparkMax(2) + self.rightLeader = wpilib.PWMSparkMax(3) + self.rightFollower = wpilib.PWMSparkMax(4) + + # Make sure both motors for each side are in the same group + self.leftLeader.addFollower(self.leftFollower) + self.rightLeader.addFollower(self.rightFollower) + + # We need to invert one side of the drivetrain so that positive voltages + # result in both sides moving forward. Depending on how your robot's + # gearbox is constructed, you might have to invert the left side instead. + self.rightLeader.setInverted(True) self.leftEncoder = wpilib.Encoder(0, 1) self.rightEncoder = wpilib.Encoder(2, 3) - self.leftGroup = wpilib.MotorControllerGroup(leftLeader, leftFollower) - self.rightGroup = wpilib.MotorControllerGroup(rightLeader, rightFollower) - self.gyro = wpilib.AnalogGyro(0) self.leftPIDController = wpimath.controller.PIDController(1.0, 0.0, 0.0) @@ -48,11 +54,6 @@ def __init__(self): self.gyro.reset() - # We need to invert one side of the drivetrain so that positive voltages - # result in both sides moving forward. Depending on how your robot's - # gearbox is constructed, you might have to invert the left side instead. - self.rightGroup.setInverted(True) - # Set the distance per pulse for the drive encoders. We can simply use the # distance traveled for one rotation of the wheel divided by the encoder # resolution. @@ -84,8 +85,9 @@ def setSpeeds(self, speeds: wpimath.kinematics.DifferentialDriveWheelSpeeds): self.rightEncoder.getRate(), speeds.right ) - self.leftGroup.setVoltage(leftOutput + leftFeedforward) - self.rightGroup.setVoltage(rightOutput + rightFeedforward) + # Controls the left and right sides of the robot using the calculated outputs + self.leftLeader.setVoltage(leftOutput + leftFeedforward) + self.rightLeader.setVoltage(rightOutput + rightFeedforward) def drive(self, xSpeed, rot): """Drives the robot with the given linear velocity and angular velocity.""" diff --git a/FrisbeeBot/subsystems/drivesubsystem.py b/FrisbeeBot/subsystems/drivesubsystem.py index dfacd12e..7b37de29 100644 --- a/FrisbeeBot/subsystems/drivesubsystem.py +++ b/FrisbeeBot/subsystems/drivesubsystem.py @@ -4,8 +4,8 @@ # the WPILib BSD license file in the root directory of this project. # -import wpilib -import wpilib.drive +from wpilib import PWMSparkMax, Encoder +from wpilib.drive import DifferentialDrive import commands2 import constants @@ -17,29 +17,28 @@ def __init__(self) -> None: super().__init__() # The motors on the left side of the drive. - self.leftMotors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor2Port), - ) + self.left1 = PWMSparkMax(constants.DriveConstants.kLeftMotor1Port) + self.left2 = PWMSparkMax(constants.DriveConstants.kLeftMotor2Port) # The motors on the right side of the drive. - self.rightMotors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor2Port), - ) + self.right1 = PWMSparkMax(constants.DriveConstants.kRightMotor1Port) + self.right2 = PWMSparkMax(constants.DriveConstants.kRightMotor2Port) + + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) # The robot's drive - self.drive = wpilib.drive.DifferentialDrive(self.leftMotors, self.rightMotors) + self.drive = DifferentialDrive(self.left1, self.right1) # The left-side drive encoder - self.leftEncoder = wpilib.Encoder( + self.leftEncoder = Encoder( constants.DriveConstants.kLeftEncoderPorts[0], constants.DriveConstants.kLeftEncoderPorts[1], constants.DriveConstants.kLeftEncoderReversed, ) # The right-side drive encoder - self.rightEncoder = wpilib.Encoder( + self.rightEncoder = Encoder( constants.DriveConstants.kRightEncoderPorts[0], constants.DriveConstants.kRightEncoderPorts[1], constants.DriveConstants.kRightEncoderReversed, @@ -48,7 +47,7 @@ def __init__(self) -> None: # We need to invert one side of the drivetrain so that positive voltages # result in both sides moving forward. Depending on how your robot's # gearbox is constructed, you might have to invert the left side instead. - self.rightMotors.setInverted(True) + self.right1.setInverted(True) # Sets the distance per pulse for the encoders self.leftEncoder.setDistancePerPulse( @@ -80,7 +79,7 @@ def getAverageEncoderDistance(self): """ return (self.leftEncoder.getDistance() + self.rightEncoder.getDistance()) / 2.0 - def getLeftEncoder(self) -> wpilib.Encoder: + def getLeftEncoder(self) -> Encoder: """ Gets the left drive encoder. @@ -88,7 +87,7 @@ def getLeftEncoder(self) -> wpilib.Encoder: """ return self.leftEncoder - def getRightEncoder(self) -> wpilib.Encoder: + def getRightEncoder(self) -> Encoder: """ Gets the right drive encoder. diff --git a/GyroDriveCommands/subsystems/drivesubsystem.py b/GyroDriveCommands/subsystems/drivesubsystem.py index da649dd1..b63efe47 100644 --- a/GyroDriveCommands/subsystems/drivesubsystem.py +++ b/GyroDriveCommands/subsystems/drivesubsystem.py @@ -4,8 +4,8 @@ # the WPILib BSD license file in the root directory of this project. # -import wpilib -import wpilib.drive +from wpilib import PWMSparkMax, Encoder, ADXRS450_Gyro +from wpilib.drive import DifferentialDrive import commands2 import math @@ -18,29 +18,28 @@ def __init__(self) -> None: super().__init__() # The motors on the left side of the drive. - self.leftMotors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kLeftMotor2Port), - ) + self.left1 = PWMSparkMax(constants.DriveConstants.kLeftMotor1Port) + self.left2 = PWMSparkMax(constants.DriveConstants.kLeftMotor2Port) # The motors on the right side of the drive. - self.rightMotors = wpilib.MotorControllerGroup( - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor1Port), - wpilib.PWMSparkMax(constants.DriveConstants.kRightMotor2Port), - ) + self.right1 = PWMSparkMax(constants.DriveConstants.kRightMotor1Port) + self.right2 = PWMSparkMax(constants.DriveConstants.kRightMotor2Port) + + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) # The robot's drive - self.drive = wpilib.drive.DifferentialDrive(self.leftMotors, self.rightMotors) + self.drive = DifferentialDrive(self.left1, self.right1) # The left-side drive encoder - self.leftEncoder = wpilib.Encoder( + self.leftEncoder = Encoder( constants.DriveConstants.kLeftEncoderPorts[0], constants.DriveConstants.kLeftEncoderPorts[1], constants.DriveConstants.kLeftEncoderReversed, ) # The right-side drive encoder - self.rightEncoder = wpilib.Encoder( + self.rightEncoder = Encoder( constants.DriveConstants.kRightEncoderPorts[0], constants.DriveConstants.kRightEncoderPorts[1], constants.DriveConstants.kRightEncoderReversed, @@ -49,7 +48,7 @@ def __init__(self) -> None: # We need to invert one side of the drivetrain so that positive voltages # result in both sides moving forward. Depending on how your robot's # gearbox is constructed, you might have to invert the left side instead. - self.rightMotors.setInverted(True) + self.right1.setInverted(True) # Sets the distance per pulse for the encoders self.leftEncoder.setDistancePerPulse( @@ -59,7 +58,7 @@ def __init__(self) -> None: constants.DriveConstants.kEncoderDistancePerPulse ) - self.gyro = wpilib.ADXRS450_Gyro() + self.gyro = ADXRS450_Gyro() def arcadeDrive(self, fwd: float, rot: float): """ @@ -83,7 +82,7 @@ def getAverageEncoderDistance(self): """ return (self.leftEncoder.getDistance() + self.rightEncoder.getDistance()) / 2.0 - def getLeftEncoder(self) -> wpilib.Encoder: + def getLeftEncoder(self) -> Encoder: """ Gets the left drive encoder. @@ -91,7 +90,7 @@ def getLeftEncoder(self) -> wpilib.Encoder: """ return self.leftEncoder - def getRightEncoder(self) -> wpilib.Encoder: + def getRightEncoder(self) -> Encoder: """ Gets the right drive encoder. diff --git a/HatchbotInlined/subsystems/drivesubsystem.py b/HatchbotInlined/subsystems/drivesubsystem.py index 1ec25b25..ef93671e 100644 --- a/HatchbotInlined/subsystems/drivesubsystem.py +++ b/HatchbotInlined/subsystems/drivesubsystem.py @@ -20,11 +20,16 @@ def __init__(self) -> None: self.right1 = wpilib.PWMVictorSPX(constants.kRightMotor1Port) self.right2 = wpilib.PWMVictorSPX(constants.kRightMotor2Port) + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) + + # We need to invert one side of the drivetrain so that positive speeds + # result in both sides moving forward. Depending on how your robot's + # drivetrain is constructed, you might have to invert the left side instead. + self.right1.setInverted(True) + # The robot's drive - self.drive = wpilib.drive.DifferentialDrive( - wpilib.MotorControllerGroup(self.left1, self.left2), - wpilib.MotorControllerGroup(self.right1, self.right2), - ) + self.drive = wpilib.drive.DifferentialDrive(self.left1, self.right1) # The left-side drive encoder self.leftEncoder = wpilib.Encoder( diff --git a/HatchbotTraditional/subsystems/drivesubsystem.py b/HatchbotTraditional/subsystems/drivesubsystem.py index 1ec25b25..f25b03be 100644 --- a/HatchbotTraditional/subsystems/drivesubsystem.py +++ b/HatchbotTraditional/subsystems/drivesubsystem.py @@ -20,11 +20,13 @@ def __init__(self) -> None: self.right1 = wpilib.PWMVictorSPX(constants.kRightMotor1Port) self.right2 = wpilib.PWMVictorSPX(constants.kRightMotor2Port) + # We need to invert one side of the drivetrain so that positive speeds + # result in both sides moving forward. Depending on how your robot's + # drivetrain is constructed, you might have to invert the left side instead. + self.right1.setInverted(True) + # The robot's drive - self.drive = wpilib.drive.DifferentialDrive( - wpilib.MotorControllerGroup(self.left1, self.left2), - wpilib.MotorControllerGroup(self.right1, self.right2), - ) + self.drive = wpilib.drive.DifferentialDrive(self.left1, self.right1) # The left-side drive encoder self.leftEncoder = wpilib.Encoder( diff --git a/RamseteCommand/subsystems/driveSubsystem.py b/RamseteCommand/subsystems/driveSubsystem.py index 99d75deb..f81486a9 100644 --- a/RamseteCommand/subsystems/driveSubsystem.py +++ b/RamseteCommand/subsystems/driveSubsystem.py @@ -6,7 +6,7 @@ from commands2 import Subsystem -from wpilib import MotorControllerGroup, PWMSparkMax, Encoder, ADXRS450_Gyro +from wpilib import PWMSparkMax, Encoder, ADXRS450_Gyro from wpilib.drive import DifferentialDrive from wpimath.kinematics import DifferentialDriveOdometry, DifferentialDriveWheelSpeeds @@ -19,19 +19,23 @@ def __init__(self): super().__init__() # The motors on the left side of the drive. - self.leftMotors = MotorControllerGroup( - PWMSparkMax(constants.kLeftMotor1Port), - PWMSparkMax(constants.kLeftMotor2Port), - ) + self.left1 = PWMSparkMax(constants.kLeftMotor1Port) + self.left2 = PWMSparkMax(constants.kLeftMotor2Port) # The motors on the right side of the drive. - self.rightMotors = MotorControllerGroup( - PWMSparkMax(constants.kRightMotor1Port), - PWMSparkMax(constants.kRightMotor2Port), - ) + self.right1 = PWMSparkMax(constants.kRightMotor1Port) + self.right2 = PWMSparkMax(constants.kRightMotor2Port) + + self.left1.addFollower(self.left2) + self.right1.addFollower(self.right2) # The robot's drive - self.drive = DifferentialDrive(self.leftMotors, self.rightMotors) + self.drive = DifferentialDrive(self.left1, self.right1) + + # We need to invert one side of the drivetrain so that positive voltages + # result in both sides moving forward. Depending on how your robot's + # gearbox is constructed, you might have to invert the left side instead. + self.right1.setInverted(True) # The left-side drive encoder self.leftEncoder = Encoder( @@ -50,11 +54,6 @@ def __init__(self): # The gyro sensor self.gyro = ADXRS450_Gyro() - # We need to invert one side of the drivetrain so that positive voltages - # result in both sides moving forward. Depending on how your robot's - # gearbox is constructed, you might have to invert the left side instead. - self.rightMotors.setInverted(True) - # Sets the distance per pulse for the encoders self.leftEncoder.setDistancePerPulse(constants.kEncoderDistancePerPulse) self.rightEncoder.setDistancePerPulse(constants.kEncoderDistancePerPulse) @@ -101,8 +100,8 @@ def arcadeDrive(self, fwd, rot): def tankDriveVolts(self, leftVolts, rightVolts): """Controls the left and right sides of the drive directly with voltages.""" - self.leftMotors.setVoltage(leftVolts) - self.rightMotors.setVoltage(rightVolts) + self.left1.setVoltage(leftVolts) + self.right1.setVoltage(rightVolts) self.drive.feed() def resetEncoders(self): diff --git a/RamseteController/drivetrain.py b/RamseteController/drivetrain.py index 7f5e2553..58111621 100644 --- a/RamseteController/drivetrain.py +++ b/RamseteController/drivetrain.py @@ -24,16 +24,16 @@ class Drivetrain: kEncoderResolution = 4096 # counts per revolution def __init__(self): - leftLeader = wpilib.PWMSparkMax(1) - leftFollower = wpilib.PWMSparkMax(2) - rightLeader = wpilib.PWMSparkMax(3) - rightFollower = wpilib.PWMSparkMax(4) + self.leftLeader = wpilib.PWMSparkMax(1) + self.leftFollower = wpilib.PWMSparkMax(2) + self.rightLeader = wpilib.PWMSparkMax(3) + self.rightFollower = wpilib.PWMSparkMax(4) self.leftEncoder = wpilib.Encoder(0, 1) self.rightEncoder = wpilib.Encoder(2, 3) - self.leftGroup = wpilib.MotorControllerGroup(leftLeader, leftFollower) - self.rightGroup = wpilib.MotorControllerGroup(rightLeader, rightFollower) + self.leftLeader.addFollower(self.leftFollower) + self.rightLeader.addFollower(self.rightFollower) self.gyro = wpilib.AnalogGyro(0) @@ -52,7 +52,7 @@ def __init__(self): # We need to invert one side of the drivetrain so that positive voltages # result in both sides moving forward. Depending on how your robot's # gearbox is constructed, you might have to invert the left side instead. - self.rightGroup.setInverted(True) + self.rightLeader.setInverted(True) # Set the distance per pulse for the drive encoders. We can simply use the # distance traveled for one rotation of the wheel divided by the encoder @@ -85,8 +85,8 @@ def setSpeeds(self, speeds: wpimath.kinematics.DifferentialDriveWheelSpeeds): self.rightEncoder.getRate(), speeds.right ) - self.leftGroup.setVoltage(leftOutput + leftFeedforward) - self.rightGroup.setVoltage(rightOutput + rightFeedforward) + self.leftLeader.setVoltage(leftOutput + leftFeedforward) + self.rightLeader.setVoltage(rightOutput + rightFeedforward) def drive(self, xSpeed, rot): """Drives the robot with the given linear velocity and angular velocity."""