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.

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:

  1. Instantiation of the box
  2. Registration of the box as a module so it can be called from other boxes through linking
  3. Box initialization

The first two steps are automatic: you have no control over the script. But you have full control over the third step. In fact, 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 "Switch LEDs" box onto the diagram panel and double-click on it.

> The script window is displayed.

The Switch Leds script window

When you edit the script of several boxes, they open in several tabs.

The "Switch LEDs" is coded in Python. This box orders the LEDs of a group of Leds (left ear by default) to switch on (onStart input) or to switch off (onStop input) in given amout of time (default 2 seconds).

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 5 methods in the "Switch LEDs" script:

  • initialize
  • onLoad
  • onUnload
  • onInput_onSet
  • onStop

How does the Switch Leds script work?

In Python, you don’t need to create a proxy to call certain NAOqi modules. This is done automatically. A proxy is an object that allows you to connect to a module on the robot and execute its “bound methods”. For example, ALMemory, ALMotion etc. are modules. ALLeds is the module which controls the LEDs of the robot.


To see a list of the modules and the methods you can use in Choregraphe, you can click on Help->API Reference when connected to a robot or a local NAOqi.

  1. When the behavior starts to play, the initialize method is executed first : GeneratedClass.__init__(self). It is a function which resets the basic box parameters that are common to all the boxes in Choregraphe. After that, the box is a "module" running in NAOqi, on the robot. The box has also initialized all its "parameters" so they are now available in the script. This method is called once per behavior. You can initialize objects that you want to have during the whole behavior (proxies for instance).

    You should never modify the first 3 lines of a script box as they are mandatory.

  2. onLoad method: This method is called when the box flow diagram is loading. It is necessarily called after the __init__ method of all the boxes of the current behavior. When a flow diagram is loading, this method is called on each box of current level before any IO can be stimulated.
  3. onUnload method: This method is called when the box flow diagram is unloading. When a flow diagram is unloading, this method is called on each box of current level. After the flow diagram unloading, boxes are disabled and cannot receive any event on their inputs. Note that the method usually stop everything running in the script, that is what you expect of the onStop input. That is why the latter calls onUnload by default.
  4. onInput_onStart method: This method is called when the onStart is stimulated. It calls ALLeds.fade(self.getParameter("Leds name"), self.getParameter("max intensity"), self.getParameter("time")) . This call to the ALLeds module orders the given group of Leds to switch on, in a given time, to max intensity. See that we never use hardcoded values, as we want the user to be able to change those values easily through Choregraphe interface. At the end of the onStart method, self.onStopped() stimulates the onStopped output of the “Switch Leds” box.
  5. onInput_onStop method: This method is called when the onStop is stimulated. The method structure is very close to the onStart method we have just described. The only difference is that a "min intensity" replaces the “max intensity” in the fade function. This simply means that when the onStop entry is stimulated, the group of Leds are ordered to switch off (depending on "min intensity" value of course).

Modifying the script

  • Now, let’s modify the script a bit. We want to modify the “Switch Leds” box script in order to make NAO's Leds group flash five times before switching off. To do so:
  1. Modify the script box as shown in the image below.

    Modifying the Switch Leds script

  2. We have used a "while" function in the onStart method to achieve our goal.

    > When the onStart input is stimulated, NAO will blink 5 times and switch off its LEDs.

    > When the onStop input is stimulated, NAO will exit the while loop the next time it goes in it, and will finish with its Leds off.

  3. Note that a lot of things could be improved in this box: interrupt the while loop quicker, prevent the box from entering the while loop several times (if onStart is stimulated more than once), make the "self.max" count appears as a parameter... Try to modify the box scripts in the Choregraphe library to create awesome behaviors!

Copyright © 2010 Aldebaran-Robotics - All rights reserved