CrossTheRoadElec / Phoenix-Releases

Release repository for Phoenix-Framework and Phoenix-Tuner
76 stars 7 forks source link

Swerve API doesn't have an easy way to set additional configs to devices #78

Closed CoryNessCTR closed 4 days ago

CoryNessCTR commented 3 months ago

Currently the Swerve API doesn't have an easy way to set additional configs to the steer/drive motors or CANcoders. This will be addressed in a future update, but in the meantime adding this snippet inside CommandSwerveDrivetrain.java's constructor is sufficient as a workaround:

        /* Do this for every module */
        for(int i = 0; i < 4; ++i) {
            var swerveModule = this.getModule(i);
            var steerMotor = swerveModule.getSteerMotor(); // Get the steer motor in this instance
            var steerMotorConfig = new TalonFXConfiguration();

            StatusCode status = StatusCode.StatusCodeNotInitialized;
            int maxRetries = 10;
            do {
                status = steerMotor.getConfigurator().refresh(steerMotorConfig);
            } while (!status.isOK() && maxRetries-- > 0);
            if(!status.isOK()) {
                System.out.println("Could not retrieve configs: " + status.toString());
                continue; // Move to next module, don't modify configs
            }

            /* Set a stator current limit on the steer motors */
            steerMotorConfig.CurrentLimits.StatorCurrentLimit = 50;
            steerMotorConfig.CurrentLimits.StatorCurrentLimitEnable = true;
            steerMotorConfig.TorqueCurrent.PeakForwardTorqueCurrent = 50;
            steerMotorConfig.TorqueCurrent.PeakReverseTorqueCurrent = -50;

            status = StatusCode.StatusCodeNotInitialized;
            maxRetries = 10;
            do{
                status = steerMotor.getConfigurator().apply(steerMotorConfig);
            } while (!status.isOK() && maxRetries-- > 0);

            if (!status.isOK()) {
                System.out.println("Could not apply configs: " + status.toString());
            }
        }
bhall-ctre commented 2 months ago

This has been fixed in 24.3.0: https://api.ctr-electronics.com/changelog#20240430

daltzctr commented 4 days ago

Documentation on this is available here