This assignment describes how to use basic networking with a focus on concepts most useful to robotics. First we will learn about pipes in Bash or a unix shell. You can do these exercises by sshing into your drone. When you type at a shell, each shell program reads from a stream, known as “standard input” and writes to two output streams, “standard output” and “standard error.” When you “print” in python, it writes its output to standard output. In another language, such as C, you use other functions, such as “printf” to write to standard output. We will ask you to write short programs in python as part of the assignment. You can do the assignment on your drone’s Rasberry Pi once you can SSH into the drone. To do this you will need to at least be able to power your PI and connect to it via the base station.
#!/usr/bin/env python import sys sys.stdout.write("Hello world\n")
echoto print things to standard output. Submit the command line that you used.
echo Hello World
In addition to writing to standard output, a program can read from
standard input. The program
cat, short for concatentate, reads from
standard input and writes the result to standard output.
catwith no arguments. First think about what might happen. Then type text at the command line and observe the result. What is happening? Why?
catreads the typed text from standard in. You also see an echo of what you typed on the console. Then it writes what you typed to standard out. So you'll see what you typed appear twice on the console.
#!/usr/bin/env python import sys while True: text = sys.stdin.readline() sys.stdout.write(text)
Now we will discuss pipes. You can use pipes to redirect standard input, standard output, and standard error. First, you can use “>” to direct standard output to a file. For example, “echo Hello World > test.txt” will write the text “Hello World” to “test.txt”. The program “cat test.txt” can be used to view the file.
echoand output redirect. Verify your work with
#!/bin/sh echo 1 > one.txt echo 2 > two.txt echo 3 > three.txt ls cat one.txt cat two.txt cat three.txt
lswrites the contents of a directory to standard output. The program
sortreads from standard input, sorts what it reads, and writes the sorted content to standard output. So you can use
ls | sortto print out a sorted directory list. Read the man page for sort (
man sort) to learn how to sort in reverse order. Modify the pipe script above to print the contents of a directory in reverse alphabetical order.
ls | sort -r
echoto print "hello world." Do not write to any files and use both commands one time.
echo Hello World | cat
echo Hello World
sort -r. Note that you will have to read the entire contents of standard input into memory in order to sort it.
#!/usr/bin/env python import sys lines = sys.stdin.readlines() for l in reversed(sorted(lines)): sys.stdout.write(l)
In addition to standard input and standard output, there is a third stream, standard error. If there is an error in a chain of pipes, it will be printed to the terminal rather than buried in the input to the next program.
ls -a | sortto see all files in a directory sorted. Redirect the output to a file named
ls -a | sort > sorted.txt
ls -a | sort > sorted.txt. What happens to the file
$ ls -ea | sort > sorted.txt ls: invalid option -- 'e' Try 'ls --help' for more information.
ls -ainstead of using
sortas above. If your program is correct, you should see your status messages printed when you run it, and see only the sorted output written to the
#!/usr/bin/env python import sys sys.stderr.write("Reading stdin.\n") lines = sys.stdin.readlines() sys.stderr.write("Sorting.\n") sorted = reversed(sorted(lines)) sys.stderr.write("Writing.\n") for l in sorted: sys.stdout.write(l)
Now we will explore the command
nc to learn more about basic
networking. The command
nc is short for “netcat” and is similar to
cat but works over network connections. It reads from standard
input and writes its contents not to standard output, but to a
nc www.google.com 80When you first connect, it will be silent. Then type any arbitrary text and press enter. What happens?
HTTP/1.0 400 Bad Requestalong with an error message in the body. However you did successfully connect to the server and send bits to it! So you know that the basic network connection is up.
GET / HTTP/1.1
ncto make a server. In one window, type
nc -l 12345. This will cause
ncto listen on port 12345. In another terminal on the same machine, type
nc localhost 12345. You can type a message in one window and it will appear in the other window (and vice versa). This trick can be very useful to test basic internet connectivity - if the client and server can send packets at all.
roscorelistens on port 11311. Try using
ncto connect to port 11311 on a machine where
roscoreis running, such as the PI. What happens? What protocol is roscore using to communicate?
roscoreuses XMLRPC carried over HTTP.
nmap, which scans through a range of ports (and optionally, through a range of IP addresses) and reports information.
nmap localhoston your Pi. What ports are open? Look up each port and submit what it does.
nc -l 1234command running from above. What is the difference? Why?
nc -l 1234is running, nmap will detect a server running on port 1234. However it
ncis not running, nmap will not detect a server.
roscore? Why or why not? Use
man nmapto find command line options for
nmapthat report the ROS port 11311.
nmap google.comfor me returns port 80 and 443. Port 80 is used for serving http, and 443 is for serving https (encrypted/private http connections). Google only opens those ports because they want to serve web pages (so they can give you search results, to make money). They don't open other ports because they don't want hackers breaking into their system, and every service you run on a port is a potential hack attempt.
Now we are going to talk about the public internet. So far we’ve been
doing our work using mostly localhost, the local machine we are
connected to, and google.com. The most common situation is that your
base station machine (your laptop or desktop) and your robot are
connected over TCP/IP to the same local network. Then you can look up
your machine’s IP address (
ifconfig in Unix; other ways in other
OSes), and your robot’s IP address, and connect them. How can you
find your robot’s IP address? Well it’s a chicken-and-egg problem.
If you knew the IP address, you can connect to the robot and run
ifconfig and find the IP address, but you don’t know the IP address!
What to do? There are several solutions.
nmapto identify the machines on each network. How many are there?
But what about if there is no public internet connection? What if
you want to fly your drone in the wilderness? Well, there does exist
cellular modems and sattellite connections, but you can also tell your
drone to act as a Wifi Hotspot. It can create a network and run a
DHCP server. You can configure this on your drone using the file
Then you can connect your laptop’s base station using the SSID and passphrase specified in that file, and connect to the drone.
Alternatively you can set up your laptop as the Wifi base station and configure the drone to connect to its network. The details will vary depending on your laptop OS and settings.
Your Pi is configured to be a Wireless AP Master by default. Connect to it with your base station.
You want to fly your drone autonomously for 10 miles while viewing the camera output continuously from your base station. Design a solution that will allow you to do this. What hardware, software, and network access providers do you need? Report the weight, CPU computation, and cost this capability will add to your drone.
You want to connect to the drone over Wifi but also be able to connect to the internet in the CIT using the RLAB network. Design a solution that will allow you to do this. What hardware, software, and access providers do you need? Report the weight, CPU computation, and cost this capability will add to your drone.
There are two alternatives. Both require purchasing something like a USB wifi card like this one.
Option 1: Connect the Wifi to the Rasberry Pi. Put one Wifi card on RLAB and the second one acting as a Wireless Master, as it is now. Set up “bridging” on the drone so that the drone itself is a router, and forwards internet packets back and forth to the base station, over two separate Wifi hops.
Option 2: Connect the Wifi to the base station. Configure the drone to act in Wireless Managed mode and to connect to the base station. Configure the base station as the wireless master, and set it up to run a DHCP server. (This is often done by setting up “Internet Connection Sharing” from your OS gui.)
Duckietown solved this problem at MIT using Option 1. They bought a USB Wifi card and put it on each robot. Then they configured the Rasberry Pi to act as a router.
When you are done, use this link to create your Networking Github Repo. Commit and push the relevant files (networking.pdf, and any scripts you wrote throughout the assignment) to this Github Repo before the deadline.