View Single Post
  #13   Spotlight this post!  
Unread 27-01-2016, 15:31
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Drive train PID control

Quote:
Originally Posted by techkid86 View Post
More or less yes. So you're using talons. PWM I assume? if so, the following code should be enough to set up your drive.

using only basic classes:
Code:
Talon leftDrive=new Talon(1);
Talon rightDrive=new Talon(2);

//if using gamepad
Joystick gamepad=new Joystick(1);

//if using two individual joysticks
Joystick leftJoy=new Joystick(1);
Joystick leftJoy=new Joystick(2);

Encoder leftEncoder=new Encoder(1,2);
Encoder rightEncoder=new Encoder(1,2);

PIDController leftPID=new PIDController(0.1,0,0, leftEncoder, leftDrive);
PIDController rightPID=new PIDController(0.1,0,0, rightEncoder, rightDrive);

public void robotInit()
{
leftEncoder.setPIDSourceType(PIDSourceType.kDisplacement); rightEncoder.setPIDSourceType(PIDSourceType.kDisplacement); rightEncoder.setReverseDirection(true); rightDrive.setInverted(true); //only one side needs inversion leftEncoder.reset(); rightEncoder.reset(); leftPID.setOutputRange(-0.75,0.75); //max speed it can set to motors rightPID.setOutputRange(-0.75,0.75); //max speed it can set to motors leftPID.enable(); rightPID.enable();
} public void autonomousPeriodic() {
leftPID.setSetpoint(400); rightPID.setSetpoint(400); //set distance. you can change distance per tick through the encoder
} public void teleopInit() {
leftPID.disable(); rightPID.disable();
} public void teleopPeriodic() {
//for two joysticks leftDrive.set(leftJoy.getY()); rightDrive.set(rightJoy.getY()); //for one gamepad leftDrive.set(gamepad.getRawAxis(1)); rightDrive.set(gamepad.getRawAxis(2)); //replace 1 and 2 axis with actual axis number.
}
Is it complicated to set this up for arcade drive with one Logitech attack 3? We don't own two matching joysticks so tank drive would be funky.

This is what I thought of doing to have it work for arcade drive I have the PID setup stuff added on I just took it out for this.
Code:
public class Robot extends IterativeRobot {
	Talon leftDrive = new Talon(0);
	Talon rightDrive = new Talon(1);

	RobotDrive tankDrive = new RobotDrive(leftDrive, rightDrive);

	Joystick joystick = new Joystick(0);

	Encoder rightEncoder = new Encoder(0, 1, true,
			EncodingType.k4X);

	PIDController rightPID = new PIDController(0.1, 0, 0, rightEncoder, rightDrive);

	public void robotInit() {
	}

	public void autonomousInit() {
	}

	public void autonomousPeriodic() {
	}

	public void teleopPeriodic() {
		tankDrive.arcadeDrive(-joystick.getY(), -joystick.getX());
	}
}

Last edited by curtis0gj : 27-01-2016 at 15:38.
Reply With Quote