The Great British Robot Code Dojo

Programming Nao Using Python

Nicholas H.Tollervey / @ntoll

3 Ways to Use Python:

  1. Scripts in "boxes";
  2. Custom Python scripts in Choregraphe;
  3. Python Modules.

We'll look at 1. We don't have time for 2 & 3.

(We do have the docs and manuals if you're interested.)


Tree of boxes defines behaviour.

Anatomy of a Box

  • Each box represents a dynamically created Python class.
  • The class has five default methods: __init__, onLoad, onUnload, onInput_onStart & onInput_onStop.
  • The class MUST inherit from GeneratedClass whose __init__ must be the first thing called in the child class's own __init__ method.

Right-click to create a new box.

(Double click the box to see the generated code.)

(Edit code in here. No need to "save".)

(Press the green "Play" button to run.)

(Yay! It works!)

What just happened?

At behaviour start:

  • The __init__ method for ALL boxes is run.
  • The boxes at root level have their onLoad method called.

Demo: Turn Nao into an adding machine

(Right click on an input and select Add Input.)

(Same again, but right click on an existing output.)

(Double click on the box to edit the code for the onInput_sum method.)

    def onInput_sum(self, p):
        The input p should be the name of a number between 0-9 (defined
        in self.numbers defined in __init__) or the word "equals".

        Numbers get added to the self.operands list. When the word
        "equals" in encountered the operands are summed then cleared
        and the result is output.

        Anything else is handled by very basic error handling.
            if p.strip() in self.numbers:
            elif p == 'equals':
                result = sum(self.operands)
                self.operands = []
                self.outputResult('The answer is {}'.format(result))
        except Exception as ex:
            # Yeah, I know... :-(
            error = ex.__class__.__name__
            msg = 'Cannot compute. I encountered a {}'.format(error)

Define a behaviour tree:

(Click on the spanner in the bottom rhs of the Speech Recognition box.)

Add the word "equals" and the digits 0-9 as a delineated semi-colon list. Set the threshold to just over 51% (but don't expect great results - experiment!).

(Live demo)