Knowledge Base

Welcome to the Robotical knowledge base! These pages will help you get the most out of your Marty

Marty Images



Marty's First Steps with Python

The quickest way to learn about Python is to start coding and Marty is a great way to make Python code come to life.

The first things to do are to check if you already have Python installed on your computer, install the martypy library and make sure you can open a Python Shell. There's a detailed guide to doing this here so please follow the guide if you haven't already done so and then continue below when you have installed martypy.

You will also need to connect your Marty to WiFi and find its IP address. Your computer on which you are running the Python code has to be connected to the same network as your Marty.

Make Marty Dance

Only three lines of Python are required to make Marty dance:

Remember: Disconnect the app before running the code below; WiFi and Bluetooth cannot be used at the same time.

from martypy import Marty
my_marty = Marty("wifi", "192.168.0.42")  # Replace 192.168.0.42 by your Marty's IP
my_marty.dance()

You can type the lines into a Python Shell one by one. Let’s take a look at what each line does:

Line 1: Import martypy

Controlling a Marty is not part of the Python programming language which is why there is the martypy library. Like any other library, martypy is a collection of code that uses whatever is available in Python to make it as easy as possible for other programmers to perform a niche task - in this case controlling Marty.

Line 1 simply tells Python that you want to use the martypy library.

Line 2: Establish connection

my_marty is a variable name. Variables are a familiar concept in most programming languages (including Scratch) and they are just a named place to hold some information that you might want to use again in the future - for instance the scores in a game might be variables called your_score and my_score.

In this case we are using my_marty to refer to Marty itself, so we say so by making my_marty equal-to (using the equals sign) the Marty that we imported in the line above.

Now, because we’re writing this program to run on a separate computer than Marty itself, we need to tell the computer how to talk to Marty so that Marty can be told what to do. The two things in the brackets are enough information to let the computer talk to Marty. The first one ("wifi") tells your computer what method to use to talk to Marty, in this case WiFi. The second argument ("192.168.1.15") is the IP address which tells your computer where on your WiFi network to find your Marty. This will often change so use the app to check Marty's current IP before you start coding. For example, you may notice that I had to change "192.168.0.42" from the example at the beginning of this section to "192.168.1.15" which is the IP that was assigned to my Marty on my network this time.

Another method of connecting to Marty is via USB. To connect your Marty to your computer use the USB C port on the back of Marty and plug it into your computer. Instead of using "wifi" in the connection line use "USB" so it looks like this my_marty = Marty("USB"). Note this method may not work with newer Martys but we are expanding support.

You may also have noticed that line 2 in the original example had a part which looked more like English and less like Python. It is a comment. In Python all the text on a line after a hash (#) is ignored. So if you are copy-pasting from the snippet at the beginning of the section, feel free to copy the comment too or leave it out like I did. Comments only provide additional information for humans; computers completely ignore them.

Line 3: Dance!

This one is probably self explanatory. When you have a variable which refers to a thing like Marty you can use the dot (.) as a way to send a message to the thing telling it what you want it to do.

Marty can do a lot more than dance and all of these capabilities are available in the same way. If you haven't closed IDLE yet, you can even keep using your my_marty variable and send it more messages - you only need the first two lines at the beginning of each Python Shell session or Python program (we will get to programs later).

So let’s now get to the promised Marty's First Steps.

Make Marty Walk

First Step

Making a step is very similar to the dancing example:

from martypy import Marty
my_marty = Marty("wifi", "192.168.0.42")  # Replace 192.168.0.42 by your Marty's IP
my_marty.walk()

If you still have your Python Shell open, you can skip the first two lines and just type in the last one (my_marty.walk()). You should see Marty take two steps forward. We will usually leave out the first two lines in subsequent examples; just remember to execute them first whenever you open a new Python Shell.

More steps

Sending a message after every 2 steps would be quite tedious if you need your Marty to cover some distance. Luckily, there is a way to tell Marty to walk farther. The following tells Marty to make 10 steps:

my_marty.walk(10)

Most messages like "walk" can accept more detailed pieces of information in the brackets behind them. These pieces of information are called arguments. We say that "walk" accepts the number of steps as its first argument.

Short steps

Another argument that "walk" accepts is the step length, but this one is written a bit differently. For example, we can make Marty take 5 very short (8mm) steps:

my_marty.walk(5, step_length=8)

Challenge: Can you use the step_length argument to make Marty walk backwards?