Problems with Java in Eclipse **please help!**

Hi I’m on team #5464, we recently changed from C++ to Java. We need help writing a starting code to make 4 motors move. You can see our code with the link below.

Thank you for your time
Bluejackets #5464

One of the nice things about the FIRST Robotics Library (WPILib) is that you don’t need to control all four motors individually!

Take a look at your “robotDrive41” object. It has two methods that you probably care about, arcadeDrive and tankDrive. It’s your choice which one to use.

For the purposes of this, I’ll use arcade drive in examples, but substitute as necessary.

If you haven’t yet, add a joystick in RobotBuilder. You can find it under the “OI” header in RobotBuilder. Drag it under Operator Interface. Alternatively, right click Operator Interface and add the joystick.

In the subsystem class, you can have a method that looks something like this:

public void arcadeDrive(double moveValue, double rotateValue){
  robotDrive41.arcadeDrive(moveValue, rotateValue);

For commands, you only need to have one command that checks the value of the joystick(s) and calls the method you just created in the subsystem.

Before you create the command, you’ll want to change a few things (some of these don’t change all that well after you create the command and export to Java):


The first thing that’s different is the “Requires” line. This line tells the robot that this command is the only command that can use the Drive subsystem when it’s running. You’ll see why it’s really useful in a little bit.

Second is unchecking the “Button on SmartDashboard”. It makes no sense to have a button for checking the joysticks.

Third is the parameter, speed. To add a parameter, click on the grey box and then “Add Parameter” in the popup. Make sure that you click enter after changing the name of the parameter. The Type should be double. Click save and close. This speed parameter will be useful later.

public class DriveCommand extends Command{
  public void execute(){
    double move= Robot.oi.getJoystick().getX();
    double rotate= -Robot.oi.getJoystick().getY();

I’m going to insert a few notes in here. First, the getY might need to be negated because of an oddity in how joysticks are designed. Secondly, some joysticks are a bit off and the axes may return a (small) value even when they aren’t being touched. The following two lines fix that:

if(Math.abs(move)<.1) move = .0;
if(Math.abs(rotate)<.1) rotate=0;

Lastly, remember the speed parameter? What we’ll do is multiply both values by the speed parameter before using the values. So if speed is 1, moving the joystick all the way will make the robot move at full speed. If speed is .5, moving the joystick all the way will make the robot move at half speed. This is really useful for fine maneuvers and lining up.

move *= m_speed;
rotate *= m_speed;, rotate);

Now we have the command set up, but it won’t actually run. We need to tell the program that unless something unusual is happening, we want to keep running that drive command. To do this, go into the Drive Subsystem (in Robot Builder), and set the default command to be DriveCommand. Since normally you want the bot to be full speed, set the speed parameter to 1 in Default command parameters.

Make sure to press enter before clicking “Save and close”. It will not save otherwise. If unsure, re-open the “Edit parameters” popup to check that the value is still 1.


Lastly, let’s make it so that if you hold down a button, the robot goes at half speed (note that depending on how your robot is mechanically, you may want .6 or .7 speed in the program to actually go half speed. Experiment until it’s how you like it).

Drag a button under the joystick (it’s under the OI header in RobotBuilder, or you can right click on the joystick “folder” and add the button there).

Name the button something like “SlowDownButton”. Set which joystick and button you want it to be bound to. The button should trigger the DriveCommand again, but with a slower speed. Again, when you set the speed, make sure to click enter and all of that. Leave the “When to Run” on whileHeld:

Export everything, save, and deploy. Reply if you have any questions or issues. Good luck in competition!

Thank you for your help!
Good luck in competition!