Need help (First time using Java)

Hello everyone,

We have completed our build, but are now stuck without an operational robot. We are attempting to use Java this time around since our attempt to copy/paste last year’s code in Labview did not go as planned. Is anyone on here able to help us get it running? We asked a few other schools, but understandably, our schedules have not worked out. We have attempted a few of the tutorials, but have run into a few issues.

In case this is needed, our robot is wired up this way:

  • 4 drive motors y-cabled into PMW 0 & 1 (CIMS with Victor SPX’s)
  • 2 arm motors y-cabled into PMW 2 (Mini-CIMS with Victor SPX’s)
  • 1 motor intake into PMW 3 (BAG Motor with Victor SPX)
  • 2 Microsoft Lifecams

Please let us know if anyone could help, or even provide a sample code that would work for us. Thank you!

This is an example for tank drive: allwpilib/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/tankdrive at main · wpilibsuite/allwpilib · GitHub

This will help you move the arm/intake with a joystick: allwpilib/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/motorcontrol/Robot.java at main · wpilibsuite/allwpilib · GitHub

This will help you get the lifecams streaming so you can see them on your dashboard: Using the CameraServer on the roboRIO — FIRST Robotics Competition documentation

If you are in a very tight time crunch let us know and we can give more specific direction, and feel free to ask questions! If you have a github that would also be helpful.

1 Like

Thank you!

We will go through this today and let you know how it goes.

Howdy! So @MolokaiRobotics got me a mentors phone number and offline we debugged a few things:

  1. Make sure you set your team number after imaging the RIO
  2. VictorSP is different than PWMVictorSPX
  3. Remember to tell your motors to stop when no buttons are being pressed

Good luck this season yall!

1 Like

Thank you so much! This is what makes the FIRST community so great!

2 Likes

Hello all,

Trying to get our autonomous working, but running into issues. Can anyone tell us what we’re doing wrong? We were using if and elseif statements but get error messages.

package frc.robot;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.MotorController;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoSink;
import edu.wpi.first.cscore.VideoSource.ConnectionStrategy;
/*

  • import edu.wpi.first.wpilibj.Timer;
    */

/**

This is a demo program showing the use of the DifferentialDrive class, specifically it contains
the code necessary to operate a robot with tank drive.
*/
public class Robot extends TimedRobot {
private DifferentialDrive m_myRobot;
private Joystick m_DriverJoystick;
private final MotorController m_leftMotor = new PWMVictorSPX(3);
private final MotorController m_rightMotor = new PWMVictorSPX(5);
private final MotorController m_leftMotor2 = new PWMVictorSPX(4);
private final MotorController m_rightMotor2 = new PWMVictorSPX(6);

private final MotorController m_armMotor = new PWMVictorSPX(7);
private final MotorController m_armMotor2 = new PWMVictorSPX(8);
private final MotorController m_intakeMotor = new PWMVictorSPX(9);

UsbCamera camera1;
UsbCamera camera0;
VideoSink server;
Timer timer;

@Override
public void robotInit() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot’s
// gearbox is constructed, you might have to invert the left side instead.
CameraServer.startAutomaticCapture();
camera1 = CameraServer.startAutomaticCapture(1);
camera0 = CameraServer.startAutomaticCapture(0);
server = CameraServer.getServer();

camera1.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
camera0.setConnectionStrategy(ConnectionStrategy.kKeepOpen);

m_leftMotor.setInverted(true);
m_leftMotor2.setInverted(true);

MotorControllerGroup rightControllerGroup = new MotorControllerGroup(m_rightMotor,m_rightMotor2);
MotorControllerGroup leftControllerGroup = new MotorControllerGroup(m_leftMotor,m_leftMotor2);
m_myRobot = new DifferentialDrive(leftControllerGroup, rightControllerGroup);
m_DriverJoystick = new Joystick(0);

}

@Override

public void autonomousInit() {

  timer.reset();

  timer.start();

}
@Override
public void autonomousPeriodic() {

//lift arm up 100 degrees
if (timer.get() < 2.0); {
m_armMotor.set(0.5);
m_armMotor2.set(0.5);
} else{
m_armMotor.set(0); // stop robot arm
m_armMotor2.set(0);}
}

@Override
public void teleopPeriodic() {

m_myRobot.tankDrive(-m_DriverJoystick.getRawAxis(1) * 0.75, -m_DriverJoystick.getRawAxis(3) * 0.75);

if(m_DriverJoystick.getRawButton(6)){
  m_armMotor.set(0.5);
  m_armMotor2.set(0.5);
} else if(m_DriverJoystick.getRawButton(8)) {
  m_armMotor.set(-0.35);
  m_armMotor2.set(-0.35);
} else {
  m_armMotor.set(0);
  m_armMotor2.set(0);
}

if(m_DriverJoystick.getRawButton(5)){
  m_intakeMotor.set(0.75);
} else if(m_DriverJoystick.getRawButton(7)) {
  m_intakeMotor.set(-0.75);
} else {
  m_intakeMotor.set(0);
}

}
}

I think your only issue was a rogue ; on line 78, this built for me. In the future you can wrap your code in a code block to make it keep nice formatting on CD.

package frc.robot;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.MotorController;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoSink;
import edu.wpi.first.cscore.VideoSource.ConnectionStrategy;

/*

import edu.wpi.first.wpilibj.Timer;
*/
/**
 * 
 * This is a demo program showing the use of the DifferentialDrive class,
 * specifically it contains
 * the code necessary to operate a robot with tank drive.
 */
public class Robot extends TimedRobot {
  private DifferentialDrive m_myRobot;
  private Joystick m_DriverJoystick;
  private final MotorController m_leftMotor = new PWMVictorSPX(3);
  private final MotorController m_rightMotor = new PWMVictorSPX(5);
  private final MotorController m_leftMotor2 = new PWMVictorSPX(4);
  private final MotorController m_rightMotor2 = new PWMVictorSPX(6);

  private final MotorController m_armMotor = new PWMVictorSPX(7);
  private final MotorController m_armMotor2 = new PWMVictorSPX(8);
  private final MotorController m_intakeMotor = new PWMVictorSPX(9);

  UsbCamera camera1;
  UsbCamera camera0;
  VideoSink server;
  Timer timer;

  @Override
  public void robotInit() {
    // We need to invert one side of the drivetrain so that positive voltages
    // result in both sides moving forward. Depending on how your robot’s
    // gearbox is constructed, you might have to invert the left side instead.
    CameraServer.startAutomaticCapture();
    camera1 = CameraServer.startAutomaticCapture(1);
    camera0 = CameraServer.startAutomaticCapture(0);
    server = CameraServer.getServer();

    camera1.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
    camera0.setConnectionStrategy(ConnectionStrategy.kKeepOpen);

    m_leftMotor.setInverted(true);
    m_leftMotor2.setInverted(true);

    MotorControllerGroup rightControllerGroup = new MotorControllerGroup(m_rightMotor, m_rightMotor2);
    MotorControllerGroup leftControllerGroup = new MotorControllerGroup(m_leftMotor, m_leftMotor2);
    m_myRobot = new DifferentialDrive(leftControllerGroup, rightControllerGroup);
    m_DriverJoystick = new Joystick(0);

  }

  @Override

  public void autonomousInit() {

    timer.reset();

    timer.start();
  }

@Override
public void autonomousPeriodic() {

//lift arm up 100 degrees
if (timer.get() < 2.0) {
m_armMotor.set(0.5);
m_armMotor2.set(0.5);
} else{
m_armMotor.set(0); // stop robot arm
m_armMotor2.set(0);}
}

  @Override
  public void teleopPeriodic() {

    m_myRobot.tankDrive(-m_DriverJoystick.getRawAxis(1) * 0.75, -m_DriverJoystick.getRawAxis(3) * 0.75);

    if (m_DriverJoystick.getRawButton(6)) {
      m_armMotor.set(0.5);
      m_armMotor2.set(0.5);
    } else if (m_DriverJoystick.getRawButton(8)) {
      m_armMotor.set(-0.35);
      m_armMotor2.set(-0.35);
    } else {
      m_armMotor.set(0);
      m_armMotor2.set(0);
    }

    if (m_DriverJoystick.getRawButton(5)) {
      m_intakeMotor.set(0.75);
    } else if (m_DriverJoystick.getRawButton(7)) {
      m_intakeMotor.set(-0.75);
    } else {
      m_intakeMotor.set(0);
    }
  }
}

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.