Behaviors are connected together with events to form more complex behaviors. In this simple example, pressing the y button on the joystick will trigger a change in the active behavior from driving the robot with an an arcade style input to driving the robot with a tank style input.
The OpMode software does the following:
It begins by setting up the behavior framework in the H1TeleOp_Js2() constructor or the opmode init() method as follows:
// ConstructorBehaviors_Joystick be; GlobalData gd; public H1TeleOp_Js2(){ // Setup the behavior model gd = new GlobalData(); be = new Behaviors_Joystick(gd); }
In our example, the behaviors framework is called Behaviors_Joystick(). GlobalData is needed to pass information back and forth in the loop() method for the opmode as follows:
// Run periodically when sensors are read@Overridepublic void loop() { // Pass Joystick to the behaviors gd.jsLeftY = gamepad1.left_stick_y; gd.jsRightY = gamepad1.right_stick_y; gd.jsLeftX = -gamepad1.left_stick_x; gd.buttonX = gamepad1.x; gd.buttonY = gamepad1.y; // Pass time to the behaviors gd.deltaTime = (float)time - gd.time; gd.time=(float)time; // Run the behaviors be.onLoop(); // write the values to the motors motorRight.setPower(gd.rightCmd); motorLeft.setPower(gd.leftCmd); telemetry.addData("_Behavior", be.getActiveBehavior().getName()); telemetry.addData("Power Left", Float.toString(gd.leftCmd)); telemetry.addData("Power Right", Float.toString(gd.rightCmd)); telemetry.addData("Left Js Y", Float.toString(gd.jsLeftY)); telemetry.addData("Right Js Y", Float.toString(gd.jsLeftY)); //DbgLog.msg("Run Mode");}
The method onLoop() calls the behavior framework. When the robot first starts up, it will begin by calling the Behavior_JsArcade behavior.
For full copies of this software, see: https://github.com/dlacres/GeekMyBot.git
This github repository has the following behaviors defined:
- Behavior_Forward - Drive forward
- Behavior_Heading - Use the dpad and the gyro to turn the robot (demonstrates a simple PID control law)
- Behavior_IrTurn - Turn toward the IR Beacon. Move forward and backward to match the signal strength.
- Behavior_JsArcade - Drive using Arcade mode
- Behavior_JsGyro - Drive using Arcade mode. Correct for drift with the gyro.
- Behavior_JsLookup - Drive using Arcade mode. Provide better low speed turns using a lookup table to shape the commands.
- Behavior_JsTank - Drive using Tank mode.
- Event_ButtonX - Transition on the rising edge of a X button push
- Event_ButtonY - Transition on the rising edge of a Y button push
- Event_Time - Transition after a period of time
If you develop a behavior that you are particularly proud of, I ask that you add a comment to this article and share your behavior along with a link to your software.
No comments:
Post a Comment