The behavior model is tied together in in the Behaviors class. The Behaviors class creates new instances of each behavior and event. It also defines which behavior is the active behavior to start with. Finally it connects the events to the behaviors
The Behaviors class implements the Behaviors interface. The methods onLoop() and getActiveBehavior() are used by Behaviors. Each method has a purpose in the framework as follows:
- onLoop() runs each time the robots's opmode is run.
- getActiveBehavior() is used by the framework to get and run the onLoop() for the active behavior.
In this example, the behaviors class will start running the Behavior_JsArcade class. This class drives the robot using the left joystick in arcade mode. When the Y joystick button is pressed, the behavior changes to the Behavior_JsTank class. This class drives the robot using the left and right joystick in tank mode. When the joystick Y button is pressed again. the behavior transitions back to the Behavior_JsArchade class. The following picture depicts this behavior:
To use Behaviors copy the Behaviors_Template.java shown below into a new class name. I start the name of all of my Behaviors classes with the naming convention Behaviors_XX where XX is the name of my Behaviors. Here is a copy of the template:
package com.qualcomm.ftcrobotcontroller.geekmybot; import java.util.List; /** * Created by David Lempia on 7/20/2015. */public class Behaviors_Template { // Public SM Inputs and Outputs // Declare the states and events // Add the states and events used in this state machine public Behaviors_Template(GlobalData gd){ // Create the states - Input is the name of the state for debug //Behavior behavior?? = new Behavior_??( gd, "?? Behavior"); // Create the events - Behavior From, Event, Behavior To //behavior??.addEvent( new Event_ButtonY(gd, behavior??)); // The start state for the state machine //activeBehavior = ?? } // Do not change this. This stays constant private Behavior activeBehavior; public void onLoop(){ List<Event> el = activeBehavior.getEventList(); //Log.i("info", "Made it to 1"); for (Event el1 : el){ //Log.i("info", "Made it to 2"); if (el1.isTriggered()){ // If the event happened, run exit, step, and entry code activeBehavior.onExit(); el1.onTransition(); // Update the active state activeBehavior = el1.getToBehavior(); activeBehavior.onEntry(); break; // Do not check the rest of the transitions } } // Run active state activeBehavior.onLoop(); } public Behavior getActiveBehavior(){ return(this.activeBehavior); } }
Begin by creating a new set of behaviors in the Behaviors_Joystick2 creator as follows:
// Create the behaviors - Input is the name of the behavior for debugBehavior behaviorJsArcade = new Behavior_JsArcade(gd, "Js Arcade"); Behavior behaviorJsTank = new Behavior_JsTank(gd, "Js Tank");
Right after this software in the same module, create a set of events and define their transitions as follows:
// Create the events (lines)behaviorJsArcade.addEvent( new Event_ButtonY(gd, behaviorJsTank)); behaviorJsTank.addEvent( new Event_ButtonY( gd, behaviorJsArcade));Finally, define the initial active behavior (The behavior the robot starts in) as follows:
// The start state for the state machineactiveBehavior = behaviorJsArcade;
The resulting Behaviors_Joystick2 class is shown below:
package com.qualcomm.ftcrobotcontroller.geekmybot; import java.util.List; /** * Created by David Lempia on 7/20/2015. */public class Behaviors_Joystick2 implements Behaviors { // Add the behaviors and events Behavior activeBehavior; public Behaviors_Joystick2(GlobalData gd){ // Create the behaviors - Input is the name of the behavior for debug Behavior behaviorJsArcade = new Behavior_JsArcade(gd, "Js Arcade"); Behavior behaviorJsTank = new Behavior_JsTank(gd, "Js Tank"); // Create the events (lines) behaviorJsArcade.addEvent( new Event_ButtonY(gd, behaviorJsTank)); behaviorJsTank.addEvent( new Event_ButtonY( gd, behaviorJsArcade)); // The start state for the state machine activeBehavior = behaviorJsArcade; } // Do not change this. This stays constant public void onLoop(){ List<Event> el = activeBehavior.getEventList(); //Log.i("info", "Made it to 1"); for (Event el1 : el){ //Log.i("info", "Made it to 2"); if (el1.isTriggered()){ // If the event happened, run exit, step, and entry code activeBehavior.onExit(); el1.onTransition(); // Update the active state activeBehavior = el1.getToBehavior(); activeBehavior.onEntry(); break; // Do not check the rest of the transitions } } // Run active state activeBehavior.onLoop(); } public Behavior getActiveBehavior(){ return(this.activeBehavior); } }
For full copies of this software, see: https://github.com/dlacres/GeekMyBot.git
If you use this behavior framework, I ask that you share the love. Print out a copy of the GeekMyBot logo and put it on your robot along with the http://geekmybot.blogspot.com/ URL address. Share this Blog with your friends.
If you develop an event 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