A method, device and storage medium for implementing a ROS2 scheduler

CN116661999BActive Publication Date: 2026-09-22GUOKE FOUNDATION STONE (CHONGQING) SOFTWARE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310633397.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-31
Publication Date
2026-09-22
Estimated Expiration
2043-05-31

AI Technical Summary

Technical Problem

[0003]由于在机器人的使用场景中,实时性和确定性的要求不太严格,ROS2的执行器不能够设置用户任务的优先级,导致执行器不能够协调用户任务的调度优先级,即用户的所有任务对于执行器的调度器来说都是同样的优先级,采用的公平调度的策略,没有对回调任务执行顺序的显式控制

Benefits of technology

[0016]本公开的实施例提供的技术方案可以包括以下有益效果:通过加载用户任务配置文件,得到任务动态库的位置信息,并根据位置信息对任务动态库进行读取,得到待处理任务;根据待处理任务创建若干线程实例,并将线程实例传输至调度器,调度器将线程实例创建为可调度的任务实体;读入调度配置文件,得到线程实例的优先级配置,通过调度配置文件对任务处理的先后顺序进行设置,能够实现不同优先级线程的按序处理,且便于优先级的修改,通过动态热加载的方式能够实时读入最新的调度配置文件,以便于进行优先级的灵活配置;根据优先级配置赋予线程实例对应的优先级,并基于优先级将任务实体添加至调度队列,生成任务线程;检测到待处理任务的运行条件具备时,根据优先级等级在调度队列中提取出对应的任务线程作为目标线程,赋予目标线程对应的权限并执行,从而实现任务调度的确定性,有利于适配不同应用场景,实现计算资源的合理利用。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116661999B_ABST
    Figure CN116661999B_ABST
Patent Text Reader

Abstract

The present disclosure relates to a ROS2 scheduler implementation method, device, equipment and storage medium, wherein the method comprises: loading a user task configuration file, obtaining the location information of a task dynamic library, reading the task dynamic library according to the location information, and obtaining a to-be-processed task; creating a plurality of thread instances according to the to-be-processed task, and transmitting the thread instances to a scheduler, and creating the thread instances as schedulable task entities; reading in a scheduling configuration file to obtain the priority configuration of the thread instances; according to the priority configuration, giving the thread instances corresponding priorities, and based on the priorities, adding the task entities to a scheduling queue to generate task threads; when it is detected that the running conditions of the to-be-processed task are met, extracting the corresponding task thread as a target thread in the scheduling queue according to the priority level, giving the target thread corresponding permissions and executing. The present disclosure realizes flexible configuration of priorities, adapts to different application scenarios, and reasonably utilizes computing resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of vehicle system technology, and in particular to a method, apparatus, electronic device and computer-readable storage medium for implementing a ROS2 scheduler. Background Technology

[0002] In related technologies, ROS2 was initially applied in the field of robotics. The native ROS2 executor has both single-threaded and multi-threaded versions. ROS2's built-in executor is used to coordinate and schedule callbacks within a process. A ROS2 application has one or more callbacks, and the executor encapsulates each callback as a user task. A single thread maps to a process in the operating system, while a multi-threaded executor maps to multiple processes in the operating system. A single-threaded executor executes callbacks within a single thread; a multi-threaded executor executes callbacks across multiple threads.

[0003] Because real-time and deterministic requirements are not very stringent in robot applications, ROS2 actuators cannot set user task priorities. This results in the actuators being unable to coordinate user task scheduling priorities; all user tasks have the same priority to the actuator scheduler, employing a fair scheduling strategy without explicit control over the execution order of callback tasks. However, to apply ROS2 in the automotive field, where real-time and deterministic requirements are higher, and to meet the diverse priority needs of user tasks in various automotive applications across different scenarios, the native ROS2 actuator scheduling method is clearly insufficient for automotive applications with high real-time and deterministic requirements. Therefore, it is necessary to address the key scheduling issues in automotive applications.

[0004] Therefore, there is an urgent need for a method to implement a ROS2 scheduler that can achieve multi-level scheduling, is easy to modify, and flexibly adapt to various application scenarios. Summary of the Invention

[0005] To overcome the problems existing in related technologies, this disclosure provides a method, apparatus, electronic device and computer-readable storage medium for implementing a ROS2 scheduler.

[0006] According to a first aspect of the present disclosure, a method for implementing a ROS2 scheduler is provided, comprising: loading a user task configuration file, obtaining location information of a task dynamic library, and reading the task dynamic library according to the location information to obtain a task to be processed; creating a plurality of thread instances according to the task to be processed, and transmitting the thread instances to the scheduler, wherein the scheduler creates the thread instances as schedulable task entities; reading a scheduling configuration file to obtain a priority configuration of the thread instances; assigning a priority to the thread instances according to the priority configuration, and adding the task entities to a scheduling queue based on the priority to generate task threads; when the running conditions of the task to be processed are detected, extracting the corresponding task thread from the scheduling queue according to the priority level as a target thread, granting the target thread the corresponding permissions, and executing it.

[0007] In some embodiments, the step of creating a plurality of thread instances based on the task to be processed and transmitting the thread instances to the scheduler, wherein the scheduler creates the thread instances as scheduled task entities, includes: encapsulating the task to be processed into an anonymous execution function, wherein the anonymous execution function can be executed after being selected by the scheduler; creating a plurality of thread instances based on the anonymous execution function and marking the thread instances with a task identifier matching the task to be processed; transmitting the thread instances to the scheduler through the scheduler interface, wherein the scheduler creates the thread instances as schedulable task entities.

[0008] In some embodiments, before reading the scheduling configuration file, the method further includes: setting corresponding priorities according to the order of the tasks to be processed; configuring priority scheduling for all tasks to be processed according to the priorities, wherein each task to be processed corresponds to a unique priority, thereby obtaining the scheduling configuration file.

[0009] In some embodiments, reading the scheduling configuration file to obtain the priority configuration of the thread instance includes: reading and parsing a pre-set scheduling configuration file to obtain configuration parameters, wherein the configuration parameters include scheduling policy type, CPU affinity, scheduling group classification, task name and priority, and the task name corresponds one-to-one with the task identifier; initializing the scheduler according to the configuration parameters, and determining the task name corresponding to the thread instance based on the task identifier; and finding the priority configuration corresponding to the thread instance in the scheduling configuration file according to the task name.

[0010] In some embodiments, the step of assigning a priority to the thread instance according to the priority configuration and adding the task entity to the scheduling queue based on the priority to generate a task thread includes: obtaining and assigning a priority to the thread instance according to the priority configuration; obtaining a preset maximum priority and adjusting the priorities of the thread instance that exceed the preset maximum priority; adding the task entity corresponding to the thread instance to the scheduling queue of the scheduler according to the adjusted priority to generate the corresponding task thread.

[0011] In some embodiments, when the running conditions of the task to be processed are detected to be met, extracting the corresponding task thread as the target thread according to the priority level in the scheduling queue, granting the target thread the corresponding permissions, and executing the task includes: detecting whether the running conditions of the task to be processed are met; when the running conditions of the task to be processed are met, performing priority traversal on the task threads that meet the running conditions in the scheduling queue according to the priority level, obtaining the task thread with the highest current priority as the target thread; adjusting the state of the target thread, granting the target thread the corresponding permissions, and executing the target thread according to the obtained permissions.

[0012] In some embodiments, the method further includes: forming a scheduler task priority structure in the scheduler based on the tasks to be processed and their priorities, the scheduler task priority structure including an array and a priority matrix corresponding to each value in the array; wherein the array uses group names as keys, the priority matrix has multiple levels of priority queues, the priority queues include multiple priorities, and each priority corresponds to several task threads to be executed; when the scheduler searches for the next task thread to be executed, any one group is selected from the multiple arrays, the priority matrix corresponding to the array is traversed, and the task thread with the highest priority in the group is found according to the priority, which is taken as the highest task thread in the group; all arrays are traversed to obtain the highest task thread in each array, the highest task thread in the group is compared, and the highest priority task thread in all arrays is obtained as the target thread.

[0013] According to a second aspect of the present disclosure, an implementation apparatus for a ROS2 scheduler is provided, comprising: a task acquisition module, configured to load a user task configuration file, acquire location information of a task dynamic library, and read the task dynamic library according to the location information to obtain a task to be processed; a task thread creation module, configured to create a plurality of thread instances according to the task to be processed, and transmit the thread instances to the scheduler, wherein the scheduler creates the thread instances as schedulable task entities; a priority configuration acquisition module, configured to read a scheduling configuration file to obtain the priority configuration of the thread instances; a priority assignment module, configured to assign a priority corresponding to the thread instances according to the priority configuration, and add the task entities to a scheduling queue based on the priority to generate task threads; and a target thread execution module, configured to, when the running conditions of the task to be processed are met, extract the corresponding task thread from the scheduling queue according to the priority level as a target thread, grant the target thread the corresponding permissions, and execute it.

[0014] According to a third aspect of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing executable instructions of the processor; the processor being configured to read the executable instructions from the memory and execute the instructions to implement the ROS2 scheduler implementation method provided in the first aspect of the present disclosure.

[0015] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, having stored thereon computer program instructions that, when executed by a processor, implement the steps of the implementation method of the ROS2 scheduler provided in the first aspect of the present disclosure.

[0016] The technical solutions provided by the embodiments of this disclosure can include the following beneficial effects: by loading the user task configuration file, the location information of the task dynamic library is obtained, and the task dynamic library is read according to the location information to obtain the task to be processed; several thread instances are created according to the task to be processed, and the thread instances are transmitted to the scheduler, which creates the thread instances as schedulable task entities; the scheduling configuration file is read to obtain the priority configuration of the thread instances, and the order of task processing is set through the scheduling configuration file, which can realize the sequential processing of threads with different priorities and facilitate the modification of priorities. The latest scheduling configuration file can be read in real time through dynamic hot loading, so as to facilitate flexible priority configuration; the thread instances are assigned the corresponding priorities according to the priority configuration, and the task entities are added to the scheduling queue based on the priorities to generate task threads; when the running conditions of the task to be processed are detected, the corresponding task thread is extracted from the scheduling queue according to the priority level as the target thread, the target thread is given the corresponding permissions and executed, thereby realizing the determinism of task scheduling, which is conducive to adapting to different application scenarios and realizing the rational utilization of computing resources.

[0017] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description

[0018] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.

[0019] Figure 1 This is a flowchart illustrating an implementation method of a ROS2 scheduler according to an exemplary embodiment.

[0020] Figure 2 This is a flowchart illustrating a method for creating a thread instance according to an exemplary embodiment.

[0021] Figure 3 This is a flowchart illustrating a priority scheduling configuration method according to an exemplary embodiment.

[0022] Figure 4 This is a flowchart illustrating a priority configuration acquisition method according to an exemplary embodiment.

[0023] Figure 5 This is a flowchart illustrating a task thread generation method according to an exemplary embodiment.

[0024] Figure 6 This is a flowchart illustrating a method for executing a target thread according to an exemplary embodiment.

[0025] Figure 7 This is a flowchart illustrating a method for obtaining a target thread according to an exemplary embodiment.

[0026] Figure 8 This is a schematic diagram illustrating a scheduler priority structure according to an exemplary embodiment.

[0027] Figure 9 This is a block diagram illustrating an implementation apparatus for a ROS2 scheduler according to an exemplary embodiment.

[0028] Figure 10 This is a block diagram illustrating an electronic device according to an exemplary embodiment. Detailed Implementation

[0029] The exemplary embodiments will now be described in detail with reference to the accompanying drawings.

[0030] It should be noted that the relevant embodiments and accompanying drawings are only for describing and illustrating exemplary embodiments provided by this disclosure, and not all embodiments of this disclosure, nor should this disclosure be understood to be limited to the relevant exemplary embodiments.

[0031] It should be noted that the terms "first," "second," etc., used in this disclosure are only used to distinguish different steps, devices, or modules. These terms do not represent any specific technical meaning, nor do they indicate any order or interdependence between them.

[0032] It should be noted that the terms “a,” “a plurality of,” and “at least one” used in this disclosure are illustrative rather than restrictive. Unless otherwise expressly indicated in the context, they should be understood as “one or more.”

[0033] It should be noted that the term "and / or" used in this disclosure is used to describe the relationship between related objects, and generally indicates that there are at least three relationships. For example, A and / or B can at least indicate: the existence of A alone, the existence of both A and B, and the existence of B alone.

[0034] It should be noted that the various steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Unless otherwise specified, the scope of this disclosure is not limited by the order in which the steps are described in the relevant embodiments.

[0035] It should be noted that all actions involving the acquisition of signals, information, or data in this disclosure are carried out in compliance with the relevant data protection laws and policies of the country where the location is situated, and with authorization from the owner of the relevant device.

[0036] Exemplary methods

[0037] Figure 1 This is a flowchart illustrating an implementation method of a ROS2 scheduler according to an exemplary embodiment, such as... Figure 1 As shown, the implementation method of the ROS2 scheduler for use in an in-vehicle system includes the following steps:

[0038] In step S110, the user task configuration file is loaded, the location information of the task dynamic library is obtained, and the task dynamic library is read according to the location information to obtain the task to be processed.

[0039] In some embodiments, the user's application exists as a dynamic library, which can load the contents of the user task configuration file TaskConfig. The module_library records the location of the task dynamic library, thereby obtaining the location information of the task dynamic library. The dynamic library management structure class_loader_manager manages the dynamic library information of all user tasks, and reads the task dynamic library according to the location information to obtain the task to be processed.

[0040] In step S120, several thread instances are created based on the task to be processed, and the thread instances are transmitted to the scheduler, which then creates the thread instances as schedulable task entities.

[0041] In some embodiments, by loading the user's task dynamic library, a task to be processed is obtained, namely several processing procedures of the user. The user's processing procedure Process is encapsulated into an anonymous function. The thread factory uses the encapsulated anonymous function to create several thread instances and transmits them to the scheduler. The scheduler creates schedulable task entities based on the thread instances so as to facilitate the scheduling of the task entities.

[0042] In step S130, the scheduling configuration file is read in to obtain the priority configuration of the thread instance.

[0043] In some embodiments, priority configuration is performed on several thread instances corresponding to the tasks to be processed to obtain a scheduling configuration file. The configuration file sets priority ranges and uses the .conf extension. Each task to be processed corresponds to a task name and its priority. When the program initializes the scheduler instance, it loads the user-defined scheduler .conf file according to the configuration, parses the corresponding fields in the file, and initializes the scheduler parameters based on these fields. This allows the priority configuration of thread instances to be obtained by reading the scheduling configuration file, meeting the multi-level task requirements in various scenarios of automotive applications. Explicitly setting task priorities through the scheduling configuration file provides a more intuitive understanding of the current task's execution status and allows for flexible configuration of thread instance priorities, facilitating modification and enabling dynamic hot-loading of the latest configuration. Dynamic hot-loading refers to obtaining and applying modified configurations without system downtime.

[0044] In step S140, the thread instance is assigned a priority according to the priority configuration, and the task entity is added to the scheduling queue based on the priority to generate the task thread.

[0045] In some embodiments, after the scheduler loads the priority configuration of the thread instance, it assigns the corresponding priority to the thread instance in DispatchTask according to the priority configuration, and adds the corresponding task entity to the scheduling queue based on the priority level to generate task threads. Several task threads process the task to be processed, and the task is scheduled and executed according to the priority of the task thread to ensure strict execution of task priorities.

[0046] In step S150, when the running conditions of the task to be processed are detected, the corresponding task thread is extracted from the scheduling queue according to the priority level as the target thread, the target thread is given the corresponding permissions and executed.

[0047] In some embodiments, after the above steps, the task to be processed and its scheduling configuration have been instantiated. Then, the running conditions of the task to be processed are checked in real time. If they are met, the highest priority task thread is extracted from the scheduling queue as the target thread, and the target thread is granted the necessary CPU privileges. The target thread is then executed according to the granted CPU privileges. After execution, the CPU privileges are released, and the next highest priority task thread is found among the remaining task threads as the new target thread. Privileges are then granted and execution steps are repeated until all task threads to which the task belongs have completed execution, thus completing the processing of the task. The above steps implement a fixed-priority execution flow, making task scheduling deterministic and ensuring that tasks are executed strictly according to their priority without priority inversion. Modifying task priorities can effectively preempt computing resources, facilitating adaptation to different application scenarios and the rational utilization of computing resources.

[0048] In the above embodiments, the location information of the task dynamic library is obtained by loading the user task configuration file, and the task dynamic library is read according to the location information to obtain the task to be processed; several thread instances are created according to the task to be processed, and the thread instances are transmitted to the scheduler, which creates the thread instances as schedulable task entities; the scheduling configuration file is read to obtain the priority configuration of the thread instances, and the order of task processing is set by the scheduling configuration file, which can realize the sequential processing of threads with different priorities and facilitate the modification of priorities. The latest scheduling configuration file can be read in real time through dynamic hot loading, so as to facilitate flexible priority configuration; the thread instances are assigned the corresponding priority according to the priority configuration, and the task entities are added to the scheduling queue based on the priority to generate task threads; when the running conditions of the task to be processed are detected, the corresponding task thread is extracted from the scheduling queue according to the priority level as the target thread, the target thread is given the corresponding permissions and executed, thereby realizing the determinism of task scheduling, which is conducive to adapting to different application scenarios and realizing the rational utilization of computing resources.

[0049] Figure 2 This is a flowchart illustrating a thread instance creation method according to an exemplary embodiment, such as... Figure 2 As shown, the thread instance creation method is used in step S120 and includes the following steps:

[0050] In step S210, the task to be processed is encapsulated as an anonymous execution function, which can be executed after being selected by the scheduler.

[0051] Specifically, the task to be processed is obtained by loading the task dynamic library, and the processing procedure of the task to be processed is encapsulated as an anonymous execution function, which can be executed after being selected by the scheduler.

[0052] In step S220, several thread instances are created based on the anonymous execution function, and each thread instance is marked with a task identifier that matches the task to be processed.

[0053] Specifically, the thread factory uses an encapsulated anonymous execution function to create several thread instances. The task to be processed is matched with all the created thread instances, and the thread instances are marked with a task identifier that matches the task to be processed, so as to facilitate the identification of thread instances.

[0054] In step S230, the thread instance is transferred to the scheduler through the scheduler interface, and the scheduler creates the thread instance as a schedulable task entity.

[0055] Specifically, the obtained thread instance is transmitted to the scheduler through the scheduler interface CreateTask. The scheduler will assign the task priority to the user thread according to the task priority in the scheduling configuration file. After the scheduler receives the thread instance, it will create a schedulable task entity based on the thread instance. The thread instance and the task entity correspond one-to-one, which facilitates subsequent task scheduling.

[0056] In this embodiment, the task to be processed is encapsulated as an anonymous execution function, which can be executed after being selected by the scheduler. Several thread instances are created based on the anonymous execution function, and each thread instance is marked with a task identifier that matches the task to be processed for identification. The thread instances are then transmitted to the scheduler through the scheduler interface, and the scheduler creates the thread instances as schedulable task entities for subsequent task scheduling.

[0057] Figure 3 This is a flowchart illustrating a priority scheduling configuration method according to an exemplary embodiment, such as... Figure 3 As shown, the priority scheduling configuration method is used before step S130 and includes the following steps.

[0058] In step S310, the corresponding priorities are set according to the order of the tasks to be processed.

[0059] Specifically, the implementation of a task requires multiple steps. For example, playing music requires opening the player, selecting the music, and playing it. Therefore, before reading the priority scheduling configuration file, priorities can be set for each process in the task according to the order of processing. The priorities for opening the player, selecting the music, and playing the music can be set to 3, 2, and 1, respectively. This ensures that the task can be processed sequentially according to the set priorities and ultimately achieve the goal of playing music, thus ensuring that the task can be scheduled and processed according to its priority.

[0060] Similarly, when there are multiple tasks to be processed, the tasks with higher priority can be processed first according to the set priority among the tasks, so that computing resources can be used rationally.

[0061] In step S320, priority scheduling configuration is performed on all tasks to be processed according to their priorities. Each task to be processed corresponds to a unique priority, and a scheduling configuration file is obtained.

[0062] Specifically, after determining the priority of the tasks to be processed, priority scheduling configuration is performed on all tasks according to their priorities. Since the priority is set based on the steps to be implemented by the task to ensure that the goal of the task is achieved, the priority of each task is usually unique. Based on the configured priority scheduling, a scheduling configuration file for the tasks to be processed is obtained. The scheduling configuration file includes the priority configuration of each process in the task. The scheduling configuration file allows for flexible setting of task priorities to meet the multi-level task requirements in various scenarios of in-vehicle applications, and facilitates intuitive display of task processing progress. In addition, setting priorities through the scheduling configuration file makes it easy to modify task priorities and allows for real-time updates of task priorities through dynamic hot reloading, improving the scheduling flexibility of the tasks to be processed.

[0063] In this embodiment, priorities are set according to the order of the tasks to be processed, so that the tasks can be processed according to their priorities, realizing the rational use of computing resources. Priority scheduling configuration is performed on the tasks to be processed according to their priorities, and each task to be processed corresponds to a unique priority, resulting in a scheduling configuration file. The scheduling configuration file realizes the setting of task priorities, which can meet the multi-level task requirements of various scenarios in vehicle applications, and can intuitively display the processing progress of tasks. At the same time, the priorities in the scheduling configuration file can be modified according to actual needs, and the priority adjustment is updated in real time using a dynamic hot-loading method, which improves the flexibility of task scheduling.

[0064] Figure 4This is a flowchart illustrating a priority configuration acquisition method according to an exemplary embodiment, such as... Figure 4 As shown, the priority configuration acquisition method is used in step S130 and includes the following steps.

[0065] In step S410, a pre-set scheduling configuration file is read and parsed to obtain configuration parameters. The configuration parameters include scheduling policy type, CPU affinity, scheduling group classification, task name and priority. The task name and task identifier correspond one-to-one.

[0066] Specifically, the pre-defined scheduling configuration file for the task to be processed is read in. The scheduling configuration file has the .conf extension. The obtained scheduling configuration file is parsed to obtain configuration parameters, including scheduling policy type, CPU affinity, scheduling group classification, task name and priority. The task name corresponds one-to-one with the task identifier. The priority setting ranges from 0 to 19, with higher values ​​indicating higher priority. The scheduling group classification can logically classify multiple user tasks.

[0067] In step S420, the scheduler is initialized according to the configuration parameters, and the task name corresponding to the thread instance is determined based on the task identifier.

[0068] Specifically, the scheduler is initialized based on the parsed configuration parameters. Since there is a one-to-one correspondence between the task identifier and the task name, the task name corresponding to the thread instance can be obtained based on the task identifier, so as to match the thread instance with the task to be processed, and configure the priority of the thread instance according to the scheduling configuration file.

[0069] In step S430, the priority configuration corresponding to the thread instance is found in the scheduling configuration file based on the task name.

[0070] Specifically, based on the task name corresponding to the thread instance, the corresponding scheduling configuration file is determined, and the priority configuration corresponding to the thread instance is found in the scheduling configuration file. This enables the acquisition of the thread instance priority configuration, so as to set the priority of the thread instance according to the priority configuration and schedule the thread according to the priority, thereby realizing multi-level task scheduling and meeting the needs of multi-level task scheduling.

[0071] In this embodiment, a pre-set scheduling configuration file is read and parsed to obtain configuration parameters, including task names that correspond one-to-one with task identifiers. The scheduler is initialized according to the configuration parameters, and the task name corresponding to the thread instance is determined based on the task identifier, so as to associate the thread instance with the task to be processed. Based on the obtained task name, the priority configuration corresponding to the thread instance is found in the scheduling configuration file, so that the priority of the thread instance can be set according to its priority configuration, thereby realizing the orderly scheduling of threads and meeting the needs of multi-level task scheduling.

[0072] Figure 5 This is a flowchart illustrating a task thread generation method according to an exemplary embodiment, such as... Figure 5 As shown, the task thread generation method is used in step S140 and includes the following steps.

[0073] In step S510, the priority corresponding to the thread instance is obtained and assigned according to the priority configuration.

[0074] Specifically, after obtaining the priority configuration of the thread instance, the scheduler loads the priority configuration and assigns the corresponding priority to the thread instance to ensure that the thread instance can be strictly executed according to the priority order.

[0075] In step S520, the preset maximum priority is obtained, and the priority of thread instances that exceeds the preset maximum priority is adjusted.

[0076] Specifically, to ensure thread scheduling efficiency, a priority upper limit threshold can be preset in the scheduler or configuration file. For example, if it is set to 19, then 19 will be the maximum priority. The priority of the thread instance will be checked. If there is a priority greater than 19, it needs to be adjusted. For example, the priority greater than 19 will be adjusted to 19 to avoid the threads becoming too cumbersome, so as to improve scheduling efficiency.

[0077] In step S530, according to the adjusted priority, the task entity corresponding to the thread instance is added to the scheduler's scheduling queue to generate the corresponding task thread.

[0078] Specifically, based on the adjusted priority, the task entity corresponding to the thread instance is added to the scheduler's scheduling queue, and the corresponding task thread is generated in the scheduling queue. The task threads in the scheduling queue can be scheduled in order according to their priority, so that the tasks can be scheduled in an orderly manner, realize the rational use of computing resources, and meet the task scheduling needs in various scenarios.

[0079] In this embodiment, the priority corresponding to the thread instance is obtained and assigned according to the priority configuration, and the preset maximum priority is obtained. When there is a thread instance with a priority exceeding the maximum priority, its priority is adjusted to ensure that the thread can be accurately scheduled. According to the adjusted priority, the task entity corresponding to the thread instance is added to the scheduler's scheduling queue to generate the corresponding task thread. The task thread can be scheduled in an orderly manner according to the priority in the queue, realizing the rational utilization of computing resources and meeting the multi-level task scheduling needs in various scenarios.

[0080] Figure 6 This is a flowchart illustrating a method for executing a target thread according to an exemplary embodiment, such as... Figure 6 As shown, the target thread execution method is used in step S150, which includes the following steps.

[0081] In step S610, it is detected whether the running conditions of the task to be processed are met.

[0082] Specifically, when the task thread corresponding to the task to be processed is in the scheduling queue, the running conditions of the task to be processed are detected in real time to determine whether the running conditions of the task to be processed are met, so as to trigger the task to be processed or continue to wait.

[0083] In step S620, when the running conditions of the task to be processed are met, the task threads that meet the running conditions are traversed in the scheduling queue according to their priority levels to obtain the task thread with the highest current priority, which is then used as the target thread.

[0084] Specifically, when the running conditions of the task to be processed are detected, all task threads corresponding to the task to be processed are obtained, all task threads are traversed according to their priority levels, all task threads are arranged from high to low priority levels, and the task thread with the highest current priority level is obtained and used as the target thread.

[0085] In step S630, the state of the target thread is adjusted, the target thread is granted the corresponding permissions, and the target thread is executed according to the obtained permissions.

[0086] Specifically, after obtaining the target thread, the target thread's state is adjusted to "running", the target thread is granted the CPU privileges required for execution, and the target thread is executed according to the obtained CPU privileges. After execution, the CPU privileges are released, and steps S620-S630 are repeated until all task threads corresponding to the pending task have been executed. This achieves the sequential processing of pending tasks, makes reasonable use of computing resources, and can flexibly adapt to different application scenarios.

[0087] In this embodiment, the system detects whether the running conditions of the task to be processed are met. If they are met, the system iterates through the task threads in the scheduling queue according to their priority levels to obtain the task thread with the highest priority, which is then used as the target thread. The system adjusts the state of the target thread, grants it the corresponding permissions, and executes the target thread according to the obtained permissions. The above steps are repeated until all task threads corresponding to the task to be processed have been executed. This achieves the sequential processing of the task to be processed, makes reasonable use of computing resources, and can flexibly adapt to different application scenarios.

[0088] Figure 7 This is a flowchart illustrating a method for obtaining a target thread according to an exemplary embodiment, such as... Figure 7 As shown, the target thread acquisition method is used in step S120 and includes the following steps.

[0089] In step S710, in the scheduler, a scheduler task priority structure is formed according to the tasks to be processed and their priorities. The scheduler priority structure includes an array and a priority matrix corresponding to each value in the array. The array uses the group name as the key, and the priority matrix has multiple priority queues. Each priority queue includes multiple priorities, and each priority corresponds to several task threads to be executed.

[0090] Specifically, such as Figure 8 As shown, in the scheduler, a scheduler priority structure is formed based on the user-configured tasks and their priorities. One structure consists of a map (array) with group names as keys and a priority matrix of different priorities within the array as values. Each group contains multiple priorities, and each priority contains multiple tasks instantiated as threads.

[0091] The array uses the group name as the key, and the priority matrix has multiple priority queues. Each priority queue includes multiple priorities, such as priorities from 0 to 9. There are multiple task threads with the same priority. Therefore, each priority corresponds to several task threads to be executed.

[0092] In step S720, when the scheduler is looking for the next task thread to be executed, it selects any one of the multiple arrays, traverses the priority matrix corresponding to the array, and finds the task thread with the highest priority in the group according to the priority, which is then used as the highest priority task thread in the group.

[0093] Specifically, after the previous task thread finishes execution, the scheduler searches for the next task thread to be executed. It selects any one of the multiple arrays, traverses the priority matrix corresponding to the array, and arranges the task threads in the priority matrix according to their priority. The task thread with the highest priority level in the priority matrix is ​​then identified and recorded as the highest priority task thread in the group.

[0094] In step S730, all arrays are traversed to obtain the highest priority task thread in each array. The highest priority task threads in each array are compared to obtain the highest priority task thread in all arrays, which is then used as the target thread.

[0095] Specifically, during priority traversal, the scheduler uses NextRoutine to select the most suitable user task from the scheduler priority structure. By traversing all arrays, the highest-priority task thread within each group is obtained. By comparing all the highest-priority task threads within all groups, the highest-priority task thread in all arrays is selected as the target thread. This process enables the search for the target thread within the scheduler and ensures that the target threads are executed sequentially according to their priority, thus guaranteeing the task scheduling order.

[0096] In this embodiment, the scheduler configures a scheduler task priority structure based on tasks and their priorities. This structure includes an array and a priority matrix corresponding to each value in the array. The array uses the group name as the key. The priority matrix contains multiple priority queues, each with multiple priorities and corresponding to several task threads to be executed. When the scheduler searches for the next task thread to be executed, it arbitrarily selects one of the arrays, traverses the priority matrix corresponding to that array, and finds the highest priority task thread within the group based on its priority. It then traverses all arrays to obtain the highest priority task thread within each array. By comparing all the highest priority task threads within each group, the highest priority task thread in all arrays is obtained as the target thread. This enables the search for the target thread in the scheduler and ensures the scheduling order of tasks.

[0097] Exemplary device

[0098] Figure 9 This is a block diagram illustrating an implementation apparatus for a ROS2 scheduler according to an exemplary embodiment. (Refer to...) Figure 9 The device 800 includes a task acquisition module 810, a task thread creation module 820, a priority configuration acquisition module 830, a priority assignment module 840, and a target thread execution module 850.

[0099] The task acquisition module 810 is used to load the user task configuration file, obtain the location information of the task dynamic library, and read the task dynamic library according to the location information to obtain the task to be processed.

[0100] The task thread creation module 820 is used to create several thread instances based on the task to be processed, and transmit the thread instances to the scheduler, which then creates the thread instances as schedulable task entities.

[0101] The priority configuration acquisition module 830 is used to read the scheduling configuration file and obtain the priority configuration of the thread instance;

[0102] The priority assignment module 840 is used to assign the priority corresponding to the thread instance according to the priority configuration, and add the task entity to the scheduling queue based on the priority to generate the task thread;

[0103] The target thread execution module 850 is used to extract the corresponding task thread from the scheduling queue as the target thread according to the priority level when the running conditions of the task to be processed are met, and to grant the target thread the corresponding permissions and execute it.

[0104] In some embodiments, the task thread creation module 820 is further configured to encapsulate the task to be processed into an anonymous execution function, which can be executed after being selected by the scheduler; create several thread instances based on the anonymous execution function, and mark the thread instances with a task identifier that matches the task to be processed; and transmit the thread instances to the scheduler through the scheduler interface, and the scheduler creates the thread instances into a schedulable task entity.

[0105] In some embodiments, the priority configuration acquisition module 830 is further configured to set corresponding priorities according to the order of the tasks to be processed; perform priority scheduling configuration on all tasks to be processed according to the priorities, with each task to be processed corresponding to a unique priority, and obtain a scheduling configuration file.

[0106] In some embodiments, the priority configuration acquisition module 830 is further configured to read and parse a pre-set scheduling configuration file to obtain configuration parameters, including scheduling policy type, CPU affinity, scheduling group classification, task name and priority, with the task name corresponding to the task identifier; initialize the scheduler according to the configuration parameters, and determine the task name corresponding to the thread instance based on the task identifier; and find the priority configuration corresponding to the thread instance in the scheduling configuration file according to the task name.

[0107] In some embodiments, the priority assignment module 840 is further configured to obtain and assign the priority corresponding to the thread instance according to the priority configuration; obtain the preset maximum priority and adjust the priority of the thread instance that exceeds the preset maximum priority; and add the task entity corresponding to the thread instance to the scheduler's scheduling queue according to the adjusted priority to generate the corresponding task thread.

[0108] In some embodiments, the target thread execution module 850 is further configured to detect whether the running conditions of the task to be processed are met; when the running conditions of the task to be processed are met, the task threads that meet the running conditions are prioritized in the scheduling queue according to the priority level, and the task thread with the highest priority at present is obtained as the target thread; the state of the target thread is adjusted, the target thread is granted the corresponding permissions, and the target thread is executed according to the obtained permissions.

[0109] In the above embodiments, the operation of the device realizes a method for implementing a ROS2 scheduler, ensuring that the device can implement all the contents of the method for implementing a ROS2 scheduler.

[0110] Exemplary electronic devices

[0111] Figure 10 This is a block diagram illustrating an electronic device 900 according to an exemplary embodiment. The electronic device 900 may be a vehicle controller, an in-vehicle terminal, an in-vehicle computer, or other types of electronic devices.

[0112] Reference Figure 10 The electronic device 900 may include at least one processor 910 and a memory 920. The processor 910 can execute instructions stored in the memory 920. The processor 910 is communicatively connected to the memory 920 via a data bus. In addition to the memory 920, the processor 910 can also be communicatively connected to an input device 930, an output device 940, and a communication device 950 via the data bus.

[0113] Processor 910 can be any conventional processor, such as a commercially available CPU. Processors may also include graphics processing units (GPUs), field-programmable gate arrays (FPGAs), systems-on-chips (SoCs), application-specific integrated circuits (ASICs), or combinations thereof.

[0114] The memory 920 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk.

[0115] In this embodiment of the present disclosure, the memory 920 stores executable instructions, and the processor 910 can read the executable instructions from the memory 920 and execute the instructions to implement all or part of the steps of the implementation method of the ROS2 scheduler in any of the exemplary embodiments described above.

[0116] Exemplary computer-readable storage media

[0117] In addition to the methods and apparatus described above, exemplary embodiments of this disclosure may also be a computer program product or a computer-readable storage medium storing the computer program product. The computer product includes computer program instructions that can be executed by a processor to perform all or part of the steps described in any of the methods in the exemplary embodiments described above.

[0118] The computer program product can be written in any combination of one or more programming languages ​​to perform the operations of the embodiments of this application. The programming languages ​​include object-oriented programming languages ​​such as Java and C++, as well as conventional procedural programming languages ​​such as C or similar languages, and scripting languages ​​(e.g., Python). The program code can be executed entirely on the user's computing device, partially on the user's device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0119] The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media include: static random access memory (SRAM) having one or more electrically connected wires, electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk, or any suitable combination thereof.

[0120] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of this disclosure. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the following claims.

[0121] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.

Claims

1. A method for implementing a ROS2 scheduler, characterized in that, include: Load the user task configuration file, obtain the location information of the task dynamic library, and read the task dynamic library according to the location information to obtain the task to be processed; Based on the task to be processed, several thread instances are created, and the thread instances are transmitted to the scheduler. The scheduler then creates the thread instances into schedulable task entities, including: encapsulating the task to be processed into an anonymous execution function, which can be executed after being selected by the scheduler; creating several thread instances based on the anonymous execution function, and marking the thread instances with a task identifier matching the task to be processed; and transmitting the thread instances to the scheduler through the scheduler interface, whereby the scheduler creates the thread instances into schedulable task entities. The latest scheduling configuration file is read in using dynamic hot reloading to obtain the priority configuration of the thread instance; Assign a priority to the thread instance according to the priority configuration, and add the task entity to the scheduling queue based on the priority to generate a task thread; When the running conditions of the task to be processed are met, the corresponding task thread is extracted from the scheduling queue according to the priority level as the target thread, the target thread is given the corresponding permissions and executed.

2. The implementation method of the ROS2 scheduler according to claim 1, characterized in that, Before reading the latest scheduling configuration file via dynamic hot reloading, the following is also included: Set the corresponding priority according to the order in which the tasks to be processed are processed; Based on the stated priority, all pending tasks are configured for priority scheduling, with each pending task corresponding to a unique priority, thus obtaining a scheduling configuration file.

3. The implementation method of the ROS2 scheduler according to claim 2, characterized in that, The step of reading the latest scheduling configuration file through dynamic hot reloading to obtain the priority configuration of the thread instance includes: Read and parse the pre-set scheduling configuration file to obtain the configuration parameters, which include scheduling policy type, CPU affinity, scheduling group classification, task name and priority, and the task name corresponds one-to-one with the task identifier; The scheduler is initialized according to the configuration parameters, and the task name corresponding to the thread instance is determined based on the task identifier; Based on the task name, the priority configuration corresponding to the thread instance is found in the scheduling configuration file.

4. The implementation method of the ROS2 scheduler according to claim 3, characterized in that, The step of assigning a priority to the thread instance according to the priority configuration, and adding the task entity to the scheduling queue based on the priority to generate a task thread includes: Based on the priority configuration, the priority corresponding to the thread instance is obtained and assigned. Obtain the preset maximum priority, and adjust the priorities of the thread instances that exceed the preset maximum priority; Based on the adjusted priority, the task entity corresponding to the thread instance is added to the scheduler's scheduling queue to generate the corresponding task thread.

5. The implementation method of the ROS2 scheduler according to claim 1, characterized in that, When the running conditions of the task to be processed are met, the corresponding task thread is extracted from the scheduling queue according to the priority level as the target thread, the target thread is granted the corresponding permissions and executed, including: Check whether the running conditions of the task to be processed are met; When the running conditions of the task to be processed are met, the task threads that meet the running conditions in the scheduling queue are traversed according to their priority levels to obtain the task thread with the highest current priority, which is then used as the target thread. Adjust the state of the target thread, grant the target thread the corresponding permissions, and execute the target thread according to the acquired permissions.

6. The method for implementing the ROS2 scheduler according to any one of claims 1-5, characterized in that, Also includes: In the scheduler, a scheduler task priority structure is formed based on the tasks to be processed and their priorities. The scheduler task priority structure includes an array and a priority matrix corresponding to each value in the array. The array uses the group name as the key, and the priority matrix has multiple levels of priority queues. The priority queues include multiple priorities, and each priority corresponds to several task threads to be executed. When the scheduler is looking for the next task thread to be executed, it selects any one of the multiple arrays, traverses the priority matrix corresponding to the array, and finds the task thread with the highest priority in the group according to the priority, which is then used as the highest priority task thread in the group. Iterate through all arrays to find the highest priority task thread in each array. Compare the highest priority task threads in each array to find the highest priority task thread in all arrays, and use it as the target thread.

7. An implementation apparatus for a ROS2 scheduler, characterized in that, include: The pending task acquisition module is used to load the user task configuration file, obtain the location information of the task dynamic library, and read the task dynamic library according to the location information to obtain the pending tasks. A task thread creation module is used to create several thread instances based on the task to be processed, and transmit the thread instances to the scheduler. The scheduler creates the thread instances into schedulable task entities, including: encapsulating the task to be processed into an anonymous execution function, which can be executed after being selected by the scheduler; creating several thread instances based on the anonymous execution function, and marking the thread instances with a task identifier matching the task to be processed; and transmitting the thread instances to the scheduler through the scheduler interface, whereby the scheduler creates the thread instances into schedulable task entities. The priority configuration acquisition module is used to read the latest scheduling configuration file through dynamic hot reloading to obtain the priority configuration of the thread instance; The priority assignment module is used to assign a priority to the thread instance according to the priority configuration, and add the task entity to the scheduling queue based on the priority to generate a task thread; The target thread execution module is used to extract the corresponding task thread as the target thread from the scheduling queue according to the priority level when the running conditions of the task to be processed are met, and to grant the target thread the corresponding permissions and execute it.

8. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the method of implementing the ROS2 scheduler according to any one of claims 1-6.

9. A computer-readable storage medium having computer program instructions stored thereon, characterized in that, When the program instructions are executed by the processor, they implement the steps of the method for implementing the ROS2 scheduler as described in any one of claims 1-6.

Citation Information

Patent Citations

  • Job scheduling method and device, computer system and computer readable storage medium

    CN113032125A

  • Real time os device

    JP2001075820A