Go to Post If it's good enough for Star Trek, it's good enough for FRC. :) - KarenH [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 04-11-2014, 18:10
MaestroRoboto's Avatar
MaestroRoboto MaestroRoboto is offline
Registered User
AKA: Hiren Bhavsar
FLL #0020 (The Rocketeers)
Team Role: Alumni
 
Join Date: Mar 2014
Rookie Year: 2012
Location: United States
Posts: 25
MaestroRoboto has a spectacular aura aboutMaestroRoboto has a spectacular aura about
Can someone explain this code?

Could someone explain what is going on in this code?

public class Drivetrain {

Talon talon1 = new Talon(1);
Talon talon2 = new Talon(2);
Talon talon3 = new Talon(3);
Talon talon4 = new Talon(4);
Talon talon5 = new Talon(5);
Talon talon6 = new Talon(6);

DoubleSolenoid shifter = new DoubleSolenoid(1, 5, 6);

Encoder leftEncoder = new Encoder(10, 11);
Encoder rightEncoder = new Encoder(13, 14);

public Drivetrain(){
leftEncoder.start();
rightEncoder.start();
}

//DigitalInput port14 = new DigitalInput(14);
public void arcadeDrive(double moveValue, double rotateValue){
double leftMotorSpeed = 0, rightMotorSpeed = 0;

rotateValue = -rotateValue;
moveValue = limit(moveValue);
rotateValue = limit(rotateValue);

if (moveValue > 0.0) {
if (rotateValue > 0.0) {
leftMotorSpeed = moveValue - rotateValue;
rightMotorSpeed = Math.max(moveValue, rotateValue);
} else {
leftMotorSpeed = Math.max(moveValue, -rotateValue);
rightMotorSpeed = moveValue + rotateValue;
}
} else {
if (rotateValue > 0.0) {
leftMotorSpeed = -Math.max(-moveValue, rotateValue);
rightMotorSpeed = moveValue + rotateValue;
} else {
leftMotorSpeed = moveValue - rotateValue;
rightMotorSpeed = -Math.max(-moveValue, -rotateValue);
}
}

//Setting talon values
talon1.set(-rightMotorSpeed);
talon2.set(rightMotorSpeed);
talon3.set(rightMotorSpeed);
talon4.set(leftMotorSpeed);
talon5.set(-leftMotorSpeed);
talon6.set(-leftMotorSpeed);
}

protected static double limit(double num) {
if (num > 1.0) {
return 1.0;
}
if (num < -1.0) {
return -1.0;
}
return num;
}

public void highGear(){
shifter.set(DoubleSolenoid.Value.kReverse);
}

public void lowGear(){
shifter.set(DoubleSolenoid.Value.kForward);
}

public void brake(){

}

public int getRightDistance(){
//return port14.get();
return rightEncoder.get();
}

public int getLeftDistance(){
return leftEncoder.get();
}

public void resetEncoders(){
leftEncoder.reset();
rightEncoder.reset();

}
}
Reply With Quote
  #2   Spotlight this post!  
Unread 04-11-2014, 18:25
JesseK's Avatar
JesseK JesseK is offline
Expert Flybot Crasher
FRC #1885 (ILITE)
Team Role: Mentor
 
Join Date: Mar 2007
Rookie Year: 2005
Location: Reston, VA
Posts: 3,661
JesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond repute
Re: Can someone explain this code?

What behavior is it exhibiting that doesn't make sense?

It looks relatively normal, but could you add code tags around it so it preserves indentation? e.g.
Code:
printf("Hello World");
[ code ] opens
[ /code ] closes
(Eliminate the spaces from the tags)

Last edited by JesseK : 04-11-2014 at 18:28.
Reply With Quote
  #3   Spotlight this post!  
Unread 04-11-2014, 19:46
MaestroRoboto's Avatar
MaestroRoboto MaestroRoboto is offline
Registered User
AKA: Hiren Bhavsar
FLL #0020 (The Rocketeers)
Team Role: Alumni
 
Join Date: Mar 2014
Rookie Year: 2012
Location: United States
Posts: 25
MaestroRoboto has a spectacular aura aboutMaestroRoboto has a spectacular aura about
Re: Can someone explain this code?

The code works fine, I just don't understand it and I am unable to contact the guy who wrote it.

The code shown is for a 6 wheel west coast drive bot
Reply With Quote
  #4   Spotlight this post!  
Unread 04-11-2014, 20:14
mwtidd's Avatar
mwtidd mwtidd is offline
Registered User
AKA: mike
FRC #0319 (Big Bad Bob)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 2003
Location: Boston, MA
Posts: 714
mwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond repute
Re: Can someone explain this code?

Here's what I've interpreted so far. The rotate left/right may be flipped.

This is a rather strange convention.
leftMotorSpeed = moveValue - rotateValue;
rightMotorSpeed = Math.max(moveValue, rotateValue);

It's probably trying to make sure the right motor is going faster than the left (i.e. turning left), but regardless this is probably not the ideal way to code this.

Code:
public class Drivetrain {

	//right speed controllers
	Talon talon1 = new Talon(1); //front
	Talon talon2 = new Talon(2); //middle
	Talon talon3 = new Talon(3); //rear
	
	//left speed controllers
	Talon talon4 = new Talon(4); //front
	Talon talon5 = new Talon(5); //middle
	Talon talon6 = new Talon(6); //rear
	
	DoubleSolenoid shifter = new DoubleSolenoid(1, 5, 6);
	
	Encoder leftEncoder = new Encoder(10, 11);
	Encoder rightEncoder = new Encoder(13, 14);
	
	public Drivetrain(){
		leftEncoder.start();
		rightEncoder.start();
	}
	
	//DigitalInput port14 = new DigitalInput(14);
	public void arcadeDrive(double moveValue, double rotateValue){
	double leftMotorSpeed = 0, rightMotorSpeed = 0;
	
	rotateValue = -rotateValue; //x axis value from joystick. inverted to get the correct rotation direction
	moveValue = limit(moveValue); //y axis value from joystick. make sure its between -1 and 1
	rotateValue = limit(rotateValue); //make sure the rotation value from before is between -1 and 1
	
	if (moveValue > 0.0) {
		//driving forward?
		if (rotateValue > 0.0) {
			//turning left?
			leftMotorSpeed = moveValue - rotateValue;
			rightMotorSpeed = Math.max(moveValue, rotateValue);
		} else {
			//stopped or turning right?
			leftMotorSpeed = Math.max(moveValue, -rotateValue);
			rightMotorSpeed = moveValue + rotateValue;
		}
	} else {
		//driving backward or stopped
		if (rotateValue > 0.0) {
			//turning left?
			leftMotorSpeed = -Math.max(-moveValue, rotateValue);
			rightMotorSpeed = moveValue + rotateValue;
		} else {
			//stopped or turning right?
			leftMotorSpeed = moveValue - rotateValue;
			rightMotorSpeed = -Math.max(-moveValue, -rotateValue);
		}
	}
	
	//Setting talon values
	talon1.set(-rightMotorSpeed); //invert the front right speed controller (probably because the leads were swapped)
	talon2.set(rightMotorSpeed);
	talon3.set(rightMotorSpeed);
	talon4.set(leftMotorSpeed); 
	talon5.set(-leftMotorSpeed); //invert the center left speed controller (probably because the leads were swapped)
	talon6.set(-leftMotorSpeed); //invert the rear left speed controller (probably because the leads were swapped)
	}
	
	protected static double limit(double num) {
		if (num > 1.0) {
			return 1.0;
		}
		if (num < -1.0) {
			return -1.0;
		}
		return num;
	}
	
	public void highGear(){
		//retract pneumatic
		shifter.set(DoubleSolenoid.Value.kReverse);
	}
	
	public void lowGear(){
		//extend pneumatic
		shifter.set(DoubleSolenoid.Value.kForward);
	}
	
	public void brake(){
	
	}
	
	public int getRightDistance(){
		return rightEncoder.get();
	}
	
	public int getLeftDistance(){
		return leftEncoder.get();
	}
	
	public void resetEncoders(){
		leftEncoder.reset();
		rightEncoder.reset();
	
	}
}
__________________
"Never let your schooling interfere with your education" -Mark Twain
Reply With Quote
  #5   Spotlight this post!  
Unread 04-11-2014, 20:34
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,567
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: Can someone explain this code?

Quote:
Originally Posted by lineskier View Post
Here's what I've interpreted so far. The rotate left/right may be flipped.

This is a rather strange convention.
leftMotorSpeed = moveValue - rotateValue;
rightMotorSpeed = Math.max(moveValue, rotateValue);

It's probably trying to make sure the right motor is going faster than the left (i.e. turning left), but regardless this is probably not the ideal way to code this.
That part is a copy of the WPILib arcadeDrive method. You can see how it works here: http://www.chiefdelphi.com/forums/sh...0&postcount=11
Reply With Quote
  #6   Spotlight this post!  
Unread 06-11-2014, 16:45
AWoL's Avatar
AWoL AWoL is offline
Lvl. 225 Dark Code Mage (Java Type)
AKA: Adam Wolnikowski
FRC #0225 (TechFire)
Team Role: Programmer
 
Join Date: Mar 2014
Rookie Year: 2014
Location: York, Pennsylvania
Posts: 116
AWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond repute
Re: Can someone explain this code?

From a style and code neatness standpoint, might I suggest putting your right talons in an array and your left talons in another? Then you can set an entire side's speeds with a for loop like so:

Code:
for (int i = 0; i < left.length; i++)
{
   left[i].set(1);
}
or now with the roboRIO (Java SE 8), with a for each loop...

Code:
for (Talon t : left)
   t.set(1);
then put it in another method for easy use...

Code:
public void setLeft(int speed)
{
   for (Talon t : left)
      t.set(speed);
}
or even a more general method for less code...

Code:
public void setSpeed(Talon[] tarray, int Speed)
{
   for (Talon t : tarray)
      t.set(speed);
}
and finally, work in the inversion...

Code:
public void setSpeed(Talon[] tarray, int Speed)
{
   for (Talon t : tarray)
   {
      if (t.equals(talon1) || t.equals(talon5) || t.equals(talon6))
         t.set(-speed);
      else
         t.set(speed);
   }
}
We handle setting our drivetrain speed in a similar fashion.
__________________
2016 Competition Results (Co-captain, Driver, and Lead Programmer; Junior)
Springside-Chestnut Hill District Event - WINNER / #2 Seed, #1 Seed's First Pick / Gracious Professionalism Award
Westtown District Event - WINNER / #1 Seed / Industrial Design Award
MAR District Championship - WINNER / #1 Seed / Industrial Design Award / Dean's List Finalist Award (Me)
World Championship, Carson Subdivision - QUARTERFINALIST / #3 Seed, #2 Seed's First Pick
Indiana Robotics Invitational - FINALIST / #14 Seed, #2 Seed's Second Pick

Last edited by AWoL : 06-11-2014 at 16:47. Reason: Note at the end.
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 22:38.

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