Go to Post The other day, I stuck my head into a box of Rhino treads and got a sugar high. - Andy Baker [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: 10 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 25-10-2011, 11:11
austin1743 austin1743 is offline
Head Programmer - Java
FRC #1743
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2009
Location: Pennsylvania
Posts: 56
austin1743 is an unknown quantity at this point
help with mecanum drive code

Hello, I am doing some off-season coding practice but I keep getting an error that i have been stumped on, Can someone help me out? just some tips or a way to go about it.

So far i have:


// things below this need to be imported first
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Dashboard;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import com.sun.squawk.util.MathUtils;

public class RobotTemplate extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
RobotDrive m_robotDrive;// robot will use PWM 1-4 for drive motors

//declears speed controllers
Victor leftFrontspeed = new Victor(4, 1);
Victor rightFrontspeed = new Victor(4, 2);
Victor leftBackspeed = new Victor(4,3);
Victor rightBackspeed = new Victor(4,4);
DriverStation driverStation; // driver station object
Dashboard dashboard;

// Declare variables for the two joysticks being used
Joystick js1; // joystick 1 (arcade stick or right tank stick)


public class RobotTemplate extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
RobotDrive m_robotDrive;// robot will use PWM 1-4 for drive motors
public void teleopPeriodic() {
// leftspeed.set(js1.getY());
// rightspeed.set(js1.getY());

//is called when teloporated mode is enabled
double lf;//is what will be set to the left wheel speed
double rf;//is what will be set to the right wheel speed
double lb; // is what will be set to the left back wheel speed
double rb; // is what will be set to the left back wheel speed
double y = js1.getY(); //gets the value of the 1'st joystick's y axis
double x = js1.getX();//gets the value of the 1'st joystick's x axis
//y = y * Math.abs(y);//makes the y value quadratic
//x = x * Math.abs(x);//makes the x value quadratic
x=-x;
//begins meckanum code which I do not understand but it works
double magnitude =Math.sqrt(js1.getY()*js1.getY()+js1.getX()*js1.ge tX());
magnitude = magnitude*Math.sqrt(2);
double rotation = js1.getTwist();
double direction=MathUtils.atan2(x, y);
double dirInRad = (direction+45);
double sinD=Math.sin(dirInRad);
double cosD=Math.cos(dirInRad)*Math.sqrt(2);
lf=-sinD*magnitude+rotation;
rf=-cosD*magnitude-rotation;
lb=-cosD*magnitude+rotation;
rb=-sinD*magnitude-rotation;
//ends mecanum code

//lf=y;
//lb=y;
//rb=y;
//rf=y;

//runs function limit to ensure the speed of the motors is in exceptable range
lf =limit(lf,1,-1);
lb =limit(lb,1,-1);
rf =limit(rf,1,-1);
rb =limit(rb,1,-1);

//sets the motors speed
leftFrontspeed.set(lf);
leftBackspeed.set(lb);
rightFrontspeed.set(-rf);
rightBackspeed.set(-rb);

System.out.println("js1.y:"+js1.getY()+"\t js1.x:"+js1.getX()+"\t lf:"+lf+"\t rf:"+rf+"\t lb:"+lb+"\t rb:"+rb);

}

}

I get the errors saying that the following lines of code are "Cannot find symbol". I do what netbeans wants me to do and it makes a class for it but then i get a lot more errors.

If someone has just a standard drive code i can look at please refer me to that link.

thanks a lot.

Last edited by austin1743 : 25-10-2011 at 21:13. Reason: edited code to bring up to recomendations
Reply With Quote
  #2   Spotlight this post!  
Unread 25-10-2011, 16:43
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,590
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: help with mecanum drive code

Where is js1 declared?
Reply With Quote
  #3   Spotlight this post!  
Unread 25-10-2011, 18:24
austin1743 austin1743 is offline
Head Programmer - Java
FRC #1743
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2009
Location: Pennsylvania
Posts: 56
austin1743 is an unknown quantity at this point
Re: help with mecanum drive code

thanks, i didn't even catch that, i had it in my first code document but not in the second...

// things below this need to be imported first
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Dashboard;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import com.sun.squawk.util.MathUtils;


public class RobotTemplate extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
RobotDrive m_robotDrive;// robot will use PWM 1-4 for drive motors

//declears speed controllers
Victor leftFrontspeed = new Victor(4, 1);
Victor rightFrontspeed = new Victor(4, 2);
Victor leftBackspeed = new Victor(4,3);
Victor rightBackspeed = new Victor(4,4);
DriverStation driverStation; // driver station object
Dashboard dashboard;

// Declare variables for the two joysticks being used
Joystick js1; // joystick 1 (arcade stick or right tank stick)



public void robotInit() {

}


/**This function is called periodically during operator control
*/
public void teleopPeriodic() {
// leftspeed.set(js1.getY());
// rightspeed.set(js1.getY());

//is called when teloporated mode is enabled
double lf;//is what will be set to the left wheel speed
double rf;//is what will be set to the right wheel speed
double lb; // is what will be set to the left back wheel speed
double rb; // is what will be set to the left back wheel speed
double y = js1.getY(); //gets the value of the 1'st joystick's y axis
double x = js1.getX();//gets the value of the 1'st joystick's x axis
//y = y * Math.abs(y);//makes the y value quadratic
//x = x * Math.abs(x);//makes the x value quadratic
x=-x;
//begins meckanum code which I do not understand but it works
double magnitude =Math.sqrt(js1.getY()*js1.getY()+js1.getX()*js1.ge tX());
magnitude = magnitude*Math.sqrt(2);
double rotation = js1.getTwist();
double direction=MathUtils.atan2(x, y);
double dirInRad = (direction+45);
double sinD=Math.sin(dirInRad);
double cosD=Math.cos(dirInRad)*Math.sqrt(2);
lf=-sinD*magnitude+rotation;
rf=-cosD*magnitude-rotation;
lb=-cosD*magnitude+rotation;
rb=-sinD*magnitude-rotation;
//ends mecanum code

//lf=y;
//lb=y;
//rb=y;
//rf=y;

//runs function limit to ensure the speed of the motors is in exceptable range
lf =limit(lf,1,-1);
lb =limit(lb,1,-1);
rf =limit(rf,1,-1);
rb =limit(rb,1,-1);




//sets the motors speed
leftFrontspeed.set(lf);
leftBackspeed.set(lb);
rightFrontspeed.set(-rf);
rightBackspeed.set(-rb);

System.out.println("js1.y:"+js1.getY()+"\t js1.x:"+js1.getX()+"\t lf:"+lf+"\t rf:"+rf+"\t lb:"+lb+"\t rb:"+rb);


}

}

I have put in the js1 (joystick 1). I have also highlighted (in bold) where I am getting my error at.
Reply With Quote
  #4   Spotlight this post!  
Unread 25-10-2011, 18:32
JosephC's Avatar
JosephC JosephC is offline
FF: Breakfast Company
AKA: Joseph Cupchack
no team (FiM Volunteer Extraordinaire)
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Waterford, Michigan
Posts: 1,752
JosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond reputeJosephC has a reputation beyond repute
Re: help with mecanum drive code

Mecanum in a nutshell

Code:
magnitude = leftStick.getMagnitude();
direction = leftStick.getDirectionDegrees();
rotation = rightStick.getX();
driveRobot.mecanumDrive_Polar(magnitude, direction, rotation);
__________________
Referee: 2015 - ?
Field Reset/Supervisor: 2013 - ?
68 Team Member: 2011 - 2013
Reply With Quote
  #5   Spotlight this post!  
Unread 25-10-2011, 21:23
austin1743 austin1743 is offline
Head Programmer - Java
FRC #1743
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2009
Location: Pennsylvania
Posts: 56
austin1743 is an unknown quantity at this point
Re: help with mecanum drive code

Ross, i have added the js1 (joystick1) into the code thanks for catching that

Joseph thanks for the mecanum in a nut shell. I know mecanum wheels have some weird math vectors behind them which i sort of understand but how does that relate to the controller? I ask this as I am a little confused on why the code would have magnitude from the controller?
Reply With Quote
  #6   Spotlight this post!  
Unread 25-10-2011, 21:33
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: help with mecanum drive code

Quote:
Originally Posted by austin1743 View Post
Ross, i have added the js1 (joystick1) into the code thanks for catching that

Joseph thanks for the mecanum in a nut shell. I know mecanum wheels have some weird math vectors behind them which i sort of understand but how does that relate to the controller? I ask this as I am a little confused on why the code would have magnitude from the controller?
If the driver is using a joystick, the magnitude can be computed using sqrt(X^2+Y^2), or perhaps max(abs(X),abs(Y)). That is used as a speed command - how fast the driver wants the vehicle to go.

The direction is computed using for example theta = atan2(-Y,X). It is the clockwise angle with zero in the forward direction. It is the driver command to tell which direction the driver wants the vehicle to move.



Last edited by Ether : 25-10-2011 at 21:51. Reason: added abs()
Reply With Quote
  #7   Spotlight this post!  
Unread 25-10-2011, 21:43
austin1743 austin1743 is offline
Head Programmer - Java
FRC #1743
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2009
Location: Pennsylvania
Posts: 56
austin1743 is an unknown quantity at this point
Re: help with mecanum drive code

thanks Ether for the explanation.

I am going to try the code out soon to see if it works on our robot.
Reply With Quote
  #8   Spotlight this post!  
Unread 12-11-2011, 18:41
austin1743 austin1743 is offline
Head Programmer - Java
FRC #1743
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2009
Location: Pennsylvania
Posts: 56
austin1743 is an unknown quantity at this point
Re: help with mecanum drive code

Ok well, unforntanetly i gotta wait to test this on our robot....

But i have come across some errors in the code.

The error i am getting is on: Public class Robot extends IterativeRobot {
is says that a class or interference expected..

you can see the java code in the google doc at:
https://docs.google.com/document/d/1...fpFrrftRQ/edit


I believe it maybe just a file i got spelled wrong but i am checking on that...
__________________
Team 1743
Head Programmer
Reply With Quote
  #9   Spotlight this post!  
Unread 24-11-2011, 22:06
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: help with mecanum drive code

This is our team's custom mecanum code:

Code:
jaguarDrive((jX+jY)/2, (jY-jX)/2, -(jY-jX)/2, -(jX+jY)/2);
jX and jY are just the X and Y coordinates our joysticks read (-1 through 1).

And the jaguarDrive method:

Code:
public void jaguarDrive(double FL, double RL, double FR, double RR)
    {
        m_frontLeftMotor.set(FL);
        m_rearLeftMotor.set(RL);
        m_frontRightMotor.set(FR);
        m_rearRightMotor.set(RR);
    }
This is by far the simplest solution I've seen.
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
Reply With Quote
  #10   Spotlight this post!  
Unread 24-11-2011, 22:36
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: help with mecanum drive code

Quote:
Originally Posted by Djur View Post
This is by far the simplest solution I've seen.
It's simple because you're only commanding 2 degrees of freedom.


Reply With Quote
  #11   Spotlight this post!  
Unread 24-11-2011, 22:42
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: help with mecanum drive code

Quote:
Originally Posted by Ether View Post
It's simple because you're only commanding 2 degrees of freedom.

What other degrees of freedom are there, though? Turning is simple enough and isn't the topic of this thread anyhow.
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
Reply With Quote
  #12   Spotlight this post!  
Unread 24-11-2011, 22:49
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: help with mecanum drive code

Quote:
Originally Posted by Djur View Post
What other degrees of freedom are there, though? Turning is simple enough and isn't the topic of this thread anyhow.
The code you posted commands only translation (fwd/rev & left/right), no rotation.

The topic of this thread is "help with mecanum drive code", so yes turning is part of mecanum drive code.

Adding rotation to the mix is not difficult. Simple code for that has been posted elsewhere.


Reply With Quote
  #13   Spotlight this post!  
Unread 24-11-2011, 22:52
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: help with mecanum drive code

Quote:
Originally Posted by Ether View Post
The code you posted commands only translation (fwd/rev & left/right), no rotation.

The topic of this thread is "help with mecanum drive code", so yes turning is part of mecanum drive code.

Ah. Then I should just add that by setting FL, RL, FR, RR to the same value will make the robot turn. Positive values turn the robot counterclockwise, negative values turn it clockwise (assuming the motors are hooked up correctly).

Very simple example use for turning:
Code:
boolean turnRightAtFullSpeed = true;
if(turnRightAtFullSpeed)
jaguarDrive(-1, -1, -1, -1);
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)

Last edited by Djur : 24-11-2011 at 23:16.
Reply With Quote
  #14   Spotlight this post!  
Unread 24-11-2011, 23:03
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
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: help with mecanum drive code

Quote:
Originally Posted by Djur View Post
Ah. Then I should just add that by setting FL, RL, FR, RR to the same value will make the robot turn. Positive values turn the robot clockwise, negative values turn it counterclockwise (assuming the motors are hooked up correctly).

Very simple example use for turning:
Code:
boolean turnRightAtFullSpeed = true;
if(turnRightAtFullSpeed)
jaguarDrive(1, 1, 1, 1);
If you want full 3 degree of freedom control (rotate while translating), you should either use the provided WPI library codes for mecanum, or do something like this:

Code:
"y" is the forward command

"x" is the strafe right command

"z" is the rotate clockwise command


Step1:

Compute the 4 wheel speeds:

FL = y + x + z

FR = y - x - z

RL = y - x + z

RR = y + x - z


Step2:

Find the max absolute value of the above four commands, 
and if it is greater than 1, 
divide all four commands by that max absolute value.

Reply With Quote
  #15   Spotlight this post!  
Unread 24-11-2011, 23:15
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: help with mecanum drive code

Quote:
Originally Posted by Ether View Post
Code:
"y" is the forward command

"x" is the strafe right command

"z" is the rotate clockwise command


Step1:

Compute the 4 wheel speeds:

FL = y + x + z

FR = y - x - z

RL = y - x + z

RR = y + x - z


Step2:

Find the max absolute value of the above four commands, 
and if it is greater than 1, 
divide all four commands by that max absolute value.
That's very interesting, although I see no point at which a robot would need to do that except for showing off.
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
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 12:50.

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