View Single Post
  #2   Spotlight this post!  
Unread 22-02-2012, 09:05
charrisTTI charrisTTI is offline
Ramblin' Wreck
AKA: Charles Harris
FRC #0623
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Vienna, VA
Posts: 106
charrisTTI has a spectacular aura aboutcharrisTTI has a spectacular aura about
Send a message via AIM to charrisTTI
Re: Java Code for Rotary Encoder

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.


Code:
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).


Code:
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.
__________________
FRC 623 2003,2004,2005,2006,2007,2008, 2009, 2010, 2011
FRC 1900 2007
FVC 60 and 193 2006
FVC 3271 2007
FTC 226 and 369 2008, 2009, 2010, 2011
FTC 3806 2010

Last edited by charrisTTI : 22-02-2012 at 09:07.
Reply With Quote