jetson nano ubuntu 18.04 ros melodic python3

configuration for jetson nano ubuntu 18.04 ros melodic python3 for nvidia-jetpack 4.5.1 for tensorflow 2.5.0 cuda gpu

# first i follow this page but it was some what strange the remove and reinstal
https://dhanoopbhaskar.com/blog/2020-05-07-working-with-python-3-in-ros-kinetic-or-melodic/

# then I follow this page
https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
https://github.com/betab0t/ros_python3_issues

# the cv_bridge problem i used this page

Compiling ROS cv_bridge with Python 3

# what i have done was more or less this

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3

#
sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential

#sudo apt-get install python-catkin-tools python3-dev python3-numpy
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge

mkdir -p ~/catkin_build_ws/src && cd ~/catkin_build_ws
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so
catkin config –install

cd src
#git clone -b melodic https://github.com/ros-perception/vision_opencv.git
git clone -b noetic https://github.com/ros-perception/vision_opencv.git

#apt-cache show ros-melodic-cv-bridge | grep Version
#Version: 1.13.0-0bionic.20210506.020113

cd vision_opencv/

#git checkout 1.13.0

cd ../../

nano /home/inaciose/catkin_build_ws/src/vision_opencv/cv_bridge/CMakeLists.txt

# line 16 (or near)
# find_package(Boost REQUIRED python37)

to

# find_package(Boost REQUIRED python3)

catkin build cv_bridge

source ~/catkin_build_ws/install/setup.bash –extend

# other links
https://www.programmersought.com/article/93196809260/
https://stackoverflow.com/questions/54094876/ros-melodic-installation-with-python-3-only-and-without-messing-up-system-librar

 

micropython-rosserial scripts and ros

The following command must be executed in the computer with the rosmaster.

rosrun rosserial_arduino serial_node.py _port:=/dev/ttyUSB0 _baud:=115200

The baud must match the baud in the uros.NodeHandle statement in the micropython-rosserial script.

micropython-rosserial publisher sample code

 

import uros
from std_msgs import ColorRGBA #message object ColorRGBA
from time import sleep
node=uros.NodeHandle(2,115200) #node initialized, for tx2/rx2 and 115200 baudrate
msg=ColorRGBA() #msg object init
msg.r=1 #values to variables assigned
msg.g=3
msg.b=4
msg.a=1
while True:
    node.publish('Colorsh',msg) #publish data to node Colorsh
    sleep(1)

 

micropython-rosserial subscriber sample code

import uros
from std_msgs import String

def cb(msg):
	print(msg.data)
	
node = uros.NodeHandle(2, 115200)
node.subscribe('chatter', String, cb)

 

micropython-rosserial subscriber and publisher sample code

 

import uros
from std_msgs import String

def cb(msg):
	print(msg.data)
	
packet=String()
packet.data='hola fpy'
node = uros.NodeHandle(2, 115200)
node.subscribe('chatter', String, cb)
while True:
	node.publish('greet', packet)

 

Node constructor

uros.NodeHandle(SERIAL_ID = 2, BAUDRATE = 115200, **KWARGS)

Remark: there are 3 serial ports in esp32

**KWARGS are arguments for a custom serial port connection.

More info about micropython-rosserial

https://pypi.org/project/micropython-rosserial/

end

micropython-rosserial create required ROS message classes from msg files

As stated in micropython-rosserial documentation, any ROS message type to be used by a node must be created  before they can be used in micropython-rosserial based scripts.

The message class files are generated by module ugenpy, one of the additional modules installed during the pip install of the micropython-rosserial.

But although the module is installed it doesn’t install the folder of with the message definition files, the *.msg files, for the std_messages.

So we need to copy the msg definition files to our MicroPython device.

Before we can do that we need:

  • the .msg files for ROS std_msgs;
  • the software to copy then to the file system of our MicroPython device;

Lets do it now.

Get the std_msgs .msgs files from github

github repository to the /std_msgs folder in our MicroPython device.

cd ~/
git clone https://github.com/FunPythonEC/uPy-genpy
cd uPy-genpy/src

Install rshell

The rshell is the software to copy the files to (and from) the microPython file system.

It can also be used as serial terminal to access the microPython console (the REPL)

sudo pip3 install rshell

After installation we can run it with the following command

rshell connect serial /dev/ttyUSB0 115200

We get automatically connected to our micropython device.

We may also just run rshell and issue the connect insider the rshell.

The command help give the available commands.

help

Documented commands (type help <topic>):
========================================
args cat connect date edit filesize help mkdir rm shell
boards cd cp echo exit filetype ls repl rsync

The boards command give the avalilable / connected boards.

boards

pyboard @ /dev/ttyUSB0 connected Epoch: 2000 Dirs: /boot.py /lib /main.py /project.pymakr /rospub.py /rossub.py /utils.py /pyboard/boot.py /pyboard/lib /pyboard/main.py /pyboard/project.pymakr /pyboard/rospub.py /pyboard/rossub.py /pyboard/utils.py

The repl command gives us access to the microphython console, the REPL.

repl

Entering REPL. Use Control-X to exit.
>
MicroPython v1.15 on 2021-04-18; ESP32 module with ESP32
Type "help()" for more information.
>>>

Our micropython device file system is available under the following path:

/pyboard

So we in order to copy our folder std_msgs from our computer to our micropython file system, we use the following command

cp -r std_msgs /pyboard

After some seconds all the files are copied and you could list the /std_msgs folder in the file system of our micropython device with:

ls /pyboard/std_msgs

Bool.msg Duration.msg Float64MultiArray.msg Int32MultiArray.msg MultiArrayDimension.msg UInt16MultiArray.msg UInt8.msg 
Byte.msg Empty.msg Header.msg Int64.msg MultiArrayLayout.msg UInt32.msg UInt8MultiArray.msg 
ByteMultiArray.msg Float32.msg Int16.msg Int64MultiArray.msg String.msg UInt32MultiArray.msg 
Char.msg Float32MultiArray.msg Int16MultiArray.msg Int8.msg Time.msg UInt64.msg 
ColorRGBA.msg Float64.msg Int32.msg Int8MultiArray.msg UInt16.msg UInt64MultiArray.msg

I just copy all the messages, but, in order to spare space, we should only copy the required ones.

Now that we have the msg files in our micropython device, we may start to create de ROS message class files required for our test scripts.

Go to the REPL (micropython console) with the following command.

repl

And enter the following lines on the REPL

from ugenpy.message import MessageGenerator

msg=MessageGenerator('std_msgs/ColorRGBA.msg')
msg.create_message()

msg=MessageGenerator('std_msgs/String.msg')
msg.create_message()

The first part import the required module

The second part, create the py class file for the file ColorRGBA.msg

The third part, create the py class file for the file String.msg

We get some info about the processed data types. For the first file is:

data type processed: float32 to be <f
data type processed: float32 to be <f
data type processed: float32 to be <f
data type processed: float32 to be <f

For the second file, we get:

data type processed: string to be <I%ss

We may check if the files exist inside the REPL, with the following commands.

> import os
>>> os.listdir('std_msgs')
['Bool.msg', 'Byte.msg', 'ByteMultiArray.msg', 'Char.msg', 
(...)
 'UInt8MultiArray.msg', '_ColorRGBA.py', '_String.py', '__init__.py']

 

https://pypi.org/project/micropython-rosserial/

https://github.com/FunPythonEC/uPy-genpy

micropython-rosserial install on ESP32-CAM

Install micropython-rosserial on ESP32-CAM it is very easy.

The easy way is install it with upip. It requires internet access. So before we install the micropython-rosserial on ESP32-CAM, we need to connect it to the internet.

The full procedure is this one:

1 – Connect the computer to the ESP32-CAM via the FTDI USB-Serial interface.

2 – Access to the MicroPython console with a terminal software, or with the pyMakr vscode extension.

3 – Connect to the internet

On MicroPython console input the following commands

import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'password')
wlan.ifconfig()

The last statement only gets the ip config, and show us in the terminal. Ex:

>>> wlan.ifconfig()
('192.168.1.108', '255.255.255.0', '192.168.1.1', '192.168.1.1')

4 – Install the micropython-rosserial module and dependencies

import upip
upip.install('micropython-rosserial')

The success message sample is something like the following one.

Installing to: /lib/
Warning: micropython.org SSL certificate is not validated
Installing micropython-rosserial 0.2.7 from https://files.pythonhosted.org/packages/39/ff/9f04ae38d6245d8c0d63b3c6fa052d87088038c2e12748c811e1196e56e4/micropython-rosserial-0.2.7.tar.gz
Installing micropython-genpy 0.0.6 from https://files.pythonhosted.org/packages/b0/eb/24c691b2cfb95c638da4c95585437b2009fb0baa0a3c8bca0791fdf34188/micropython-genpy-0.0.6.tar.gz
Installing micropython-rosserial-msgs 0.0.1 from https://files.pythonhosted.org/packages/e5/4d/cd3f085dab632d499bc256e0d50c2a180adf2c43ae008efabeaa1a292fa5/micropython-rosserial_msgs-0.0.1.tar.gz
Installing micropython-logging 0.3 from https://micropython.org/pi/logging/logging-0.3.tar.gz
>>>

After we may check the contents of the lib folder

import os
os.listdir('lib')
['logging.py', 'rosserial_msgs', 'ugenpy', 'uros']

The installation is done.

Lets explore it!

Source:

 

https://pypi.org/project/micropython-rosserial/

Preparar um sdcard com o rospibot6

webid#sdcard0703

Download da imagem em:

https://downloads.ubiquityrobotics.com/pi.html

Gravar num sdcard

Fazer o login (password: ubuntu), ssh ubuntu@10.42.0.1

Trocar a password

locale-gen pt_PT.UTF-8

locale-gen en_US.UTF-8

sudo apt update (optional: sudo apt upgrade)

sudo systemctl disable magni-base (o serviço roscore continua a funcionar)

trocar o hostname, https://www.cyberciti.biz/faq/ubuntu-change-hostname-command/

pifi add sid password

reboot

Bibliotecas Instaladas

INSTALL I2Cdevlib:

sudo mkdir -p /usr/share/arduino/libraries cd /usr/share/arduino/libraries
sudo git clone https://github.com/chrisspen/i2cdevlib.git

 

INSTALL Bcm2835:

cd /tmp
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz 
tar zxvf bcm2835-1.50.tar.gz 
cd bcm2835-1.50 
./configure 
make 
sudo make check 
sudo make install

 

Pacotes ros instalados

sudo apt-get install ros-kinetic-xv-11-laser-driver

sudo apt-get install ros-kinetic-gmapping

sudo apt-get install ros-kinetic-amcl

sudo apt-get install ros-kinetic-dwa-local-planner

sudo apt install ros-kinetic-robot-pose-ekf

 

Instalar o pacote do rospibot6

catkin_make (compilar)

 

sudo bash -c “source /opt/ros/kinetic/setup.bash; source /home/ubuntu/catkin_ws/devel/setup.bash; roslaunch rospibot6 robot6.launch”

rostopic pub –once cmd std_msgs/Int16 “data: 3”

roslaunch rospibot6 teleop.launch

 

Arduino ROS custom messages

Para usar ROS custom messages (mensagens personalizadas) no Arduino com o rosserial, é obrigatório ter instalados no computador os pacotes do ROS:  rosserial e rosserial arduino. Para instalar pode-se usar os seguintes comandos:

sudo apt install ros-melodic-rosserial
sudo apt install ros-melodic-rosserial-arduino

Quando o programa para o Arduino requer mensagens personalizadas do ROS  (ROS custom messages). Como por exemplo quando inclui uma linha como a seguinte:

#include <ntbd_msgs/Motors_Array.h>

Teremos que efectuar um procedimento especial, que envolve a compilação do pacote de ROS que tem as mensagens personalizadas (neste caso o ntbd_msgs), e obter o ficheiro de header necessário (neste caso, o ficheiro Motors_Array.h).

Para obter o ficheiro header , é necessário, antes de mais, colocar o pacote do ROS com a definição da mensagem na pasta adequada do catkin e proceder á sua compilação:

cd ~/catkin_sw/src
git clone https://github.com/HotBlackRobotics/ntbd
cd ~/catkin_sw
catkin_make

De seguida, tem de se usar um dos seguintes comandos (pelos vistos existe a informação de vários devido à evolução das várias versões do ROS):

No ROS Melodic, o comando que funcionou foi  o ultimo dos indicados abaixo.

rosrun rosserial_client make_library.py ~/ ntbd_msgs
rosrun rosserial_arduino make_library.py ~/ ntbd_msgs
rosrun rosserial_arduino make_libraries.py ~/ ntbd_msgs

~/ representa a pasta de destino onde se encontra a ros_lib (biblioteca rosserial para o arduino)

ntbd_msgs, indica o pacote (com as mensagens) a ser compilado

O comando que usei (o último) gerou uma pasta ros_lib na home com toda a biblioteca do rosserial incluindo uma pasta ntbd_msgs que contem o ficheiro pretendido.

Nesta fase, e tendo em consideração o exemplo, podemos optar por uma das duas soluções:

  • Copiar a pasta ~/ros_lib/ntbd_msgs para ~/Arduino/libraries/ros_lib;
  • Copiar a pasta para dentro da pasta do sketch do arduino, e alterar uma linha de include para passar a incluir a biblioteca localmente: #include “ntbd_msgs/Motors_Array.h”

Já podemos compilar e enviar o programa para o Arduino

 

ROS custom messages

As mensagens personalizadas no ROS (ROS custom messages) são constituidas a partir das  mensagens préviamente disponiveis.

Para criar novas mensagens personalizadas (custom messages) deveremos proceder do seguinte modo:

Primeiro deveremos definir o novo tipo de mensagem num ficheiro que tem de ser criado dentro da pasta msg do respectivo pacote.

Preferencialmente deve existir um pacote exclusivo para as mensagens pois assim elas são mais facilmente reutilizaveis.

# MyCustomMsg.msg

Header header
float64 data

Para criar as mensagens, quer seja num pacote dedicado quer sejam incluidas num outro pacote em que convivem com outras funcionalidades, as instruções de compilação tem de obedecer a alguns requisitos:

O ficheiro package.xml deve conter:

<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>

No ficheiro CMakeLists.txt, tem de incluir vários blocos de texto:

Incluir o pacote message_generation na entrada find_package, no ficheiro CMakeLists.txt, como no exemplo abaixo:

find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)

Exportar a dependência em tempo de execução incluindo a seguinte entrada no ficheiro CMakeLists.txt:

catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...
)

Declarar o ficheiro com a mensagem personalizada com a seguinte entrada no ficheiro  CMakeLists.txt.

add_message_files(
  FILES
  MyCustomMsg.msg
)

Se existirem mais mensagens será é uma por linha. Adequar o nome do ficheiro(s) indicado nesta entrada.

Fica assim completa a parte dos requisitos para a nossa mensagem personalizada ser compilada e disponivel para ser usada.

Caso esta mensagem esteja num pacote diferente daquele onde vai ser usadas, por exemplo um pacote dedicado a mensagens, para usar esta mensagem é necessário incluir as seguintes entradas nos ficheiros package.xlm e CMakeLists.txt.

No ficheiro package.xlm incluir o nome do pacote (substituir pelo nome correcto do pacote), conforme o exemplo abaixo:

<depend package="message_package" />

No ficheiro CMakeLists.txt, incluir message_package (substituir pelo nome correcto do pacote) na entrada adequada (ver exemplo abaixo).

find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_package
)

No que respeita ao inclusão e utilização da mensagem personalizada no código C++, é necessário proceder da seguinte forma:

Importar para o código C++ code.

#include <message_package/MyCustomMsg.h>

Declarar o topic  publisher:

my_msg_pub_ = n.advertise<message_package::MyCustomMsg>("topic", 10);

Criar uma instancia da mensagem numa variavel:

message_package::MyCustomMsg msg;

Atribuir o(s) valor(es) e publicar a mensagem.

msg.header.stamp = ros::Time::now();
msg.header.frame_id = "/world";
msg.data = 0.0;

my_msg_pub_.publish(msg);

Que me lembre é tudo.

Fontes:

  • http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv
  • http://wiki.ros.org/ROS/Tutorials/DefiningCustomMessages

 

Joystick no ROS

http://wiki.ros.org/joy/Tutorials/ConfiguringALinuxJoystick

# instalar software
sudo apt-get install ros-melodic-joy

# ligar o joystick e verificar se aparece o jsX ex. js0 ou js1
ls /dev/input/

# testar o joystick
sudo jstest /dev/input/js0

# verificar as permissões
ls -l /dev/input/js0

# dar permissões
sudo chmod a+rw /dev/input/js0

# rosmaster
roscore &

# configurar o joystick no parameter server do ROS
rosparam set joy_node/dev “/dev/input/js0”

# Correr o node
rosrun joy joy_node

# Verificar o ROS topic
rostopic echo joy

 

Instalar nodes de teleop (twist msg)

sudo apt install ros-noetic-teleop-tools

  • key_teleop
  • joy_teleop
  • mouse_teleop

Instalar um node de joystick teleop (twist msg)

sudo apt install ros-noetic-teleop-twist-joy

Mais informação em:

http://wiki.ros.org/teleop_twist_joy

Programar um node de joystick teleop (twist msg)

http://wiki.ros.org/joy/Tutorials/WritingTeleopNode

end