Posted   —   № 8
Page 2 of 2

ROSing around

So now that you’ve gotten to know and ROS a little better, let’s dip our toes into the ROS ocean and see what Marty is capable of. As a warning, I’d recommend doing a few of the official ROS tutorials first. Having gone over the basic concepts already, it’s crucial that you get a ground-level understanding of how to actually use ROS.

Things you’ll need

Getting set up

If you haven’t done so already, see our post on how to get a Pi set up in your Marty. Ideally, using our official Pi image would be best, as it would save you time with getting ROS set up. So I’ll assume that your Pi has that.

On your computer, I’d recommend installing tmux - a terminal multiplexer - as the ability to open multiple panes within the one terminal is super useful for ROS (and terminal use in general). I’d also recommend a network scanner like AngryIP, which will let you scan your network for IP addresses.

Networking

The first thing you have to do to get your Marty and your Computer talking to each other. As ROS is network based, this is crucial to do anything!

On your computer, open up a terminal and enter:

$ ifconfig

This will list your computer’s networking information. What you’re interested in here is the IP address of your computer. Look for your network device (if you’re on Wi-Fi it might be something like wlan0) and take note of the inet addr. Usually, this takes the form of 192.168.x.x.

Next, fire up a network scanner and find the IP address of your Marty. In this case, we’ll use the aforementioned AngryIP. If it hasn’t been set by default, set the IP range in the top left to go from 192.168.0.0 to 192.168.0.255 and click Start.

If your Marty is connected to the same network as your computer, it’ll find it. Scroll down until you see your Marty’s name in the Hostname collumn and note it’s IP address.

Now, we can get them talking to each other.

Add your Marty’s local IP address and hostname to your computer’s hosts file located in /etc. The easiest way to do this would be through the terminal:

sudo nano /etc/hosts

Your hosts file should look something similar to this:

127.0.0.1   localhost
127.0.1.1   MyComputer
# INSERT MARTY CREDENTIALS HERE

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Insert your Marty’s local IP and hostname in the same format as the existing hosts.

From here onwards, all examples will use a Marty’s IP address of 192.168.0.12 and a name of marty – it is likely that yours will be different so take care to change these to the IP and name of your Marty where appropriate.

127.0.0.1     localhost
127.0.1.1     MyComputer
192.168.0.12  marty

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Now, we want to do the same thing on Marty, but with your computer’s details.

First, open a new terminal and ssh into your Marty. If you’ve entered your Marty’s local IP and hostname correctly, you should be able to do this easily through a terminal command. Using an example name of marty:

$ ssh pi@marty

The default password is: marty.

If you’re asked to authenticate, just enter yes.

Your prompt should change to pi@marty, which indicates that you have successfully ssh‘ed into your Marty. Now, you want to edit your hosts file similarly to before but with your computers IP and hostname.

Finaly, on your computer, you may wish to add the following to the end of your .bashrc file:

export ROS_MASTER_URI=http://192.168.0.12:11311
source /opt/ros/kinetic/setup.bash

Your .bashrc is located in your Home directory and can be edited from the command line like so:

$ nano ~/.bashrc

Every time a new terminal is opened, this sets the ROS_MASTER_URI of your PC to point to the Marty, and in addition sources the correct setup file. Of course, if your Marty’s IP is different, then you’ll have to change it accordingly.

And that should be you all set up to use ROS! Congrats.

Since ROS is network dependent, it's suscepible to issues that can occur on your network, such as latency. If you're experiencing slow down or delays then the prime suspect will be your network.

Actual ROSing around

Okay, so now that your computer and your Marty can talk to each other, let’s get stuck in.

By now, you may have a lot of terminal windows open, so you can probably close most if not all of them and we’ll start fresh.

Let’s assume for now that your robot’s name is marty.

The first thing you want to do is ssh into your Marty from a new terminal:

$ ssh pi@marty

The official Pi image already has tmux installed and configured, and I’ve taken the liberty of placing a little script there to get a session started a bit quicker. In the terminal that you’ve ssh‘ed into, enter:

$ devmux

This will open a tmux session called martydev, with three panes. On one of them (doesn’t matter which, but maybe the left one) enter:

$ rostopic list

This will list all of the topics that Marty can see on the network. For example, you’ll see /marty/accel near the top of the list, which publishes Marty’s accelerometer data. We can see what this looks like from the terminal. In another pane, enter:

$ rostopic echo /marty/accel

You should see a stream of data in your terminal pane that looks something like:

---
x: 0.0
y: 0.0
z: 1.0
---

Don’t worry if it’s fluctuating, that’s normal.

Now, carefully pick your Marty up and tilt your robot slowly from an upright position, to horizontally left, and then horizontally right. Notice how the z value changes as you tilt?

Technically, the laptop you’re using isn’t the one receiving this data, as you’ve ssh‘ed in to your Marty. So let’s make it! Go ahead an open up another terminal, either an entirely new window or a tab (Ctrl + Shift + T). Assuming your nework setup has been right, you’ll be able to see the topics on the network and subscribe to /marty/accel in the exact same way. Go ahead and do so. Your computer should now be receiving a stream of data. This data has travelled from the Rick, to the Pi, across your network and finally to your computer. Pretty neat, right?

Reading data is all fine and great, and something you’ll definitely need to know how to do if you’re going to develop cool applications, but you’ll also need to know how to send data.

Switch back to your ssh terminal window. In any pane, enter:

rostopic pub /marty/enable_motors std_msgs/Bool "data: true"
rostopic pub /marty/keyframe_file std_msgs/String "data: 'wave'"

Your Marty should have waved his arms. Good stuff! This command doesn’t specifically tell Marty which joint to move etc but instead tells Marty to read something called a keyframe file. This file describes which servos to move, by how much and for how long, so it’s super easy to string together a sequence of actions. The wave file is located on your Marty, at ~/marty_ws/src/ros_marty/cfg/keyframes. If you navigate to this directory through the command line and use nano, you can see the contents and edit:

nano wave.csv

Each ‘frame’ of motion is one line in the file. Each action is described sequentially by three parameters: time of movement (seconds), servo number (0 to 8), servo position (-126 to 126). Feel free to edit the values here and experiment. When you’re done, make sure to save! Then you can enter the keyframe_file command like before.

You can also send Marty direct servo commands through the command line, if you wish. Use Tab completion for this one, as you can’t enter it manually. In a terminal pane, enter:

rostopic pub /marty/servo marty_msgs/ServoMsg

And then press Tab twice (make sure there’s a space after marty_msgs/ServoMsg), this will autocomplete the data section of the message to be published. From here, just use the arrow keys on your keyboard to move the cursor and edit the servo_id (0 to 8) and servo_cmd (-126 to 126) values to whatever you wish. Occasionally, the terminal input can be a bit buggy and get confused with the line break, but Ctrl + C and retyping the command will fix it (or pressing the Up arrow if you’ve already typed it in before).

For an example script, take a look in the ~/marty_ws/src/ros_marty/scripts folder. If you run servo_test.py, Marty will continually move his eyes and left arm. Feel free to take a look at this script for an example on how to set up a simple publisher. I’d also recommend looking at the official ROS tutorials (once again!) on how to write a simple publisher and subscriber, there are C++ and Python versions.

ROS is a relatively big, complex and powerful tool, and I hope this very basic guide has made things a little easier to get started. Don’t be put off if you don’t understand something straight away, or if something isn’t working quite right. Keep at it.

On a final note, if you’re having a problem and you’re struggling to figure out exactly what it is, the roswtf tool is very handy. Just enter roswtf and wait for a report, it might not point to the exact cause of your issue but it might push you in the right direction.