ROS using RT-Thread
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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.
After ROS is installed, it needs to be initialized, but it only takes a few lines of commands:
If we start ROS, we need to make sure it runs in the background, so we can use tmux:
After starting the ROS master node in tmux, we can exit it by pressing Ctrl + BD, and the ROS master node will still run in the background.
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:
PWM configuration:
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.
motors.h
motors.c
Only this little bit of code is needed to connect to ROS, so ROS is not that mysterious. It is so popular because it is simple and easy to use.
Now that RT-Thread has been configured, the next step is to configure ROS.
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:
If you see the following output, the connection is successfully established:
Now that the connection has been successfully established, the next step is to write the code for controlling the car.
Let's first initialize a working range:
Next, create a package:
This will automatically create a ROS package in the src directory.
We create a new file ros_cmd_vel_pub.py in the catkin_workspace/src/my_first_pkg/src directory:
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:
This will compile the package in the catkin_worspace directory.
We can finally start the program to control the car's movement from the computer:
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.
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:
The development board can communicate with ROS through TCP connection, which is very convenient.
Since we use TCP connection, we also need to start a server on ROS. Previously, we used the serial port to establish a connection, but now we use TCP:
The rest of the code does not need to be changed at all, and we have now implemented a ROS wirelessly controlled car.
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.
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 for installation 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 , which is very detailed and I will not repeat it here.