Chapter 9

From IPRE Wiki
Jump to: navigation, search


Who ya gonna call?
-: Ray Parkey, Jr. in the song Ghostbusters.

Your robot doesn't have a direct way of communicating with another robot. But it can communicate with your computer, and your computer can communicate with other computers. There are many ways that one computer can send a message to another computer, for example email. If you wanted to carry on a conversation, email is a bit slow. But one could also use instant messaging, or IM for short.

Myro contains an IM interface so that you can write programs that send and receive messages. To test this out, you can try this with a friend. First you will need to log into the IM server with a name and a password. You can make both of these up, but you should remember the password/ID pair. If you forget, you will need to make up a new pair. Also, you will need to know the ID of the person that you want to send a message to.

First, let's begin by making a Chat object. Let's say your robot's name is Rosie, and you have a friend whose robot is Sandy:

>>> chat = Chat("rosie", "mypassword")  #important: remember this password for later use!
>>> chat.send("sandy", "Hi, how are you?")

Your friend might do the same:

>>> ch = Chat("sandy", "xxyyz")  #important: tell your friend to remember this password for
                                 #later use!
>>> ch.receive()
>>> [("", "Hi, how are you?")]
>>> ch.send("rosie", "I'm fine, thanks!")

And you would receive:

>>> chat.receive()
[("", "I'm fine, thanks!")]

For basic chatting, you can actually create a Chat object with any username and password, and send a message to anyone whose username you know. You'll soon see that you can also use a Chat object to command other robots. However, you can only do this using a Chat object with your robot's name (in this example, 'rosie') and your robot's associated password (in this example, 'mypassword') . So, the Chat object with your robot's name is very special. Also, all username-password pairs created will be stored, so be sure to remember the password associated with your robot's name.

Remote Robot Control

Chat actually extends beyond just sending messages to your friends and their robots. There is also a way to control another person's robot using Chat. In fact, as you'll see a little later, there's another way to control other people's robots without directly using Chat.

By using Chat, both you and your friend can control each other's robots, as well as your own robots. It's probably more exciting for you, however, to control your friend's robot, since you already have a very direct method of controlling your own robot (the basic forward(1,1), for example).

Let's say your friend wants to control your robot (still named "Rosie"). You have to set up a system (RemoteControl) that allows your robot to receive the commands sent to it. To allow your robot to be controlled, first type:

>>> robot = Scribbler() # enter comX
>>> robot.initializeRemoteControl("mypassword")

To process just one command:

>>> robot.processRemoteControl()

To process commands continually:

>>> robot.processRemoteControlLoop()

What this loop does is allow your computer to continually be listening for commands, which it will (almost) instantaneously deliver to your robot.

Now your friend, Sandy's 'parent', wants to control your robot. To do so, your friend types:

>>> ch = Chat("sandy", "xxyyz")  #important: tell your friend to remember this password for
                                 #later use!
>>> ch.send("rosie", "robot.turnLeft(.4, 1)")

Your robot, Rosie, should turn left for a second.

Another way to control your friends' robots is using RemoteRobot. RemoteRobot is a constructor; it works by creating a Chat object that can only send messages to one particular robot (say Sandy), which you name. So, it acts like the robot to which it refers, but what it's actually doing is sending the commands it receives to that robot. Let's look at how to set up remote-controlling, and then I'll illustrate in more detail how it works.

You can control your friend's robot Sandy by typing the following:

>>> remoteBot = RemoteRobot("sandy")
>>> remoteBot.turnLeft(.4, 1)

This will make Sandy turn left for a second.

So you've hooked up a remote-control to Sandy. Now typing remoteBot.anyCommand() will work as if Sandy's parent had typed in robot.anyCommand(), so long as Sandy's parent has the processRemoteControlLoop running. In other words, remoteBot acts as if it is the robot Sandy. In reality, though, the Remote Robot is not Sandy; it's just sending command messages via chat over to Sandy, which Sandy then follows.

Sending Email

In order to send mail, you'll need to know your Simple Mail Transport Protocol (SMTP) server name. At Bryn Mawr College, it is

First, import the smtplib Python library:

>>> import smtplib

Then, you can make an SMTP object:

>>> s = smtplib.SMTP("")

And then use it to send mail. sendmail() takes three arguments: a from address, a list of recipient email addresses, and a text message.

>>> s.sendmail("", ["", "", ""], "Hi! Send money!")

This example will send the same message to three people, mom, dad and sis. If you want to put line breaks in your message, you'll need to use the \n (backslash followed by a small n).

(There are a few reasons while your email might not go through. For one, this could be flagged as "spam". Because email is this easy to send to millions of people at once, some have taken advantage of this fact to send unsolicited email, aka spam. Some installations might prevent email to come from any random computer without a special password. Check with your TA for details about your site.)

Previous Chapter: Chapter 8, Up: Introduction to Computer Science via Robots, Next Chapter: Chapter 10