|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Java help
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.
|
|
#2
|
||||
|
||||
|
Re: Java help
WPILib resources:
Screensteps API Reference (Java) API Reference (C++) Using the command based architecture, you need to first create a subsystem that the motor belongs to Code:
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);
}
}
Code:
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);
}
}
Code:
// somewhere in OI.java new RunMotorCommand(new JoystickButton(myJoystick, something)); |
|
#3
|
|||
|
|||
|
Re: Java help
Here is what the above would look like in Java:
Code:
public class MyMotorSubsystem extends Subsystem {
ControllerType myMotor;
public MyMotorSubsystem () {
myMotor = new ControllerType(RobotMap.ID_MY_MOTOR);
}
public void setMotor(double value) {
myMotor.set(value);
}
}
Code:
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);
}
}
Code:
// somewhere in OI.java new RunMotorCommand(new JoystickButton(myJoystick, something)); |
|
#4
|
||||
|
||||
|
Re: Java help
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. Its mostly very simple command-based stuff, but if you have any questions you can PM me. Hope that helps!
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|