How do you define a box in python?
Creating Python boxes¶Simple modifications of a box script¶All the boxes in Choregraphe have their own script, so you can easily edit them and modify the script. Show
Description of the script logic¶The script programming language in Choregraphe is Python. Each box in Choregraphe is a module. Briefly, a module is like an application, completely independent, that can run on its own and communicate easily with other modules. The box creation is done in several steps:
The first two steps are automatic: you have no control over the script. But you have full control over the third step. Actually, this step executes some part of code you have written in the box script. Description of a box script¶To examine a box script, drag and drop the Set LEDs box onto the diagram panel and double-click on it. The script window is displayed, showing the following script: class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) def onLoad(self): #~ puts code for box initialization here pass def onUnload(self): #~ puts code for box cleanup here pass def onInput_onSet(self): ALLeds.fade(self.getParameter("LEDs group"), self.getParameter("Intensity (%)")/100., self.getParameter("Duration (s)")) self.onReady() # activate output of the box This box orders the LEDs of a group of LEDs (ear LEDs by default) to switch on (onStart input) in the given amount of time (default 1 second). When writing script, you must remember that you are defining methods of a module class. A method is a function the program calls to execute each time the same action. There are 4 methods in the Set LEDs script:
How does the Set LEDs script work¶
Modifying the script¶Modify the “Set LEDs” box script in order to make NAO’s LEDs group flash five times before switching off. To do so:
class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) self.max = 5 def onLoad(self): #~ puts code for box initialization here pass def onUnload(self): self.i = self.max #~ puts code for box cleanup here pass def onInput_onSet(self): self.i = 1 while (self.i < self.max): ALLeds.fade(self.getParameter("LEDs group"), self.getParameter("Intensity (%)")/100., self.getParameter("Duration (s)")) ALLeds.fade(self.getParameter("LEDs group"), 0, self.getParameter("Duration (s)")) self.i += 1 self.onUnload() # activate output of the box pass
Creating a box script from scratch using parameters¶Introduction¶In Choregraphe, it is possible to write your own box, defining their complete workings.
How does it work¶For instance:
The generated script of a new box would look like this: class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) def onLoad(self): #~ puts code for box initialization here pass def onUnload(self): #~ puts code for box cleanup here pass def onInput_onStart(self): #~ self.onStopped() #~ activate output of the box pass def onInput_onStop(self): self.onUnload() #~ it is recommended to call onUnload of this box in a onStop method, as the code written in onUnload is used to stop the box as well pass
Example¶
So, what about running a little script to increase and decrease intensity over time, with self.step = 0.02 for instance. A classic Python script would look like this: r = 0 while (r+ self.step < 1): r = r +self.step ALLeds.setIntensity ("LeftFootLeds", r) time.sleep(0.01) while (r - self.step > 0): r = r - self.step ALLeds.setIntensity ("LeftFootLeds", r) time.sleep(0.01)
Note Three standard entries to your box are automatically created: onStart input, onStop input and onStopped output. You can modify these entries and create new ones by editing the box.
class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) self.ledsName = "RightEarLeds" self.step = 0.01 def onLoad(self): #~ puts code for box initialization here self.isRunning = False pass def onUnload(self): #~ puts code for box cleanup here pass def onInput_onStart(self): #~ self.onStopped() #~ activate output of the box if(self.isRunning): return self.isRunning = True r = 0 while (r + self.step < 1 and self.isRunning): r += self.step ALLeds.setIntensity(self.ledsName, r) time.sleep(self.step) while (r - self.step > 0 and self.isRunning): r -= self.step ALLeds.setIntensity(self.ledsName, r) time.sleep(self.step) self.isRunning = False self.onStopped() def onInput_onStop(self): self.onStopped() #~ it is recommended to call onUnload of this box in a onStop method, as the code written in onUnload is used to stop the box as well pass Note Note the code that we had to put in onUnload to make sure the box exits its loop if we call onStop or if the flow diagram is unloaded. Using parameters¶Parameters allow you to customize the way the box works. After having created the parameters of the box, you will not have to edit a single line of script to use it. To create the parameters of the box:
class MyClass(GeneratedClass): def __init__(self): GeneratedClass.__init__(self) self.step = 0.01 def onLoad(self): #~ puts code for box initialization here self.isRunning = False pass def onUnload(self): #~ puts code for box cleanup here pass def onInput_onStart(self): #~ self.onStopped() #~ activate output of the box if(self.isRunning): return self.isRunning = True r = 0 while (r + self.step < 1 and self.isRunning): r += self.step ALLeds.setIntensity(self.getParameter("Leds name"), r) time.sleep(self.step) while (r - self.step > 0 and self.isRunning): r -= self.step ALLeds.setIntensity(self.getParameter("Leds name"), r) time.sleep(self.step) self.isRunning = False self.onStopped() def onInput_onStop(self): self.onStopped() #~ it is recommended to call onUnload of this box in a onStop method, as the code written in onUnload is used to stop the box as well pass
Creating a box to retrieve right bumper value using ALMemory¶Cannot be tested on a simulated robot.This tutorial will show you how to retrieve an ALMemory value directly from your behavior.
You are now capable to retrieve an ALMemory value from any behavior. tutorial_almemory_bumper_behavior.crg Transmit a value from a behavior to another using ALMemory events¶Cannot be tested on a simulated robot.In this tutorial we will see how to use the ALMemory module to transmit your own values between two behaviors. For more information about the ALMemory module, see the ALMemory API. Create the two following behaviors and make them interact:
Writing a value in ALMemory¶Let’s start with the sender module:
tutorial_almemory_sender_behavior.crg Reading a value from ALMemory¶Let’s create the receiver behavior:
tutorial_almemory_receiver_behavior.crg Playing two interacting behaviors¶Now we will execute the sender and the receiver together. To do it, we will need to execute the receiver using the behavior manager.
You are now capable to send values from a behavior to another using the AlMemory module. How do you make a box in python?To do so:. Drag and drop the Audio > Voice > Say box into the diagram.. Double-click it to display the inner diagram.. Copy the Say text box.. Click the root label to come back to the behavior.. Paste the Say text box.. Delete the Say box.. What is box in python?Overview. Box is designed to be an easy drop in transparently replacements for dictionaries, thanks to Python's duck typing capabilities, which adds dot notation access. Any sub dictionaries or ones set after initiation will be automatically converted to a Box object.
How do you display messages in python?In python, the print statement is used to display text. In python with the print statement, you can use Single Quotes(') or Double Quotes(").
|