Spark max pid not stoping

I am trying to get a neo 550 to go to a position using the CANPIDController and setReference() but no matter what I set my PID values and reference to the motor just goes at full speed forever here is the code I’m running as well as my PID values does anyone know what I am doing wrong.

//adjuster pid

maxAdjusterPID.setP(sparkP);

maxAdjusterPID.setI(sparkI);

maxAdjusterPID.setD(sparkD);

maxAdjusterPID.setOutputRange(1, -1);

public void adjust(int position) {

SmartDashboard.putNumber("enc pos", getAdjusterPosition());

maxAdjusterPID.setReference(position, ControlType.kPosition);

}

What’s your feedback device? Sounds like it’s polarity is reversed (counting down when the PID controller expects it to count up)

i’m using the built in encoder for the neo 550

Oh sorry, didn’t realize that was a REV class, so it’s using that automatically. Do you have your output range min/max reversed? At quick glance, I’d expect it to be -1:1, not 1:-1

it was originally -1,1 but it would not run at all but when i reversed it it started spinning just not stopping, looking at the number on the smart dashboard i can see it start at 0 and go up and just keep going past the give position here is the full subsystem the method is question is public void adjust(int position)

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

/* Copyright © 2019 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.wpilibj2.command.SubsystemBase;

import frc.robot.Constants;

import com.ctre.phoenix.motorcontrol.ControlMode;

import com.ctre.phoenix.motorcontrol.FeedbackDevice;

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

import com.revrobotics.*;

import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class ShooterSubsystem extends SubsystemBase {

// motor controllers

WPI_TalonFX fxShooter = new WPI_TalonFX(Constants.fxShooter);

CANSparkMax maxAdjuster = new CANSparkMax(Constants.maxAdjuster, MotorType.kBrushless);

public CANPIDController maxAdjusterPID = new CANPIDController(maxAdjuster);

public CANEncoder maxAdjusterEncoder = new CANEncoder(maxAdjuster);

// variables TODO tune via instrustions at:

// Talon SRX Velocity Control

int timeOutMs = 30;

int loopIDX = 0;

double f = .053;

double p = .097 ;

double i = 0;

double d = 0;

double sparkP = .1;

double sparkI = 0;

double sparkD = 0;

public ShooterSubsystem() {

//adjuster pid

maxAdjusterPID.setFeedbackDevice(maxAdjusterEncoder);

maxAdjusterPID.setP(sparkP);

maxAdjusterPID.setI(sparkI);

maxAdjusterPID.setD(sparkD);

maxAdjusterPID.setOutputRange(1, -1);

maxAdjusterEncoder.setPosition(0);

// reset to factory default

fxShooter.configFactoryDefault();

// config feedback sensor for PID

fxShooter.configSelectedFeedbackSensor(FeedbackDevice.IntegratedSensor, 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(true);

fxShooter.setSensorPhase(true);

// 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, targetRPM / .292);

}

public void setSpeed(double speed) {

fxShooter.set(ControlMode.PercentOutput, speed);

}

public double currentRPM() {

// current rpm = rpm of motor x gear ratio

double motorRPM = fxShooter.getSelectedSensorVelocity() * .292;

double gearRatio = 1;

return motorRPM * gearRatio;

}

public void adjust(int position) {

SmartDashboard.putNumber("enc pos", getAdjusterPosition());

maxAdjusterPID.setReference(position, ControlType.kPosition);

}

public double getAdjusterPosition() {

return maxAdjusterEncoder.getPosition();

}

@Override

public void periodic() {

// This method will be called once per scheduler run

}

}

Please post a link to github (or similar) or wrap your block of code with triple backticks(```) before and after to format it. It’s very difficult to read/understand otherwise.


/*----------------------------------------------------------------------------*/

/* Copyright (c) 2019 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.wpilibj2.command.SubsystemBase;

import frc.robot.Constants;

import com.ctre.phoenix.motorcontrol.ControlMode;

import com.ctre.phoenix.motorcontrol.FeedbackDevice;

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

import com.revrobotics.*;

import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class ShooterSubsystem extends SubsystemBase {

  // motor controllers

  WPI_TalonFX fxShooter = new WPI_TalonFX(Constants.fxShooter);

  CANSparkMax maxAdjuster = new CANSparkMax(Constants.maxAdjuster, MotorType.kBrushless);

  public CANPIDController maxAdjusterPID = new CANPIDController(maxAdjuster);

  public CANEncoder maxAdjusterEncoder = new CANEncoder(maxAdjuster);

  // variables TODO tune via instrustions at:

  // https://www.chiefdelphi.com/t/talon-srx-velocity-control/148337/3

  int timeOutMs = 30;

  int loopIDX = 0;

  double f = .053;

  double p = .097 ;

  double i = 0;

  double d = 0;

  double sparkP = .1;

  double sparkI = 0;

  double sparkD = 0;

  public ShooterSubsystem() {

    //adjuster pid

    maxAdjusterPID.setFeedbackDevice(maxAdjusterEncoder);

    maxAdjusterPID.setP(sparkP);

    maxAdjusterPID.setI(sparkI);

    maxAdjusterPID.setD(sparkD);

    maxAdjusterPID.setOutputRange(1, -1);

    maxAdjusterEncoder.setPosition(0);

    // reset to factory default

    fxShooter.configFactoryDefault();

    // config feedback sensor for PID

    fxShooter.configSelectedFeedbackSensor(FeedbackDevice.IntegratedSensor, 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(true);

    fxShooter.setSensorPhase(true);

    // 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, targetRPM / .292);

  }

  public void setSpeed(double speed) {

    fxShooter.set(ControlMode.PercentOutput, speed);

  }

  public double currentRPM() {

    // current rpm = rpm of motor x gear ratio

    double motorRPM = fxShooter.getSelectedSensorVelocity() * .292;

    double gearRatio = 1;

    return motorRPM * gearRatio;

  }

  public void adjust(int position) {    

    SmartDashboard.putNumber("enc pos", getAdjusterPosition());

    maxAdjusterPID.setReference(position, ControlType.kPosition);

  }

  public double getAdjusterPosition() {

    return maxAdjusterEncoder.getPosition();

  }

  @Override

  public void periodic() {

    // This method will be called once per scheduler run

  }

}

i haven’t quite gotten my github repository set up so this will have to do for now

oh turns out you were right was supposed to be -1,1 I must have had something else stopping it from moving at that point thank you

1 Like

No problem! Glad you got it working.

And we can help with Github issues too. Don’t be afraid to ask for help.

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