The most practical serial communication solution for STM32 and ROS robots in academia, most people do not tell him

Now the most practical serial communication scheme of STM32 and ROS robot in academia, I don't tell him to ordinary people

The most practical serial communication solution for STM32 and ROS robots in the whole network (a large number of netizens call it successfully)

The specific protocol is roughly as follows, easy to read, easy to call, easy to expand, and easy to change.

20669bee2cbbdb4393f97bf77bf58b9b.png

Source code file: Enter the following public account: Xiaobaixue mobile robot, send: serial communication upgrade. can be obtained.

The problem solved by this solution: Solve the serial communication problem of using STM32 as the underlying driver of the ROS robot.

Why write an article? :

Recently, it has been found that more and more small partners have entered the field of ROS robots, and the problem of serial communication between ROS robots and the underlying driver is a difficult problem for everyone to learn. Many small partners are not familiar with STM32 microcontroller, and their understanding of serial communication is not thorough. Solving this problem by themselves is time-consuming and laborious, and may not have good results in the end, and this is not the focus of most learning ROS robots. Finally, I found that there is no good tutorial on the Internet (or maybe I didn't find it), so based on my development experience, here is an efficient, stable and easy-to-use common solution for ROS robot and STM32 serial communication.

If you don't want to know the details, you can just read the introduction of the plan and the quick use of the plan section below. You don't need to spend time thinking about the principle of the plan, but I still hope that everyone will be in awe of the details.

API provided by this solution: (the most basic ROS robot needs the protocol)

STM32 sends the left wheel real-time wheel speed, right wheel real-time wheel speed, real-time angle, and reserved control bits to ROS.

ROS sends left wheel set speed, right wheel set speed and reserved control bits to STM32

Advantages of this program:

Tested, long-term stable operation

Guaranteed high data accuracy

The frequency is about 50HZ, can be higher, according to your own sending frequency settings

Easy to use, just import relevant header files, low coupling

------------------------------------------------ gorgeous The dividing line --------------------------------------------

An Introduction:

This scheme adds the data header to the beginning of the data, and the data cyclic redundancy check and the data tail to the end of the data. The data is packaged and sent to ensure the correctness of the data and avoid some undetectable problems. Also according to STM32 and Linux systems are equipped with corresponding data parsing protocols.

In this scheme, the STM32 lower computer relies on the transceiver protocol written by USART1, and the ROS host computer relies on the transceiver protocol written by boost::asio. The serial port is not necessarily serial port 1, it can be changed, but some content needs to be changed by the program (very easy, there are three marks in the program).

This solution cleverly uses the characteristics of the common body to perform data analysis (that is, it does not need to use data separation technology to analyze the data). All you need to know about unions is the following:

A mechanism of .C language, a mechanism for different members of the structure to share memory, (that is, the memory address is consistent)

.At the same time, only one of the members can be accessed

.Different members, memory access is performed according to the nature of the member type

For those who don't understand, you can look at the picture below and have an intuitive understanding. There are pictures and truth

Reminder: Read the picture from left to right.

2b62092b72c149f0fb7adaac99e763c5.png

----------------------------------------------- gorgeous Demarcation Line------------------------------------------------------

Program quick use:

Hardware environment preparation: (required)

STM32 serial port + TTL to USB module (CH340) + Linux hardware device

Line connection: (with pictures and truth)

4b90dbcc96fa38b7c01fa95a20d726a6.png

Introduction to the use of STM32 lower computer software:

The first is the configuration of STM32 serial port parameters: (the configuration code here is consistent with the related routines)

·Baud rate=115200

·Data length = 8 bits

·Stop bit = 1

Parity bit = None

hardware flow control = none

Followed by the function usage instructions, the encapsulation function is as follows:

edad47227facad29dcc82f4fb26c92fe.png

Here is a brief explanation:

The function usartReceiveOneData(int *p_leftSpeedSet,int *p_rightSpeedSet,unsigned char *p_crtlFlag), fill in the address parameter for data acquisition, and put it in the interrupt service function of the corresponding serial port when using it. For example, the serial port 1 used here, as shown in the following figure:

97a3b33d0f8ba6377b95dcf3a058590f.png

The function usartSendData(short leftVel, short rightVel, short angle, unsigned char ctrlFlag), fill in the data variable that needs to be sent for sending, put it into the loop with the specified frequency when using, and send data once each time, preferably with a delay of 10- 15ms, it takes time for the lower computer to send and the upper computer to receive (the time is related to the baud rate of the serial port). What I use is shown in the picture below:

6046fe7b73126170670d554e67e6fb24.png

The remaining two functions are called by the above two functions, so I won't say more here.

Notice:

When referencing functions externally, be careful to refer to the header file #include "mbotLinuxUsart.h"

When compiling, it may appear that  sys.h does not exist in the #include "mbotLinuxUsart.h" file, because the routines that are not punctual atoms are used. At this time, #include is replaced with the configuration header file of the respective version, If it is not clear, just copy and paste the existing header files in other .h files, about stm32

Introduction to the use of ROS host computer software: (PC)

Looking at the picture, there are four functions below. You can understand the purpose by looking at the function name and parameters.

a16986d337576bb33ff698e88062ce22.png

The first is to call the header file #include "mbot_linux_serial.h", and then initialize the serial port

When the program is initialized, the serialInit() function is called, and the built-in serial port parameters are consistent with the serial port parameters of the lower computer.

Then the writeSpeed(double RobotV, double YawRate, unsigned char ctrlFlag) function is called, and the parameters are the linear speed and angular speed of the robot, that is, the data of /cmd_vel. Send the speed that the robot needs to set to the lower computer.

The last thing is to call the readSpeed(double &vx, double &vth, double &th, unsigned char &ctrlFlag) function. For the reference used here, input the variables that store the linear velocity, angular velocity, and angle of the robot. For publishing robot odometers.

Notice:

Here you need to change two parameters according to your own robot, ROBOT_LENGTH robot real wheel spacing (the distance from the center of the left wheel to the center of the right wheel), ROBOT_RADIUS half of the robot wheel spacing.

The device name of boost::asio::serial_port sp(iosev, "/dev/mbot"); in the text is the device name of my serial port, and friends can change it according to their own, for example, /dev/ttyUSB0.

Everyone here will be able to use it happily. If you want to know the details, please read below.

-------------------------------------------Gorgeous dividing line-- ---------------------------------------------

The principle explanation of the scheme:

This plan uses the idea of ​​​​community, and the above small partners also have a general understanding of sharing. This is a feature of accessing memory according to the data type of the members of the union, and different data types access memory according to their own types. The principle of the upper computer and the lower computer is the same. Both define the constants of the data header, the data tail, and the transceiver community.

The data protocol sent by the lower computer: the data protocol sent by the upper computer:

3c8c2e0ac7ed2d6d5445ec277ed07326.png

The serial port receiving principle of STM32: Every time a byte is received, an interrupt will be triggered. I use the serial port interrupt service function to analyze the data reception. The specific function is reflected in receiveTo103()

Judgment and analysis are carried out according to the protocol sent by the host computer, see the code for details, and the comments are clear.

The Linux host computer adopts ASIO. ASIO not only supports network communication, but also supports serial communication.

Here we use boost::asio::write(sp, boost::asio::buffer(buf)); to send data

使用boost::asio::read_until(sp, response, "\r\n",err);

copy(istream_iterator(istream(&response)>>noskipws),

istream_iterator(),buf);

To obtain data, see the source code explanation for the specific details. The idea in my mind is to put the corresponding data in the corresponding position, there is no concept of data analysis, and after the corresponding character data is stored, it can be accessed through another member.

At the beginning of this year, I recorded a relatively systematic introductory single-chip microcomputer tutorial. If you want, you can ask me to get it for free, and you can send me a private message~ You can also get it by clicking on my avatar in black font and adding 隱重麺.

Guess you like

Origin blog.csdn.net/danpianji777/article/details/124085661