Chapter 9

From IPRE Wiki
Revision as of 13:38, 13 July 2007 by Natasha (Talk | contribs)

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")
>>> chat.send("sandy", "Hi, how are you?")

Your friend might do the same:

>>> ch = Chat("sandy", "xxyyz")
>>> ch.receive()
>>> [("", "Hi, how are you?")]
>>> ch.send("rosie", "I'm fine, thanks!")

And you would receive:

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

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 messages sent to it. For 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 the robot.

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

>>> chat = Chat("sandy", "xxyyz")
>>> chat.send("Rosie", "robot.turnLeft(.4)")
>>> chat.receive() 

There is also a RemoteRobot constructor which acts like a regular robot, but sends the commands to the other robot.

>>> robot = RemoteRobot("remote_robot_name")
>>> robot.turnLeft(.4)

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