Simple Tankdrive with 4 CIMs

Hello all,

I’m part of a newbie team this year and after some research and long hours of watching videos I have a basic idea of the concepts of java programming, but I’m not quite sure how to actually write code that will be able to drive the robot.

My first task is to find out how to write the code using tank drive with 4 CIM motors. Our team is planning on using the two included joysticks for left and right motor control.

It seems like setting up the tank drive system is relatively easy (to experienced programmers) but I don’t even know where to start. Can any of you offer any guidance as to how write the code for a tank drive system?

I have downloaded Netbeans and the FRC plugins so I think I am ready to start writing code. Any help would be greatly appreciated, thanks!

This might help. You probably need to start from ‘Creating a new Project…’ on page 9 since you’re already installed everything.

I would suggest loading one of the FRC sample projects this will show you plus give you a good point to start changing code. This is done by going to File -> New Project -> Samples -> FRC Java -> then choose a project.

Also check out the below links.

Java CookBook
WPI Robotics Library Users Guide
Java Getting started
Brad Miller Videos
Java doc

If you need some detail help you can always PM me.

Ok so I have read over all those documents and followed the getting started with Java guide to put together some code using simple robot template.

here is my code:

package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;

public class TankDrive extends SimpleRobot {

RobotDrive drive = new RobotDrive(1,2,3,4);
Joystick leftStick = new Joystick(1);
Joystick rightStick = new Joystick(2);

public void operatorControl()
{
while(isOperatorControl() && isEnabled())

{

drive.tankDrive(leftStick, rightStick);
}
}
}

I basically just copied the quick start guide…

Some questions:

  1. is this code even going to do anything?
  2. when you create the joystick objects and put (1) and (2) for ports, how does the computer know that joysticks are connected? Same for the motors, is just putting 1,2,3,4 for the 4 motors going to work? (I guess I just don’t fully understand what ports we are connecting the motors and the joysticks to)
  3. I think our team is planning on using the command based template when we code for the full robot, how do I implement this type of thing using the subsystem, command base, OI, and all of the different java files that go along with it? (It seems like the different parts need to be split up in order to work properly)
  1. is this code even going to do anything? I am going to say yes. seem like no errors.
  2. when you create the joystick objects and put (1) and (2) for ports, how does the computer know that joysticks are connected? You can see the Joystick and what port they are connected to by looking at the driverstation.
    Same for the motors, is just putting 1,2,3,4 for the 4 motors going to work?Yes, this will work. These will be the PWM of the digtal side card. I would suggest doing this tho Jaguar LeftFront = new Jaguar(1); …
  3. I think our team is planning on using the command based template when we code for the full robot, how do I implement this type of thing using the subsystem, command base, OI, and all of the different java files that go along with it? I would watch the videos i linked.

Thanks! So to deploy this to the robot I would connect the computer to the cRIO and then hit the “run” button? Will operator control be enabled automatically?

Everything looks right assuming you have 4 motors plugged to PWM 1,2,3,4 respectively to what RobotDrive method wants.

And like they said, you can always check and drag joysticks in the Driver Station.

Are ports 1/2 for the left motors and 3/4 for the right? Just making sure we set it up correctly

(Front Left, Rear Left, Front Right, Rear Right) is the order. So, yes, that is correct.

ok great, thanks

No, you have to click enable in the Driver Station. This is for safety reasons, among other things.

Code for my test DriveTrain (although it has an arcade drive method as well.)


package edu.wpi.first.wpilibj.test;


import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.RobotDrive;

/**
 *
 * @author Gus Michel
 */
public class DriveTrain {
    private int LEFT_FRONT_JAG_PORT = 1;
    private int LEFT_BACK_JAG_PORT = 2;
    private int RIGHT_FRONT_JAG_PORT = 3;
    private int RIGHT_BACK_JAG_PORT = 4;
    private static DriveTrain instance = null;
    private Jaguar leftFront;
    private Jaguar leftBack;
    private Jaguar rightFront;
    private Jaguar rightBack;
    private RobotDrive robotDrive;
    
    private DriveTrain() {
        leftFront = new Jaguar(LEFT_FRONT_JAG_PORT);
        leftBack = new Jaguar(LEFT_BACK_JAG_PORT);
        rightFront = new Jaguar(RIGHT_FRONT_JAG_PORT);
        rightBack = new Jaguar(RIGHT_BACK_JAG_PORT);
        robotDrive = new RobotDrive(leftFront, leftBack, rightFront, rightBack);
    }
    
    public static DriveTrain getInstance() {
        if(instance == null) {
            instance = new DriveTrain();
        }
        return instance;
    }
    
    public void drive(double throttle, double turn) {
        robotDrive.drive(throttle, turn);
    }
    
    public void arcadeDrive(double throttle, double turn) {
        robotDrive.arcadeDrive(throttle, turn);
    }
    
    public void tankDrive(double left, double right) {
        robotDrive.tankDrive(left, right);
    }
    
    public double getLeftFrontSpeed() {
        return leftFront.getSpeed();
    }
    
    public double getLeftBackSpeed() {
        return leftBack.getSpeed();
    }
    
    public double getRightFrontSpeed() {
        return rightFront.getSpeed();
    }
    
    public double getRightBackSpeed() {
        return rightBack.getSpeed();
    }
    
    public void stop() {
        robotDrive.drive(0,0);
    }
}

Ok so now that I think that the code I wrote will work, I want to put it in the command based template. I watched the videos that Brad Miller posted and I know how the template works but am not sure how to set it up to make the tank drive work.

Take a look at the GearsBot Robot sample provided. (New Project -> Samples -> FRC Java -> GearsBot)

Interestingly enough, it’s actually the same code from the video. :cool:

Just make sure you comment out this line in CommandBase.java:

Subsystem.registerDefaultCommands();
  • offtopic edit -
    I see you’re from Cinci. Looking forward to competing with you guys at Queen City this year. :wink:

I think we actually might be traveling to purdue for our regional competition. The queen city regional is on easter weekend and we are a christian school so that wouldn’t go so well.

And I did look at the gearsbot code and I wasn’t sure if we need to set it up the same way because we aren’t coding it to go a certain distance. Would I still set up the drivetrain as a PID subsystem?

I think I all I need to do is add a subsystem, command, and modify both the OI and the RobotMap and I should be good. I’ll try it out later and see what I come up with.

Well, if you have 4 motors, that should be correct. But if you only have 2 motors (one on the left and one on the right), it’ll be RobotDrive(1,2); – where 1 is the left motor and 2 is the right motor.

Yep we have four motors.