Lab 7 Adding a Command to the robot - MDHSRobotics/TeamWiki GitHub Wiki

Home / References / [Introduction to Programming - Part 2](Introduction to Programming - Part 2)

#Lab 7 - Adding a Command to the robot

###Objective In this lab, you will create a new Command for the robot.

###Prerequisite

  • Tote2016 project opened in Eclipse

###Steps

  1. Create a new Command class

    • Expand the src folder in the package explorer
    • Right click on the org.usfirst.frc.team4141.robot.commands package to bring up the context menu
    • Select the Class option from the new Menu
    • Enter a name for your Command class, e.g. MyCommand
    • Set the Superclass to org.usfirst.frc.team4141.MDRobotBase.MDCommand
    • click the Finish button
  2. Implement required methods

    • Hover over the class name, and select the auto fix option, this will add a constructor to the class.
  3. Override and implement the execute() method

     @Override
     protected void execute() {
     	super.execute();
     	int count = getRobot().getSubsystems().get("mySubsystem").getConfigSettings().get("setting").getInt();
     	for(int i=0;i<count;i++){
     		log("execute", "MyCommand executed");
     	}
     }
    
  4. Configure the OI to use the newly created command. Update the configureOI() method of the OI class to be the following:

     public void configureOI(){
     	//A robot should have 1 or more operator interfaces (OI)
     	//Typically, a robot will have at least 1 joystick and 1 console
     	
     	//Configure the joystick(s) here
     	add(new MDJoystick(getRobot(), "joystick1", 0)
     		.whenPressed("A",3,new MyCommand(getRobot(),"MyCommand"))
     		.configure()
     	);
    
     	//Configure the RioHID here
     	// Uncomment the following to attach a command to the user button on the RoboRio
    

// add(new RioHID(getRobot()) // .whileHeld(new MDPrintCommand(getRobot(),"ExampleCommand1","ExampleCommand1 message")) // .configure() // );

	//Configure the MDConsole OI here		
	add(new ConsoleOI(getRobot())
			.whenPressed("Command1",0,new MDPrintCommand(getRobot(),"Command1","Command1 message"))
			.whenPressed("MyCommand",0,new MyCommand(getRobot(),"MyCommand"))
			.configure()
		);		
	
}
5. Save and close any files you have created or modified.