Go to Post Dance Dance Revolution is the only solution. That rhymed. I should not be on chief at this hour of the morning. >_< - Eugenia Gabrielov [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 12-12-2011, 10:31
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: Help with mecanum Drive!

Here are some sections of code from FRC 623's mecanum drive from 2011.

First, as mentioned above you need 4 motors for mecanum. Additionally, 623 used a three axis joy stick: x, y, and twist.

The following class implements a "low pass" filter on the joystick inputs (reduces sensitivity) and transforms x, y, and twist to magnitude, direction, and rotation required by the mecanumDrive_Polar method.

Code:
public class JoystickFRC623Drive extends Joystick
{
    // number of samples for averaging
    private static final int FILTER_SAMPLE_SIZE = 5;

    public double getCurrentAngle()
    {
        synchronized (this)
        {
            return currentAngle;
        }
    }

    public double getCurrentMagnitude()
    {
        synchronized (this)
        {
        return currentMagnitude;
        }
    }

    public double getCurrentTwist()
    {
        synchronized (this)
        {
        return currentTwist;
        }
    }
    
    int position;
    double [] xValues;
    double [] yValues;
    double [] twistValues;

    double currentMagnitude;
    double currentAngle;
    double currentTwist;

    // override to allow implementation of filtering
    public JoystickFRC623Drive( int port )
    {
        super(port);
        position = 0;
        xValues = new double [FILTER_SAMPLE_SIZE];
        yValues = new double [FILTER_SAMPLE_SIZE];
        twistValues = new double [FILTER_SAMPLE_SIZE];
        for(int i=0; i<FILTER_SAMPLE_SIZE; i++) {xValues[i]=0; yValues[i]=0; twistValues[i]=0;}

    }

    public void teleopInit()
    {
        // danny you may want to reset the arrays to zero here --steven
        for(int i=0; i<FILTER_SAMPLE_SIZE; i++) {xValues[i]=0; yValues[i]=0; twistValues[i]=0;}
        // call when teleop starts
    }

    public void teleopPeriodic()
    {
        // called each time new driver station data is available


        xValues[position] = super.getAxis(AxisType.kX);
        yValues[position] = super.getAxis(AxisType.kY);
        twistValues[position] = super.getAxis(AxisType.kTwist);
        position++;
        if (position>=FILTER_SAMPLE_SIZE) position = 0;

        double averageX = averageArray(xValues);
        double averageY = averageArray(yValues);
        double averageTwist = averageArray(twistValues);

        setMagnitudeAngleTwist(averageX, averageY, averageTwist);

    }

    public double averageArray(double [] array)
    {
        double sum = 0;
        for(int i=0; i<array.length; i++)
            sum+=array[i];
        return sum/array.length;
    }

    public void setMagnitudeAngleTwist(double averageX, double averageY, double averageTwist)
    {
        synchronized (this)
        {
            currentMagnitude = Math.sqrt(averageX*averageX+averageY*averageY);
            if (currentMagnitude>1.0) currentMagnitude = 1.0;

            currentAngle = Math.toDegrees(MathUtils.atan2(averageX, -averageY));
            currentTwist = averageTwist;
        }
    }
}

The following snippets of code show setup the hardware(We used CAN bus for motor control) and how to pass data from joystick to robot drive. Our full implementation has a gyro for stabilization when not intentionally using twist as well as a stabilized "idle" mode (robot not moving, but gyro engaged so that if robot is bumped it will attempt to stay in position) This was accomplished with a state machine implementation with the following states:

STATE_IDLE
STATE_DRIVER_XY
STATE_DRIVER_TWIST
STATE_IDLE_WITH_GYRO

Code:
    // drive motors
    public CANJaguar leftFrontMotor;
    public CANJaguar leftRearMotor;
    public CANJaguar rightFrontMotor;
    public CANJaguar rightRearMotor;

    // robot drive instance
    public RobotDrive robotDrive;

    // our joystick class
    public JoystickFRC623Drive driveJoystick;

    public void robotInit() {

    try {
            leftFrontMotor = new CANJaguar(12);
            rightFrontMotor = new CANJaguar(13);
            leftRearMotor = new CANJaguar(14);
            rightRearMotor = new CANJaguar(11);

        } catch (CANTimeoutException ex) {
            System.out.println("Stuck in CANTimeoutError");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            DriverStationLCD.getInstance().println(DriverStationLCD.Line.kMain6, 1, "testing");
            DriverStationLCD.getInstance().updateLCD();
        }

        driveJoystick = new JoystickFRC623Drive(1);

        robotDrive = new RobotDrive(leftFrontMotor, leftRearMotor, rightFrontMotor, rightRearMotor);
        robotDrive.setInvertedMotor(RobotDrive.MotorType.kFrontRight, true);
        robotDrive.setInvertedMotor(RobotDrive.MotorType.kRearRight, true);

        // create gyro instance
        steeringGyro = new Gyro(1, 2);
    }

    public void teleopInit() {
        // call teleop init for joysticks
        this.driveJoystick.teleopInit();
    }

    public void teleopPeriodic() {
        // call telop periodic for joysticks to preprocess inputs
        this.driveJoystick.teleopPeriodic();

        this.robotDrive.mecanumDrive_Polar(
                     this.driveJoystick.getCurrentMagnitude(),
                     this.driveJoystick.getCurrentAngle(),
                     this.driveJoystick.getCurrentTwist() );

}

If you would like to see state machine implementation let me know.
__________________
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
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:43.

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