Talon srx Velocity controll

I have been trying to use the talon srx Velocity control mode to keep out motor at a given rpm but no matter what a set the rpm to the motor spins up to full speed I’m not sure I fully understand how the velocity control mode works. Here is our current code for driving the motor the method in question is spinToRPM() if anyone knows a solution help would be much appreciated

/----------------------------------------------------------------------------/

/* Copyright © 2018 FIRST. All Rights Reserved. */

/* Open Source Software - may be modified and shared by FRC teams. The code */

/* must be accompanied by the FIRST BSD license file in the root directory of */

/* the project. */

/----------------------------------------------------------------------------/

package frc.robot.subsystems;

import edu.wpi.first.wpilibj.command.Subsystem;

import frc.robot.RobotMap;

import com.ctre.phoenix.motorcontrol.ControlMode;

import com.ctre.phoenix.motorcontrol.FeedbackDevice;

import com.ctre.phoenix.motorcontrol.can.*;

public class ShooterSubsystem extends Subsystem {

// motor controllerS

//WPI_TalonFX fxShooter = new WPI_TalonFX(RobotMap.fxShooter);

WPI_TalonSRX fxShooter = new WPI_TalonSRX(RobotMap.fxShooter);

// variables TODO tune via instrustions at:

// Talon SRX Velocity Control

int timeOutMs = 30;

int loopIDX = 0;

double f = 1023;

double p = .25;

double i = .001;

double d = 20;

@Override

public void initDefaultCommand() {

}

public ShooterSubsystem() {

// reset to factory default

fxShooter.configFactoryDefault();

// config feedback sensor for PID

// fxShooter.configSelectedFeedbackSensor(FeedbackDevice.IntegratedSensor, loopIDX, timeOutMs);

fxShooter.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, loopIDX, timeOutMs);

// Config peak and nominal outputs

fxShooter.configNominalOutputForward(0, timeOutMs);

fxShooter.configNominalOutputReverse(0, timeOutMs);

fxShooter.configPeakOutputForward(1, timeOutMs);

fxShooter.configPeakOutputReverse(-1, timeOutMs);

fxShooter.setInverted(false);

fxShooter.setSensorPhase(false);

// config P,I,D,F values

fxShooter.config_kF(loopIDX, f, timeOutMs);

fxShooter.config_kP(loopIDX, p, timeOutMs);

fxShooter.config_kI(loopIDX, i, timeOutMs);

fxShooter.config_kD(loopIDX, d, timeOutMs);

}

public void spinToRPM(int targetRPM) {

//sets shooter to target rpm

fxShooter.set(ControlMode.Velocity,600);        

}

public void setSpeed(double speed) {

fxShooter.set(ControlMode.PercentOutput,speed);

}

public int currentRPM() {

//current rpm = rpm of motor x gear ratio

int motorRPM = fxShooter.getSelectedSensorVelocity();

int gearRatio = 1;

return motorRPM*gearRatio;

}

}

To tune velocity loops on the Talon, set the feedforward term to zero. Send your desired velocity to the controller, then slowly increase your feed forward value until the motor is running at the speed you set.

1 Like

Here’s an interactive example of how you should be expecting the P, I, D, and F gains to interact:

2 Likes

I was able to get it tuned correctly this way thank you

I would recommend reading (or re-reading) the CTRE documentation on the Velocity Closed Loop:

That is the first paragraph tip in their section on velocity closed loop found here.

You have set your kF value to 1023, which is full. That means that your current closed loop control is telling the motor to spin at full speed as the feed-forward, which is what you are observing.

The better setup is to zero out all your variables, kf, kp, ki, and kd. Slowly increase kF until the motor is at the setpoint you desire, then add in kp, kd, and ki (if necessary) to adjust for the error when things happen like a ball hitting your flyweel in the shooter which will slow down the motor.

Another thing that I suggest is checking how close the kf puts the motor to your desired speed because you don’t want to put too much stress on PID. PID should be doing fine adjustment not a whole lot.
To do this, plot out the desired velocity and the actual velocity. If the middle of both lines are intersecting, that is good. If they are nowhere close to each other, you probably have too low of kf.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.