I have this logitech gamepad and would like to be able to control the direction and speed of a motorcontroller via one of its joysticks.
I’m following the 2020 FRC programming examples that FRC made available on Github, which detail how to work with the new 2020 Command, RobotContainer, Subsystem, etc paradigm. I am able to press the controller’s L1 button and get my single motor controller (defined in my DriveTrain
subsystem) to move.
- How can I retrieve the gamepad’s joystick values (x and y axes)? I’m able to retrieve button presses in the RobotContainer, but how do I access the values of the joysticks?:
private void configureButtonBindings() {
//selecting the gamepad's L1 button
final JoystickButton rightJoystickOnGamepad = new JoystickButton(m_joystick, 5);
//when pressed, trigger command:
rightJoystickOnGamepad.whenHeld(new TankDrive(m_drivetrain));
}
- How can I pass the values of the speed dynamically? I currently have these values hard-coded in my
TankDrive
command because the new FRC 2020 examples show the need to instantiate a new instance of the Command when using it (as shown above). MyTankDrive
class looks like this:
public class TankDrive extends CommandBase {
private DriveTrain m_drivetrain;
/**
* Creates a new TankDrive.
*/
public TankDrive(DriveTrain driveTrain) {
// Use addRequirements() here to declare subsystem dependencies.
m_drivetrain = driveTrain;
addRequirements(m_drivetrain);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
//How does one pass the joystick value here? There has to be a better way.
m_drivetrain.drive(1.0);
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
System.out.println("Stopping");
m_drivetrain.drive(0.0);
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
Below is my subsystem:
public class DriveTrain extends SubsystemBase {
//Motorcontroller attached to PWM port 0
private static final Victor victorMotorController = new Victor(0);
public void drive(double joystickValue) {
//set the speed/direction of the motorcontroller
System.out.println("joystick value is: " + joystickValue);
victorMotorController.set(joystickValue);
}
/**
* Creates a new DriveTrain.
*/
public DriveTrain() {
super();
}
@Override
public void periodic() {
// This method will be called once per scheduler run
}
}
Any help would be greatly appreciated!