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.
Here, I will create an empty Python box with Choregraphe and explain the Python box while looking inside it.
Right-click on the flow diagram and ** select Python ... from the New Box menu **
Enter ** Test as the name [A] ** and click the ** [OK] button [B] **
This will create a Python box.
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.
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
.
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.
Since MyClass
inherits from GeneratedClass
, you can use the built-in functions provided by GeneratedClass
in your scripts.
A method corresponding to each output in the box is defined with the name **
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)
You can also use the parameters specified in the box. You can use one of the following methods.
How to use the getParameter
method
To get the parameter named * param1 *, call the getParameter
method as follows:
value = self.getParameter("param1")
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
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