Introduction: How to Write a Simple Drivetrain for FRC (Java)

Picture of How to Write a Simple Drivetrain for FRC (Java)

This is a tutorial on how to make a simple drivetrain for a FRC robot. This tutorial assumes that you know that basics of java, eclipse and already have wpilib installed, as well as CTRE libraries.

Step 1:

Picture of

Open Eclipse

Step 2: Create a New Robot Project

Picture of Create a New Robot Project

  1. Right Click on the package explorer, click new, then other.
  2. Scroll down till you see WPILib Robot Java Development (if you don't see it you don't have the wpilib resources installed.)
  3. Click on Robot Java Project
  4. Then fill in the project name and click the command-based robot project type.(Package should already be filled out and same with simulation world.)

Step 3: Create/Fill in RobotMap

Picture of Create/Fill in RobotMap

  1. Create final variables that contain the number of the different talon ports.

Step 4: Delete Example Command and Example Subsystem

Picture of Delete Example Command and Example Subsystem

Step 5: Create DriveTrainSubSystem

Picture of Create DriveTrainSubSystem

  1. Create a new constructor in the newly created drivetrain subsystem. Then create CANTalon objects that correspond to the talons on the drivetrain.
  2. Create a RobotDrive called Drive
  3. Instantiate these Objects in the constructor(make sure to use the values for the talons that we created in the robot map). For the Robot Drive we will use the constructor that uses 4 Motor Controllers (RobotDrive(SpeedController frontLeftMotor, SpeedController rearLeftMotor, SpeedController frontRightMotor, SpeedController rearRightMotor))
  4. Then create an arcadeDrive() method with two input variables were x is forward and backward and y is right and left. Inside you will call the drive.arcade with the forward and rotate values
  5. Then change the initDefaultCommand() to contain the line setDefaultCommand(new DriveTrainCommand());.
  6. Don't worry about all the errors yet.

Step 6: Create the DriveTrainCommand

Picture of Create the DriveTrainCommand
  1. First start off by going to the menu that we have used to create the subsystem and the robot project its self( this is the last time I will show this actual step in the future steps I will say just to make a command or to make a subsystem and you will assume that it is in this menu.) Click command and fill in the class name with DriveTrainCommand (if you do change the names of these file you can not have them be the same).
  2. In the new DriveTrainCommand you will see that there are 6 methods one is the constructor and the other 5 are parts of the code that robot will call when the command is running. We know what the constructor does so lets explain Initialize, execute, isFinished, end, and interrupted. Initialize is called once each time the command is called, the execute method is called continuously until the command ends, which is caused by the is finished when the isFinished method returns true the command will stop running, The end method is called once after the isFinished method is called, and the interrupted is called when the commands resources are being used by another command and the command will end (without calling the end method).
  3. First in the DriveTrainCommand in the constructor you need to add the requires line(requires(Robot.DriveTrainSub)) notice that the DriveTrainSub is not the same as the subsystem name and that is on purpose.

Step 7: Jump to Robot

Picture of Jump to Robot
  1. Next we are going to jump to the Robot Class
  2. then we are going to change the line (public static final ExampleSubsystem exampleSubsystem = new ExampleSubsystem();) to (public static final DriveTrainSubsystem DriveTrainSub = new DriveTrainSubsystem();) notice the first name after final is the same as the subsystem name and the name after new also notice that the DriveTrainSub is the same as the name that we put in our last step and that it is not the same as the subsystem name (You MUST have a object name(DriveTrainSub) that is not the same as the subsystem name).
  3. Then import our DriveTrainSubSystem.
  4. Next we will remove the line(chooser.addDefault("Default Auto", new ExampleCommand());)
  5. Then remove the unused imports.
  6. Then save.

Step 8: Go Back to DriveTrainCommand

Picture of Go Back to DriveTrainCommand

  1. import Robot(the second one in the image)
  2. Then Save

Step 9: Go to DriveTrainSub

Picture of Go to DriveTrainSub

  1. Import DriveTrainCommand
  2. Then save.

Step 10: Next We Will Create the OI Code.

Picture of Next We Will Create the OI Code.

  1. Go to OI.
  2. Create a new public joystick object with the port of 0.
  3. And remove the unused imports.
  4. Save.

Step 11: Go to DriveTrainCommand

Picture of Go to DriveTrainCommand
  1. Go to DriveTrainCommand.
  2. Now we will do the part that take the joysticks and apply them to move the robot. Inside the execute(because it runs constantly) add the line (Robot.DriveTrainSub.arcadeDrive(Robot.oi.Driver.getRawAxis(1), Robot.oi.Driver.getRawAxis(4));) where we call the arcadeDrive in our subsystem with the values of Robot.io.Driver.getRawAxis(1) which returns the value of the joystick and where 1 is the axis of the left y axis and the same for the second value except 4 is the right x axis. so this would mean that the left stick would go forward and backward and the right stick would go right and left. (if you wanted you could go back to the RobotMap and create new values for the joystick axis then call them with RobotMap.(name of axis) and don't forget to make them final if you do it this way.)
  3. Next remove any unused imports from the whole project.

Step 12: Finally Compile and Upload the Code to the Robot

Picture of Finally Compile and Upload the Code to the Robot

  1. Click the green arrow in the eclipse toolbar and go down to the WPILib Java Deploy (don't forget to connect to the robot before doing this :P).

Comments

Ian01 (author)2017-11-24

"Drivetrain" sounds like a mechanical thing to me (i.e. something you don't write in Java). Does it have a different meaning in FRC?

ForgeL (author)Ian012017-11-25

In FRC a drivetrain is the motors and wheels the move the robot and everything on the robot in FRC needs to be coded so...

Swansong (author)2017-11-22

That looks neat, do you have a video of the robot in action?

ForgeL (author)Swansong2017-11-22

Thanks, I don't have a video of this code working how ever we do have videos of our robot working as a whole during our competition season where very similar code was used.

About This Instructable

879views

13favorites

More by ForgeL:How to Write a Simple Drivetrain for FRC (Java)
Add instructable to: