Raspberry and ROS

From Rsewiki

Back to Robobot

Installation from blank SD-flash

Install Raspberry

Use Raspberry pi imager (download from https://www.raspberrypi.com/software/)

rpi-imager

First configure settings: click the gear icon, and set:

  • enable ssh, hostname, English language, Danish keyboard, possibly SSID, time zone, username (local) and password (remember to spell the password correctly).

then

Select operating system 'Raspberry Pi OS lite' and storage, an empty SD-flash of at least 16GB, then write.

When finished, mount the SD into the Raspberry

Start Raspberry

If everything is configured as above, then you just need to figure out the IP.

else

You need a screen and keyboard. Connect to the network to get an IP and for updating and date/time.

Find your IP address using:

sudo apt install net-tools nmap
ifconfig

Or from another computer on the same local net (if your local net is 192.168.2.XX)

nmap -sP 192.168.2.0/24

Wifi connect

The Raspberry uses the configuration in

cd /etc/wpa_supplicant
sudo nano wpa_supplicant.conf

@todo how to setup - copy from old installguide?

SSH from another computer

If the Raspberry has an IP of 192.168.2.39, then:

ssh local@192.168.2.39

or

ssh -X local@192.168.2.39

If your computer support X-graphics, e.g. has X-ming installed

Putty or other tools could be used too.

Install tools

Install subversion and other tools

Note: if you are on DTU network, then the date on the Raspberry will most likely not be set correct, and that may give problems for updating the operating system. If so you may need to install NTP and NTPDATE first and then configure /etc/ntpconf as described below.

ntp               # time setup
ntpdate           # time setup from DTU net
libreadline-dev   # command line memory
libopencv-dev     # openCV
python3-pyqtgraph # fancy graphs in Python
python3-scipy     # scientific functions 
python3-serial    # USB communication
pyqt5-dev         # QT designed GUI
pyqt5-dev-tools   # design QT GUI
festival          # text to sound
sox               # sound format read/write
libsox-fmt-all    # formats for SOX
v4l-utils         # video utilities
apt-file          # for finding missing system files (optional)
qv4l2             # for testing camera (optional)
kate              # graphical editor (optional - nano is faster)
xinit             # to run server (graphical user interface)
cmake             # to build apps
pip               # Python package install
sudo apt update
sudo apt dist-upgrade
sudo apt install ntp ntpdate libreadline-dev subversion libopencv-dev python3-pyqtgraph python3-scipy python3-serial pyqt5-dev pyqt5-dev-tools festival sox libsox-fmt-all apt-file kate qv4l2 v4l-utils xinit cmake pip

NTP when on DTU net

Network time protocol (NTP), to keep clocks in sync. At DTU most clock sources are blocked, the clock source needs to be configured.

At DTU, edit /etc/ntp.conf, add ntp.ait.du.dk to the NTP server pool, like:

...
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool ntp.ait.dtu.dk
pool 0.ubuntu.pool.ntp.org iburst
...

This should ensure a proper date on a DTU net and when the internet is available elsewhere.

Sync time (if on DTU net)

sudo ntpdate -u ntp.ait.dtu.dk
date

Install ROS base

I will use Noetic (if it is ethical to do so).

Follow the instructions at http://docs.ros.org/en/humble/Installation.html like this:

Setup your computer to accept software from packages.ros.org.

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

Set up your keys

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

Update package list with the new source

sudo apt update

but it may fail with "The repository 'http://packages.ros.org/ros/ubuntu jammy Release' does not have a Release file."

The repository adds a file /etc/apt/sources.list.d/ros-latest.list. Modify the line in that file to include [trusted=yes], like:

sudo nano /etc/apt/sources.list.d/ros-latest.list
deb [trusted=yes] http://packages.ros.org/ros/ubuntu bullseye main

Then redo the (a few failed to fetch is OK)

sudo apt update

Install ros-desktop-full-dev or ros-desktop-full and a bit more to use a Catkin workspace:

sudo apt install ros-desktop-full python3-rosinstall libmap-msgs-dev

Install ROS 2 base (mostly for ubuntu 22.04)

NB! do not use ROS2 for Robobot on Raspeberry Pi in 2023 version!, not compatible

Using "humble" version.

In general, follow the instructions at http://wiki.ros.org/noetic/Installation/Ubuntu like this:

Set up keys (may need to install curl first 'sudo apt install curl')

sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

Add package source

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

Update package list with the new source

sudo apt update
sudo apt dist-upgrade

Install some tools

sudo apt install \
 python3-flake8-docstrings \
 python3-pip \
 python3-pytest-cov \
 ros-dev-tools

And for Ubuntu 22.04:

sudo apt install \
  python3-flake8-blind-except \
  python3-flake8-builtins \
  python3-flake8-class-newline \
  python3-flake8-comprehensions \
  python3-flake8-deprecated \
  python3-flake8-import-order \
  python3-flake8-quotes \
  python3-pytest-repeat \
  python3-pytest-rerunfailures

Install in the ros-desktop version:

sudo apt install ros-humble-desktop
sudo apt install python3-colcon-common-extensions python3-rosinstall libmap-msgs-dev

Add the installed environment to .bashrc and run it once for this terminal.

echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source /opt/ros/humble/setup.bash

Make a ROS2 workspace.

mkdir -p ros2_ws/src
cd ros2_ws
colcon build --symlink-install
source install/setup.bash

Get the ROS2 Teensy bridge into the workspace

cd ~/ros2_ws/src
svn co svn://repos.gbar.dtu.dk/jcan/ros2_bridge/test_bridge
svn co svn://repos.gbar.dtu.dk/jcan/ros2_bridge/teensy_bridge_messages

Compile the bridge

cd ~ros2_ws
colcon build

Robobot from SVN

Install the needed Regbot and Fejemis packages. Fejemis is for the bridge, which also is a ROS bridge. Regbot is for the firmware, and the user interface used for the sensor calibration.

We will install these packages in an 'svn' directory, and make an easy link to the relevant parts.

mkdir svn
cd svn
svn checkout svn://repos.gbar.dtu.dk/jcan/fejemis
svn checkout svn://repos.gbar.dtu.dk/jcan/regbot
svn checkout svn://repos.gbar.dtu.dk/jcan/robobot

and then the links

cd
ln -s svn/regbot/regbot_gui
ln -s svn/fejemis/ROS/catkin_ws

Teensy firmware compile

NB! This procedure normally requires that the Raspberry is allowed to show graphics, i.e. use the '-X' option when ssh to the Raspberry. This is to install teensyduino and upload code to Teensy itself. All other steps are command-line only. A command-line alternative is described below.

Download Arduino from https://www.arduino.cc/ ; find the software and download version 1.8.19 for Linux arm 32 bit.

Download also TeensyduinoInstall.linuxarm and 00-teensy.rules from https://www.pjrc.com/teensy/td_download.html - again the arm 32 bit version

Assuming these files end in the Downloads directory, then

cd
cd Downloads
tar -xf arduino-1.8.19-linuxarm.tar.xz

Arduino is now ready to run, but we need the Teensy addition, so:

sudo cp 00-teensy.rules /etc/udev/rules.d/
chmod +x TeensyduinoInstall.linuxarm
./TeensyduinoInstall.linuxarm

This should start the Teensyduino installer, guide the installation to the Arduino.1.8.19 just unpacked and install all packages.

We need some extra packages; we put them in a new 'git' directory:

cd
mkdir git
cd git
git clone https://github.com/adafruit/Adafruit_SSD1306.git
git clone https://github.com/adafruit/Adafruit-GFX-Library.git
git clone https://github.com/adafruit/Adafruit_BusIO.git

Next, these libraries need to be put where the Makefile looks for them, so link as follows:

cd
cd Downloads/arduino-1.8.19/hardware/teensy/avr/libraries
ln -s ~/git/Adafruit-GFX-Library
ln -s Adafruit-GFX-Library Adafruit_GFX_Library
ln -s ~/git/Adafruit_SSD1306
ln -s ~/git/Adafruit_BusIO

(The GFX library needs to be renamed to use '_' for '-' for some strange Arduino reason)

Modify Makefile

If svn version is older than 1561, then see the next section.

Modify the Makefile (in regbot/regbot, regbot/regbot/4.1, regbot/regbot/3.5 as needed)

Modify the BASEPATH to the Arduino/Teensyduino library about line 31 of the Makefile (for Robobot it should work as is):

ARDUINO_ROBOBOT := $(shell ls -d /home/local/Downloads/arduino-1.8.19/hardware 2>/dev/null)
ifeq ($(ARDUINO_ROBOBOT),)
  # home installation - modify as needed
  BASEPATH := /home/chr/Downloads/arduino/arduino-1.8.19/hardware
else
  # raspberry pi based RoboBot
  BASEPATH := /home/local/Downloads/arduino-1.8.19/hardware
endif

This change is actuated, as my development tool (Kdevelop) failed to work with the previous symbolic links, but it is also a more standard solution.


Compile and upload

Now it should be possible to compile the Teensy firmware. It is a Teensy 4.1, so:

cd 4.1
make -j3

The option '-j3' requests using 3 CPU cores; it is faster but eats memory and heats up the CPU. If that seems to be a problem, then use a lower number.

To install the newly compiled regbot.hex:

Press the (red) power button on the Regbot board (to maintain power while the processor is reprogrammed)

make upload

Command line upload to Teensy

A command line upload utility, that do not use any graphics is available here: https://github.com/PaulStoffregen/teensy_loader_cli

I would normally put such utilities in a GIT directory

cd
mkdir -p git
cd git
git clone https://github.com/PaulStoffregen/teensy_loader_cli 
cd teensy_loader_cli

To compile the following package is needed

sudo apt-get install libusb-dev

The compile

cd ~/git/teensy_loader_cli
make

This makes the command teensy_loader_cli, but it needs to know which processor the HEX code is intended for, so make a script

cd <directory with compiled HEX code>
ln -s ~/git/teensy_loader_cli/teensy_loader_cli .
nano upload.bash

and enter

#!/bin/bash
~/git/teensy_loader_cli/teensy_loader_cli -v -w --mcu=TEENSY41 regbot.hex

if the HEX code is in regbot.hex and Teensy is 4.1, else modify. See also https://www.pjrc.com/teensy/loader_cli.html for more details.

Make the new script executable:

chmod +x upload.bash

And execute when needed (and press button on Teensy, when requested).

./upload.bash

Initialize the ROS environment

Initialize sets up the needed environment variables and makes a script for remaining settings for this project.

cd ~/catkin_ws
rosws init . /usr/share

This outputs a message about the location of the ROS setup script:

rosinstall update complete.
Type 'source /home/local/svn/fejemis/ROS/catkin_ws/setup.bash' to change into this environment. 
Add that source command to the bottom of your ~/.bashrc to set it up every time you log in.

Put this script into ~/.bashrc, e.g. by

echo "source /home/local/svn/fejemis/ROS/catkin_ws/setup.bash" >> ~/.bashrc

Run catkin_make in the catkin_ws directory

cd ~/catkin_ws
catkin_make

Autostart ROSCORE and bridge

Link the startup script to the home directory of user 'local' as well as a link to the bridge executable and the initialization-file used by the bridge.

cd
ln -s svn/robobot/setup/start_bridge.sh
ln -s catkin_ws/devel/lib/bridge/bridge
ln -s svn/robobot/setup/bridge.ini

This script starts roscore and the bridge in this order.

Add the script to autostart at boot:

crontab -e

Select an editor; if asked, I have selected 'nano' (an ok simple editor) In the editor, add this line:

@reboot /home/local/start_bridge.sh

End with ctrl-s to save and ctrl-x to exit (if nano is the editor)

For info: the start script is executed as user 'local' and has the following content:

#!/bin/bash
# start roscore and bridge
# this script is added as a root cronjob
# to see and edit the job
# $ crontab -e
#
cd /home/local/
date >> rebootinfo.txt
/usr/bin/roscore &
# wait for ROSCORE to start
sleep 8    
#start the bridge (as a daemon)
./bridge -d  &
echo "Bridge started with PID:" >> rebootinfo.txt
sleep 1
pgrep -l bridge >> rebootinfo.txt
exit 0

Reboot

sudo reboot now

Reenter

ssh -X local@<IP>

If all goes well, then the 'rebootinfo.txt' should end with something like this:

Sun 27 Nov 13:03:52 CET 2022
Bridge started with PID:
426 start_bridge.sh
617 bridge

If not you may see what cronjob is executed by:

grep CRON /var/log/syslog

Rename host to match robot

When the robot bridge gets the robot name from the Regbot board, it writes the name in a hostname file in the local home directory. At the next boot, this file is used to rename the hostname of the Raspberry pi.

Create the script that triggers the rename in /etc/init.d

sudo nano /etc/init.d/robobot_rename.sh

Enter the following line into the new file

bash /home/local/svn/robobot/setup/rename_host.sh

Exit the editor (ctrl-s + ctrl-x) and make the script executable, and add it to run-level 2 (before networking is started)

sudo chmod +x /home/local/rename_host.sh
ln -s /etc/init.d/robobot_rename_test.sh /etc/rc2.d/s99robobot_rename

The script is then executed when the Raspberry boots, and if the hostname does not match that of the Teensy, then the hostname is modified.

Clone the just installed image

Use SD card clone/copier (on Raspberry) Attach an extra USB based SD-card read/writer

use rpi-clone to make a copy of the running SD-card.

(takes about 5 minutes, depending on the SD-card speed)