package frc.robot.subsystems; import com.revrobotics.CANSparkMax; import edu.wpi.first.math.controller.ProfiledPIDController; import edu.wpi.first.math.controller.SimpleMotorFeedforward; import edu.wpi.first.math.geometry.Pose2d; import edu.wpi.first.math.kinematics.ChassisSpeeds; import edu.wpi.first.math.kinematics.DifferentialDriveKinematics; import edu.wpi.first.math.kinematics.DifferentialDriveOdometry; import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds; import edu.wpi.first.math.trajectory.Trajectory; import edu.wpi.first.units.Distance; import edu.wpi.first.units.Measure; import edu.wpi.first.units.Velocity; import edu.wpi.first.units.Voltage; import edu.wpi.first.units.MutableMeasure; import edu.wpi.first.wpilibj.Encoder; import edu.wpi.first.wpilibj.RobotController; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Commands; import edu.wpi.first.wpilibj2.command.SubsystemBase; import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine; import static edu.wpi.first.units.MutableMeasure.mutable; import static edu.wpi.first.units.Units.Volts; import org.littletonrobotics.junction.Logger; import static edu.wpi.first.units.Units.Meters; import static edu.wpi.first.units.Units.MetersPerSecond; import frc.robot.Constants; import frc.robot.util.IMUGyro; public class Drivetrain extends SubsystemBase { private IMUGyro m_gyro = Constants.gyro; private CANSparkMax m_motorL_leader = Constants.motorL_leader; private CANSparkMax m_motorL_follower = Constants.motorL_follower; private CANSparkMax m_motorR_leader = Constants.motorR_leader; private CANSparkMax m_motorR_follower = Constants.motorR_follower; private Encoder m_encoderL = Constants.encoderL; private Encoder m_encoderR = Constants.encoderR; private SimpleMotorFeedforward m_feedforward = Constants.motor_feedforward; private ProfiledPIDController m_pidL = new ProfiledPIDController(0.2, 0, 0, Constants.motor_constraints); private ProfiledPIDController m_pidR = new ProfiledPIDController(0.2, 0, 0, Constants.motor_constraints); private DifferentialDriveKinematics m_kinematics = new DifferentialDriveKinematics(Constants.TrackWidth); private DifferentialDriveOdometry m_odometry; // For system identification private final MutableMeasure m_voltage = mutable(Volts.of(0)); private final MutableMeasure m_distance = mutable(Meters.of(0)); private final MutableMeasure> m_velocity = mutable(MetersPerSecond.of(0)); private final SysIdRoutine m_sysIdRoutine = new SysIdRoutine( new SysIdRoutine.Config(), new SysIdRoutine.Mechanism( (Measure volts) -> { m_motorL_leader.set(-volts.in(Volts)); m_motorR_leader.set(-volts.in(Volts)); }, log -> { log.motor("drive-left") .voltage( m_voltage.mut_replace( m_motorL_leader.get() * RobotController.getBatteryVoltage(), Volts) ) .linearPosition(m_distance.mut_replace(m_encoderL.getDistance(), Meters)) .linearVelocity(m_velocity.mut_replace(m_encoderL.getRate(), MetersPerSecond)); log.motor("driver-right") .voltage( m_voltage.mut_replace( m_motorR_leader.get() * RobotController.getBatteryVoltage(), Volts) ) .linearPosition(m_distance.mut_replace(m_encoderR.getDistance(), Meters)) .linearVelocity(m_velocity.mut_replace(m_encoderR.getRate(), MetersPerSecond)); }, this)); public Drivetrain() { m_gyro.reset(); m_gyro.calibrateGyro(10000); m_motorR_follower.follow(m_motorR_leader); m_motorL_follower.follow(m_motorL_leader); m_motorL_leader.setInverted(true); m_encoderL.setReverseDirection(true); m_encoderL.setDistancePerPulse(Math.PI * Constants.WheelDiameter / Constants.EncoderResolution); m_encoderR.setDistancePerPulse(Math.PI * Constants.WheelDiameter / Constants.EncoderResolution); m_encoderL.reset(); m_encoderR.reset(); m_odometry = new DifferentialDriveOdometry( m_gyro.toRotation2d(), Constants.encoderL.getDistance(), Constants.encoderR.getDistance()); } // speed - linear velocity in m/s // rot - angular velocity in rad/s public void arcade(double speed, double rot) { speed(m_kinematics.toWheelSpeeds(new ChassisSpeeds(speed, 0.0, rot))); } // in m/s public void tank(double left, double right) { speed(new DifferentialDriveWheelSpeeds(left, right)); } public void speed(DifferentialDriveWheelSpeeds speeds) { Logger.recordOutput(getName() + "/leftSpeed", speeds.leftMetersPerSecond); Logger.recordOutput(getName() + "/rightSpeed", speeds.rightMetersPerSecond); voltage( m_pidL.calculate(m_encoderL.getRate(), speeds.leftMetersPerSecond) + m_feedforward.calculate(speeds.leftMetersPerSecond), m_pidR.calculate(m_encoderR.getRate(), speeds.rightMetersPerSecond) + m_feedforward.calculate(speeds.rightMetersPerSecond) ); } public void voltage(double leftVolts, double rightVolts) { Logger.recordOutput(getName() + "/leftVolts", leftVolts); Logger.recordOutput(getName() + "/rightVolts", rightVolts); m_motorL_leader.setVoltage(leftVolts); m_motorR_leader.setVoltage(rightVolts); } public Pose2d pose2d() { return m_odometry.getPoseMeters(); } public void reset() { m_gyro.reset(); m_encoderL.reset(); m_encoderR.reset(); m_odometry = new DifferentialDriveOdometry( m_gyro.toRotation2d(), Constants.encoderL.getDistance(), Constants.encoderR.getDistance()); } @Override public void periodic() { m_gyro.update(); m_odometry.update( m_gyro.toRotation2d(), m_encoderL.getDistance(), m_encoderR.getDistance()); Logger.recordOutput(getName() + "/gyro", m_gyro.toRotation2d().getDegrees()); Logger.recordOutput(getName() + "/pose", pose2d()); Logger.recordOutput(getName() + "/encoderL", m_encoderL.getRate()); Logger.recordOutput(getName() + "/encoderR", m_encoderR.getRate()); } public Command followTrajectory(Trajectory trajectory) { // RamseteCommand ramsete = new RamseteCommand( // trajectory, this::pose2d, // new RamseteController(0, 0), // m_feedforward, m_kinematics, // () -> new DifferentialDriveWheelSpeeds(m_encoderL.getRate(), m_encoderR.getRate()), // m_pidR, m_pidL, this::voltage, this); // return Commands.runOnce(() -> this.reset()) // .andThen(ramsete) // .andThen(Commands.runOnce(() -> voltage(0, 0))); return Commands.none(); } public Command sysIdQausistatic(SysIdRoutine.Direction direction) { return Commands.race(m_sysIdRoutine.quasistatic(direction), this.runOnce(() -> { while(m_encoderL.getDistance() < 6 && m_encoderR.getDistance() < 6); })); } public Command sysIdDynamic(SysIdRoutine.Direction direction) { return Commands.race(m_sysIdRoutine.dynamic(direction), this.runOnce(() -> { while(m_encoderL.getDistance() < 6 && m_encoderR.getDistance() < 6); })); } }