Joystick Selection Auto Java Cases

Before getting to the field, alliances typically talk autonomous and TeleOp strategy which requires changes in the code to account for the other robot’s movement. Similarly, it is common to write 6 or more programs for each color alliance that assume different situations in autonomous mode. It is nearly impossible to predict the movement of another robot which is why we have a way to “edit” the program’s intention before a match begins. Java supplies switch cases, a method of code selection where the code will execute a section of code based on the value of variables. The selection process takes a few seconds where while the autonomous program is initialized, the driver or coach picks up the controller and switches through the cases indicating order of operations, variable values, etc.

Java Cases

Java has a function called switch statements, visualized in the switch block, that contain cases. The switch statement takes an expression as an "argument" which will indicate which case to execute. You could have something that looks like this:

switch(caseNum){

case 0:

if(conditional)

{

action = false;

}

break;

case 1:

if(conditional){}

break;

default:

caseNum = defaultAction;

// any other action

break;

}

The caseNum variable, in this instance, is the indicator of what case to execute. This caseNum variable is of type int but it also works with byte, short, char and int primitive data types as well as enumerated types.

The default keyword handles all values that are not explicitly handled by one of the case sections but you can also refer it back to another case.

Joystick Selection

How do you apply cases to select order of operations or "edit' code?

Upon initialization of your opMode, you can set up if-conditionals linked to gamepad buttons that can jump through the cases. Once you are in a case you can link up other buttons that will alter the value of variables that will be mentioned in the running of the code to execute action. This works best in an iterative environment so that you can switch through cases and alter variables within the cases.

1.) Have a way to toggle between cases.

You could have one button to increase # of current case and another to decrease. If in iterative, place this section in the init_loop(). If in linear, place section in a while loop before the opmode begins (while(!isStarted()&& !isStopRequested())).

Set up boolean such as selectionButtonPressed with the gamepad button conditional so that a button must only be pressed once to function.

2.) Switch block

3.) Case subject : toggle between action variable values

Set up your switch block so that you can indicate what the code will do before performing it. This is where you will alter variables that are going to be used upon running your program to indicate whether or not an operation will execute. Within your case set up a conditional with two buttons to toggle in between values such as true and false, a specific string or another string, to increase or decrease, etc. (Make sure that variable toggling makes sense eg. delayTime can't be negative)

4.) Add telemetry on phone to see status of current case

telemetry.addData("> Set Action Value", "Current Value: " + action);

5.) Break Case

6.) Set default

Within default, you can check if your case indicator variable makes sense and set it back to the previous value if it does not.

7.) Add telemetry outside of switch block to show status of all cases

8.) Add conditionals in Run section to execute desired actions

if(caseAction){

motor.setPower(.7);

}

if(caseActionStr == "red"){

driveMultiplier = -1;

}

if(caseActionInt > 0){

crServo.setPower(.7);

}

Joystick Compiled Code


int caseActionInt = 0;

String caseActionStr;

boolean caseActionBoolean = false;

int caseNum = 0; // alter for case indicator of other data type

boolean selectionButtonPressed = false;

...

while(!isStarted()&& !isStopRequested())

{

if(gamepad1.button&&!selectionButtonPressed )

{

caseNum++;//toggle up from case 0 to case...

selectionButtonPressed = true;

}

else if(gamepad1.otherButton&&!selectionButtonPressed )

{

caseNum++;//toggle down from current case

selectionButtonPressed = true;

}

else if(!gamepad1.Button&&!gamepad1.otherButton&&selectionButtonPressed)

{

selectionButtonPressed = true;

}

switch (caseNum){

//case actions could indicate whether to go for powershots, towergoals, how many wobble goals, add time delay, change order of operations, etc.

case 0:

if(gamepad1.button1){

caseActionBoolean = false;

} else if (gamepad1.otherButton1) {

caseActionBoolean = true;

}

telemetry.addData("Label", "label value " + caseActionBoolean);

break;

case 1:

if(gamepad1.button1){

caseActionStr = "red";

} else if (gamepad1.otherButton1) {

caseActionStr = "blue";

}

telemetry.addData("Label", "label value " + caseActionStr);

break;

case 2:

if(gamepad1.button1&&!selectionButtonPressed )

{

caseActionInt++;//toggle up int

selectionButtonPressed = true;

}

else if(gamepad1.otherButton1&&!selectionButtonPressed )

{

caseActionInt++;//toggle down int

selectionButtonPressed = true;

}

else if(!gamepad1.Button1&&!gamepad1.otherButton1&&selectionButtonPressed)

{

selectionButtonPressed = true;

}

telemetry.addData("Label", "label value " + caseActionInt);

break;

default:

if(caseNum>0){

caseNum = 0;

}

else{

caseNum = caseNum;

}

//same as caseNum = caseNum < 0 ? 0: caseNum;

if(caseNum>caseNumLimit){

caseNum = caseNumLimit;

}

else{

caseNum = caseNum;

}

//same as caseNum = caseNum > caseNumLimit ? caseNumLimit: caseNum;

}

telemetry.addData("All Case Status", "labels and values ");

telemetry.addData("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::", "");

//start at 1 instead of 0 to be user friendly

telemetry.addData("1) isBoolean", caseActionBoolean);

telemetry.addData("2) string", caseActionStr);

telemetry.addData("3) int", caseActionInt);

telemetry.update();

}

if(opModeIsActive())

{

if(caseActionBoolean)//same as if(caseActionBoolean==true)

{

motor.setPower(.7);//add any hardware action, more variable values, etc.

}

if(caseActionStr == "red")

{

driveMultiplier = -1;

}

if(caseActionInt > 0)

{

crServo.setPower(.7);

}

}