Venelin Petkov Making things work since 1982

Blog Migration

Update: Blog Migration to Github

For over a year now I have been running this blog on the Ghost platform inside a cheap Digital Ocean instance. I needed a server for some hobby projects, so it was not a big deal, I am not expecting big traffic here anyway. Recently I grew more ambitious (see Engineering Learning Machines), therefore I don’t have much use for the Digital Ocean instance anymore, so it didn’t make sense to support it only for the blog. Fortunately, GitHub offers a free hosting solution called GitHub Pages (which I did use a couple of years back) that is free for lower-traffic sites like this one. This platform is based on Jekyll, a static web page generator.

The migration was a breeze, mostly due to the Jekyll Ghost Importer plugin. The following post by Nikhita Raghunath also offers some valuable tips on how to configure your Jekyll instance as a blog.

How to install a Raspberry Camera Node on ROS Kinetic (Raspbian Stretch)

Raspicam Installation Tutorial

In order to use the Raspberry Pi 3 camera v2, we need to install a third-party ROS node from source, since it is not part of the ROS distribution at the moment. The installation is not that straightforward using only the barebones ROS installation, since there are a few dependencies on other packages. Looking at the package definition package.xml, we see the following dependencies:

  • catkin
  • compressed_image_transport
  • roscpp
  • std_msgs
  • std_srvs
  • sensor_msgs
  • camera_info_manager
  • dynamic_reconfigure
  • libraspberrypi0

The highlighted ones are missing from the ros_comm stack, so we need to install them manually. The approach here is simply to fetch the missing packages and then merge them into the existing barebones catkin workspace. Lastly, we build and test raspicam_node.

1. Install all dependencies

Fetch the package information for all the missing packages and their ROS dependencies:

rosinstall_generator compressed_image_transport --rosdistro kinetic --deps --wet-only --tar > kinetic-compressed_image_transport-wet.rosinstall

rosinstall_generator camera_info_manager --rosdistro kinetic --deps --wet-only --tar > kinetic-camera_info_manager-wet.rosinstall

rosinstall_generator dynamic_reconfigure --rosdistro kinetic --deps --wet-only --tar > kinetic-dynamic_reconfigure-wet.rosinstall

Now we need to fetch the sources and put them to the ~/ros_catkin_ws/src where all the other packages from the barebone installation are located:

wstool merge -t src kinetic-compressed_image_transport-wet.rosinstall
wstool merge -t src kinetic-camera_info_manager-wet.rosinstall
wstool merge -t src kinetic-dynamic_reconfigure-wet.rosinstall
wstool update -t src

Fetch any additional Raspbian libraries that are needed

rosdep install --from-paths src --ignore-src --rosdistro kinetic -y

Build the packages. Please, note that this takes a very long time, so it might be a good idea to build it overnight in a tmux window.

./src/catkin/bin/catkin_make_isolated -j1 --install --install-space /opt/ros/kinetic -DCMAKE_BUILD_TYPE=Release

It turns out that raspicam_node depends on the raspberry pi library, so we also install the headers:

sudo apt-get install libraspberrypi-dev
2. Build the raspicam node

Check out the source code for raspicam_node from Github in the workspace src directory:

cd ~/ros_catkin_ws
git clone https://github.com/UbiquityRobotics/raspicam_node.git

Install other library dependencies automatically:

rosdep install --from-paths src --ignore-src --rosdistro kinetic -y

Finally, build and install raspicam_node. It should be possible to do this more specifically with --pkg raspicam and save some time, but this hasn’t been tried yet. Two compilation processes -j2 are a safe option here:

./src/catkin/bin/catkin_make_isolated -j2 --install --install-space /opt/ros/kinetic -DCMAKE_BUILD_TYPE=Release
3. Test the camera

Now that we have the camera node installed, we can test the Raspberry camera if we haven’t done that yet. It needs to be enabled with raspi-config from the interface menu:

sudo raspi-config

Take a test shot

raspistill -o test.jpg

Everything is fine, so we can test the raspicam node.

4. Test raspicam_node

Start a new tmux session and source the setup file in every relevant window

source /opt/ros/kinetic/setup.bash

Open a new window for roscore and start it there. Find the launch definitions in ~/ros_catkin_ws/src/raspicam_node/launch/ and go there:

cd ~/ros_catkin_ws/src/raspicam_node/launch/

Start raspicam_node with the launch configuration of choice:

roslaunch camerav2_1280x960.launch

A simple topic check shows us that the node is active:

[email protected]:~$ rostopic list
/raspicam_node/camera_info
/raspicam_node/image/compressed
/raspicam_node/parameter_descriptions
/raspicam_node/parameter_updates
/rosout
/rosout_agg

How to Install ROS (Robot Operating System) On Raspberry Pi 3 with Raspbian Stretch

Installation Instructions

An easier way to install ROS on Raspberry Pi is to use the Linux Mate distribution for Raspberry. However, I am unwilling to use bloated software, so here we do it the hard way :).

This guide describes how to install a barebone kinetic ROS distribution. The installation of additional packages is described in follow-up posts.

Preparation

We need to install some catkin bootstrap packages first. In order to do this, we must add the ros software repository. It seems that Raspbian Stretch is missing the certificate management service by default, so we have to install it first:

sudo apt-get install dirmngr

Now we can add the ROS repository to apt:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

Add the public key:

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

Update apt:

sudo apt-get update

Install the bootstrap dependencies:

sudo apt-get install python-rosdep python-rosinstall-generator python-wstool python-rosinstall build-essential

This will pull some other package dependencies, for instance build-essentials.

Installation

1. Initialize the ROS package system

The first step is to initialize the rosdep package manager. As per the documentation, do not use sudo for rosdep update:

sudo rosdep init
rosdep update
2. Create the catkin workspace
mkdir ~/ros_catkin_ws
cd ~/ros_catkin_ws
3. Build a list of packages and dependencies

The command rosinstall_generator is used to plan out the build process by generating a *.rosinstall file. It resolves all dependencies for the specified package and writes them down in the rosinstall file. That file is used by the wstool to fetch the source code for all packages.

The very minimal installation is based on the ros_comm package:

rosinstall_generator ros_comm --rosdistro kinetic --deps --wet-only --tar > kinetic-ros_comm-wet.rosinstall

Here --wet-only describes that only catkin (the new build system) packages are to be fetched. If you don’t want to start with the very basic system, there are a few other options available:

  • robot
  • perception
  • move-arm
  • simulators
  • viz

For more info check the documentation at the ROS Wiki.

4. Fetch the packages

The command wstool is used to obtain the source code automatically. It can download the packages in parallel, controlled by the -j option. In this step, a higher parallelization (-j8) is fine:

wstool init -j8 src kinetic-ros_comm-wet.rosinstall

If the wstool init command fails or it gets interrupted, the download can be resumed as follows:

wstool update -j4 -t src

When finished, the source code for the required packages can be found in

~/ros_catkin_ws/src

Even though we have downloaded the packages in ros_comm, there might be external dependencies (libraries and tools) that are not yet installed on the Raspbian OS. In order to fetch them automatically, we invoke the following command:

rosdep install --from-paths src --ignore-src --rosdistro kinetic -y

It installs everything via the apt package manager. The dependecies are resolved for all ROS source packages in the --from-paths directory. Since the above command is recursive, we tell it via --ignore-src not to install the packages already in the src directory. As we are not working in the ROS environment yet, we have to also specify the distribution name with the --rosdistro parameter.

5. Build the ROS packages

The command for building the packages has been fetched with wstool along with the source code and is located in the src directory. The catkin_make_isolated is used for a mix of catkin and cmake packages. If only catkin packages are to be built, the catkin_make should be used instead (custom-built ROS packages).

The Raspberry 3 has limited memory. With the default parallel setting -j4, it overflows and the build process fails. Empirically, -j2 is a good parameter.

Finally, the install location can be specified with the --make-space parameter. In order to simplify the process, we can make the current user owner of the target directory where we want to install ROS:

sudo mkdir -p /opt/ros/kinetic
sudo chown rseed42:rseed42 /opt/ros/kinetic

Start the build command:

./src/catkin/bin/catkin_make_isolated -j2 --install --install-space /opt/ros/kinetic -DCMAKE_BUILD_TYPE=Release

If there is a problem with the compilation (crash due to memory exhaustion), the command can be issued again and the packages that are already compiled will be skipped, so it continues from where it left off.

6. Test the installation

ROS uses a few environment variables that have to be set up prior to using it. According to the build command above, we can find a setup script at:

source /opt/ros/kinetic/setup.bash

Check that the ROS environment variables are set:

ROS_ETC_DIR=/opt/ros/kinetic/etc/ros
ROS_ROOT=/opt/ros/kinetic/share/ros
ROS_MASTER_URI=http://localhost:11311
ROS_PACKAGE_PATH=/opt/ros/kinetic/share
ROSLISP_PACKAGE_DIRECTORIES=
ROS_DISTRO=kinetic

We need to start the ROS core process as follows:

roscore

We can now check the default topics with:

[email protected]:~/ros_catkin_ws$ rostopic list
/rosout
/rosout_agg

Everything seems fine, this was the easy part!

Installing Arduino on Raspberry Pi 3

Overview

This guide will show how to install the latest Arduino IDE and supporting packages to enable the programming of Arduino devices from the Raspberry Pi 3.

You will need a USB to Serial FTDI converter in order to program the Arduino devices and to receive the output of the serial console when testing them.

The complete setup allows us to develop and program Arduino devices on the command line, without any IDE. We are going to use up-to-date versions of the software, so most of the packages will have to be fetched from Github and installed locally.

Preliminary Steps

First, let’s organize the locally installed packages on the Raspberry. I like to use a directory called “local” for this purpose. Since there are no other users, it makes no sense to install anything in system directories. We will place them in ~/local:

mkdir ~/local
cd local

Arduino IDE

Because of some compatibility issues, I prefer to install an up-to-date version of the Arduino IDE. The official Debian package is just too old and not compatible anymore with newer libraries.

Download to the website https://www.arduino.cc/en/Main/Software. Make sure that you select the Linux ARM (experimental) version. The AMD64 architecture doesn’t work on the Raspberry. The current version is

ARDUINO 1.6.13

Download the package and move it to the Raspberry with something like:

scp arduino-1.6.13-linuxarm.tar.xz raspi:local

The package can be decompressed inside the ~/local directory using:

cd ~/local
tar -xf arduino-1.6.13-linuxarm.tar.xz

Now we have the Arduino IDE installed under

~/local/arduino-1.6.13

It contains libraries, board description files, etc.

We can not compile the source files (Arduino sketches) with a plain old Make file, because there are many dependencies and hidden parts from the developer. Therefore, we need Arduino.mk which enables us to work on the command line. It lives on Github, so we just need to clone it to the ~/local directory:

[email protected]:~$ git clone https://github.com/sudar/Arduino-Makefile ~/local/arduino_mk
Cloning into '/home/vpetkov/local/arduino_mk'...
remote: Counting objects: 2483, done.
remote: Total 2483 (delta 0), reused 0 (delta 0), pack-reused 2483
Receiving objects: 100% (2483/2483), 1.48 MiB | 522.00 KiB/s, done.
Resolving deltas: 100% (1097/1097), done.
Checking connectivity... done.

Now we are set up to make a first test.

Test Arduino Installation

First, create a directory that contains the code:

mkdir ~/test

We need a Makefile and an *.ino file. Let’s create these:

cd ~/test
touch Makefile
touch Test.ino

Now, please keep in mind that the programming test requires the following devices:

  1. A USB to Serial FTDI adapter
  2. Arduino Pro Mini 3.3v

The Makefile is tailored for this particular device. If you have another, the hardware-specific parts have to be updated.

Makefile:

BOARD_TAG=pro328
MCU=atmega328
MONITOR_PORT=/dev/ttyUSB0
ARDUINO_DIR = /home/$(shell whoami)/local/arduino-1.6.13
ARDMK_DIR = /home/$(shell whoami)/local/arduino_mk
USER_LIB_PATH=/home/$(shell whoami)/local/lib
BOARD_TAG = pro
BOARD_SUB = ATmega328
MCU = atmega328p
F_CPU=8000000L
AVRDUDE_ARD_BAUDRATE = 57600
include $(ARDMK_DIR)/Arduino.mk

The source code is also very simple:

Test.ino:

void setup(){
  Serial.begin(9600);
  Serial.println("Arduino on Raspberry Pi Test");
}
void loop(){
  delay(2000);
  Serial.println("Still there");
  Serial.print("\n");
}

Simply compile with “make”. All tools are contained in the Arduino IDE and all configuration in the Makefile:

make

The program should now be compiled successfully.

When you connect the FTDI converter, make sure that it appears as /tty/USB0. This can be checked with dmesg:

[email protected]:~/test$ dmesg | tail
[ 8864.644767] usb 1-1.4: Product: CP2102 USB to UART Bridge Controller
[ 8864.644780] usb 1-1.4: Manufacturer: Silicon Labs
[ 8864.644792] usb 1-1.4: SerialNumber: 0001
[ 8865.808457] usbcore: registered new interface driver usbserial
[ 8865.808562] usbcore: registered new interface driver usbserial_generic
[ 8865.808649] usbserial: USB Serial support registered for generic
[ 8865.812262] usbcore: registered new interface driver cp210x
[ 8865.812395] usbserial: USB Serial support registered for cp210x
[ 8865.812559] cp210x 1-1.4:1.0: cp210x converter detected
[ 8865.813036] usb 1-1.4: cp210x converter now attached to ttyUSB0

We can now program the Arduino. Make sure that you have correctly connected the RX/TX and the reset pins, as well as the Vcc/Ground. You need the 100nF capacitor on the reset line, for it to work correctly.

Please, also make sure that the Python python-serial package is installed, since it is used by Arduino.mk:

make upload

If sucessful, you should see something like:

/home/vpetkov/local/arduino-1.6.13/hardware/tools/avr/bin/avrdude -q -V -p atmega328p -C /home/vpetkov/local/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf -D -c arduino -b 57600 -P /dev/ttyUSB0 \
		-U flash:w:build-pro-ATmega328/test.hex:i

avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "build-pro-ATmega328/test.hex"
avrdude: writing flash (1668 bytes):
avrdude: 1668 bytes of flash written

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

make[1]: Leaving directory '/home/vpetkov/test'

Now we can test the message that the Arduino sends to us. I like to use a python command to monitor the serial port:

python -m serial.tools.miniterm -p /dev/ttyUSB0 -b 9600

You should see period messages like:

Still there

Press Ctrl and ] to exit.

User Libraries

You might have noticed the line

USER_LIB_PATH=/home/$(shell whoami)/local/lib

This is where I put my user libraries. For instance, my temperature/humidity sensor uses the DHT library:

git clone https://github.com/adafruit/DHT-sensor-library ~/local/lib/DHT

Also, I use MySensors for the wireless network:

git clone https://github.com/mysensors/MySensors ~/local/mysensors

Raspberry Pi 3 Software Install

Preliminary Configuration

First, let’s update the system:

[email protected]:~$ sudo apt-get update

and upgrade any available packages

[email protected]:~$ sudo apt-get upgrade

Useful Software Packages

First, let’s install a familiar text editor. This goes a little bit against the minimalist concept, but since I am also using the Raspberry for development, it is actually useful to have something like that here. You are free to choose your tools, of course.

sudo apt-get install emacs

A mandatory package is Git

sudo apt-get install -y git

If you plan to program Arduino from the Raspberry Pi on the command line, then one very useful tool is the Python serial library.

sudo apt-get install -y python-serial

I like to use a terminal multiplexer to run long-running processes that I haven’t daemonized yet:

sudo apt-get install -y tmux

A useful resource monitoring tool is a modern version of top:

sudo apt-get install htop

One of the most important commands is gpio utility of the wiringpi library. It can be used directly to turn on/off gpio pins and also to show their state.

sudo apt-get install wiringpi

Example test command

[email protected]:~$ gpio readall
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+

Enable Some Kernel Features

If you want to connect some devices directly to the Raspberry, you might need to use a couple of additional features, so you can optionally enable them with the

sudo raspi-config

tool.

More specifically, these are interfaces that allow you to program microcontrollers and connect some devices over i2c:

  • Enable SPI interface
  • Enable i2c interface