Go to Post Be yourself and do what you love. - Greg Marra [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 Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #16   Spotlight this post!  
Unread 22-04-2015, 03:00
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,574
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Getting Gyro Working w/ Mecanum Drive

Quote:
Originally Posted by WillNess View Post
How do I set the robotdrive to the MecanumDrive method?
mecanumDrive_cartesian() is a method within the standard RobotDrive class in both C++ and java WPIlibs. For what are hopefully obvious reasons, you must use one of the four-controller forms of the new RobotDrive() constructor in order to use the mecanum methods.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #17   Spotlight this post!  
Unread 26-04-2015, 00:20
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

Quote:
Originally Posted by GeeTwo View Post
mecanumDrive_cartesian() is a method within the standard RobotDrive class in both C++ and java WPIlibs. For what are hopefully obvious reasons, you must use one of the four-controller forms of the new RobotDrive() constructor in order to use the mecanum methods.
So in my teleopPeriodic would I put:
Code:
RobotMap.driveRobotDrive41.mecanumDrive_Cartesian(OI.joystick0.getX(), OI.joystick0.getY(), OI.joystick0.getZ(), 0);
or where should I do that? Also, should I set the fourth variable to my Gyro Angle or to 0?
__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West
Reply With Quote
  #18   Spotlight this post!  
Unread 26-04-2015, 08:48
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Getting Gyro Working w/ Mecanum Drive

Quote:
Originally Posted by WillNess View Post
should I set the fourth variable to my Gyro Angle or to 0?
As explained in earlier posts in this thread, passing the gyro angle to the fourth parameter gives you field-centric control. You said that's not what you want.

Quote:
Originally Posted by Ether View Post
What you described above will make the driver interface field-centric
Quote:
Originally Posted by GeeTwo View Post
Do not use the fourth "gyroscope" input to MecanumDrive_cartesian; this is only used for "field coordinates".
See the note "Optional Input for field-centric control of XY motion" in the code diagram linked in this post:
Quote:
Originally Posted by Ether View Post
if your goal is to make the bot drive straight when the driver is commanding zero rotation, you could do something like this:

http://www.chiefdelphi.com/forums/at...4&d=1420598437


Last edited by Ether : 26-04-2015 at 09:16.
Reply With Quote
  #19   Spotlight this post!  
Unread 26-04-2015, 11:04
dawonn's Avatar
dawonn dawonn is offline
Mentor
AKA: Dereck
FRC #2586 (Calumet Copperbots)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Auburn Hills, Mi
Posts: 53
dawonn is an unknown quantity at this point
Talking Re: Getting Gyro Working w/ Mecanum Drive

The code is not particularly well documented, but here's the code we used this year:

https://github.com/CopperBots/Copper...obot.java#L632

We pass in the rotation joystick value that has already been deadband'ed.
If the joystick is at zero rotation, we use a proportional controller to keep the robot straight.
If the joystick is not at zero, we update the current heading of the robot and pass the joystick value back to the drive system unchanged.

We also have the option to disable gyro mode at any time in case something goes wrong like a bad calibration, or a loose cable.
There is also an option to update the kP value from the smart dashboard and some diagnostic information sent back to help troubleshoot when things go wrong.
Reply With Quote
  #20   Spotlight this post!  
Unread 26-04-2015, 12:31
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

Well what I'm asking is what do I put in for the fourth parameter, because I have to put something in it.

Quote:
Originally Posted by Ether View Post
As explained in earlier posts in this thread, passing the gyro angle to the fourth parameter gives you field-centric control. You said that's not what you want.





See the note "Optional Input for field-centric control of XY motion" in the code diagram linked in this post:


__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West
Reply With Quote
  #21   Spotlight this post!  
Unread 26-04-2015, 13:01
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

Thanks, this helps a BUNCH! The problem is mixing mecanum drive with your code

Quote:
Originally Posted by dawonn View Post
The code is not particularly well documented, but here's the code we used this year:

https://github.com/CopperBots/Copper...obot.java#L632

We pass in the rotation joystick value that has already been deadband'ed.
If the joystick is at zero rotation, we use a proportional controller to keep the robot straight.
If the joystick is not at zero, we update the current heading of the robot and pass the joystick value back to the drive system unchanged.

We also have the option to disable gyro mode at any time in case something goes wrong like a bad calibration, or a loose cable.
There is also an option to update the kP value from the smart dashboard and some diagnostic information sent back to help troubleshoot when things go wrong.
__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West
Reply With Quote
  #22   Spotlight this post!  
Unread 26-04-2015, 13:16
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Getting Gyro Working w/ Mecanum Drive

Quote:
Originally Posted by dawonn View Post
If the joystick is at zero rotation, we use a proportional controller to keep the robot straight.
If the joystick is not at zero, we update the current heading of the robot and pass the joystick value back to the drive system unchanged
Sounds just like the note in the lower left corner of this code diagram


Reply With Quote
  #23   Spotlight this post!  
Unread 26-04-2015, 13:28
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Getting Gyro Working w/ Mecanum Drive

Quote:
Originally Posted by WillNess View Post
Well what I'm asking is what do I put in for the fourth parameter, because I have to put something in it.
Look at the WPILib code for mecanumDrive_Cartesian():
Code:
    public void mecanumDrive_Cartesian(double x, double y, double rotation, double gyroAngle) {
        if(!kMecanumCartesian_Reported) {
            UsageReporting.report(tResourceType.kResourceType_RobotDrive, getNumMotors(), tInstances.kRobotDrive_MecanumCartesian);
            kMecanumCartesian_Reported = true;
        }
        double xIn = x;
        double yIn = y;
        // Negate y for the joystick.
        yIn = -yIn;
        // Compenstate for gyro angle.
        double rotated[] = rotateVector(xIn, yIn, gyroAngle);

Now look at the WPILib code for rotateVector():
Code:
    /**
     * Rotate a vector in Cartesian space.
     */
    protected static double[] rotateVector(double x, double y, double angle) {
        double cosA = Math.cos(angle * (3.14159 / 180.0));
        double sinA = Math.sin(angle * (3.14159 / 180.0));
        double out[] = new double[2];
        out[0] = x * cosA - y * sinA;
        out[1] = x * sinA + y * cosA;
        return out;
    }
The cosine of zero is 1. The sign of zero is 0. So passing zero as the fourth parameter to mecanumDrive_Cartesian() will result in no rotation of the vector, which is what you said you want.


Reply With Quote
  #24   Spotlight this post!  
Unread 26-04-2015, 13:35
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

Thank you Ether, that helps tons.

Quote:
Originally Posted by Ether View Post
Look at the WPILib code for mecanumDrive_Cartesian():
Code:
    public void mecanumDrive_Cartesian(double x, double y, double rotation, double gyroAngle) {
        if(!kMecanumCartesian_Reported) {
            UsageReporting.report(tResourceType.kResourceType_RobotDrive, getNumMotors(), tInstances.kRobotDrive_MecanumCartesian);
            kMecanumCartesian_Reported = true;
        }
        double xIn = x;
        double yIn = y;
        // Negate y for the joystick.
        yIn = -yIn;
        // Compenstate for gyro angle.
        double rotated[] = rotateVector(xIn, yIn, gyroAngle);

Now look at the WPILib code for rotateVector():
Code:
    /**
     * Rotate a vector in Cartesian space.
     */
    protected static double[] rotateVector(double x, double y, double angle) {
        double cosA = Math.cos(angle * (3.14159 / 180.0));
        double sinA = Math.sin(angle * (3.14159 / 180.0));
        double out[] = new double[2];
        out[0] = x * cosA - y * sinA;
        out[1] = x * sinA + y * cosA;
        return out;
    }
The cosine of zero is 1. The sign of zero is 0. So passing zero as the fourth parameter to mecanumDrive_Cartesian() will result in no rotation of the vector, which is what you said you want.


__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West
Reply With Quote
  #25   Spotlight this post!  
Unread 26-04-2015, 13:40
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

Do you think this will work? (I only included the drive code)

Code:
public void teleopInit(){
	gyroMode = true;
}

public void teleopPeriodic() {
        Jx = OI.joystick0.getX(); //Drive Joystick X
        Jy = OI.joystick0.getY(); //Drive Joystick Y
        Jz = OI.joystick0.getZ(); //Drive Joystick Z
        rotationSpeedForError = RobotMap.GyroMod(Jz);
        RobotMap.driveRobotDrive41.mecanumDrive_Cartesian(Jx, Jy, Jz + rotationSpeedForError, 0);
}

public double GyroMod(double rotation){
	if (gyroMode == true) {
		double error = gyro.getAngle() - gyroHeading;
		double kP = .05;
		if (rotation == 0) {
			rotation = rotation + kP * error;
		} else {
			gyroHeading = gyro.getAngle();
		}
	}
	return rotation;
	}
}
__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West

Last edited by WillNess : 26-04-2015 at 23:00.
Reply With Quote
  #26   Spotlight this post!  
Unread 26-04-2015, 23:37
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Re: Getting Gyro Working w/ Mecanum Drive

I believe I have gotten gyro code to work! It is a little tricky while strafing, but for every other use it works great, and it really does help limit the turning while strafing according to our driver. So here you go, FYI: I only included code related to the driving, don't replace whole sections with this code:

Code:
public void teleopInit() {
        RobotMap.gyro.reset();
        gyroMode = true;
}

public void teleopPeriodic() {
        Jx = OI.joystick0.getX(); //Drive Joystick X
        Jy = OI.joystick0.getY(); //Drive Joystick Y
        Jz = OI.joystick0.getZ(); //Drive Joystick Z
        //gyroHeading = 0;
        if(OI.joystick0.getTrigger()){
        	rotationSpeedForError = RobotMap.GyroMod(0);
        	RobotMap.setVelocity(Jx, 0, 0); //This is strafe mode
        }else{
        	rotationSpeedForError = RobotMap.GyroMod(Jz);
            RobotMap.setVelocity(Jx, Jy, Jz);
        }
}

public static void setVelocity(double x,double y,double z){
	double fMax = Math.sin(-0*Math.PI/4+1*Math.PI/4);
	double fR = Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
	double rL = -Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
	double fL = -Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
	double rR = Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
  	if(Math.abs(z)<.1){
  		drivefrontLeft.set(fL- rotationSpeedForError);
		drivefrontRight.set(fR - rotationSpeedForError);
   		driverearLeft.set(rL - rotationSpeedForError);
    		driverearRight.set(rR - rotationSpeedForError);
		// The reason that all of them are subtracting the error is that on our robot, the motors on the right side are flipped upside down, so to make them go the opposite way of the left, you have to flip the signs. These signs will change from bot to bot, so be prepared to change these.
    	}else{
    		drivefrontLeft.set(fL+z/2);
    		drivefrontRight.set(fR+z/2);
    		driverearLeft.set(rL+z/2);
    		driverearRight.set(rR+z/2);
		// The reason that all of them are adding the Z is that on our robot, the motors on the right side are flipped upside down, so to make them go the opposite way of the left, you have to flip the signs. These signs will change from bot to bot, so be prepared to change these.
    		gyroHeading = gyro.getAngle();
    	}
}
public static double GyroMod(double rotation){
	if (gyroMode) {
		double error = gyro.getAngle() - gyroHeading;
		double kP = .03;
		if (Math.abs(rotation) < .1) { 
			rotation = rotation + kP * error;
		}else {
			
		}
	}
	return rotation;
}
This code is also very useful because in autonomous instead of turning your robot for X amount of seconds, you just add X amount of degrees to the gyroHeading value and it will be very accurate, or in teleop you can have various buttons to fine-adjust the angle or flip the robot around, or etc.. There's many applications to this.
One thing I might try to do in the future is make the correction only read [-180,180] because when we set our heading to 0, we could rotate around 3-4 times and instead of going to the nearest "0" it would rotate back the exact amount. The reason this could help is that if the game is in a year with a goal, you could press a button and the robot would rotate to face the goal exactly, but this wouldn't work if maybe you had spun around several times before.

Thanks to: GeeTwo, Ether, and dawonn
__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West

Last edited by WillNess : 26-04-2015 at 23:43. Reason: Added info @ Bottom & included helper names
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 11:13.

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