Adjust Motor Speed with JoyStick Slider

Hi everyone, I once again apologize for the probably simple question, however I couldn’t find any related resources online.

My team and I utilize Thrustmaster T.16000M JoySticks to control our Robot with Tank Drive.
With this, we obviously have button bindings for our shooter, intake, so on and so forth.

I was wondering if there was a specific way I could adjust the speed of the intake (live) with the sliders provided on the JoyStick. As I understand with some preliminary research the sliders are considered an axis. I know I have to fetch that number as a double but I’m unsure how to make it pass through and control the motor speed live.

Intake Command

public class IntakeManipulator extends CommandBase {
    private final Intake intake;

    public IntakeManipulator(Intake intake) {
        this.intake = intake;
        // each subsystem used by the command must be passed into the
        // addRequirements() method (which takes a vararg of Subsystem)
        addRequirements(this.intake);
    }

    /**
     * The initial subroutine of a command.  Called once when the command is initially scheduled.
     */
    @Override
    public void initialize() {

    }
    @Override
    public void execute() {
        SmartDashboard.putString("Intake Enabled", "yes");
        intake.intakeBalls();
    }
    @Override
    public boolean isFinished() {
        // TODO: Make this return true when this Command no longer needs to run execute()
        return false;
    }
    @Override
    public void end(boolean interrupted) {
        SmartDashboard.putString("Intake Enabled", "no");
        intake.stopMotor();
    }
}

Intake Subsystem

public class Intake extends SubsystemBase {
    private final VictorSPX intake_motor;

    public Intake() {
        intake_motor = new VictorSPX(Constants.INTAKE_MOTOR_PORT);

        intake_motor.clearStickyFaults();
        intake_motor.setInverted(InvertType.None);

        intake_motor.configFactoryDefault();
    }
    public synchronized void intakeBalls() {
        intake_motor.set(ControlMode.PercentOutput, 1);
    }

    public synchronized void stopMotor() {
        intake_motor.set(ControlMode.PercentOutput, 0);
    }
    @Override
    public void periodic() {
        // This method will be called once per scheduler run
    }

    @Override
    public void simulationPeriodic() {
        // This method will be called once per scheduler run during simulation
    }
}

Initiation of command

    putBallsIn.whileHeld(new IntakeManipulator(intake));

So with that I’m not exactly sure how to pass the data to the joystick all the way through to the place were the motor speed is set, and live.

Any help would be appreicated.

One way to do this is by giving intakeBalls() a parameter double speed then set the motor speed using speed by running intake_motor.set(ControlMode.PercentOutput, a_speed). Then, in execute() in the IntakeManipulator command, when you call intake.intakeBalls() you can pass in the joystick axis.

Basically, you should use the joystick axis in the command.

The way my team usually handles these kinds of things, is by having a method in the intake subsystem that looks like:

public void setIntakeMotor(double speed) {
  intakeMotor.set(ControlMode.PercentOutput, speed);
}

Then a command collectCargo where the execute method looks like

public void execute() {
  intake.setIntakeMotor(joystick.getAxis);
}

and an end method that looks like

public void end(boolean interrupted) {
  intake.setIntakeMotor(0);
}

My problem is I don’t know how to get the joystick into the execute() function there. I don’t want to initialize it twice, I already have it initialized in the RobotContainer.

What my team does is we create a DoubleSupplier object as an instance variable and a parameter in the command’s constructor. When we construct the command in the robot container, we put this pass the slider into the command’s constructor

this example uses booleansuppliers but the concept is the same:

in the robot container:
climb = new Climb(climber, () -> controller.getLeftBumper(), () -> controller.getRightBumper());

Command:

public class Climb extends CommandBase {

    public Climber climber;

    public BooleanSupplier leftBumper;

    public BooleanSupplier rightBumper;

    public boolean finished;

    public Climb(Climber c, BooleanSupplier lB, BooleanSupplier rB) {

        climber = c;

        leftBumper = lB;

        rightBumper = rB;

        addRequirements(climber);

    }

    @Override

    public void initialize() {

    }

    @Override

    public void execute() {

        if (leftBumper.getAsBoolean()) {

            climber.up();

        } else if (rightBumper.getAsBoolean()) {

            climber.down();

        } else {

            climber.off();

        }

    }

You can pass in the joystick object to the command like you do with the intake subsystem, or you can make the joystick public and use RobotContainer.joystick.getAxis.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.