Go to Post Every year I develop new connections and greater friendships with FIRSTers - and every year I am surprised to find just how many amazing people there are here. - MysterE [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 21-02-2012, 15:02
Cem8301's Avatar
Cem8301 Cem8301 is offline
Carolyn
AKA: student president
FRC #3413 (Mad Cows)
Team Role: Leadership
 
Join Date: Aug 2011
Rookie Year: 2010
Location: Coppell, Texas
Posts: 31
Cem8301 is an unknown quantity at this point
Java Code for Rotary Encoder

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! (:
__________________
The steaks are high. Prepare for udder destruction!! <3 Mad 3:O's
Reply With Quote
  #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
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:53.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi