How do we read an analog or PWM encoder in Java code?

Our team has been using quadrature encoders, and the programmers are familiar with using the Java “Encoder” class for interacting with them.

This year, the build team is thinking of using either an analog or PWM encoder (specifically an MA3 from US Digital). I don’t know how that would connect to the RoboRIO, or how we would read the values from it in Java code.

I’ve searched through the forums for the past couple of hours, and have found posts relating to this, but nothing definite enough to feel confident I can help the programmers make use of this device. I’m hoping someone can point me to a post or a reference that can explain what they would need to do.

My understanding is that they would be using it to measure the rotation of a shaft, and wouldn’t need absolute positioning, just relative positioning.

Thanks in advance for any help.

I would use either the PWM class of AnalogInput class. Both can be found in the api reference here.
I’m not particularly experienced with either of the classes, but from looking at the documentation I would use getPosition() or getRaw() from the PWM class or getVoltage() or getValue() from the AnalogInput class. Maybe try outputting both to the DS and pick the one that you like most. There are also many other methods in the reference which might be helpful in both classes, but I don’t know exactly what they do. Look around in there and you can probably find something that will fit your needs.

The PWM class is for output, not input, as clearly noted in the documentation

The documentation seems to imply that output methods do exist in the PWM class, as well as implying here that somehow the raw value is retrieved from the device.

PWM Speed and Position setters convert to a raw PWM value (between 0 and 255), which is then sent out to hardware. The getters are there to get the stored raw value, or convert it to Speed or Position. You can see this here. The value that is returned is from whatever the PWM output was set to in code, and is not read from the PWM output bus.

If you want to measure relative position (especially over more than one revolution) the MA3 might be the wrong sensor for the job.

The fact that the MA3 is a digital sensor is essentially hidden from the end user (at least for the P/Ns I have used and are available from AndyMark). As you spin the shaft, it linearly ramps up from 0 to 5V over the course of one revolution, then hits a crossover point where it immediately drops from 5V to 0V and repeats. Spinning the sensor continuously will give you a sawtooth waveform. You would use AnalogInput class.

Thanks a bunch for the help and the clear explanation.

After talking more with the build team, they picked this encoder for fine-grained control of the turret rotation. I thought they wanted to use it to control flywheel speed, but I was mistaken.

https://www.chiefdelphi.com/forums/showpost.php?p=1538588

https://www.chiefdelphi.com/forums/showpost.php?p=1538598

https://www.chiefdelphi.com/forums/showpost.php?p=1538623