Can someone explain to me how we can group two motors together and get them run together with a button.It would be a big help it’s been a bit difficult for me to do so any help would be amazing because the docs haven’t really helped me
Okay, are these in the same subsystem or different subsystems?
If they are in the same subsystem and you want them to move exactly the same, use follow() to make one follow the other.
If they are in separate subsystems, you can bind both commands to the same Trigger in configureButtonBindings() or you can declare a SequentialCommandGroup.
The follow will be in your subsystem and you will have a method that runs the motors. The button is separate and is in RobotContainer where you will declare something like: controller.button().whileTrue( new RunCommand(() -> subsystem.method(), subsystem));
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkLowLevel.MotorType;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class Subsystem extends SubsystemBase {
private final CANSparkMax one;
private final CANSparkMax two;
/** Creates a new subsystem. */
public Subsystem() {
one = new CANSparkMax(1, MotorType.kBrushless);
two = new CANSparkMax(2, MotorType.kBrushless);
two.follow(one);
}
public void run() {
one.set(0.2);
}
}
RobotContainer:
import frc.robot.Constants.OperatorConstants;
import frc.robot.subsystems.Subsystem;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
public class RobotContainer {
// The robot's subsystems and commands are defined here...
private final Subsystem m_subsystem = new Subsystem();
// Replace with CommandPS4Controller or CommandJoystick if needed
private final CommandXboxController m_driverController =
new CommandXboxController(OperatorConstants.kDriverControllerPort);
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
// Configure the trigger bindings
configureBindings();
}
private void configureBindings() {
m_driverController.x().onTrue(
m_subsystem.runOnce(m_subsystem.run()));
}
}
You should use subsystem.runOnce(…) instead of new InstantCommand(…, subsystem). Also, a whileTrue binding of an InstantCommand is equivalent to (the simpler, better-named in this case) onTrue.
We are trying to use the code provided but when we get to the run at the very end in robot container it’s telling us that it dosent exist in robot container. If you could help us fix this it would be amazing
Sorry about that, bad method name on my part. run() is a method to run a runnable action and that is what is happening here. Replace that with whatever your method is called.