Python Box Concept (Pepper Tech Fes Tech Session)

Choregraphe and Python

In the tutorials so far, we've looked at the various boxes available in Choregraphe. You can achieve a variety of applications with just a box, but you can do even more by accessing the API directly using Python.

In the technical session of Pepper Tech Festival 2014, we introduced the Python box as one of the ways to use Python. This tutorial gives an overview of the concept of Python boxes.

The version of Python used in Choregraphe is 2.7. For a basic Python tutorial, see http://docs.python.jp/2/tutorial/ and so on. Also, refer to the API document [Bumper sensor event detection #Document confirmation](http://qiita.com/Atelier-Akihabara/items/d87553893fe2caa26d67#%E3%83%89%E3%82%AD%E3 % 83% A5% E3% 83% A1% E3% 83% B3% E3% 83% 88% E3% 81% AE% E7% A2% BA% E8% AA% 8D) Please refer to this.

Python box

Here, I will create an empty Python box with Choregraphe and explain the Python box while looking inside it.

  1. Right-click on the flow diagram and ** select Python ... from the New Box menu ** new-python-box.png

  2. Enter ** Test as the name [A] ** and click the ** [OK] button [B] ** new-python-dialog.png

This will create a Python box. python-box.png

Try double-clicking on the created Test box. A script editor opens with code similar to the following.

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)

    def onLoad(self):
        #put initialization code here
        pass

    def onUnload(self):
        #put clean-up code here
        pass

    def onInput_onStart(self):
        #self.onStopped() #activate the output of the box
        pass

    def onInput_onStop(self):
        self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
        self.onStopped() #activate the output of the box

The behavior of Python boxes is defined by the MyClass class. The GeneratedClass class is automatically generated when the behavior runs and provides additional built-in functions as defined in the box.

Methods defined in the box

input

The MyClass class needs to define a method that corresponds to the box input. These names are defined as ** onInput_ ** and will be called when the input is made.

The argument changes depending on Input type. For example, if you create an input named * myInput * that is a simple event (Bang),

    def onInput_myInput(self):
        pass

In the case of Number, String, and Dynamic,

    def onInput_myNumberInput(self, p):
        pass

    def onInput_myStringInput(self, p):
        pass

    def onInput_myDynamicInput(self, p):
        pass

And it will have an argument. In the case of Dynamic, it may be called like Bang (without passing a value), so it can be defined as follows.

    def onInput_myDynamicInput(self, p = None):
        pass

By default, * onStart * and * onStop * inputs are defined, so two methods are generated here: ʻonInput_onStart and ʻonInput_onStop.

Load and unload

The MyClass class defines the ʻonLoad and ʻonUnload methods, which are called when the box is loaded or unloaded.

    def onLoad(self):
        #put initialization code here
        pass

    def onUnload(self):
        #put clean-up code here
        pass

It is recommended to call ʻonUnload` with the method equivalent to stopping the box (onStop input, etc.) because it will be reinitialized after the box is stopped.

Built-in functions provided by the box

Since MyClass inherits from GeneratedClass, you can use the built-in functions provided by GeneratedClass in your scripts.

output

A method corresponding to each output in the box is defined with the name ** **. You can output the box by calling this method.

As with the input, the arguments change depending on the Output Type (http://qiita.com/Atelier-Akihabara/items/7a898f5e4d878b1ad889#3-2). If you define an output called * myBangOutput *, which is a simple event (Bang),

    def onInput_onStart(self):
        self.myBangOutput()

You can pass a value to the output destination by calling it without an argument, such as, and in the case of a number (Number), string (String), or dynamic (Dynamic), by calling it with an argument.

    def onInput_onStart(self):
        self.myNumberOutput(1000)

Parameters

You can also use the parameters specified in the box. You can use one of the following methods.

  1. How to use the getParameter method To get the parameter named * param1 *, call the getParameter method as follows:

        value = self.getParameter("param1")
    
  2. How to override the setParameter method You can also describe what happens when a parameter changes by overriding the setParameter method in the MyClass class.

    def setParameter(self, parameterName, newValue):
        if(parameterName == "param1"):
            self.param1 = newValue
    

log

You can also output the log from the Python box. The output log can be viewed with the log viewer.

Method function
self.log("my message") Message output at info level
self.logger.fatal("my message") Message output at fatal level
self.logger.error("my message") Message output at error level
self.logger.warning("my message") Message output at warning level
self.logger.info("my message") Message output at info level
self.logger.debug("my message") Message output at debug level

Note that the print statement cannot be output to the log viewer. You need to use these methods.

By creating a Python box like this, you can write Python code directly in the box. By calling the API directly in this code, you can do things that the box library doesn't cover. In the future, we plan to take a look at the concept of these Python boxes through concrete examples in the tutorial.

Recommended Posts

Python Box Concept (Pepper Tech Fes Tech Session)
Say hello with Python SDK (Pepper Tech Fes technical session)
Understand Python for Pepper development. -Python box self-made function-
Understand Python for Pepper development. -Introduction to Python Box-
Python Golf Tech (AtCoder)