How to Configure the Robot - MDHSRobotics/TeamWiki GitHub Wiki

Home / References / [Understanding MaterBot](Understanding MaterBot)

#How to Configure the Robot?

The robot is initially configured in the Java code. One of the improvements over the out of the box framework has been the simplification of the steps needed to configure the robot. All robot configuration is done in only 2 places:

  • Robot.java - this is used to configure subsystems, settings, sensor readings and commands
  • OI.java - this is used to configure the OI, like mapping commands to joystick buttons

The Robot.java file is where one configures the commands, subsystems, actuators and sensors of the robot. The configuration code is put in the configureRobot() method. There are a few global settings that are also configured in this method.

The OI.java file is where one configures operator interface related items, like what command is triggered when a button is pressed an in what direction a robot moves when one of the joysticks is moved in a particular manner. The configuration code is put in the configureOI() method.

####Setting the Autonomous Command An autonomous command needs to be configured for the autonomous period. This is done by using the setAutonomousCommand() method:

 	//A commands needs to be configured for the autonomous mode.
 	//In some cases it is desirable to have more than 1 auto command and make a decision at game time which command to use
 	setAutonomousCommand(new MDCommand[]{
 			new MDPrintCommand(this,"AutonomousCommand1","AutonomousCommand1 message"),
 			new MDPrintCommand(this,"AutonomousCommand2","AutonomousCommand2 message"),
 			new MDPrintCommand(this,"AutonomousCommand3","AutonomousCommand3 message"),
 			new MDPrintCommand(this,"AutonomousCommand4","AutonomousCommand4 message")
 		}
 		, "AutonomousCommand4"  //specify the default
 	);

####Drive Subsystem The Drive subsystem is a special subsystem in that there is code in the framework specifically geared to facilitating drive system functions. To configure a drive system, add and configure an instance of MDDriveSystem to Robot class. Note the structure below. The MDDriveSystem constructor is used to specify a name to the subsystem and indicate the type of drive system is desired. Motors and sensors are added by calling the appropriate add method from the MDDriveSystem class. Important The configuration of a subsystem is finalized by calling the configure() method of the MDDriveSystem class. Below is an example of a configuration for a drive system:

add(new MDDriveSubsystem(this, "driveSystem", Type.TankDrive)
  .add(MotorPosition.left, new Victor(0))
  .add(MotorPosition.right, new Victor(1))
  .add("distanceSensor",new MDAnalogInput(0))
  .add("accelerometer", new MD_BuiltInAccelerometer())
  .configure()
);	

####Subsystem Configuring a subsystem is a 2 step process.

  1. Create a class that extends MDSubsystem

    Below is an example of a creating a subsystem class.

public class CoreSubsystem extends MDSubsystem {

public CoreSubsystem(MDRobotBase robot, String name) {
	super(robot, name);
	setCore(true);
}

@Override
protected void initDefaultCommand() {
}

@Override
protected void setUp() {
	if(getConfigSettings().containsKey("name")){
		getRobot().setName(getConfigSettings().get("name").getString());
	}
	if(getConfigSettings().containsKey("autoCommand")){
		getRobot().setAutoCommand(getConfigSettings().get("autoCommand").getString());
	}
}

@Override
public void settingChangeListener(ConfigSetting changedSetting) {
	if(getConfigSettings()!=null && getConfigSettings().containsKey("autoCommand")){
		getRobot().setAutoCommand(getConfigSettings().get("autoCommand").getString());
	}	
	if(getConfigSettings()!=null && getConfigSettings().containsKey("name")){
		getRobot().setName(getConfigSettings().get("name").getString());
	}	
}

}


2. Configure robot to include the subsystem

Below is an example of a configuration for a subsystem.  In this example, a canon subsystem has a tile motor, an encoder to detect the angular position of the canon, a shooting motor, and a contact switch to see if the canon is loaded
```java
add(
new CanonSubsystem(this,"canon")
 .add("tiltMotor", new Victor(3))
 .add("shootMotor", new Victor(4))
 .add("loadSolenoid",new Solenoid(0))
 .add("tiltEncoder", new MDAnalogInput(2))
 .add("isLoadedSwitch", new MDDigitalInput(2))
 .add("shootSpeed",new DoubleConfigSetting(0.0, 1.0, 0.87))
 .configure()
);	

####Example Robot Configuration Below are examples of the Robot.java and OI.java files used to configure the robot.

  • Robot.java

public class Robot extends MDRobotBase { /** * This function is run when the robot is first started up and should be * used for any initialization code. */

@Override
protected void configureRobot() {

	//A commands needs to be configured for the autonomous mode.
	//In some cases it is desirable to have more than 1 auto command and make a decision at game time which command to use
	setAutonomousCommand(new MDCommand[]{
			new MDPrintCommand(this,"AutonomousCommand1","AutonomousCommand1 message"),
			new MDPrintCommand(this,"AutonomousCommand2","AutonomousCommand2 message"),
			new MDPrintCommand(this,"AutonomousCommand3","AutonomousCommand3 message"),
			new MDPrintCommand(this,"AutonomousCommand4","AutonomousCommand4 message")
		}
		, "AutonomousCommand4"  //specify the default
	);

	
	
	//A robot is composed of subsystems
	//A robot will typically have 1 drive system and several other fit to purpose subsystems		
	//The Drive system is a special subsystem in that it has specific logic handle the speed controllers
	add(new MDDriveSubsystem(this, "driveSystem", Type.TankDrive)
			.add(MotorPosition.left, new Victor(0))
			.add(MotorPosition.right, new Victor(1))
			.add("accelerometer", new MD_BuiltInAccelerometer())
			.configure()
	);	

	
	//Special Subsystem used for RobotDiagnostics
	add( new DiagnosticsSubsystem(this, "diagnosticsSubsystem")
			 .add("diagnosticsSensor",new RobotDiagnostics())
			 .add("diagnosticsScanPeriod",new DoubleConfigSetting(0.05, 1.0, 0.1))
			 .configure()
	);
	
	//Subsystem to manage robot wide config settings
	add( new CoreSubsystem(this, "core")
			 .add("name",new StringConfigSetting("MaterBot"))					//go ahead name your robot
			 .add("autoCommand",new StringConfigSetting("AutonomousCommand1"))		//name of autoCommand you wish to start with
			 .configure()
	);
	
	//Subsystem to manage WebSocket Communications
	add( new WebSocketSubsystem(this, "WebSockets")
			 .add("enableWebSockets",new BooleanConfigSetting(true))
			 .configure()
	);



}




//Override lifecycle methods, as needed
//	@Override
//	public void teleopPeriodic() {
//		super.teleopPeriodic();
//		...
//	}
//	@Override
//	public void autonomousPeriodic() {
//		super.autonomousPeriodic();
//		...
//	}	

	
	//Event manager WebSocket related methods
	//Override as needed
//	@Override
//	public void onConnect(Session session) {
//		super.onConnect(session);
//		...
//	}

}


* OI.java

```java
public class OI extends OIBase{
 
 public OI(MDRobotBase robot) {
 	super(robot);
 	System.out.println("OI created");
 }


 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 MDPrintCommand(getRobot(),"A Command","A Command message"))
 		.whileHeld("leftBumper",6,new MDPrintCommand(getRobot(),"Left Bumper Command","Left Bumper Command message"))
 		.configure()
 	);
 	//Configure the RioHID here
 	add(new RioHID(getRobot())
 		.whileHeld(new MDPrintCommand(getRobot(),"ExampleCommand1","ExampleCommand1 message"))
 		.configure()
 	);
 	//Configure the MDConsole OI here		
 	add(new ConsoleOI(getRobot())
 			.whileHeld("ExampleCommand1",0,new MDPrintCommand(getRobot(),"ExampleCommand1","ExampleCommand1 message"))
 			.whenPressed("ExampleCommand2",1,new MDPrintCommand(getRobot(),"ExampleCommand2","ExampleCommand2 message"))
 			.configure()
 		);		
 	
 }

}