Log in

View Full Version : Can someone explain this code?


MaestroRoboto
04-11-2014, 18:10
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();

}
}

JesseK
04-11-2014, 18:25
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. printf("Hello World");

[ code ] opens
[ /code ] closes
(Eliminate the spaces from the tags)

MaestroRoboto
04-11-2014, 19:46
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

mwtidd
04-11-2014, 20:14
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.


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();

}
}

Joe Ross
04-11-2014, 20:34
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/showpost.php?p=1133680&postcount=11

AWoL
06-11-2014, 16:45
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:


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...


for (Talon t : left)
t.set(1);

then put it in another method for easy use...


public void setLeft(int speed)
{
for (Talon t : left)
t.set(speed);
}

or even a more general method for less code...


public void setSpeed(Talon[] tarray, int Speed)
{
for (Talon t : tarray)
t.set(speed);
}

and finally, work in the inversion...


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.