Help using Neo motor encoders

I am trying to use PID to control our FRC drivetrain. I have done it before with Falcon 500 motors but I don’t understand how to do it with NEO motors and Can Spark Max motor controllers. Any help will be much appreciated. I can’t figure out how the encoders work with the Spark Max and just a simple drive until encoder ticks program would be nice. Here is a link to the code. I am using Git to save code to github so this code is always current.

Here is an example on how to use closed loop control using NEO motors and Spark MAXes. If you have any questions about the code, let me know. I can throw together some example code for driving a distance without PID (drive at speed until past a certain distance) in in a couple hours.

Also, here’s an example just getting the position of the encoder. You could do something as simple as (pseudocode):

double avgDistance = leftEncoder.getPosition() + rightEncoder.getPosition();
if (avgDistance < 20) {
    arcadeDrive(0.1, 0);
} else {
    arcadeDrive(0,0)
}

The 20 in that pseudocode is just an arbitrary number of rotations. For simplicity, I left this value as just a number, but I would set the conversion factor of your encoders so that getPosition() returns in units of inches rather than motor rotations. Again, this example is simple by design, and there are definitely more effective methods. This is just the simplest.

1 Like

Do you know how many encoder ticks there are for one rotation? I am struggling to find any info.

“Hall-Sensor Encoder Resolution: 42 counts per rev.”
from: NEO Brushless Motor - REV Robotics

thank you

The default output of getPosition() on the encoder object outputs a double in motor revolutions.

I am going to have to take you up on that offer. I tried doing this myself and ran into a bunch of problems. I intend to use this in autonomous. I use command based code. Here is my current code after I attempted to just drive with encoders. In an ideal scenario it would be PID but I can’t figure out how to properly implement it.

By default the SparkMax getPosition method returns rotations, not encoder ticks.

Here is a very basic example of using PID control to drive a distance. If you want to improve it, consider using motion profiles and the PID features of the SparkMax.