|
| 1 | +# |
| 2 | +# Copyright (c) FIRST and other WPILib contributors. |
| 3 | +# Open Source Software; you can modify and/or share it under the terms of |
| 4 | +# the WPILib BSD license file in the root directory of this project. |
| 5 | +# |
| 6 | + |
| 7 | +from commands2 import Command, Subsystem |
| 8 | +from commands2.sysid import SysIdRoutine |
| 9 | +from wpilib.sysid import SysIdRoutineLog |
| 10 | + |
| 11 | +from wpilib import Encoder, PWMSparkMax, RobotController |
| 12 | +from wpilib.drive import DifferentialDrive |
| 13 | + |
| 14 | +from wpimath.units import volts |
| 15 | + |
| 16 | +from constants import DriveConstants |
| 17 | + |
| 18 | +from typing import Callable |
| 19 | + |
| 20 | + |
| 21 | +class Drive(Subsystem): |
| 22 | + def __init__(self) -> None: |
| 23 | + # The motors on the left side of the drive |
| 24 | + self.left_motor = PWMSparkMax(DriveConstants.kLeftMotor1Port) |
| 25 | + self.left_motor.addFollower(PWMSparkMax(DriveConstants.kLeftMotor2Port)) |
| 26 | + |
| 27 | + # The motors on the right side of the drive |
| 28 | + self.right_motor = PWMSparkMax(DriveConstants.kRightMotor1Port) |
| 29 | + self.right_motor.addFollower(PWMSparkMax(DriveConstants.kRightMotor2Port)) |
| 30 | + |
| 31 | + # At least one side of the drive train needs to be inverted. This ensures that positive voltages sent to each motor group result in forward motion. |
| 32 | + self.right_motor.setInverted(True) |
| 33 | + |
| 34 | + # The robot's drive |
| 35 | + self.drive = DifferentialDrive(self.left_motor, self.right_motor) |
| 36 | + |
| 37 | + # The left-side drive encoder |
| 38 | + self.left_encoder = Encoder( |
| 39 | + DriveConstants.kLeftEncoderPorts[0], |
| 40 | + DriveConstants.kLeftEncoderPorts[1], |
| 41 | + DriveConstants.kLeftEncoderReversed, |
| 42 | + ) |
| 43 | + |
| 44 | + # The right-side drive encoder |
| 45 | + self.right_encoder = Encoder( |
| 46 | + DriveConstants.kRightEncoderPorts[0], |
| 47 | + DriveConstants.kRightEncoderPorts[1], |
| 48 | + DriveConstants.kRightEncoderReversed, |
| 49 | + ) |
| 50 | + |
| 51 | + # Sets the distance per pulse for the encoders |
| 52 | + self.left_encoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse) |
| 53 | + self.right_encoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse) |
| 54 | + |
| 55 | + # Tell SysId how to plumb the driving voltage to the motors. |
| 56 | + def drive(voltage: volts) -> None: |
| 57 | + self.left_motor.setVoltage(voltage) |
| 58 | + self.right_motor.setVoltage(voltage) |
| 59 | + |
| 60 | + # Tell SysId to make generated commands require this subsystem, suffix test state in |
| 61 | + # WPILog with this subsystem's name ("drive") |
| 62 | + self.sys_id_routine = SysIdRoutine( |
| 63 | + SysIdRoutine.Config(), |
| 64 | + SysIdRoutine.Mechanism(drive, self.log, self), |
| 65 | + ) |
| 66 | + |
| 67 | + # Tell SysId how to record a frame of data for each motor on the mechanism being |
| 68 | + # characterized. |
| 69 | + def log(self, sys_id_routine: SysIdRoutineLog) -> None: |
| 70 | + # Record a frame for the left motors. Since these share an encoder, we consider |
| 71 | + # the entire group to be one motor. |
| 72 | + sys_id_routine.motor("drive-left").voltage( |
| 73 | + self.left_motor.get() * RobotController.getBatteryVoltage() |
| 74 | + ).position(self.left_encoder.getDistance()).velocity( |
| 75 | + self.left_encoder.getRate() |
| 76 | + ) |
| 77 | + # Record a frame for the right motors. Since these share an encoder, we consider |
| 78 | + # the entire group to be one motor. |
| 79 | + sys_id_routine.motor("drive-right").voltage( |
| 80 | + self.right_motor.get() * RobotController.getBatteryVoltage() |
| 81 | + ).position(self.right_encoder.getDistance()).velocity( |
| 82 | + self.right_encoder.getRate() |
| 83 | + ) |
| 84 | + |
| 85 | + def arcadeDriveCommand( |
| 86 | + self, fwd: Callable[[], float], rot: Callable[[], float] |
| 87 | + ) -> Command: |
| 88 | + """Returns a command that drives the robot with arcade controls. |
| 89 | +
|
| 90 | + :param fwd: the commanded forward movement |
| 91 | + :param rot: the commanded rotation |
| 92 | + """ |
| 93 | + |
| 94 | + # A split-stick arcade command, with forward/backward controlled by the left |
| 95 | + # hand, and turning controlled by the right. |
| 96 | + return self.run(lambda: self.drive.arcadeDrive(fwd(), rot())) |
| 97 | + |
| 98 | + def sysIdQuasistatic(self, direction: SysIdRoutine.Direction) -> Command: |
| 99 | + return self.sys_id_routine.quasistatic(direction) |
| 100 | + |
| 101 | + def sysIdDynamic(self, direction: SysIdRoutine.Direction) -> Command: |
| 102 | + return self.sys_id_routine.dynamic(direction) |
0 commit comments