We've got a new Marty User's site for Activities, Tutorials and Educational content       Check it out →  

Get Started with MartyPy
Connecting and Coding

Posted   —   № 5
6,049 Views

Page 2 of 2

Connecting to Marty over WiFi

The most common way of actually using martypy to talk to a Marty is over WiFi. To do this, you’ll need to know the IP Address of your Robot. You can use this online tool to discover Martys on your network.

Now you’ve got the IP address of your Marty, we can write a small script to get you up and running. Open a new python file in your editor of choice - We’d recommend Atom or Sublime Text for beginners. You could also use an IDE (Integrated Development Environment) for Python, like IDLE or PyCharm.

Copy & Paste the following code into the file, changing the IP address to the one found using the online tool:

1
2
3
import martypy
mymarty = martypy.Marty('socket://192.168.0.x') # Change IP accordingly
mymarty.hello()  # Move to zero positions and wink

Save this as mymarty.py in the martypy-tutorial folder we made earlier. Now we can run the code! If you’re using an IDE there’ll probably be a button for doing this, but otherwise, open a terminal, navigate to the martypy-tutorial folder, check you’ve source‘d the Virtual Environment, then type the blow and hit enter.

$ python ./mymarty.py

Marty should move to zero position and wink. This means that Marty should be standing in an upright, straight position.

Doing a bit more

Just as in the last step, open the mymarty.py file. We’ll add something a little bit more initeresting than just a wink – We’ll make Marty walk a couple of steps!

The function for walking is, funnilly enough, called walk. We’ll add a little bit of code so that your Marty will walk forward a couple of steps each time you press the Enter key

1
2
3
4
5
6
7
8
import martypy
mymarty = martypy.Marty('socket://192.168.0.x') # Change IP accordingly

print("Hit 'Enter' to walk two steps forward.")
print("Press Control + C to exit.)
while True:
    input()
    mymarty.walk()

More Examples…

Make Marty play a sound:

1
2
3
import martypy
mymarty = martypy.Marty('socket://192.168.0.x') # Change IP accordingly
mymarty.play_sound(8000, 200, 1000) # Play a tone
1
2
3
import martypy
mymarty = martypy.Marty('socket://192.168.0.x') # Change IP accordingly
mymarty.play_sound(8000, 200, 1000) # Play a tone

Now visit the docs for all the other functions in MartyPy and how you can use them! and keep an eye on the Tutorials and Articles section of the site :D