![]() |
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(); } } |
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 ] closes (Eliminate the spaces from the tags) |
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 |
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 { |
Re: Can someone explain this code?
Quote:
|
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++)Code:
for (Talon t : left)Code:
public void setLeft(int speed)Code:
public void setSpeed(Talon[] tarray, int Speed)Code:
public void setSpeed(Talon[] tarray, int Speed) |
| All times are GMT -5. The time now is 22:38. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi