Motors twitch when calling a certain command

I have a shoot command for our shooter. Its mapped to the X button. When I hit the x button, the motors just twitch. This would make me think there is a different command that’s requiring the shooter subsystem that’s currently running, but there isn’t. I have two shoot commands, but only one is done and its the only one that has the shooter subsystem declared as a requirement. I’ve looked all over the code trying to find somewhere in which the motors are set to a different value than what the command is saying, but I cannot find it. My command is as follows, where startShooter and startFeeder are just methods to set both motors to full power inside of the shooter subsystem.

public void shootOne(){ startShooter(); startFeeder(); new WaitCommand(2); stopShooter(); stopFeeder(); }

The command im trying to use is an “InstantCommand”, but I have tried using both types of commands. the “shootOne()” method is currently being called in the initialize method of the InstantCommand. I’m typing this in school because I’d like to have an answer for tonight, so if you can’t understand anything I say or need more code please just ask and I’ll respond ASAP. Every other command works fine.

Instantiating a WaitCommand() does not perform any action. You need to schedule the command for it to do anything. Commands are not subroutines. Additionally, scheduling a WaitCommand on its own will not do anything, regardless; it needs to be part of a command group to be of any use.

I suggest reading the command-based documentation to get a better idea of how the command-based framework is supposed to work. The action you are attempting to perform would probably be expressed as a sequential command group.

The command is scheduled every time I hit the X button. Do the motors twitch BECAUSE of the wait command? Does it immediately set them and then set them back to zero because of my wait command thing not actually doing anything?

The code you’ve posted isn’t a command; it’s just a subroutine. Calling that subroutine is not the same as scheduling a command.

When you call that subroutine, it will turn on the motors, instantiate a WaitCommand, and turn off the motors. A “twitch” sounds about right for what that’d cause in terms of robot behavior.

You’re probably intending to do something more like this:

new RunCommand(() -> {
    startShooter();
    startFeeder();
  }).withTimeout(2)
  .andThen(() -> {
    stopShooter();
    stopFeeder();
  }).schedule();

The difference here is that a RunCommand will run until you stop it, so startShooter() and startFeeder() will get called for each period, keeping motor safety from stopping the motor. Next, we use withTimeout() so the command will be interrupted in 2 seconds. Finally, we stop the feeder and stop the shooter using andThen().
The final missing piece is that you need to schedule this command group with schedule().

1 Like

Could you explain more where I’d implement this? I’m pretty unfamiliar to lambdas in Java. Would it go in my command class?

Again, I suggest reading through the command-based documentation (linked above). It covers the recommended project structure, and will answer your questions.

1 Like

I agree with @Oblarg, you’ll want to read the docs! I linked to several topics in my response, but here is the main topic you’ll want to read through:
https://frc-docs.readthedocs.io/en/latest/docs/software/commandbased/index.html

To answer the question directly, the most straight-forward thing to do is put this in RobotContainer.configureButtonBindings(). I gets pretty long so you probably want to give the structure some thought. Something like this would probably work:

new JoystickButton(driverController, XboxController.Button.kX.value)
  .whenPressed(new RunCommand(() -> {
      powerCellSubsystem.startShooter();
      powerCellSubsystem.startFeeder();
    }).withTimeout(2)
    .andThen(() -> {
      powerCellSubsystem.stopShooter();
      powerCellSubsystem.stopFeeder();
    }));

In this case you don’t call schedule() because the command you constructed will be scheduled when the button is pressed.

1 Like

Thank you both. I’ve read through the entire doc, just some parts of it didn’t make sense to me, but you’ve both done a good job showing how the things are used rather than describing what they do.

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