so this code that used to work to make our tank drive move now only makes it turn. I have been working on this for days and I’m not sure if I’m missing something obvious or if something changed recently with the differentialdrive
class or something. Thanks for the help!
RobotContainer:
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.commands.Drive;
import frc.robot.subsystems.DriveTrain;
/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
* "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
* periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
* subsystems, commands, and button mappings) should be declared here.
*/
public class RobotContainer {
// The robot's subsystems and commands are defined here...
DriveTrain driveTrain = new DriveTrain();
Drive drive;
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
// Configure the button bindings
configureButtonBindings();
}
/**
* Use this method to define your button->command mappings. Buttons can be created by
* instantiating a {@link GenericHID} or one of its subclasses ({@link
* edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a {@link
* edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/
private void configureButtonBindings() {
XboxController controller = new XboxController(0);
drive = new Drive(driveTrain, () -> controller.getLeftY(), () -> controller.getRightX());
driveTrain.setDefaultCommand(drive);
}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
*
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
// An ExampleCommand will run in autonomous
return null;
}
}
drivetrain subsystem:
package frc.robot.subsystems;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class DriveTrain extends SubsystemBase {
public CANSparkMax leftLeader;
public CANSparkMax leftFollower;
public CANSparkMax rightLeader;
public CANSparkMax rightFollower;
MotorControllerGroup left;
MotorControllerGroup right;
public DifferentialDrive drive;
public DriveTrain() {
leftLeader = new CANSparkMax(1, MotorType.kBrushless);
leftFollower = new CANSparkMax(3, MotorType.kBrushless);
rightLeader = new CANSparkMax(2, MotorType.kBrushless);
rightFollower = new CANSparkMax(4, MotorType.kBrushless);
left = new MotorControllerGroup(leftLeader, leftFollower);
right = new MotorControllerGroup(rightLeader, rightFollower);
left.setInverted(true);
right.setInverted(false);
drive = new DifferentialDrive(left, right);
}
public void drive(double f, double t) {
drive.arcadeDrive(f, t);
}
}
drive command:
package frc.robot.commands;
import java.util.function.DoubleSupplier;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.subsystems.DriveTrain;
public class Drive extends CommandBase {
public DriveTrain driveTrain;
public DoubleSupplier forward;
public DoubleSupplier turn;
public boolean finished;
public Drive(DriveTrain d, DoubleSupplier f, DoubleSupplier t) {
driveTrain = d;
forward = f;
turn = t;
addRequirements(driveTrain);
}
@Override
public void execute() {
driveTrain.drive(forward.getAsDouble(), turn.getAsDouble());
}
@Override
public boolean isFinished(){
return finished;
}
}