Publishing and subscribing method and system for static memory allocation in unmanned aerial vehicle flight control software
By adopting publish subscription mode and static memory allocation in drone flight control software, memory waste and fragmentation problems are solved, flexible function expansion and efficient memory management are achieved, and the performance and reliability of the system are improved.
Patent Information
- Application Number
- CN202510405342.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-01
- Publication Date
- 2025-07-08
AI Technical Summary
In existing drone flight control software, static memory allocation leads to waste of memory space and inflexible function expansion, while dynamic memory allocation has memory fragmentation and system real-time problems.
The publish subscription model is adopted to abstract the data as a theme, store it in the same area of static memory, automatically calculate the number of topics through the compiler, realize loosely coupled asynchronous communication, and use the timer to calculate the publishing frequency to avoid memory fragmentation and delay.
It improves system performance and reliability, realizes flexible function expansion, meets real-time requirements, avoids memory waste and fragmentation, and improves system stability and efficiency.
Smart Images

Figure CN120276859A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of memory allocation, and in particular, to a publish-subscribe method and system for static memory allocation in an unmanned aerial vehicle (UAV) flight control software. Background Art
[0002] The UAV flight control software is the core of the UAV flight control system. By processing data from multiple sensors, it can perform attitude control and flight navigation on the UAV in real time and manage various tasks executed by the UAV. Effective and reasonable memory management in the flight control software ensures the stability and reliability of the system and improves the flight safety of the UAV.
[0003] Existing memory allocation includes static memory allocation and dynamic memory allocation. Static memory allocation refers to a memory allocation method in which the size and life cycle of the memory space are determined at compile time; dynamic memory allocation refers to a method of allocating and releasing memory space as needed during program execution.
[0004] Although dynamic memory allocation has great flexibility, it requires dynamic management of memory allocation and release during development, which increases the programming complexity and has risks such as memory leaks and wild pointers, which may seriously affect flight safety. For example, frequent memory allocation and release may generate a lot of memory fragments, resulting in memory exhaustion, or cause uncertain system running jitter due to the memory sorting algorithm, or because the running path of its code is uncertain, it may cause the system to be unable to complete a specific program operation within a specific time period, affecting the real-time performance of the system. Moreover, if the dynamic design is improper, such as the function execution of a certain cycle times out, all subsequent programs will be affected. This may cause the program execution time to become inaccurate, which is not conducive to the control of the aircraft, and may even cause the aircraft to lose control in severe cases.
[0005] In view of the disadvantages of dynamic memory allocation, some flight control software clearly states that only the static memory allocation method can be used. However, existing static memory allocation usually allocates and initializes a sufficiently large memory space at the beginning of the program, which may lead to excessive memory allocation, resulting in memory idling and waste. The memory data provider and user are tightly coupled together. Not only do they need to reach an agreement on the data structure and format in the shared memory in advance, but they usually also need to adopt a synchronization mechanism to avoid conflicts and data inconsistency problems caused by simultaneous reading and writing of the shared memory. In addition, since the UAV flight control software needs to be continuously updated and upgraded to add new functions, the existing static memory allocation method limits the flexibility of function expansion. Summary of the Invention
[0006] In view of the above analysis, the embodiments of the present invention aim to provide a publish-subscribe method for static memory allocation in an unmanned aerial vehicle (UAV) flight control software, so as to solve the problems of the solidification of the space of existing static memory allocation and inflexible allocation, as well as the easy generation of memory fragmentation in dynamic memory allocation, which affects the real-time performance and stability of the system.
[0007] On the one hand, the embodiments of the present invention provide a publish-subscribe method for static memory allocation in an unmanned aerial vehicle (UAV) flight control software, including the following steps:
[0008] Receive the name, data size, and maximum subscription quantity of each topic, create and initialize the global variables of each topic, and place them in the storage area with the same name;
[0009] According to the name of the topic to be subscribed, obtain the global variable of the topic to be subscribed; construct a subscription node, and add the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed;
[0010] Construct the data to be published corresponding to each topic from the data collected by each hardware component of the UAV, publish the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic, obtain the subscriber array of the corresponding topic, and sequentially notify each subscription node in the subscriber array.
[0011] Based on a further improvement of the above method, the method further includes: before subscription and publication, initialize the topic information in the storage area; create a timer and start the timer at a preset frequency; according to the topic information in the storage area, periodically call the callback function of the timer to calculate the average publication frequency of each topic.
[0012] Based on a further improvement of the above method, the initialization of the topic information in the storage area includes:
[0013] By defining a macro for the starting address of the topic storage area and a macro for the ending address of the topic storage area, identify the compiler adopted in the current flight control software, and then use the instructions of the compiler to obtain the starting address of the topic and the ending address of the topic in the storage area;
[0014] Obtain the number of topics in the storage area by subtracting the starting address of the topic from the ending address of the topic after forcibly converting them to the pointer type of the topic structure.
[0015] Based on a further improvement of the above method, the placement in the storage area with the same name is achieved by defining a topic storage macro, using the received parameters as the name of the storage area in the topic storage macro, identifying the compiler adopted in the current flight control software, and then using the instructions of the compiler to place the global variable of each topic in the storage area named with the name.
[0016] Based on further improvements to the above method, the global variables of each topic are of the type of topic structure and occupy the same memory space; the attributes in the topic structure include: name, data size, pointer to topic data, whether published, whether suspended, publish time, pointer to subscriber array, maximum number of subscriptions, number of subscribed, publish frequency, frequency estimation window, and window index.
[0017] Based on further improvements to the above method, upon receiving the name, data size, and maximum number of subscriptions of each topic, create and initialize the global variables of each topic, including:
[0018] Define the first static array of each topic according to the name and data size of each topic;
[0019] Define the second static array of each topic according to the name and maximum number of subscriptions of each topic;
[0020] Define and declare the global variables of each topic according to the name of each topic and the type of topic structure, and set initial values for each attribute in the global variables of each topic; among them, set the pointer to topic data to point to the address of the first static array of the corresponding topic; set the pointer to the subscriber array to point to the address of the second static array of the corresponding topic.
[0021] Based on further improvements to the above method, adding the subscription node to the subscriber array of the topic to be subscribed according to the pointer to the global variable of the topic to be subscribed includes:
[0022] According to the pointer to the global variable of the topic to be subscribed, obtain the pointer to the subscriber array, the number of subscribed, and the pointer to the topic data of the topic to be subscribed;
[0023] In the critical section, obtain the index of the subscription node in the subscriber array by incrementing the number of subscribed, obtain the node address at this index in the subscriber array, and assign it to the subscription node; initialize the subscription node according to the type and its attributes of the node structure.
[0024] Outside the critical section, if the topic to be subscribed is in the published state and the subscription node has a callback function, then call the callback function of the subscription node and pass in the pointer to the topic data of the topic to be subscribed.
[0025] Based on further improvements to the above method, publishing the data to be published to the corresponding topic according to the pointer to the global variable of the corresponding topic is, in the critical section, according to the pointer to the global variable of the corresponding topic, obtain the data size and the pointer to the topic data of the corresponding topic; copy the data to be published to the memory area pointed to by the pointer to the topic data according to the data size.
[0026] Based on the further improvement of the above method, obtaining the subscriber array corresponding to the topic and sequentially notifying each subscription node in the subscriber array includes:
[0027] According to the pointer of the global variable corresponding to the topic, obtain the pointer of the subscriber array corresponding to the topic, the number of subscribed users, and the pointer of the topic data;
[0028] In the critical section, traverse and extract each subscription node according to the pointer of the subscriber array and the number of subscribed users, identify whether each subscription node has an event handle, and if so, send an event to wake up the waiting subscription node;
[0029] Outside the critical section, traverse and extract each subscription node according to the pointer of the subscriber array and the number of subscribed users, identify whether each subscription node has a callback function, and if so, call the callback function of the subscription node and pass in the pointer of the topic data corresponding to the topic.
[0030] On the other hand, the embodiment of the present invention provides a publish-subscribe system for static memory allocation in an unmanned aerial vehicle flight control software, including:
[0031] A topic storage module, which is used to receive the name, data size, and maximum number of subscriptions of each topic, create and initialize the global variable of each topic, and place it in the storage area with the same name;
[0032] A topic subscription module, which is used to obtain the global variable of the topic to be subscribed according to the name of the topic to be subscribed; construct a subscription node, and add the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed;
[0033] A topic publishing module, which is used to construct the data collected by each hardware component of the unmanned aerial vehicle into the data to be published corresponding to the topic respectively, publish the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic, obtain the subscriber array corresponding to the topic, and sequentially notify each subscription node in the subscriber array.
[0034] Compared with the prior art, the present invention can at least achieve one of the following beneficial effects:
[0035] 1. Abstract the data in the unmanned aerial vehicle flight control software as topics and store them in the same area of static memory, realizing continuous address space static allocation, avoiding the generation of memory fragmentation, avoiding the latency and uncertainty of dynamic memory allocation, improving the running efficiency, and enhancing the system performance.
[0036] 2. By using the publish-subscribe pattern, asynchronous communication between the publisher and the subscriber is realized in a loosely coupled form, with flexible expansion. On this basis, combined with static memory allocation, the compiler automatically calculates the number of topics, which not only realizes loose coupling but also makes memory management more efficient and stable, further improving the performance, reliability, and scalability of the system.
[0037] 3. By using compiler instructions to complete static memory allocation at compile time, there is no need to spend time on memory allocation operations at runtime, and the message processing speed is faster, enabling timely response and processing of messages sent by the publisher, meeting application scenarios with high real-time requirements.
[0038] In the present invention, the above technical solutions can also be combined with each other to achieve more preferred combination schemes. Other features and advantages of the present invention will be described in the subsequent specification, and some advantages can be made obvious from the specification or understood by implementing the present invention. The objectives and other advantages of the present invention can be achieved and obtained through the content specifically pointed out in the specification and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] The drawings are only for the purpose of showing specific embodiments and are not considered as a limitation to the present invention. Throughout the drawings, the same reference signs represent the same components.
[0040] Figure 1 It is a flowchart of the publish-subscribe method for static memory allocation in the UAV flight control software in Embodiment 1 of the present invention.
[0041] Figure 2 It is a schematic structural diagram of the publish-subscribe system for static memory allocation in the UAV flight control software in Embodiment 2 of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0042] The following will specifically describe the preferred embodiments of the present invention in conjunction with the drawings. The drawings form a part of this application and are used together with the embodiments of the present invention to explain the principles of the present invention, rather than to limit the scope of the present invention.
[0043] The drone flight control software in this embodiment generally manages multiple hardware components, such as sensors, motors, and GPS modules. There are multiple control algorithms in the flight control software that need to process the data collected by the hardware components. By analyzing the data required by each control algorithm, the required data is abstracted into different topics, and the publish-subscribe pattern is used to achieve asynchronous communication in a loosely coupled form between the hardware components (publishers) and the control algorithms (subscribers). After the publisher publishes a message, it does not need to wait for the subscriber to process the message. The subscriber can subscribe to one or more topics according to its own needs and can also cancel the subscription at any time. All published topics are stored in static memory, and the number of topics is automatically calculated during compilation without initializing a sufficiently large space in static memory.
[0044] Embodiment 1
[0045] A specific embodiment of the present invention discloses a publish-subscribe method for static memory allocation in a drone flight control software, which supports dynamic changes of topics and topic subscribers in the static memory allocation mode, avoids the disadvantages of the dynamic memory allocation mode, and thus improves the security and reliability of the drone flight control software. As Figure 1 shown, the method includes the following steps:
[0046] S1. Receive the name, data size, and maximum subscription number of each topic, create and initialize the global variable of each topic, and place it in the storage area with the same name.
[0047] It should be noted that receiving the name and data size of each topic and creating and initializing the global variable of each topic includes:
[0048] ① Define the first static array of each topic according to the name and data size of each topic, which is used to store the data to be published for each topic.
[0049] Exemplarily, static uint8_t _ipc_topic_data_##_name[_size] = {0}; indicates that the data array of the topic is a static uint8_t array initialized to 0, the array size is the data size _size of the received topic, and the name is _ipc_topic_data_##_name, where ## is the string concatenation operator of the preprocessor, which is used to splice the name _name of the received topic to the back of _ipc_topic_data_.
[0050] ② Define the second static array of each topic according to the name and maximum subscription number of each topic, which is used to store the subscriber nodes of each topic.
[0051] That is to say, the size of the second static array is the maximum number of subscriptions received. If the maximum number of subscriptions is empty, it is set to the default maximum number of subscriptions, such as 10.
[0052] ③ Define and declare global variables for each topic according to the name of each topic and the type of the topic structure, and set initial values for each attribute in the global variables of each topic. Among them, set the pointer of the topic data to point to the address of the first static array of the corresponding topic; set the pointer of the subscriber array to point to the address of the second static array of the corresponding topic.
[0053] It should be noted that the type ipc_topic of the topic structure is a custom data type in this embodiment. The attributes in the topic structure include: name, data size, pointer to topic data, whether to publish, whether to suspend, publish time, pointer to subscriber array, maximum number of subscriptions, number of subscribed users, publish frequency, frequency estimation window, and window index. Among them, the pointer to topic data is a void pointer pointing to any type of data, and the pointer to the subscriber array is a pointer of type ipc_node pointing to the node structure.
[0054] Furthermore, the type ipc_node of the node structure is also a custom data type in this embodiment. The attributes of the node structure include: whether the data is updated, event handle, callback function pointer, and whether the subscribed node has been initialized. Among them, the callback function pointer points to a callback function, and the parameter of this callback function is a pointer of type void* for passing any type of data.
[0055] Define and declare global variables for each topic according to the name of each topic and the type of the topic structure, so that the global variables of each topic are of the type of the topic structure and occupy the same memory space. Setting initial values for each attribute in the global variables of each topic includes: setting the name to the name of the received topic, setting the data size to the data size of the received topic, setting the pointer of the topic data to point to the address of the first static array of the corresponding topic; setting whether to publish, whether to suspend, publish time, number of subscribed users, publish frequency, frequency estimation window, and window index to 0; setting the maximum number of subscriptions to the maximum number of subscriptions received, and setting the pointer of the subscriber array to point to the address of the second static array of the corresponding topic.
[0056] To avoid the global variables of each topic being optimized by the compiler, mark the global variables of each topic using the topic usage macro IPC_TOPIC_USED. Exemplarily, IPC_TOPIC_USED ipc_topic g_ipc_topic_##_name = {...} means that the variable g_ipc_topic_##_name of type ipc_topic is marked as must be retained.
[0057] It should be noted that different flight control software may use different compilers, such as: GCC compiler (GNU Compiler Collection, GNU compiler suite), IAR compiler (IAR Embedded Workbench), and ARM compiler (ARM Compiler). In order to support the use of multiple compilers simultaneously, the theme uses macros to identify the compiler adopted in the current flight control software, and then uses the instructions of that compiler. The variables using the theme's macros are retained during compilation.
[0058] Specifically, various compilers have specific macro definitions to distinguish different compiler environments. In the theme's used macro IPC_TOPIC_USED, it is determined whether the ARM compiler is used through __CC_ARM and __CLANG_ARM, whether the IAR compiler is used through __IAR_SYSTEM_ICC, and whether the GCC compiler is used through __GNUC__; if the ARM compiler and GCC compiler are used, both are compatible with GCC instructions, and variables must be retained through __attribute__((used)); if the IAR compiler is used, variables must be retained through __root.
[0059] Furthermore, placing the global variables of each theme in the storage area with the same name is achieved by defining a theme storage macro. In the theme storage macro, the received parameter is used as the name of the storage area. By identifying the compiler adopted in the current flight control software, the instructions of that compiler are then used to place the global variables of each theme in the storage area named with the said name.
[0060] Specifically, the theme storage macro IPC_TOPIC_SECTION receives the name parameter x of the storage area, and identifies the adopted compiler in the same way as the theme used macro IPC_TOPIC_USED. If the ARM compiler and GCC compiler are used, the variables are placed in the storage area named x through __attribute__((section(x))); if the IAR compiler is used, the variables are placed in the storage area named x through @x.
[0061] Exemplarily, IPC_TOPIC_USED ipc_topic g_ipc_topic_##_name IPC_TOPIC_SECTION(“ipcTopics”) = {...} means that “ipcTopics” is passed into the topic storage macro IPC_TOPIC_SECTION, and the g_ipc_topic_##_name variable of type ipc_topic (the global variable with the topic name _name) is placed in the storage area named “ipcTopics”.
[0062] It should be noted that step S1 is used to define each topic, initialize and store the global variables of each topic. For the convenience of external use, the processing procedure of step S1 is defined as the interface macro IPC_TOPIC_DEFINE_SUB provided for external use in the module header file.
[0063] Specifically, #define IPC_TOPIC_DEFINE_SUB(_name, _size, _max_sub) means that the interface macro IPC_TOPIC_DEFINE_SUB is defined, which receives the topic name _name, data size _size, and maximum subscription number _max_sub, and the above processing procedure is executed inside the macro.
[0064] It should be noted that the data to be published for each topic usually consists of multiple values, and the data type of the data to be published for each topic is represented by a custom structure and its attributes. Exemplarily, the name of a topic is my_topic, and the data to be published for this topic is of the type my_data_t of a custom structure. The attributes of this structure include: sensor ID, speed value, acceleration value, and flight distance. Then, first introduce the module header file where the interface macro corresponding to step S1 is located into the code, and then call IPC_TOPIC_DEFINE to define the topic my_topic. If multiple topics need to be defined, just call IPC_TOPIC_DEFINE multiple times and pass in the names and data sizes of each topic, without the need to repeatedly write the code to create and initialize the global variables of each topic and place them in the same storage area.
[0065] Exemplarily, the code for defining a topic is: IPC_TOPIC_DEFINE(my_topic, sizeof(my_data_t)); which means passing the name of a specific topic my_topic and the data size of the topic sizeof(my_data_t) into IPC_TOPIC_DEFINE. This macro will generate and initialize the global variable g_ipc_topic_my_topic of the topic my_topic of type ipc_topic and place it in the storage area ipcTopics.
[0066] S2. According to the name of the topic to be subscribed, obtain the global variable of the topic to be subscribed; construct a subscription node, and add the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed.
[0067] It should be noted that since the global variable of the topic is generated according to a preset rule, therefore, according to the name of the topic to be subscribed, the global variable of the topic to be subscribed can be obtained. Similar to defining a topic in step S1, the method of obtaining the global variable of the topic can also be defined as an interface macro and placed in the module header file.
[0068] Exemplarily, #define IPC_TOPIC_DECLARE(_name) extern ipc_topic g_ipc_topic_##_name means defining an interface macro IPC_TOPIC_DECLARE that receives the name _name of the topic and obtains the global variable of the ipc_topic type of this topic.
[0069] Furthermore, adding the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed includes:
[0070] According to the pointer of the global variable of the topic to be subscribed, obtain the pointer of the subscriber array of the topic to be subscribed, the number of subscribed users, and the pointer of the topic data;
[0071] In the critical section, obtain the index of the subscription node in the subscriber array by incrementing the number of subscribed users, obtain the node address at this index in the subscriber array, and assign it to the subscription node; initialize the subscription node according to the type and attributes of the node structure; when initializing, set whether the subscription node has been initialized to initialized;
[0072] Outside the critical section, if the topic to be subscribed is in the published state and the subscription node has a callback function, then call the callback function of the subscription node and pass in the pointer of the topic data of the topic to be subscribed.
[0073] Preferably, after successful subscription, a pointer to the subscribed node is returned. If, before entering the critical section, it is recognized that the subscriber array of the topic to be subscribed is empty, or the number of subscribed users is greater than or equal to the maximum number of subscriptions, then an empty value is directly returned, indicating that the subscription fails.
[0074] S3. Construct the data collected by each hardware component of the drone into the data to be published for the corresponding topic, publish the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic, obtain the subscriber array of the corresponding topic, and sequentially notify each subscribed node in the subscriber array.
[0075] It should be noted that, according to the collected data, declarations are made according to the data types corresponding to the data to be published for each topic, and the data to be published for each topic is obtained.
[0076] Exemplarily, my_data_t data_to_publish = {A01, 20, 3, 5000}; which means constructing the speed value of 20 m / s, acceleration of 3 m / s 2 and flight distance of 5000 m collected by sensor A01 into the data to be published of type my_data_t, namely data_to_publish.
[0077] Furthermore, publishing the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic is to, within the critical section, obtain the data size of the corresponding topic and the pointer to the topic data according to the pointer of the global variable of the corresponding topic; copy the data to be published to the memory area pointed to by the pointer to the topic data according to the data size.
[0078] It should be noted that entering the critical section is to ensure that the access to shared resources in a multi-threaded environment is thread-safe.
[0079] Preferably, before entering the critical section, obtain the value of the suspended attribute according to the pointer of the global variable of the corresponding topic, and then verify whether the topic is suspended. If it is suspended, return an error code. In order to subsequently count the publishing frequency of each topic, within the critical section, increment the count at the position of the current window index in the frequency estimation window of the corresponding topic, which is convenient for calculating the number of publications.
[0080] Furthermore, each time a topic publishes data, it is necessary to notify all subscribed nodes of the topic to update the data. In this embodiment, two notification mechanisms are provided: event handle and callback function, and each subscribed node sets one notification mechanism. Among them, the event handle wakes up the waiting subscribed nodes by sending an event within the critical section, and the subscribed nodes update the data by themselves; the callback function directly calls the function provided by the subscribed node outside the critical section for data update.
[0081] Specifically, obtaining the subscriber array corresponding to the topic and notifying each subscription node in the subscriber array in sequence includes:
[0082] According to the pointer of the global variable corresponding to the topic, obtaining the pointer of the subscriber array corresponding to the topic, the subscribed quantity, and the pointer of the topic data;
[0083] In the critical section, traversing and taking out each subscription node according to the pointer of the subscriber array and the subscribed quantity, identifying whether each subscription node has an event handle, and if so, sending an event to wake up the waiting subscription node;
[0084] Outside the critical section, traversing and taking out each subscription node according to the pointer of the subscriber array and the subscribed quantity, identifying whether each subscription node has a callback function, and if so, calling the callback function of the subscription node and passing in the pointer of the topic data corresponding to the topic.
[0085] Preferably, before notifying the subscription node, identifying whether the subscription node has been initialized, and if not, not notifying; after notification, setting whether the data of the subscription node is updated to the updated state, setting whether the topic is published to the published state, and updating the publish time.
[0086] It can be understood that if the subscribed quantity of the topic is 0, there is no need to notify the subscription node when publishing the topic, so steps S3 and S2 do not necessarily have a sequential relationship.
[0087] It should be noted that through the above steps, this embodiment realizes the publish-subscribe of static memory allocation, and at the same time calculates the publish frequency of all topics by creating a timer, which is convenient for counting the publish and subscribe information of all topics in the memory.
[0088] Specifically, before subscribing and publishing, initializing the topic information in the storage area; creating a timer and starting the timer at a preset frequency; according to the topic information in the storage area, regularly calling the callback function of the timer to calculate the average publish frequency of each topic.
[0089] Among them, initializing the topic information in the storage area includes:
[0090] By defining the macro of the starting address of the topic storage area and the macro of the ending address of the topic storage area, identifying the compiler adopted in the current flight control software, and then using the instructions of this compiler to obtain the starting address of the topic in the storage area and the ending address of the topic;
[0091] Obtaining the number of topics in the storage area by subtracting the result of casting the ending address of the topic and the starting address of the topic to the pointer type of the topic structure.
[0092] It should be noted that the macro TOPIC_START_ADDR for defining the starting address of the topic storage area and the macro TOPIC_END_ADDR for defining the ending address of the topic storage area are defined. In these two macros, if the ARM compiler (__CC_ARM) is used, the starting and ending addresses of the specified storage area generated by the linker script are obtained by using the $$ symbol, such as &ipcTopics$$Base and &ipcTopics$$Limit; if the IAR compiler (__ICCARM__ or __ICCRX__) is used, the starting and ending addresses of the specified storage area are obtained by using the built-in functions __section_begin and __section_end of the IAR compiler, such as __section_begin("ipcTopics") and __section_end("ipcTopics"); if the GCC compiler (__GNUC__) is used, the starting and ending addresses of the specified storage area generated by the linker script are obtained, such as &_ipcTopics_start and &_ipcTopics_end.
[0093] It should be noted that the starting and ending addresses of the topics in the storage area obtained by various compilers are of the void type by default. After being forcibly cast to the pointer type of the topic structure, it is convenient to correctly consider the size of the topic structure when calculating the number of topics. Therefore, subtracting the starting address from the ending address, the compiler calculates the number of topics stored between these two addresses according to the size of the topic structure.
[0094] Furthermore, a timer is created and a pointer to the callback function of the timer is provided, and the timer is started at a preset frequency, such as a preset frequency of 1 Hz. The timer triggers an interrupt regularly at this frequency and calls the callback function to calculate the average publishing frequency of each topic.
[0095] It should be noted that since the starting address of the topic in the storage area is the address of the first topic, then according to the calculated number of topics, each topic in the storage area can be taken out in turn through traversal.
[0096] Specifically, calculating the average publishing frequency of each topic includes: for each topic, according to the total length of the frequency estimation window, the count values at each window index position are taken out in turn and accumulated to obtain the total count value; the total count value is divided by the total length of the frequency estimation window to obtain the average publishing frequency.
[0097] It should be noted that after the timer calculates the average publishing frequency, it increments the window index and resets the count at the incremented window index position to 0 to prepare for the next data publication.
[0098] It should be noted that since the total length of the frequency estimation window is fixed, when the total length is reached, the window index needs to loop back to 0. Therefore, the window index is incremented by adding 1 to the window index and then taking the modulus with the total length to avoid exceeding the total length of the frequency estimation window. The looping of the window index realizes the sliding of the window, and only the number of publications within the total length of the frequency estimation window is considered each time.
[0099] Exemplarily, the total length of the frequency estimation window is 5. When initializing the global variable of the topic in step S1, the window index window_index is set to 0, and the frequency estimation window freq_est_window is initialized to {0, 0, 0, 0, 0}. For the first publication, window_index is 0, and freq_est_window[0] is incremented by 1 to become 1; window_index is incremented to 1; and so on. For the fourth publication, window_index is 3, and freq_est_window[3] is incremented by 1 to become 1; window_index is incremented to 4. At this time, the average publication frequency is (1 + 1 + 1 + 1 + 0) / 5 = 0.8. For the fifth publication, window_index is 4, and freq_est_window[4] is incremented by 1 to become 1; window_index is incremented to 5, and after taking the modulus, it becomes 0, and freq_est_window[0] is reset to 0. At this time, the average publication frequency is (1 + 1 + 1 + 1 + 1) / 5 = 1. For the sixth publication, window_index is 0, and freq_est_window[0] is incremented by 1 to become 1; window_index is incremented to 1, and freq_est_window[1] is reset to 0. At this time, the average publication frequency is (1 + 0 + 1 + 1 + 1) / 5 = 0.8.
[0100] Preferably, the statistical information of all topics is output regularly, including: the name of the topic, the number of subscribed users, the average publication frequency, and whether it is suspended.
[0101] Compared with the prior art, a publish-subscribe method for static memory allocation in an unmanned aerial vehicle (UAV) flight control software provided in this embodiment abstracts the data in the UAV flight control software into topics and stores them in the same area of static memory, achieving continuous address space static allocation, avoiding the generation of memory fragmentation, avoiding the latency and uncertainty of dynamic memory allocation, improving the operation efficiency, and enhancing the system performance. By using the publish-subscribe pattern, asynchronous communication between the publisher and the subscriber is realized in a loosely coupled form, with flexible expansion; on this basis, combined with static memory allocation, the compiler automatically calculates the number of topics, which not only realizes loose coupling but also makes memory management more efficient and stable, further enhancing the system performance, reliability, and scalability. By using the instructions of the compiler to complete static memory allocation during compilation, there is no need to spend time on memory allocation operations during runtime, the message processing speed is faster, and it can respond to and process the messages sent by the publisher in a timely manner, meeting the application scenarios with high real-time requirements.
[0102] Embodiment 2
[0103] Another embodiment of the present invention discloses a publish-subscribe system for static memory allocation in an unmanned aerial vehicle (UAV) flight control software, so as to implement the publish-subscribe method for static memory allocation in an unmanned aerial vehicle (UAV) flight control software in Embodiment 1. The specific implementation manners of each module refer to the corresponding descriptions in Embodiment 1. As Figure 2 shown, the system includes:
[0104] A topic storage module 101, configured to receive the name, data size, and maximum subscription number of each topic, create and initialize the global variable of each topic, and place it in the storage area with the same name;
[0105] A topic subscription module 102, configured to obtain the global variable of the topic to be subscribed according to the name of the topic to be subscribed; construct a new subscription node, and add the new subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed;
[0106] A topic publishing module 103, configured to respectively construct the data to be published corresponding to each topic from the data collected by each hardware component of the UAV, publish the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic, obtain the subscriber array of the corresponding topic, and sequentially notify each subscription node in the subscriber array.
[0107] Since the publish-subscribe system for static memory allocation in an unmanned aerial vehicle (UAV) flight control software in this embodiment can be mutually referred to with the aforementioned publish-subscribe method for static memory allocation in an unmanned aerial vehicle (UAV) flight control software, the repeated descriptions are omitted here. Since the principle of the system embodiment in this system is the same as that of the method embodiment above, the system embodiment in this system also has the corresponding technical effects of the method embodiment above.
[0108] Those skilled in the art can understand that all or part of the processes for implementing the methods of the above embodiments can be completed by instructing relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. Among them, the computer-readable storage medium is a magnetic disk, an optical disk, a read-only memory, a random access memory, etc.
[0109] As mentioned above, the above are only the preferred specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by those skilled in the art within the technical scope disclosed by the present invention should be covered by the protection scope of the present invention.
Claims
1. A publish-subscribe method for static memory allocation in an unmanned aerial vehicle flight control software, characterized in that It includes the following steps: Receive the name, data size, and maximum subscription quantity of each topic, create and initialize the global variable of each topic, and place it in the storage area with the same name; Obtain the global variable of the topic to be subscribed according to the name of the topic to be subscribed; Construct a subscription node, and add the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed; Construct the data to be published for the corresponding topic respectively from the data collected by each hardware component of the drone, publish the data to be published to the corresponding topic according to the pointer of the global variable of the corresponding topic, obtain the subscriber array of the corresponding topic, and notify each subscription node in the subscriber array in turn.
2. The publish-subscribe method for static memory allocation in the UAV flight control software according to claim 1, wherein The method further includes: before subscription and publication, initialize the topic information in the storage area; create a timer and start the timer at a preset frequency; according to the topic information in the storage area, regularly call the callback function of the timer to calculate the average publication frequency of each topic.
3. The publish-subscribe method for static memory allocation in the UAV flight control software according to claim 2, characterized in that, The initialization of the topic information in the storage area includes: By defining the macro of the starting address of the topic storage area and the macro of the ending address of the topic storage area, identify the compiler adopted in the current flight control software, and then use the instructions of the compiler to obtain the starting address and the ending address of the topic in the storage area; Obtain the number of topics in the storage area by subtracting the starting address of the topic from the ending address of the topic after forcibly converting them to the pointer type of the topic structure.
4. The publish-subscribe method for static memory allocation in the drone flight control software according to claim 1, characterized in that, The placement in the storage area with the same name is achieved by defining a topic storage macro, using the received parameters as the name of the storage area in the topic storage macro, identifying the compiler adopted in the current flight control software, and then using the instructions of the compiler to place the global variable of each topic in the storage area named with the name.
5. The publish-subscribe method for static memory allocation in the UAV flight control software according to any one of claims 1-4, characterized in that, The global variable of each topic is of the type of the topic structure and occupies the same memory space; The attributes in the topic structure include: name, data size, pointer to the topic data, whether to publish, whether to suspend, publication time, pointer to the subscriber array, maximum subscription quantity, subscribed quantity, publication frequency, frequency estimation window, and window index.
6. The publish-subscribe method for static memory allocation in the drone flight control software according to claim 5, wherein The receiving of the name, data size, and maximum subscription quantity of each topic, creating and initializing the global variable of each topic includes: Define the first static array of each topic according to the name and data size of each topic; Define the second static array of each topic according to the name and maximum subscription quantity of each topic; Define and declare the global variable of each topic according to the name of each topic and the type of the topic structure, and set the initial value for each attribute in the global variable of each topic; among them, set the pointer to the topic data to point to the address of the first static array of the corresponding topic; set the pointer to the subscriber array to point to the address of the second static array of the corresponding topic.
7. The publish-subscribe method for static memory allocation in the UAV flight control software according to claim 5, characterized in that, The adding of the subscription node to the subscriber array of the topic to be subscribed according to the pointer of the global variable of the topic to be subscribed includes: Obtain the pointer to the subscriber array of the topic to be subscribed, the number of subscribed users, and the pointer to the topic data according to the pointer to the global variable of the topic to be subscribed; In the critical section, obtain the index of the subscription node in the subscriber array by incrementing the number of subscribed users, obtain the node address at this index in the subscriber array, and assign it to the subscription node; initialize the subscription node according to the type and its attributes of the node structure; Outside the critical section, if the topic to be subscribed is in the published state and the subscription node has a callback function, then call the callback function of the subscription node and pass in the pointer to the topic data of the topic to be subscribed.
8. The publish-subscribe method for static memory allocation in the UAV flight control software according to claim 5, wherein, The step of publishing the data to be published to the corresponding topic according to the pointer to the global variable of the corresponding topic is, in the critical section, obtain the data size and the pointer to the topic data of the corresponding topic according to the pointer to the global variable of the corresponding topic; copy the data to be published to the memory area pointed to by the pointer to the topic data according to the data size.
9. The publish-subscribe method for static memory allocation in the UAV flight control software according to claim 8, wherein The step of obtaining the subscriber array of the corresponding topic and notifying each subscription node in the subscriber array in turn includes: Obtain the pointer to the subscriber array of the corresponding topic, the number of subscribed users, and the pointer to the topic data according to the pointer to the global variable of the corresponding topic; In the critical section, traverse and retrieve each subscription node according to the pointer to the subscriber array and the number of subscribed users, identify whether each subscription node has an event handle, and if so, send an event to wake up the waiting subscription node; Outside the critical section, traverse and retrieve each subscription node according to the pointer to the subscriber array and the number of subscribed users, identify whether each subscription node has a callback function, and if so, call the callback function of the subscription node and pass in the pointer to the topic data of the corresponding topic.
10. A publish-subscribe system for static memory allocation in an unmanned aerial vehicle flight control software, characterized in that, Including: A topic storage module, which is used to receive the name, data size, and maximum number of subscriptions of each topic, create and initialize the global variable of each topic, and place it in the storage area with the same name; A topic subscription module, which is used to obtain the global variable of the topic to be subscribed according to the name of the topic to be subscribed; Construct a subscription node and add the subscription node to the subscriber array of the topic to be subscribed according to the pointer to the global variable of the topic to be subscribed; A topic publishing module, which is used to construct the data collected by each hardware component of the drone into the data to be published corresponding to the topic respectively, publish the data to be published to the corresponding topic according to the pointer to the global variable of the corresponding topic, obtain the subscriber array of the corresponding topic, and notify each subscription node in the subscriber array in turn.