How does one write (java) code to use this encoder? There is only one PWM port, and the Encoder class requires two.
The Encoder class is for a quadrature encoder, which is why it uses two DIO ports (not PWM). The encoder you linked needs to be connected to an analog input. Its output voltage is proportional to the angle.
Here is some psuedo-Java that could be used to get an angle:
AnalogInput encoder = new AnalogEncoder(0);
double angle = (encoder.getVoltage() / 5.0) * 360.0;
You could also try looking at AnalogPotentiometer, which can do the scaling for you.
The am-2899 (US Digital part number MA3-A10) has analog output, not PWM, and must be wired to an analog input.
The MA3-P10 (and -P12) have PWM output.
The FPGA, sampling at 40MHz, certainly is fast enough to detect and accurately time-stamp the edges of a PWM signal, but the FRC FPGA code does not support PWM decoding.
Thanks. We looked around and found some Talon encoders which work better, but we’ll keep this in mind for the next time we use these encoders!
<off-topic>
The FPGA does support this, though the documentation for the semi-period mode feature (at least in the Javadocs) is misleading.
There is more accurate documentation here:
https://wpilib.screenstepslive.com/s/3120/m/7912/l/85635-using-counters
I verified last night that I was able to use a pair of counters (one in semi-period mode capturing the rising-to-falling period, and one in normal mode for capturing rising-to-rising period) to measure the duty cycle of the 12-bit PWM MA3.
</off-topic>
When you do it that way, how do you ask the FPGA to give you the rising-falling and rising-rising from the same cycle?
You can’t do it atomically to my knowledge. But rising-rising is pretty much constant from one sample to the next (the oscillator is thermally sensitive, so it changes slowly).
We initially implemented it as two semi-period counters (one for T(on) and one for T(off)), and as you’d imagine, there were synchronization issues near the rollover point…