REV Encoder Programming Help

Our programmer was trying to get a PID loop to work, but was having issues with the motor (NEO) not stopping at the correct point. Now he is trying to make the motor move to a specific position when a button is pushed (not in a PID loop); however, when he runs the program it still overshoots the target and doesn’t stop turning. The motor is attached to a 100:1 gearbox.

He has code written to get the value of the encoder and update it as the motor turns, and from what I can tell he is using that to compare to the value he wants the motor to go to, but it isn’t working.

In my mind, it seems like it should be a simple program, but I’m not a programmer. Any ideas? He is using Java

1 Like

Could you post sample code?

Are you calculating the distance correctly? The encoder tracks the rotations of the motor, and not the distance of the wheel. Here’s what we’re doing to get the distance for our robot:

public double getAverageEncoderDistance(){
    double positionConversionFactor = Math.PI * Constants.Drive.wheelDiameter / Constants.ROBOT_CONSTANTS.GEAR_RATIO;
    return ((frontLeftEncoder.getPosition() * positionConversionFactor) + (frontRightEncoder.getPosition()* positionConversionFactor)) / 2;
  }

If this doesn’t help, could you show me your code for how you’re trying to do this? I highly recommend using SmartDashboard to display values so you can see what your code is outputting.

1 Like

I suggest using a PID. If it overshoots tune the P, then I, and finally D.

I second this. A PID is very helpful in accurately and efficiently moving to the target. However, make sure the robot is first correctly trying to stop before implementing something else.

True, there have been so many times that the robot zoomed when I was playing around with PathPlanner. The issue was that I had a wrong encoder pulses per revolution value :sweat_smile:.
Edit: I put 4096 instead of 2048, imagine that…

He was trying to use a PID and that’s what wasn’t working. The arm motor would not stop rotating.

1 Like

Here is the link to his github. I’m not sure how all of that works, and I have no clue about the programming, but here is the link he sent me:

I’d argue its a much better approach to tune any kind of feed forward values first, then P, then D. The integral component generally shouldn’t be needed, but if for whatever reason it is, it should be done last.

May seem like a stupid question but did you try inverting the motor? (Assuming conversion factors, using SparkmaxPIDController, etc)

Could you ask your programmer to post a link to the file they are modifying? Are you trying to get Swerve working with this PID?

You also do not need to supply these parameters to get the encoder. It’s actually deprecated.

https://codedocs.revrobotics.com/java/com/revrobotics/cansparkmax#getEncoder()

Either the encoder to rotation conversion is wrong or the P or D value is too high or low.

I’ll ask him what file when he gets here this afternoon. He is trying to get the PID loop to work. He has the swerve working already.

1 Like