How to use the
Learning Portal

Learning Portal

Our hands-on, comprehensive lesson plans span a range of levels. Browse our free STEM and coding learning resources.

Marty Image

An Army Of Martys

Introduction

Using Python, you will create a program that will find all of the Martys connected to your network and control them all using the same commands.

The video above is an example of what you can do whilst controlling more than one robot. In this tutorial we will go over how you can connect and code multiple robots at the same time using a Python script!

what you will need

  • A computer with Python 3 and martypy installed (installation guides here)
  • At least 2 or more Martys

What will you learn about?

  • Introductions to martypy
  • Basic lists and loops in Python

extra information for educators

N/A

Finding the Martys

The first thing we need to do is find all of the Martys we can, our army needs as many bodies as possible.

First thing's first, open up IDLE (you will have this application after installing Python) and create a new file by either pressing ctrl+N or going to File->New File

To then allow Python to talk to our Martys we need to write the following at the top of our file. This tells Python to go and get all of the functions that can talk to Marty so that we can then use them.

import martypy

We can now tell Python to look for any Marty connected to the same WiFi as the computer that we are using. The following line will help us with that.

martypy.SocketClient.discover(timeout=10);

Note that if we just write this and run our program, nothing will happen. This is because we're not doing anything with what the function is telling us, so for now we will save what it returns into a variable called unrecruitedMartys and then print it out.

unrecruitedMartys = martypy.SocketClient.discover(timeout=10) print(unrecruitedMartys)

What is a variable?

In programming, we use variables to remember information for us. This information could be anything from a specific number or the string of text that was just typed into our program by the user. We can then call on the variable when we need the information that it is storing!

You can run your program by selecting Run->Run Module or by pressing F5. When you run your program you should see a list of the available Martys on your network that you can connect to. It should look similar to the following,

[{('192.168.0.57', 4000): b'martyrocks'}, {('192.168.0.58', 4000): b'Anna'}]

Connecting to the Martys

Now that we can see the Martys, the next step is to connect to them in order to start controlling them.

We need to use the following command to connect to Marty.

m = martypy.Marty(url='socket://192.168.0.__', client_types=dict(), default_lifelike=True)

However, we need to know the IP address of the Marty we want to connect to. Luckily, we can get that information from the list we printed out earlier after running the discover command.

We can get this information by going through the list of Martys and pulling out all of the IP addresses. To do this we will need to use a loop,

for marty in unrecruitedMartys: print(marty)

What is a loop?

In programming, we use loops to repeat a certain action a number of times. For example, if you were going to walk in a square, you would repeat the command walk forwards and turn 90 degrees right four times. Using loops is an easy way to stop duplicating lines of code and makes our programs more readable!

This loop will start with the first Marty in the list and print out that entry before moving through the list and doing the same for each Marty entry in the list.

If we run our program now with this loop included then one of the entries when printed will look like the following,

{('192.168.0.57',4000):b'martyrocks'}

We can now get a single Marty from our list, but this gives us a lot more information than we actually need. We will only need to extract the IP address from each entry so that we can connect to that Marty. To do this, we need to change our loop slightly,

for marty in unrecruitedMartys: ip = marty.popitem()[0][0]

Lets break this command down a little bit

The popitem() command will take a single entry from the list and return it.

We then use [0] to get the first element in the returned entry which will give us ('192.168.0.57',4000)

Repeating this and using [0] again, we will get the IP address section which is then saved into the variable called ip

To get the URL we need to connect to that Marty, we can simply concatenate (add together) the text string 'socket://' with the string held in our variable, ip. So our command for connecting to a Marty will look like the following,

m = martypy.Marty(url='socket://' + ip, client_types=dict(), default_lifelike=True)

Finally we can connect to our Martys. As we may not know how many Martys we will be controlling we're going to need a list that holds all of the connections. Let's create an empty list just above the loop we created to gather the IP addresses,

Martys = []

We can then put it all together and connect to all of the Martys that are on the same WiFi as our device,

import martypy
unrecruitedMartys = martypy.SocketClient.discover(timeout=10);
martys = []
for marty in unrecruitedMartys:
print("getting")
ip = marty.popitem()
print(ip) ip = ip[0][0]
print(ip) m = martypy.Marty(url='socket://'+ ip, client_types=dict(), default_lifelike=True)
martys.append(m)

Challenge: Commanding your army!

We now have a list of connections to our Marty army, lets actually use them!

To control our Martys, we will need to run through the list of Martys that we have connected to and call .hello() on each Marty in the list.

The .hello() is the equivalent to the get ready block in Scratch - it tells Marty to reset all of the motors and get ready to move!

Add in another loop to your program and add a few moves for your Marty army to complete. You might want to look at the martypy documentation here for some help and ideas...

I need some help!

Here is some example code to help you out with controlling your Marty army,

What Next?

You now have control of your very own Marty army that you can code and control. If you want to continue to explore using martypy to code multiple robots at the same time, here are some ideas!

  • Pick a song and code your very own robot flash mob dance routine
  • Cast and code your own play featuring your Martys. You will need to think about controlling the different robots to do different things!