The “Hello World” Dance!

Tutorial written by Yosser D'Avanzo, 2016.

Like any first-time programming experience, we will create a basic "Hello World" function. To start, we need to connect to the robot. For this, we need to add the MMM.py and the built-in Python “time” libraries:

from MMM import *
import time

To use functions that move the robot, we need an MMM object. The constructor function tells us that we need the portName to initialize an instance of the MMM class. where portName is the Arduino Mega's connected serial port. Calling MMM( portName ) will return the desired object, which we will save as "johnny". In this example, we will attach it to COM5. After sending the command to create the MMM instance, we should wait 5 seconds to allow the connection to be established:

johnny = MMM( ‘COM5’ )
time.sleep( 5 )

We can now tell the robot what to do! The robot will default to its standard posture when booted up. To make johnny move, we start changing his variables using functions from MMM.py and then send the package. We call update() after every variable change:

However, using this method will cause delays and misinterpretations to happen regularly. In order to fix this, we need to add threading. The method above is like trying to do two tasks with just one hand. Threading, on the other hand, allows us to have a more open and stable connection to the Arduino with multi-core processing, akin to doing two tasks with two hands. It does so by allowing the computer to run your code, telling the Arduino what to do via update() and listening to what the Arduino is saying via parseData() all at the same time.

Import the built-in ‘threading’ module. Now, we need to write an updateRobot() function for the threading to run. We need to make sure that all the variable changes we’re about to send are within their restrictions by calling clampAll(), sending them via update(), giving the Arduino a moment to process it and send us back info by calling sleep() for 0.1 seconds, and then receiving the Arduino’s information via parseData(). This function is repeated indefinitely:

The “Hello World” dance protocol is flexible, so feel free to be creative! Here, we’ll make him turn clockwise, wait 1 second, turn counterclockwise, wait 1 second, and then reset to the original position. And there you have it! A robot showing you the dance of his people:

Note: functions within the MMM.py file can be called by using johnny.functionFromMMM( args ). Functions made outside that file require the MMM obj to be handed in such as functionOutsideMMM( johnny, args )