Difference between revisions of "Chapter 9"

From IPRE Wiki
Jump to: navigation, search
Line 9: Line 9:
 
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.
 
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. Assume that your robot's name is Rosie:
+
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 = Chat("rosie", "mypassword")
 
  >>> chat.send("sandy", "Hi, how are you?")
 
  >>> chat.send("sandy", "Hi, how are you?")
  
Your friend, who's robot is Sandy, might do the same:
+
Your friend might do the same:
  
 
  >>> ch = Chat("sandy", "xxyyz")
 
  >>> ch = Chat("sandy", "xxyyz")
Line 28: Line 28:
 
== Remote Robot Control ==
 
== Remote Robot Control ==
  
The robot that will be controlled (its name is "Rosie"):
+
Chat actually extends beyond just sending messages to your friends and their robots.  There is also a way to control a robot using Chat.  In fact, as you'll see a little later, there's another way to control the robots without directly using Chat.
 +
 
 +
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).
 +
 
 +
The robot that will be controlled (your robot, "Rosie"):
  
 
  >>> robot = Scribbler() # enter comX
 
  >>> robot = Scribbler() # enter comX
Line 43: Line 47:
 
  >>>  
 
  >>>  
  
What this does is allow your computer to continually be listening for commands, which it will (almost) instantaneously deliver to the robot.
+
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:
 
Now let's say your friend, Sandy's 'parent', wants to control your robot. To do so, your friend types:
Line 53: Line 57:
 
There is also a RemoteRobot constructor which acts like a regular robot, but sends the commands to the other robot.
 
There is also a RemoteRobot constructor which acts like a regular robot, but sends the commands to the other robot.
  
  >>> robot = RemoteRobot("remoterobotname")
+
  >>> robot = RemoteRobot("remote_robot_name")
 
  >>> robot.turnLeft(.4)
 
  >>> robot.turnLeft(.4)
  

Revision as of 13:09, 13 July 2007

Communication

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()
>>> [("rosie@myro.roboteducation.org", "Hi, how are you?")]
>>> ch.send("rosie", "I'm fine, thanks!")

And you would receive:

>>> chat.receive()
[("sandy@myro.roboteducation.org", "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 a robot using Chat. In fact, as you'll see a little later, there's another way to control the robots without directly using Chat.

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).

The robot that will be controlled (your robot, "Rosie"):

>>> 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 smtp.brynmawr.edu.

First, import the smtplib Python library:

>>> import smtplib

Then, you can make an SMTP object:

>>> s = smtplib.SMTP("smtp.brynmawr.edu")

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("yourid@brynmawr.edu", ["mom@aol.com", "dad@gmail.com", "sis@hotmail.com"], "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