Java Swerve Drive Encoder

Hello, I have been having trouble with getting values from my MA3 absolute encoder (am-2899). The Encoder is plugged into the RoboRio into the analog port. I was trying to use the WPI AnalogInput class, but when I use getValue() it outputs values around 670 and doesn’t change when I rotate the swivel motor the encoder is attached to. Can someone help me with java programming with this specific encoder?

If this is the analog version of USDigital’s MA3 encoder, you might have a wiring problem. I’d check there first.

Otherwise, it’s the PWM version. If it’s the PWM version, you’ll need to track the duty cycle of the PWM signal using WPI’s Counter class. We’ve implemented a wrapper to measure the PWM signal here: https://gist.github.com/jlmcmchl/294adf83de1ab6a87f57669370c247c1

AnalogInput.getValue() returns the raw 12-bit ADC output which is not what you are going to want. You are going to want to use the AnalogInput.getVoltage() method instead which returns the reading in volts.

1 Like

New for 2020, WPILib has this functionality built in as the new DutyCycleEncoder class with full FPGA support backing it.

3 Likes

I tried using the AnalogInput.getVolltage() which returned values around .8 and hardly changed when I was or wasn’t turning the wheel. What conversion factors would I have to use to get these values to degrees. How are you supposed to use the voltage values to turn the wheel to a degree?

If the value isn’t changing there may be a wiring issue. I would double-check to make sure that you haven’t accidentally swapped the ground and 5V when you plugged the encoder in. If you’re using the 3-Pin encoder cable (am-3083) the orange wire is 5V, blue is signal, and brown is ground.

To convert the voltage values to an angle you have to first divide the voltage returned from the encoder by the voltage of the RoboRIO’s 5V rail. You can just divide by 5, but we like to divide by the actual 5V rail voltage which can be read using the RobotController.getVoltage5V(). After the division you end up with rotations of the encoder so you can convert to degrees by multiplying by 360.

We end up doing something like this:
double angle = (angleEncoder.getVoltage() / RobotController.getVoltage5V()) * 360.0;

1 Like

Thank you! The encoder ended up being wired wrong so we fixed it. I can now get the rotation of the wheel in degrees.

1 Like

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