Hello! We are looking into using the Austriamicrosystems Magnetic Rotary Encoder. Since we are on a super time crunch, we were wondering if anyone already has it programmed in Java. If so, we would love if you could share it with us! (:
Here is the simplest approach. On JP2 of the sensor wire to Ana, 5V, and GND (white, red, black of pwm cable).
There is one drawback to this implementation. There is an RC filter on the sensor board which gives a 100 ms delay when transitioning from 0 to 360 or 360 to zero. If you do not need to cross the 0/360 boundary or you are turning slowly this will not be a problem.
import edu.wpi.first.wpilibj.AnalogChannel;
/**
*
* @author charris
*/
public class AS5030Analog extends AnalogChannel {
public AS5030Analog( int channel ){
super( channel );
}
public AS5030Analog( int slot, int channel ){
super( slot, channel );
}
public double GetAngle()
{
return ( getVoltage() / 5.0 ) * 360.0;
}
/**
* Get the angle of the encoder for use with PIDControllers
* @return the current angle according to the encoder
*/
public double pidGet() {
return getAngle();
}
}
The following approach will work at high speeds and does not have the RC delay. It requires 2 analog channels.
Remove solder jumper P1 on the sensor board.
On JP2 of the sensor wire to 5V and GND. On JP3 of the sensor wire to SIN and COS. Use two cables, one for each analog channel (SIN and COS).
import com.sun.squawk.util.MathUtils;
import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.SensorBase;
/**
*
* @author charris
*/
public class AS5030DualAnalog extends SensorBase implements PIDSource {
private static final double biasVoltage = 2.25;
private AnalogChannel sinChannel;
private AnalogChannel cosChannel;
public AS5030DualAnalog( int slotParam, int sinChannelParam, int cosChannelParam)
{
sinChannel = new AnalogChannel( slotParam, sinChannelParam );
cosChannel = new AnalogChannel( slotParam, cosChannelParam );
}
public AS5030DualAnalog(AnalogChannel sin, AnalogChannel cos)
{
sinChannel = sin;
cosChannel = cos;
}
protected double getCos()
{
return cosChannel.getVoltage() - biasVoltage;
}
protected double getSin()
{
return sinChannel.getVoltage() - biasVoltage;
}
public double getAngle()
{
// calculate the angle
double theta = MathUtils.atan2( getSin(), getCos() );
// convert to degrees
return Math.toDegrees(theta) + 180.0;
}
/**
* Get the angle of the encoder for use with PIDControllers
* @return the current angle according to the encoder
*/
public double pidGet() {
return getAngle();
}
}
Digital interface is also possible. I have code from two years ago to use SPI interface to the encoder, but some WPILIB API changes prevent it from building without some changes and I do not want to post example code which I have not tested.