Programming in Autonomous

Hi, I’m wondering how to code a robot to run in autonomous just to move forward to the baseline. The code that the Robot Builder looks like a base code, I’m wondering what I have to add to it for it to work.

// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc6397.Robot6397v5.commands;
import edu.wpi.first.wpilibj.command.Command;
import org.usfirst.frc6397.Robot6397v5.Robot;

/**
 *
 */
public class AutonomousCommand extends Command {

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
 
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
    public AutonomousCommand() {

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING

        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
        requires(Robot.driveTrain);

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
}

Just call the command you used for driving in teleop for a certain amount of time.
Our team used mecanums for the entire drive base, so a simple command to use for mecanums is mecanumDrive_Polar(1 (magnitude: speed in a direction), 0 (angle: direction in degrees), 0 (rotation: speed of rotation independent of the other two))

How would I do that exactly? Can you show me an example?
Thanks

The following example assumes that your Robot.driveTrain subsystem has a method called arcadeDrive(throttle, steer) in it that can be used to set power to the drive motors and that you want to want to drive at a certain power for 5 seconds.

A TODO comment has been included in each section indicating that you will need to make adjustments based on your robot and the methods you have available in your subsystem.


public class AutonomousCommand extends Command {

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
 
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
    public AutonomousCommand() {

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING

        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
        requires(Robot.driveTrain);

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {

        // TODO: Change to your subsystem's method that moves the robot
        Robot.driveTrain.arcadeDrive(0.5, 0.0);

    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {

        // TODO: You will need to determine how long you want it to keep driving
        double driveTimeSecs = 5.0;
        return (timeSinceInitialized() >= driveTimeSecs);

    }

    // Called once after isFinished returns true
    protected void end() {

        // TODO: You will want to call the method in your subsystem that stops the drive
        Robot.driveTrain.arcadeDrive(0, 0);

    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
         end();
    }
}

Hope that helps, or at least gives you a starting point to work from.

You can do that or simple using a TimedCommand and skip the isFinished part and all. Just a little more comfortable in these situations.

Okay, thank you all for you help!

Sorry to ask again, but could you clarify if my DriveTrain DOES indeed have the method called arcadeDrive? I see it here, but I don’t know if it’s what I’m supposed to have. Thanks.

// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc6397.Robot6397v4.subsystems;

import org.usfirst.frc6397.Robot6397v4.RobotMap;
import org.usfirst.frc6397.Robot6397v4.commands.*;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.SpeedController;

import edu.wpi.first.wpilibj.command.Subsystem;


/**
 *
 */
public class DriveTrain extends Subsystem {

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS

    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    private final SpeedController speedController1 = RobotMap.driveTrainSpeedController1;
    private final SpeedController speedController2 = RobotMap.driveTrainSpeedController2;
    private final SpeedController speedController3 = RobotMap.driveTrainSpeedController3;
    private final SpeedController speedController4 = RobotMap.driveTrainSpeedController4;
    private final RobotDrive robotDrive4 = RobotMap.driveTrainRobotDrive4;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS


    // Put methods for controlling this subsystem
    // here. Call these from Commands.

    public void initDefaultCommand() {
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND

        setDefaultCommand(new DriveWithJoystick());

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND

        // Set the default command for a subsystem here.
        // setDefaultCommand(new MySpecialCommand());
    }
    
    public void takeJoystickInputs(Joystick DriverJoy) {
  		robotDrive4.arcadeDrive(DriverJoy);
  	}
      
    public void stop() {
      	robotDrive4.drive(0, 0);
      	
      
    }
}

Your drivetrain subsystem does not itself contain a method called arcadeDrive, however, robotDrive4 does a have method called arcadeDrive which is able to output to all of your drivetrain motor controllers.

You are currently using arcade drive with a joystick as input which is perfectly fine for teleop, however, it clearly won’t be the signature of arcade drive you will want to use, rather: arcadeDrive(double moveValue, double rotateValue) with
@param moveValue The value to use for fowards/backwards
@param rotateValue The value to use for the rotate right/left”

Either reference the robotDrive4 directly in your command or create a method in your drivetrain subsystem that inputs both moveValue and rotateValue for the arcade drive to a call of arcade drive on the robot drive.

How would I reference it?

Just like you do in the beginning of your drivetrain: RobotMap.driveTrainRobotDrive4

Okay, thank you for your help!