Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
This document mainly introduces how to use serial port or wireless to connect to ROS in RT-Thread, and will include the following contents:
Part 1: ROS environment construction
Part 2: RT-Thread rosserial package
Part 2: RT-Thread adds USART2 and PWM
Part 3: RT-Thread uses ESP8266 AT firmware to connect to the Internet
Here we first introduce what is ROS and why we need to connect to ROS?
The Robot Operating System ROS (Robots Operating System) was originally a software framework from Stanford University. Now both industrial robots and entertainment robots are running ROS.
A robot usually has many parts and sensors. In order to ensure that the robot does not fail due to a sensor failure, a distributed node is used to collect sensor data and control instructions through communication between different nodes. The communication protocol used later in this document is rosserial .
The advantage of connecting with ROS is that, on the one hand, it is more stable to manage each robot node by ROS, and on the other hand, ROS now has a lot of mature software packages. Using ROS, you can easily add advanced functions such as camera image recognition, lidar mapping and navigation to your robot.
However, this document will only cover the basic connection between RT-Thread and ROS to achieve motion control of the car. There may be subsequent documents to introduce how to connect to the lidar to build maps and perform global path planning.
This article assumes that everyone can use RT-Thread's env tool to download software packages, generate projects and upload firmware to stm32, and is familiar with the basic use of Ubuntu.
The development environment here actually needs to be built in two parts, one is the ARM development board on the car (Raspberry Pi, NanoPi, etc.), and the other is your own computer, because we want to use the computer as a ROS slave node and connect it to the ROS master node on the car. However, the ROS installation of the development board and the computer is exactly the same.
Since we need to connect to ROS, we must first have a running ROS. Installing ROS is actually very simple. We recommend using Ubuntu 18 (Armbian is recommended for the development board) because the official support for Ubuntu is the highest priority. You can also refer to the official website for installation instructions .
Just enter the following 4 lines of commands to install ROS on Ubuntu.
I used the image source of Tsinghua University above, so that downloading ROS from China will be much faster, and I only installed the basic software package of ROS, and did not install the graphical software package gviz, gazebo, etc., because they were not used later.
This section will introduce how to use the serial port to connect the STM32 development board running RT-Thread and the ARM development board running ROS. This is what it looks like.
Here we explain the division of labor of different development boards. STM32 runs RT-Thread to control the motor and receive sensor information; ARM runs ROS to perform global control, such as issuing forward instructions to the car.
First we need to open usart2, because usart1 is used by msh, it is quite convenient to keep it for debugging.
In CubeMX, I turned on USART2 and also turned on 4-channel PWM, because I used 2 motors later, and each motor needed 2-channel PWM to control forward and backward respectively.
Next, you need to open the corresponding options in menuconfig. Considering that the default bsp of some development boards may not have these options, you can modify board/Kconfig and add the following content.
Serial port configuration:
menuconfig BSP_USING_UART
bool "Enable UART"
default y
select RT_USING_SERIAL
if BSP_USING_UART
config BSP_USING_UART1
bool "Enable UART1"
default y
config BSP_UART1_RX_USING_DMA
bool "Enable UART1 RX DMA"
depends on BSP_USING_UART1 && RT_SERIAL_USING_DMA
default n
config BSP_USING_UART2
bool "Enable UART2"
default y
config BSP_UART2_RX_USING_DMA
bool "Enable UART2 RX DMA"
depends on BSP_USING_UART2 && RT_SERIAL_USING_DMA
default n
endifcopymistakeCopy Success
PWM configuration:
menuconfig BSP_USING_PWM
bool "Enable pwm"
default n
select RT_USING_PWM
if BSP_USING_PWM
menuconfig BSP_USING_PWM3
bool "Enable timer3 output pwm"
default n
if BSP_USING_PWM3
config BSP_USING_PWM3_CH1
bool "Enable PWM3 channel1"
default n
config BSP_USING_PWM3_CH2
bool "Enable PWM3 channel2"
default n
config BSP_USING_PWM3_CH3
bool "Enable PWM3 channel3"
default n
config BSP_USING_PWM3_CH4
bool "Enable PWM3 channel4"
default n
endif
endifcopymistakeCopy Success
In this way, we can see the corresponding configuration under env
In addition, we also need to select the rosserial package:
You can see that the default serial port above is USART2, so we can generate the corresponding project:
If we open the Keil project, we first need to change main.c to main.cpp, because many data format definitions of rosserial are written in C++, so if we want to use the rosserial library, we must first change the suffix to cpp, so that Keil will compile with the C++ compiler.
Below is the content of main.cpp, which actually initializes the motor and publishes two topics, one is /vel_x to tell ROS the current speed of the car, and the other is /turn_bias to tell ROS the current rotation speed of the car. At the same time, it subscribes to a topic /cmd_vel to receive control commands from ROS.
The code is not particularly long, and I have added some comments, so I will not analyze it line by line here.
There is also the corresponding motor control code, but everyone's car is different, and the drive should also be different. Since there is no encoder on the car motor, all of them are open-loop controlled.
After we upload the RT-Thread firmware to the board, we can use a USB-TTL to connect to the USART2 of the STM32 control board on one side and plug the other side into the USB port of the ARM control board. Then we can establish the connection and enter the command on the ARM board:
This is our Python control program. We can use the keyboard's wasd to control the car to move forward and backward, and rotate clockwise and counterclockwise. We need to add executable permissions to it:
It can be seen that the amount of code to implement car control using ROS is not that much. You only need to publish some topics on the original code of your car, tell ROS the current status of the car, and subscribe to a topic to receive ROS control instructions.
In fact, wireless connection and wired connection are almost exactly the same, except that you first use ESP8266 to connect your control board to the Internet, and then use TCP connection to communicate with ROS. For tutorials on how to use ESP8266 to access the Internet with RT-Thread, please refer to the official website , which is very detailed and I will not repeat it here.
After ensuring that the development board has a network connection, we can configure it in rosserial to use a TCP connection:
We just need to add one line of code to main.cpp from the previous section:
To summarize here, it is actually very simple to establish a connection between RT-Thread and ROS using the rosserial software package. You only need to publish some messages based on the original code of your own car, tell ROS the current status of the car, and subscribe to the control instructions from ROS.