I uploaded this code and the second I press Enable for teleop the robot goes crazy and all the motors fire. This program is only made to straffe and go forwards/backwards.
Here it is:
package org.usfirst.frc.team4623.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
public XBox stick = new XBox(0);
SpeedController frontLeftMotor = new Jaguar(0);
SpeedController frontRightMotor = new Jaguar(1);
SpeedController rearLeftMotor = new Jaguar(2);
SpeedController rearRightMotor = new Jaguar(3);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
}
/**
* This function is run once each time the robot enters autonomous mode
*/
public void autonomousInit() {
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
}
/**
* This function is called once each time the robot enters tele-operated mode
*/
public void teleopInit(){
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
mecanumDrive(
(XBox.LEFTJOY_X+XBox.LEFTJOY_Y)/2,
(XBox.LEFTJOY_Y-XBox.LEFTJOY_X)/2,
-(XBox.LEFTJOY_Y-XBox.LEFTJOY_X)/2,
-(XBox.LEFTJOY_X+XBox.LEFTJOY_Y)/2);
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
LiveWindow.run();
}
public void mecanumDrive(double FL, double RL, double FR, double RR) {
frontLeftMotor.set(FL);
rearLeftMotor.set(RL);
frontRightMotor.set(FR);
rearRightMotor.set(RR);
}
}
It is pulling XBox stuff from this class:
package org.usfirst.frc.team4623.robot;
import edu.wpi.first.wpilibj.Joystick;
/**
*
* @author FrankyMonez
*/
public class XBox extends Joystick{
public XBox(int port) {
super(port);
}
public static final int
A_BUTTON = 1,
B_BUTTON = 2,
X_BUTTON = 3,
Y_BUTTON = 4,
START_BUTTON = 8,
BACK_BUTTON = 7,
LS_BUTTON = 9,
RS_BUTTON = 10,
RB_BUTTON = 6,
LB_BUTTON = 5,
LEFTJOY_Y = 2,
LEFTJOY_X = 1,
RIGHTJOY_Y = 5,
RIGHTJOY_X = 4,
LEFT_TRIGGER = 3,
RIGHT_TRIGGER = 3;
public boolean getButtonA() {
return getRawButton(A_BUTTON);
}
public boolean getButtonB() {
return getRawButton(B_BUTTON);
}
public boolean getButtonX() {
return getRawButton(X_BUTTON);
}
public boolean getButtonY() {
return getRawButton(Y_BUTTON);
}
public boolean getButtonRB() {
return getRawButton(RB_BUTTON);
}
public boolean getButtonLB() {
return getRawButton(LB_BUTTON);
}
public boolean getButtonRS() {
return getRawButton(RS_BUTTON);
}
public boolean getButtonLS() {
return getRawButton(LS_BUTTON);
}
public boolean getButtonStart() {
return getRawButton(START_BUTTON);
}
public boolean getButtonBack() {
return getRawButton(BACK_BUTTON);
}
public double getLeftJoyY() {
return getRawAxis(LEFTJOY_Y);
}
public double getLeftJoyX() {
return getRawAxis(LEFTJOY_X);
}
public double getRightJoyY() {
return getRawAxis(RIGHTJOY_Y);
}
public double getRightJoyX(){
return getRawAxis(RIGHTJOY_X);
}
public double getRightTrigger() {
return -Math.min(getRawAxis(RIGHT_TRIGGER), 0);
}
public double getLeftTrigger() {
return Math.max(getRawAxis(LEFT_TRIGGER), 0);
}
}
package org.usfirst.frc.team4623.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
public XBox stick = new XBox(0);
SpeedController frontLeftMotor = new Jaguar(0);
SpeedController frontRightMotor = new Jaguar(1);
SpeedController rearLeftMotor = new Jaguar(2);
SpeedController rearRightMotor = new Jaguar(3);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
}
/**
* This function is run once each time the robot enters autonomous mode
*/
public void autonomousInit() {
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
}
/**
* This function is called once each time the robot enters tele-operated mode
*/
public void teleopInit(){
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
mecanumDrive(
(stick.getLeftJoyX()+stick.getLeftJoyY())/2,
(stick.getLeftJoyY()-stick.getLeftJoyX())/2,
-(stick.getLeftJoyY()-stick.getLeftJoyX())/2,
-(stick.getLeftJoyX()+stick.getLeftJoyY())/2);
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
LiveWindow.run();
}
public void mecanumDrive(double FL, double RL, double FR, double RR) {
frontLeftMotor.set(FL);
rearLeftMotor.set(RL);
frontRightMotor.set(FR);
rearRightMotor.set(RR);
}
}
But now only the Y axis of the left joystick does anything and it only activates two motors. And then the left trigger activates two motors. Im so confused because I never even took the values for the left trigger. Is my mecanum code even right - like will it even ever work. And if not how do I program it to work?
package org.usfirst.frc.team4623.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
XBox stick = new XBox(0);
SpeedController frontLeftMotor = new Jaguar(0);
SpeedController frontRightMotor = new Jaguar(1);
SpeedController rearLeftMotor = new Jaguar(2);
SpeedController rearRightMotor = new Jaguar(3);
RobotDrive drive = new RobotDrive(0, 1, 2, 3);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
}
/**
* This function is run once each time the robot enters autonomous mode
*/
public void autonomousInit() {
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
}
/**
* This function is called once each time the robot enters tele-operated mode
*/
public void teleopInit(){
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
drive.mecanumDrive_Polar(stick.getLeftJoyY(), stick.getLeftJoyX(), 0);
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
LiveWindow.run();
}
public void mecanumDrive(double FL, double RL, double FR, double RR) {
frontLeftMotor.set(FL);
rearLeftMotor.set(RL);
frontRightMotor.set(FR);
rearRightMotor.set(RR);
}
}
What is most likely happening is that you are instantiating the SpeedControllers with PWM channels of 0,1,2 and 3 and then RobotDrive is attempting to do the same therefore throwing the exception.
RobotDrive has two constructors. One, which you are currently using in your code, takes in integers for the PWM channels of the motor controllers. The other takes in SpeedController objects for the motor controllers.
Both do so in this order:
frontLeftMotor
rearLeftMotor
frontRightMotor
rearRightMotor
If you want control over the motor controllers elsewhere I recommend passing those into the constructor rather than just the PWM channels