Monday, August 29, 2016

FTC Robot Behavior Model (Part 1 - Behavior)

This is part 1 of a 5 part series on a robot behavior framework. Read Introduction to the FTC Robot Behavior Model to learn more.

The basic building block of this behavior framework is a behavior. A great behavior does one thing (behavior) and does it robustly. A behavior is active when it's onLoop() method is run by the opMode for the robot. A behavior is inactive when it is waiting for a trigger.

Events transition from one behavior to the next behavior. Events are covered in part 2 of this article.

The behavior class implements the behavior interface. The methods onEntry(), onExit(), onLoop(), addEvent(), getName(), and getEventList() are used by the framework. Each method has a purpose in the framework as follows:

 - onEntry() runs when any event results in a transition into the behavior.
 - onExit() runs when any event results in a transition out of the behavior.
 - onLoop() runs each time the robot's opmode is run.
 - addEvent() is used by the framework to add events that transition from this behavior to a list
 - getName() is used by the framework to return the name of the active behavior for display
 - getEventList() is used by the framework to return the list of events. This list is used to check if the isTriggered() method is true resulting in a transitioning to a new active behavior.

To use this behavior, copy the Behavior_Template shown below into a new class name. I start the name of all of my event classes with the name Behavior_xx where xx is a description of the behavior. This naming convention helps when our team shares behaviors for use in different programs. Here is a copy of the template:


package com.qualcomm.ftcrobotcontroller.geekmybot;

import java.util.ArrayList;
import java.util.List;

/** * Created by david lempia on 7/20/2015. * * Copy this template and rename it with the name of your behavior. i.e. Behavior_Forward. * Inputs and Outputs are put in the Behaviors_xx file. Access them using gd.xx * Add the behavior to the Behaviors_xx file. * Add inputs and outputs to the Behaviors_xx file. * Pass parameters in through the Event_Template Constructor. */public class Behavior_Template implements Behavior {
    // Change the name of this behavior (for diagnostics)    String behaviorName="none";

    // Add code for onEntry. This method is called one time when entering the behavior.    public void onEntry(){

    }
    // Add code for onExit. This method is called one time when exiting the behavior.    public void onExit(){

    }
    // Add code for onLoop. This method is called from loop as long as the behavior is active.    public void onLoop(){

    }

    // Add new classes as needed    List <Event> eventList;
    GlobalData gd;
    public Behavior_Template(GlobalData gd, String behaviorName){
        this.gd=gd;
        eventList=new ArrayList<Event>();
        this.behaviorName=behaviorName;
    }
    public List<Event> getEventList(){
        return(eventList);
    }
    public void addEvent(Event newEv){
        eventList.add(newEv);
    }

    @Override    public String getName() {
        return(this.behaviorName);
    }
}

Next, add the following code to the onLoop method:

// Drive calculations using Joystick inputs;float throttle = gd.jsLeftY/2.0f;
float direction = gd.jsLeftX/3.0f;
float right =  throttle - direction;
float left = throttle + direction;
gd.rightCmd = Range.clip(right, -1, 1);
gd.leftCmd = Range.clip(left, -1, 1);


Also, change the name of this behavior:

// Change the name of this behavior (for diagnostics)
String behaviorName="JS Arcade";



This string will be sent to the android phone connected to the joystick to indicate which behavior is active.



The JsTank behavior can likewise be created. Copy this template into a new class titled Behavior_JsTank. Add the following code to the onLoop method:

// Drive calculations using Joystick inputs;gd.rightCmd = gd.jsRightY;
gd.leftCmd = gd.jsLeftY;

 Also, change the name of this behavior:

// Change the name of this behavior (for diagnostics)
String behaviorName="JS Tank";


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 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