Log in

View Full Version : Java help


Sk84d2
16-10-2016, 11:58
I am part of team 4592 and I took on the task of being programming captain. I don't know much and i need some help. I need to run a motor while a button is being pressed and held. I've gone through wpilib and can't figure it out. Thanks to anyone who can help it is greatly appreciated.

euhlmann
16-10-2016, 13:42
WPILib resources:
Screensteps (http://wpilib.screenstepslive.com/s/4485)
API Reference (Java) (http://first.wpi.edu/FRC/roborio/release/docs/java/)
API Reference (C++) (http://first.wpi.edu/FRC/roborio/stable/docs/cpp/)

Using the command based architecture, you need to first create a subsystem that the motor belongs to

public class MyMotorSubsystem extends Subsystem {
CANSomething myMotor;
public MyMotorSubsystem {
myMotor = new CANSomething(RobotMap.CAN_ID_MY_MOTOR);
}
public void setMotor(double value) {
myMotor.set(value);
}
}

Then you need a command to run the motor.

public class CommandBase extends Command {
public static MyMotorSubsystem myMotorSubsystem;
public static void init() {
myMotorSubsystem = new MyMotorSubsystem();
}
}

public class RunMotorCommand extends CommandBase {
JoystickButton runMotorButton;
public RunMotorCommand(JoystickButton runMotorButton) {
this.runMotorButton = runMotorButton;
runMotorButton.whenPressed(this);
}
void initialize() {
requires(myMotorSubsystem);
}
void run() {
myMotorSubsystem->setMotor(1.0);
}
void isFinished() {
return runMotorButton.get();
}
void end() {
myMotorSubsystem->setMotor(0.0);
}
void interrupted() {
myMotorSubsystem->setMotor(0.0);
}
}

Then you need to create an instance of the command

// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));

Lireal
17-10-2016, 00:35
Here is what the above would look like in Java:


public class MyMotorSubsystem extends Subsystem {
ControllerType myMotor;
public MyMotorSubsystem () {
myMotor = new ControllerType(RobotMap.ID_MY_MOTOR);
}
public void setMotor(double value) {
myMotor.set(value);
}
}

Command to run the motor.

public class CommandBase extends Command {
public static MyMotorSubsystem myMotorSubsystem;
public static void init() {
myMotorSubsystem = new MyMotorSubsystem();
}
}

public class RunMotorCommand extends CommandBase {
JoystickButton runMotorButton;
public RunMotorCommand(JoystickButton runMotorButton) {
this.runMotorButton = runMotorButton;
runMotorButton.whenPressed(this);
}
void initialize() {
requires(myMotorSubsystem);
}
void execute() {
myMotorSubsystem.setMotor(1.0);
}
boolean isFinished() {
return !runMotorButton.get();
}
void end() {
myMotorSubsystem.setMotor(0.0);
}
void interrupted() {
myMotorSubsystem.setMotor(0.0);
}
}

Create an instance of the command

// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));


My team does not use the CommandBase, and we usually instantiate subsystems in the Robot.java file, so I am not 100% sure this is right, but the basic idea should be apparent. Basically, create a command that in the execute will call a method from a subsystem that sets the motor to the value you want, which is triggered by a button and will end when that button isn't pressed.

bankst
19-10-2016, 20:10
2016 was our first year with Java too, and being the sole programmer, I kinda got thrown into the fire. Here's our code from 2016 (https://github.com/oscarrobotics/FRC2016-Leatherback). Its mostly very simple command-based stuff, but if you have any questions you can PM me. Hope that helps!