Flexiv RDK APIs  0.10
Public Member Functions | List of all members
flexiv::Scheduler Class Reference

Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are configured independently. More...

#include <Scheduler.hpp>

Public Member Functions

 Scheduler ()
 [Blocking] Create a flexiv::Scheduler instance and initialize the real-time scheduler. More...
 
void addTask (std::function< void(void)> &&callback, const std::string &taskName, int interval, int priority, int cpuAffinity=-1)
 [Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigned to a dedicated thread with independent thread configuration. More...
 
void start ()
 [Blocking] Start all added tasks. A dedicated thread will be created for each added task and the periodic execution will begin. More...
 
void stop ()
 [Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed with the resources released. More...
 
int maxPriority () const
 [Non-blocking] Get maximum available priority for user tasks. More...
 
int minPriority () const
 [Non-blocking] Get minimum available priority for user tasks. More...
 
size_t numTasks () const
 [Non-blocking] Get number of tasks added to the scheduler. More...
 

Detailed Description

Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are configured independently.

Examples
intermediate1_realtime_joint_position_control.cpp, intermediate2_realtime_joint_torque_control.cpp, intermediate3_realtime_joint_floating.cpp, intermediate4_realtime_cartesian_pure_motion_control.cpp, intermediate5_realtime_cartesian_motion_force_control.cpp, and intermediate6_robot_dynamics.cpp.

Definition at line 20 of file Scheduler.hpp.

Constructor & Destructor Documentation

◆ Scheduler()

flexiv::Scheduler::Scheduler ( )

[Blocking] Create a flexiv::Scheduler instance and initialize the real-time scheduler.

Exceptions
InitExceptionif the initialization sequence failed.
ClientExceptionif an error is triggered by the client computer.
Warning
This constructor blocks until the initialization sequence is successfully finished.

Member Function Documentation

◆ addTask()

void flexiv::Scheduler::addTask ( std::function< void(void)> &&  callback,
const std::string &  taskName,
int  interval,
int  priority,
int  cpuAffinity = -1 
)

[Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigned to a dedicated thread with independent thread configuration.

Parameters
[in]callbackCallback function of user task.
[in]taskNameA unique name for this task.
[in]intervalExecution interval of this periodic task [ms]. The minimum available interval is 1 ms, equivalent to 1 kHz loop frequency.
[in]priorityPriority for this task thread, can be set to minPriority()–maxPriority() for real-time scheduling, or 0 for non-real-time scheduling. When the priority is set to use real-time scheduling, this thread becomes a real-time thread and can only be interrupted by threads with higher priority. When the priority is set to use non-real-time scheduling (i.e. 0), this thread becomes a non-real-time thread and can be interrupted by any real-time threads. The common practice is to set priority of the most critical tasks to maxPriority() or near, and set priority of other non-critical tasks to 0 or near. To avoid race conditions, the same priority should be assigned to only one task.
[in]cpuAffinityCPU core for this task thread to bind to, can be set to 2–(num_cores - 1). This task thread will only run on the specified CPU core. If left with the default value (-1), then this task thread will not bind to any CPU core, and the system will decide which core to run this task thread on according to the system's own strategy. The common practice is to bind the high-priority task to a dedicated spare core, and bind low-priority tasks to other cores or just leave them unbound (cpuAffinity = -1).
Exceptions
LogicExceptionif the scheduler is already started or is not fully initialized yet.
InputExceptionif the specified interval/priority/affinity is invalid or the specified task name is duplicate.
ClientExceptionif an error is triggered by the client computer.
Note
Setting CPU affinity on macOS has no effect, as its Mach kernel takes full control of thread placement so CPU binding is not supported.
Warning
Calling this method after start() is not allowed.
For maximum scheduling performance, setting CPU affinity to 0 or 1 is not allowed: core 0 is usually the default core for system processes and can be crowded; core 1 is reserved for the scheduler itself.
Examples
intermediate1_realtime_joint_position_control.cpp, intermediate2_realtime_joint_torque_control.cpp, intermediate3_realtime_joint_floating.cpp, intermediate4_realtime_cartesian_pure_motion_control.cpp, intermediate5_realtime_cartesian_motion_force_control.cpp, and intermediate6_robot_dynamics.cpp.

◆ maxPriority()

int flexiv::Scheduler::maxPriority ( ) const

[Non-blocking] Get maximum available priority for user tasks.

Returns
The maximum priority that can be set for a user task with real-time scheduling policy when calling addTask().
Examples
intermediate1_realtime_joint_position_control.cpp, intermediate2_realtime_joint_torque_control.cpp, intermediate3_realtime_joint_floating.cpp, intermediate4_realtime_cartesian_pure_motion_control.cpp, intermediate5_realtime_cartesian_motion_force_control.cpp, and intermediate6_robot_dynamics.cpp.

◆ minPriority()

int flexiv::Scheduler::minPriority ( ) const

[Non-blocking] Get minimum available priority for user tasks.

Returns
The minimum priority that can be set for a user task with real-time scheduling policy when calling addTask().

◆ numTasks()

size_t flexiv::Scheduler::numTasks ( ) const

[Non-blocking] Get number of tasks added to the scheduler.

Returns
Number of added tasks.

◆ start()

void flexiv::Scheduler::start ( )

[Blocking] Start all added tasks. A dedicated thread will be created for each added task and the periodic execution will begin.

Exceptions
LogicExceptionif the scheduler is not initialized yet.
ClientExceptionif failed to start the tasks.
Warning
This function blocks until all added tasks are started.
Examples
intermediate1_realtime_joint_position_control.cpp, intermediate2_realtime_joint_torque_control.cpp, intermediate3_realtime_joint_floating.cpp, intermediate4_realtime_cartesian_pure_motion_control.cpp, intermediate5_realtime_cartesian_motion_force_control.cpp, and intermediate6_robot_dynamics.cpp.

◆ stop()

void flexiv::Scheduler::stop ( )

[Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed with the resources released.

Exceptions
LogicExceptionif the scheduler is not initialized or the tasks are not started yet.
ClientExceptionif failed to stop the tasks.
Note
Calling start() again can restart the added tasks.
Warning
This function blocks until all task threads have exited and resources are released.
This function cannot be called from within a task thread.
Examples
intermediate1_realtime_joint_position_control.cpp, intermediate2_realtime_joint_torque_control.cpp, intermediate3_realtime_joint_floating.cpp, intermediate4_realtime_cartesian_pure_motion_control.cpp, intermediate5_realtime_cartesian_motion_force_control.cpp, and intermediate6_robot_dynamics.cpp.

The documentation for this class was generated from the following file: