Trouble Counting Rotations on Absolute Encoder (am-2899)

We use this encoder for our swerve modules to read rotation and they work just fine, but this year we’ve been trying to use one on our elevator and have ran into issues.

First and foremost the swerve encoders take up all four of the roboRIO’s analogIN ports so we have to use one of the analogIN ports on the navX-MXP for the elevator. The problem with these ports is that roboRIO analog ports read values from 0 - 4096 whereas these read from really weird values, the lowest we could read being 143 and the most being 858.

Another difference from the swerve encoders is that the shaft must rotate 6 or 7 times for the whole span of the elevator, so we had to make a counter to tell when the encoder makes a full rotation.

The problem we’re facing is that the encoder value in relation to the physical position of the elevator drifts as we drive it, our rotation counter seems to work at first but if you go up and down a few times the value it reads at the bottom shifts upwards from what it initially read, sometimes even jumping by up to a full rotation.

We’ve thoroughly inspected the physical mechanisms and the chain isn’t slipping nor the coupling from the shaft to the encoder, so we don’t know if it’s a problem with the rotation counter or the navx ports being different, but we’re fairly sure it’s programmatical.

Here’s the code for our counter

public static double getAngle(){

startAngle = getRawAngle();

f((startAngle > 155 && startAngle < 255) && (lastAngle > >750 && lastAngle < 850)){

revolutions++;

}

else if((startAngle > 750 && startAngle < 850) && (lastAngle >> 155 && lastAngle < 255)){

revolutions–;

}

angle = startAngle + (revolutions*695);

lastAngle = startAngle;

return angle;

}

This is ran constantly 100 times per second while the robot is enabled, the value this returns is what is drifting, and “getRawAngle()” is a simple method getting the value from the encoder.

OK first off I’m going to assume that the >> was a copy-paste error.

As you said, the lowest reading is 143. With the current code you have, I’m guessing that the encoder value is going from like 820 to 143, then going to 150. If that’s the case (or something like that) then it won’t account for the change because the last encoder value has been set to 143.

I recommend creating a method called getCorrectAngle() which returns a value from 0 to 360 or 0 to 1. Once you make that method, the code in the getAngle() method will become a lot simpler because you don’t have to make the checks to see if it’s above 155 or below 850, you can just make one check, for the startAngle, and one check for lastAngle.

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