ROS2 multi-thread actuator scheduling method based on deadline driving
By adopting the scheduling method based on deadline and error correction mechanism in ROS2 multi-threaded executor, the priority inversion and delay problems in the existing scheduling methods are solved, real-time support for security-critical areas is achieved, and the application is improved Real-time performance and stability.
Patent Information
- Application Number
- CN202411101999.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-12
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2044-08-12
AI Technical Summary
The existing scheduling methods of ROS2 multithreaded executors have problems with priority inversion and low-priority chain latency, especially when the system load is high, low-priority chains may experience 'starvation', which cannot meet hard real-time or security-critical areas. real-time requirements.
Using a scheduling method based on deadline, the callback instance is prioritized through the deadline of the chain instance, a single lock mechanism and error correction mechanism are introduced to ensure data consistency and priority sorting, and the CallbackExecutable structure is designed to associate callbacks and chains. The instance deadline is used to realize priority granularity division at the chain instance level.
Improve the real-time performance of ROS2 applications, ensure real-time constraints in security-critical areas, avoid data competition and priority reversal, and improve the real-time and stability of multi-threaded executors.
Smart Images

Figure CN118964031A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of robot operating systems, and in particular to a deadline-driven ROS2 multi-threaded executor scheduling method. Background Art
[0002] The Robot Operating System (ROS) is an open source middleware framework widely used in the development of robot applications. ROS improves the development efficiency of robot applications through the modularity and composability of software. However, over the past few decades, ROS has exposed its major flaw of lacking real-time performance, which limits its use in safety-critical fields with high real-time requirements. Against this background, in 2017, the ROS community released the second-generation robot operating system ROS2, which was reconstructed based on the ROS framework. One of the main goals of the ROS2 design is to improve the real-time performance of the framework to support the development of real-time applications for robots. ROS2 uses the data distribution service (DDS) as the underlying communication framework for end-to-end data exchange and supports deployment on top of real-time operating systems. Compared with ROS, although ROS2 can provide better real-time support for robot applications, it is not yet fully suitable for robot applications in hard real-time or safety-critical fields.
[0003] The core component related to the real-time performance of ROS2 is the executor. ROS2 provides two types of executors: one is a single-threaded executor with a single thread built in the executor, which executes ready callbacks sequentially in a single thread; the other is a multi-threaded executor with multiple threads built in the executor, which distributes ready callbacks to multiple threads for parallel execution. As robot applications become increasingly complex, it is usually necessary to implement different applications on multi-threaded executors to fully utilize the parallel processing capabilities of multi-core platforms; the existing scheduling methods of ROS2 multi-threaded executors have the following problems:
[0004] (1) The default scheduling method of ROS2 multi-threaded executor adopts polling scheduling method, which is a fairness-oriented scheduling method. It updates the ready callback set at the polling point, which is prone to priority inversion problems and is not suitable for applications in safety-critical fields with high real-time requirements.
[0005] (2) The PiCAS scheduling method of the ROS2 multi-threaded executor has good real-time performance on chains with higher priority, but the scheduling of low-priority chains may be disproportionately preempted by high-priority chains, resulting in large delays; especially when the system load is heavy, low-priority chains may "starve to death".
[0006] Therefore, the present invention proposes a deadline-driven ROS2 multi-threaded executor scheduling method to solve the above-mentioned problems. Summary of the invention
[0007] In view of the problems existing in the prior art, the purpose of the present invention is to provide a deadline-driven ROS2 multi-threaded executor scheduling method to solve the problems raised in the above background technology.
[0008] To achieve the above object, the present invention provides a deadline-driven ROS2 multi-threaded executor scheduling method, which is applied to a robot operating system. The method uses the deadline of a chain instance as the basis for scheduling a callback instance of a ROS2 multi-threaded executor. The specific steps are as follows:
[0009] Step S1: Multiple threads of the multi-threaded executor compete to acquire the lock. The thread that acquires the lock executes the next step, and the thread that does not acquire the lock waits for the lock to be released until the thread holding the lock releases the lock and competes for the lock again.
[0010] Step S2: Use the temporary variable t to temporarily store the head element of the priority queue of the multi-threaded executor, delete the head element, and the callback instance corresponding to the CallbackExecutable structure pointed to by the temporary variable t is the callback instance with the highest current priority;
[0011] Step S3: Release the lock;
[0012] Step S4: Execute the function pointed to by the function handle of the CallbackExecutable structure pointed to by the temporary variable t;
[0013] Step S5: Check whether there is a newly released callback instance, and if so, add the CallbackExecutable structure pointer corresponding to the newly released callback instance to the priority queue;
[0014] Step S6: Return to step S1.
[0015] Furthermore, the method divides the callback instances into priority granularities at the chain instance level, so as to ensure that ROS2 applications in safety-critical areas meet strict real-time constraints.
[0016] Furthermore, the multi-threaded executor has only one lock, ensuring that only one thread holds the lock at a time. Other threads need to wait for the lock to be released before they can continue execution, thus avoiding data competition and inconsistency problems caused by multiple threads accessing the priority queue at the same time.
[0017] Furthermore, in order to associate the deadlines of the callback instance and the chain instance, the scheduling unit is designed as follows: the callback function handle, callback type and corresponding chain attributes are encapsulated into a structure CallbackExecutable as the scheduling unit of the ROS2 multi-threaded executor.
[0018] Furthermore, the relative deadline and release period of the chain are defined by the application developer. The relative deadline of the chain is represented as D, the release period of the chain is represented as T, and the release time of the chain instance represents the time when the chain releases the instance with a period of T.
[0019] Furthermore, for the CallbackExecutable, the deadline of the chain instance is equal to the release time of the chain instance plus the relative deadline of the chain, and all callback instances of the same chain instance are based on the same deadline.
[0020] Furthermore, the deadlines field of CallbackExecutable corresponding to all callbacks in the same chain points to the same vector chain_deadlines_deque, and the size of chain_deadlines_deque is set to , a deadline queue of different instances of the storage chain, and the head element of the queue represents the deadline of the chain instance.
[0021] Furthermore, the cur_index field in CallbackExecutable indicates the chain instance number corresponding to the current callback instance.
[0022] Furthermore, after any callback instance is scheduled by the ROS2 multithreaded executor, its cur_index is updated to , indicating that the next instance scheduled to this callback belongs to the next chain instance.
[0023] Furthermore, an error correction mechanism is introduced to autonomously and quickly eliminate priority inversion; the error correction mechanism ensures that when two chain instances have the same cur_index, the priority of the chain instance released first is greater than the priority of the chain instance released later.
[0024] Compared with the existing ROS2 multi-threaded executor scheduling method, the beneficial effect of the present invention is that the deadline-driven ROS2 multi-threaded executor scheduling method divides the callback instances into priority granularities at the chain instance level, thereby improving the real-time performance of the ROS2 application. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Figure 1 A thread workflow diagram in a ROS2 multi-threaded executor according to the present invention is shown;
[0026] Figure 2 shows a diagram of a CallbackExecutable structure instance according to the present invention;
[0027] Figure 3 A schematic diagram of the ROS2 multi-threaded executor architecture according to the present invention is shown. DETAILED DESCRIPTION
[0028] The technical solution of the present invention will be described clearly and completely below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0029] In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0030] In the description of the present invention, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0031] The following combination Figure 1-Figure 3 The specific embodiments of the present invention are described in detail. It should be understood that the specific embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.
[0032] The inventive concept of the deadline-driven ROS2 multi-threaded executor scheduling method according to the present invention is: designing a scheduling unit, which is used for the ROS2 multi-threaded executor to read and update the chain instance attributes corresponding to the callback instance when scheduling; introducing an error correction mechanism to autonomously and quickly eliminate priority inversion; reconstructing the ROS2 multi-threaded executor architecture to perform real-time priority sorting on the ready callback instance set and reduce runtime overhead; the present invention provides a chain instance-level priority granularity division of the callback instance for the ROS2 multi-threaded executor, which is used to ensure that ROS2 applications in safety-critical areas meet strict real-time constraints.
[0033] In one embodiment, Figure 1-Figure 3 As shown, a ROS2 multi-threaded executor scheduling method based on deadline driving takes the deadline of the chain instance as the basis for scheduling the callback instance of the ROS2 multi-threaded executor. The specific implementation steps of the scheduling method are as follows:
[0034] Step S1: Multiple threads of the multi-threaded executor compete to acquire the lock (the multi-threaded executor has only one lock). The thread that acquires the lock executes the next step, and the thread that does not acquire the lock waits for the lock to be released until the thread holding the lock releases the lock and competes for the lock again.
[0035] The multi-threaded executor has only one lock, ensuring that only one thread holds the lock at the same time. Other threads need to wait for the lock to be released before they can continue execution, thus avoiding data competition and inconsistency problems caused by multiple threads accessing the priority queue at the same time.
[0036] Step S2: Use temporary variable t to temporarily store the head element of the priority queue of the multi-threaded executor. The head element of the queue is deleted. The callback instance corresponding to the CallbackExecutable structure pointed to by t is the callback instance with the highest current priority. The head element of the queue indicates the deadline of the chain instance.
[0037] Step S3: Release the lock.
[0038] Step S4: Execute the function pointed to by the function handle of the CallbackExecutable structure pointed to by t.
[0039] Step S5: Check whether there is a newly released callback instance. If yes, add the CallbackExecutable structure pointer corresponding to the newly released callback instance to the priority queue.
[0040] Step S6: Return to step S1.
[0041] The specific implementation scheme of the ROS2 multi-threaded executor to implement the scheduling method includes:
[0042] In order to associate the deadline of the callback instance with the chain instance, the scheduling unit is designed to encapsulate the callback function handle, callback type and corresponding chain attributes into a structure CallbackExecutable.
[0043] The relative deadline and release period of a chain are defined by the application developer. The relative deadline of a chain is represented by D, and the release period of a chain is represented by T. The release time of a chain instance represents the time when the chain releases the instance with a period of T. The deadline of a chain instance is equal to the release time of the chain instance plus D. All callback instances of the same chain instance are based on the same deadline. The deadlines field of the CallbackExecutable structure corresponding to all callbacks of the same chain points to the same chain_deadlines_deque vector. The size of the chain_deadlines_deque vector is set to , a deadline queue of different instances of the storage chain, and the head element of the queue represents the deadline of the chain instance.
[0044] The cur_index field in the CallbackExecutable structure indicates the chain instance number corresponding to the current callback instance. After any callback instance is scheduled by the ROS2 multithreaded executor, its cur_index is updated to , indicating that the next instance scheduled to this callback belongs to the next chain instance.
[0045] The deadline queue update rule in the chain_deadlines_deque vector is:
[0046] (1) If the callback instance is the first callback of a chain instance, the interval from the current time to the next chain instance release time is obtained through the timer_until_trigger field of the callback. This interval plus the relative deadline of the chain is the deadline of the next chain instance. This deadline is added to the first of the chain_deadlines_deque vectors. in a queue.
[0047] (2) If the callback instance is the last callback of the chain instance, remove the head element of the cur_index queue of the chain_deadlines_deque vector.
[0048] The error correction mechanism ensures that when two chain instances have the same cur_index, the priority of the chain instance released first is greater than the priority of the chain instance released later; a counter counter is added to the CallbackExecutable structure to indicate the number of times the callback corresponding to the CallbackExecutable structure is scheduled. The priority comparison rule of the callback instance is:
[0049] (1) The deadlines of the chain instances corresponding to the callback instances are different. The one with the shorter deadline has the higher priority.
[0050] (2) If the deadline of the chain instance corresponding to the callback instance is the same, the one with a smaller counter has a higher priority.
[0051] The reconstructed ROS2 multi-threaded executor architecture includes a priority queue and a hash table. The hash table stores the CallbackExecutable structures corresponding to all callbacks in the system, and realizes the mapping from the callback function handle to the CallbackExecutable structure corresponding to the callback; the priority queue stores the pointers to the CallbackExecutable structures of the ready callback instances to reduce runtime overhead, and sorts the pointers to the CallbackExecutable structures according to the priority of the callback instances; the ROS2 multi-threaded executor threads schedule the CallbackExecutable structure pointed to by the head pointer of the priority queue in a mutually exclusive manner, and then delete the head element of the priority queue, and perform specific operations according to the function handle of the CallbackExecutable structure; after each execution of the callback instance, the ROS2 multi-threaded executor checks whether there is a newly released callback instance, and adds the CallbackExecutable structure pointer corresponding to the newly released callback instance to the priority queue.
[0052] According to the deadline-driven ROS2 multi-threaded executor scheduling method of the present invention, the chain instance uses the deadline as the basis for the priority size. The smaller the deadline of the chain instance, the greater the priority of the chain instance; the priority of the callback instance is equal to the priority of its corresponding chain instance; the threads of the ROS2 multi-threaded executor access the ready callback instance set in a mutually exclusive manner, and the thread schedules the callback instance with the highest priority each time.
[0053] The method of the present invention further comprises:
[0054] (1) Design a scheduling unit to read and update the chain instance attributes corresponding to the callback instance during scheduling of the ROS2 multi-threaded executor;
[0055] (2) Introduce an error correction mechanism to autonomously and quickly eliminate priority inversion situations;
[0056] (3) Refactor the ROS2 multi-threaded executor architecture to perform real-time prioritization of the set of ready callback instances and reduce runtime overhead.
[0057] Compared with the existing ROS2 multi-threaded executor scheduling method, the ROS2 multi-threaded executor scheduling method based on deadline-driven scheduling of the present invention divides the callback instances into priority granularities at the chain instance level, thereby improving the real-time performance of ROS2 applications.
[0058] Any process or method description in the flowchart of the present invention or described in other ways herein can be understood as a module, segment or part of a code including one or more executable instructions for implementing the steps of a specific logical function or process, which can be implemented in any computer-readable medium for use by an instruction execution system, device or equipment, and the computer-readable medium can be any medium containing storage, communication, propagation or transmission programs for use by execution systems, devices or equipment, including read-only memories, magnetic disks or optical disks, etc.
[0059] In the description of this specification, the description with reference to the terms "embodiment", "example", etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. In addition, those skilled in the art can combine or combine different embodiments or examples described in this specification and the features therein without causing any contradiction.
[0060] Although the above content has shown and described the embodiments of the present invention, it can be understood that the above embodiments are exemplary and cannot be understood as limitations of the present invention. Ordinary technicians in the field can change, modify, replace, modify and other update operations on the above embodiments within the scope of the present invention.
Claims
1. A deadline-driven ROS2 multi-threaded executor scheduling method, applied to a robot operating system, characterized in that: The method uses the deadline of the chain instance as the basis for scheduling the callback instance of the ROS2 multi-threaded executor. The specific steps are as follows: Step S1: Multiple threads of the multi-threaded executor compete to acquire the lock. The thread that acquires the lock executes the next step, and the thread that does not acquire the lock waits for the lock to be released until the thread holding the lock releases the lock and competes for the lock again. Step S2: Use the temporary variable t to temporarily store the head element of the priority queue of the multi-threaded executor, delete the head element, and the callback instance corresponding to the CallbackExecutable structure pointed to by the temporary variable t is the callback instance with the highest current priority; Step S3: Release the lock; Step S4: Execute the function pointed to by the function handle of the CallbackExecutable structure pointed to by the temporary variable t; Step S5: Check whether there is a newly released callback instance, and if so, add the CallbackExecutable structure pointer corresponding to the newly released callback instance to the priority queue; Step S6: Return to step S1.
2. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 1 is characterized in that: The method divides the callback instances into priority granularities at the chain instance level, which is used to ensure that ROS2 applications in safety-critical areas meet strict real-time constraints.
3. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 1 is characterized in that: The multi-threaded executor has only one lock, ensuring that only one thread holds the lock at the same time. Other threads need to wait for the lock to be released before they can continue execution, thus avoiding data competition and inconsistency problems caused by multiple threads accessing the priority queue at the same time.
4. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 3 is characterized in that: In order to associate the deadlines of the callback instance and the chain instance, the scheduling unit is designed as follows: the callback function handle, callback type and corresponding chain attributes are encapsulated into a structure CallbackExecutable as the scheduling unit of the ROS2 multi-threaded executor.
5. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 4 is characterized in that: The relative deadline and release cycle of the chain are defined by the application developer. The relative deadline of the chain is represented by D, and the release cycle of the chain is represented by T. The release time of the chain instance represents the time when the chain releases the instance with a period of T.
6. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 5, characterized in that: For the CallbackExecutable, the deadline of the chain instance is equal to the release time of the chain instance plus the relative deadline of the chain. All callback instances of the same chain instance are based on the same deadline.
7. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 6 is characterized in that: The deadlines field of CallbackExecutable corresponding to all callbacks of the same chain points to the same vector chain_deadlines_deque, and the size of chain_deadlines_deque is set to , a deadline queue of different instances of the storage chain, and the head element of the queue represents the deadline of the chain instance.
8. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 4, characterized in that: The cur_index field in CallbackExecutable indicates the chain instance number corresponding to the current callback instance.
9. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 8, characterized in that: After any callback instance is scheduled by the ROS2 multithreaded executor, its cur_index is updated to , indicating that the next instance scheduled to this callback belongs to the next chain instance.
10. The deadline-driven ROS2 multi-threaded executor scheduling method according to claim 9, characterized in that: Introducing an error correction mechanism to autonomously and quickly eliminate priority inversion situations; The error correction mechanism ensures that when two chain instances have the same cur_index, the priority of the chain instance released first is higher than the priority of the chain instance released later.
Citation Information
Patent Citations
Real-time task scheduling method and device for non-preemptive system
CN116438520A
Realization method and device of ROS2 scheduler, equipment and storage medium
CN116661999A
Callback system and method based on event triggering, electronic equipment, storage medium and vehicle
CN116932246A
Lightweight DDS middleware and application thereof in AUTOSAR Classic platform
CN118075309A
Method for minimizing lock contention among threads when tasks are distributed in multithreaded system and appratus using the same
US20160335135A1
Cited By
ROS2 hybrid scheduling actuator system
CN119557076A
Robot operating system communication method, electronic equipment and storage medium
CN121077982A
Robot operating system communication method, electronic device, and storage medium
CN121077982B
