View Single Post
  #2   Spotlight this post!  
Unread 12-13-2016, 12:25 PM
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 298
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Need help understanding command-based Robots

Command: Basically some action that you can define. This might be an autonomous mode action, or a teleop action, or something you use in both modes. initialize() sets up the action to happen, then execute() is repeatedly called until isFinished() is true, then end() is called to clean up.
Example:
Code:
public class MoveToPositionCommand extends Command {
    private int encoderTicksToDrive;
    
    public MoveToPositionCommand(int encoderTicksToDrive) {
        this.encoderTicksToDrive = encoderTicksToDrive;
        require(driveSubsystem);
    }
    
    public void initialize() {
        driveSubsystem.resetEncoders();
    }

    public void execute() {
        int leftEncoder = driveSubsystem.getLeftEncoder();
        int rightEncoder = driveSubsystem.getRightEncoder();
        double turn = DriveSubsystem.SOME_TURN_CONSTANT * (rightEncoder - leftEncoder);
        double throttle = DriveSubsystem.AUTO_DRIVE_SPEED;
        driveSubsystem.drive(throttle, turn);
    }

    public boolean isFinished() {
        return driveSubsystem.getLeftEncoder() > encoderTicksToDrive || driveSubsystem.getRightEncoder() > encoderTicksToDrive;
    }

    public void end() {
        driveSubsystem.drive(0, 0);
    }
}
Subsystem: Exactly what it sounds like. A virtual representation of a robot subsystem. Subsystems have WPILib objects and define methods that commands can use
Example:
Code:
public class DriveSubsystem {
    public static final double SOME_CONSTANT = 42;

    CANTalon leftDrive, rightDrive;
    Encoder leftEncoder, rightEncoder;

    public DriveSubsystem() {
        // initialize objects
    }

    public void drive(double throttle, double turn) {
        // do the driving
    }

    // other methods that commands need, etc
}
Edit: oops I just realized I posted Java in the C++ forum. It shouldn't be too difficult to translate though
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org

Last edited by euhlmann : 12-13-2016 at 07:20 PM.
Reply With Quote