Hello, we are brand new to programming in Java this year. We are following the cookbook from firstforge. We were able to move the robot using a joystick but cannot figure out how to use a button to enable a motor. We created a subsystem called shooter then a command called ShootBall. We get an error in out code and can’t figure out why. Our code is in the attachment.
Here is shooter:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.wpilibj.templates.subsystems;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.templates.commands.ShootBall;
/**
*
* @author Developer
*/
public class Shooter extends Subsystem {
Jaguar leftMotor, rightMotor;
// Put methods for controlling this subsystem
// here. Call these from Commands.
public void initDefaultCommand() {
setDefaultCommand(new ShootBall());
// Set the default command for a subsystem here.
//setDefaultCommand(new MySpecialCommand());
}
public Shooter(){
leftMotor = new Jaguar(3);
rightMotor = new Jaguar(4);
leftMotor.setSafetyEnabled(false);
rightMotor.setSafetyEnabled(false);
}
public void ShootBall(Button a){
a.whileHeld(new ShootBall());
}
}
And here is ShootBall:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.wpi.first.wpilibj.templates.commands;
/**
*
* @author Developer
*/
public class ShootBall extends CommandBase {
public ShootBall() {
requires(shooter);
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
}
// Called just before this Command runs the first time
protected void initialize() {
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
shooter.ShootBall(oi.getButton());
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return false;
}
// Called once after isFinished returns true
protected void end() {
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}
Here is OI in case you need it:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
/**
* This class is the glue that binds the controls on the physical operator
* interface to the commands and command groups that allow control of the robot.
*/
public class OI {
public static final int JOYSTICK_PORT = 1;
private Joystick stick;
private Button a;
public OI(){
stick = new Joystick(JOYSTICK_PORT);
a = new JoystickButton(stick, 1);
}
public Joystick getJoystick(){
return stick;
}
public Button getButton(){
return a;
}
//// CREATING BUTTONS
// One type of button is a joystick button which is any button on a joystick.
// You create one by telling it which joystick it's on and which button
// number it is.
// Joystick stick = new Joystick(port);
// Button button = new JoystickButton(stick, buttonNumber);
// Another type of button you can create is a DigitalIOButton, which is
// a button or switch hooked up to the cypress module. These are useful if
// you want to build a customized operator interface.
// Button button = new DigitalIOButton(1);
// There are a few additional built in buttons you can use. Additionally,
// by subclassing Button you can create custom triggers and bind those to
// commands the same as any other Button.
//// TRIGGERING COMMANDS WITH BUTTONS
// Once you have a button, it's trivial to bind it to a button in one of
// three ways:
// Start the command when the button is pressed and let it run the command
// until it is finished as determined by it's isFinished method.
// button.whenPressed(new ExampleCommand());
// Run the command while the button is being held down and interrupt it once
// the button is released.
// button.whileHeld(new ExampleCommand());
// Start the command when the button is released and let it run the command
// until it is finished as determined by it's isFinished method.
// button.whenReleased(new ExampleCommand());
}
Sorry if we posted things wrong. We didn’t know how to attach code