Skip to content

3. Build SMARTmBOT (Software)

SMART Lab at Purdue University edited this page May 27, 2021 · 38 revisions

Introduction

In this tutorial, we explain about how to setup software including Ubuntu and ROS2 for SMARTmBOT, and run SMARTmBOT based on Raspberry Pi and ROS2.

The content of this tutorial is as follows:

  1. Software Setup

  2. ROS2 Packages

You can also read or download a PDF version of the SMARTmBOT Guide/Manual, and watch a full demo video of the SMARTmBOT at https://youtu.be/cn3vcqFgf90.

1. Software Setup

This section introduces the initial settings for running ROS2 packages based on the tutorial posted on the official ubuntu website: https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#1-overview

A. Install Ubuntu 20.04 LTS Server on Raspberry Pi 4 4GB

  • Step 1. Install preinstalled Ubuntu 20.04Server on SD card (> 16GB)

Download the Raspberry Pi imager for:

Windows

macOS

Ubuntu

Run the imager, insert the SD card to your device, and select Other general purpose OS - Ubuntu - Ubuntu Server 20.04 LTS. Click "WRITE".

  • Step 2. Activate Wi-Fi

$ sudo touch /etc/netplan/01-netcfg.yaml

$ sudo nano /etc/netplan/01-netcfg.yaml

  • Add commands below to /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  wifis:
    wlan0:
      dhcp4: yes
      dhcp6: yes
      access-points:
        "<your-wifi-name>":
          password: "<your-wifi-password>"

$ sudo netplan apply

$ sudo reboot

  • (Optional) Step 3. Run SSH on your PC (or other devices).

$ ssh ubuntu@<your-wifi-ip-address>


B. Install Robot Operating System (ROS) 2

You need to follow an instruction to install ROS2-Base (bare Bones) via this link; (ROS2 Installation). The ROS2-Base installs necessary libraries: Communication libraries, message packages, command-line tools. No GUI tools.

sudo apt install -y ros-foxy-ros-base


C. Install SMARTmBOT Package

To control SMARTmBOT, you need to download a SMARTmBOT's workspace from https://github.com/SMARTlab-Purdue/smart_mbot_ws.

cd ~/<your_work_space>/src

git clone https://github.com/SMARTlab-Purdue/smart_mbot_ws

colcon build && source install/setup.bash

You will now have the SMARTmBOT packages installed and ready to run.


D. Pinout of the SMARTmBOT PCB (3rd PCB)

pin_out

E. Enable SPI and I2C

Please refer to https://askubuntu.com/questions/1130052/enable-i2c-on-raspberry-pi-ubuntu

wget https://archive.raspberrypi.org/debian/pool/main/r/raspi-config/raspi-config_20160527_all.deb -P /tmp

apt-get install libnewt0.52 whiptail parted triggerhappy lua5.1 alsa-utils -y

apt-get install -fy

dpkg -i /tmp/raspi-config_20160527_all.deb

sudo mount /dev/mmcblk0p1 /boot

sudo raspi-config

You can confirm that you have enabled I2C successfully if ls /dev/i2c* shows /dev/i2c-1.


(Optional) F. Enable sound of Raspberry Pi

cd /boot/firmware

sudo nano config.txt

Add dtparam=audio=on to to the config.txt


2. ROS2 Packages

ROS2 system

ROS2 topic list

To launch the package, type the following commands to the terminal:

cd ~/smart_mbot_ws

colcon build

source install/setup.bash

ros2 launch smart_mbot_pkg smart_mbot_pkg.launch.py


A. Control GPIO Node (WS2812B)

LED
  • topic_name: /<namespace>/writing_ws2813_rgb_strip

  • topic_type: std_msgs/msg/Float32MultiArray

  • topic_info: "data: [Red(0~255), Green(0~255), Blue(0~255), led_gap(from 1 to num_pixels(=27)), intensity(0.0 ~ 1.0), mode(0: Turn on, 1: chase, 2: rainbow), wait time for mode 1&2]"

To change the LED strip's color, publish the following command by typing ros2 topic pub /<namespace>/writing_ws2813_rgb_strip std_msgs/msg/Float32MultiArray "data: [<red>, <blue>, <green>, <led gap>, <intensity>, <mode>, <wait time>]" to the terminal.

ex) ros2 topic pub /<namespace>/writing_ws2813_rgb_strip std_msgs/msg/Float32MultiArray "data: [255, 0, 0, 1, 0.5, 0, 1]"


B. Reading ADC via SPI Node

LED
  • topic_name: /<namespace>/reading_spi_adc

  • topic_type: std_msgs/msg/Float32MultiArray

  • topic_info: [right_line_sensor, left_line_sensor, extra_ADC_pin_3, extra_ADC_pin_4, extra_ADC_pin_5, extra_ADC_pin_6, extra_ADC_pin_7, extra_ADC_pin_8]

To read from the sensors, type the following command ros2 topic echo /<namespace>/reading_spi_adc to the terminal.


C. Control DC Motors Node

  • topic_name: /<namespace>/writing_dc_motor_vel

  • topic_type: std_msgs/msg/Float32MultiArray

  • topic_info: "data: [right_motor_pwm(-255~255), left_motor_pwm(-255~255)]"

To change the motors' speeds, publish the following command by typing ros2 topic pub /<namespace>/writing_dc_motor_vel std_msgs/msg/Float32MultiArray "data: [<right_motor_pwm>,<left_motor_pwm>]" to the terminal.

ex) ros2 topic pub /<namespace>/writing_dc_motor_vel std_msgs/msg/Float32MultiArray "data: [100,100]"

If both right and left motors have different speeds, you need to change parameters called motor_weight on the raspberry pi board.

cd /home/ubuntu/smart_mbot_ws/src/smart_mbot_pkg/config

Copy existing the yaml file to a new YAML including the new name of the namespace, then change parameters in the pasted yaml file.

cp smartmbot_1__Writing_DC_Motor.yaml smartmbot_<namespace>__Writing_DC_Mtor.yaml

sudo nano smartmbot_<namespace>__Writing_DC_Motor.yaml

smartmbot_<namespace>:
  Writing_DC_Motor:
    ros__parameters:
      use_sim_time: false
      w_left_motor: 1.0
      w_right_motor: 1.0

Then, re-build the workspace as below:

cd /home/ubuntu/smart_mbot_ws

colcon build --symlink-install

source ./install/setup.bash

ros2 launch smart_mbot_pkg smart_mbot_pkg.launch.py


D. Reading ToF sensors

  • topic_name: /<namespace>/reading_i2c_VL53L0X_tof

  • topic_type: std_msgs/msg/int32MultiArray

  • topic_info: [0 degree ToF, 45 degree ToF, 90 degree ToF, 135 degree ToF, 180 degree ToF, 225 degree ToF, 270 degree ToF, 315 degree ToF]

To read from the sensors, type the following command ros2 topic echo /<namespace>/reading_i2c_VL53L0X_tof to the terminal.

To make sure that all 8 sensors are connected to the I2C bus, type the following command to the terminal.

sudo apt install i2c-tools

sudo i2cdetect -y 1

The addresses of the sensors should show up.


E. Streaming a Pi Cam Node

To stream video using a Pi Cam, we used v4l2_camera package by following this site: https://medium.com/swlh/raspberry-pi-ros-2-camera-eef8f8b94304

  • Step 1. you need to add start_x=1 to the /boot/firmware/config.txt file to enable reading pi cam as /dev/video0 Go to the /boot/firmware/config.txt

nano /boot/firmware/config.txt

add start_x=1 to the config.txt

sudo apt install ros-foxy-image-transport-plugins

cd ~/[your_ros2_worksapce]/src

$ git clone --branch foxy https://gitlab.com/boldhearts/ros2_v4l2_camera.git

$ git clone --branch ros2 https://github.com/ros-perception/vision_opencv.git

$ git clone --branch ros2 https://github.com/ros-perception/image_common.git

$ git clone --branch ros2 https://github.com/ros-perception/image_transport_plugins.git

$ cd ..

$ sudo rosdep init

$ rosdep update

$ rosdep install --from-paths src -r -y

  • Step 3. build new packages on the workspace.

source /opt/ros/foxy/setup.bash

$ colcon build --packages-up-to v4l2_camera image_transport_plugins

$ source install/setup.bash

Then, run the v4l2_camera node.

$ ros2 run v4l2_camera v4l2_camera_node

In order to test the camera topic, run the RQT image viewer on your host machine.

ros2 run rqt_image_view rqt_image_view

  • Test Pi Camera node using a Pi camera (CMOS type)

(Optional) F. Start Raspberry Pi without logging in

Refer to this document: Link

sudo systemctl edit getty@tty1

Copy the following codes:

[Service]
ExecStart=
ExecStart=-/sbin/agetty -a ubuntu --noclear %I $TERM

(Optional) G. Set an auto-running script

Refer to this document: Link

cd /etc/systemd/system/

sudo touch startup.service

sudo nano startup.service

Copy the following lines to startup.service

[Unit]
Description=start sequence

[Service]
Type=simple
User=root
Workingdirectory=/home/ubuntu/smart_mbot_ws
ExecStart=/home/ubuntu/smart_mbot_ws/startup.sh

[Install]
WantedBy=multi-user.target

cd /home/ubuntu/smart_mbot_ws

sudo touch startup.sh

sudo nano startup.sh

Copy the following lines to startup.sh

#!/bin/bash
cd /home/ubuntu/smart_mbot_ws;sudo su;source /opt/ros/foxy/setup.bash;colcon build --symlink-install;source ./install/setup.bash;ros2 launch smart_mbot_pkg smart_mbot_pkg.launch.py

sudo chmod 644 /etc/systemd/system/startup.service

sudo chmod 744 /home/ubuntu/smart_mbot_ws/startup.sh

sudo systemctl enable startup.service

sudo systemctl start startup.service

Check the status with the following line:

systemctl status startup

Stopping the startup.service to modify the codes,

sudo systemctl stop startup.service