Tuesday, August 30, 2016

FTC Robot Behavior Model (Part 2 - Events)

This is part 2 of a 5 part series on on a robot behavior framework.


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 event class implements the event interface. The methods onTransition(), isTriggered(), and getToBehavior() are used by the behavior. Each method has a purpose in the framework as follows:

 - onTransition() runs only when the event is triggered resulting in a transition between behaviors. The order of transitions are set in the behaviors class. This will be covered in the part 3.
 - isTriggered() returns a boolean value. If the Boolean is True, the transition occurs.
 - getToBehavior() is used by the framework when it requests the destination behavior.


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

package com.qualcomm.ftcrobotcontroller.geekmybot;

/** * Created by David Lempia on 8/2/2015. * * Copy this template and rename it with the name of your event. i.e. Event_ButtonX. * Inputs and Outputs are put in the Behaviors_xx file. Access them using gd.xx * Add the event to the Behaviors_xx file. * Add inputs and outputs to the Behaviors_xx file. * Pass parameters in through the Event_Template Constructor. */public class Event_Template implements Event {
    private Behavior toBehavior;

    // Returning true triggers the event    // Pass variables    public boolean isTriggered(){
        return(false);
    }

    // This runs if the event is triggered    public void onTransition(){

    }

    // Change Behaviors_XX to the name of your behaviors class    // Add parameters as needed    GlobalData gd;
    Event_Template(GlobalData gd, Behavior toBehavior){
        this.gd=gd;
        this.toBehavior = toBehavior;
    }

    // No change needed    public Behavior getToBehavior() {
        return(this.toBehavior);
    }
}

To create an event that transitions whenever the Y joystick button is pressed, add a new RiseEdgeTrigger to the constructor. Check for a button press in the isTriggered() method. Here is what the final sofware looks like for the Event_ButtonY.java class:

package com.qualcomm.ftcrobotcontroller.geekmybot;

/** * Created by David Lempia on 8/2/2015. */public class Event_ButtonY implements Event {

    // Returning true triggers the event    public boolean isTriggered(){
        boolean button_pressed = pressed.calculate(gd.buttonY);
        return(button_pressed);
    }

    // This runs if the event is triggered    public void onTransition(){

    }

    // No change needed    RiseEdgeTrigger pressed;
    private Behavior toBehavior;
    GlobalData gd;
    Event_ButtonY(GlobalData gd, Behavior toBehavior){
        this.gd = gd;
        this.toBehavior = toBehavior;
        pressed = new RiseEdgeTrigger();
    }

    // No change needed    public Behavior getToBehavior() {
        return(this.toBehavior);
    }
}

Here is the software for the RiseEdgeTrigger.java class:
 
package com.qualcomm.ftcrobotcontroller.geekmybot;

/** * Created by David Lempia on 1/17/2016. */public class RiseEdgeTrigger {
    private boolean z1=false;
    private boolean out=false;

    public boolean calculate(boolean in){

        if (in & !this.z1) this.out=true;
        else this.out=false;

        this.z1=in;

        return(this.out);
    }
}

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