|
APIs, concepts, guides, and more
|
Learn how to use Real-Time Tasks (RTTasks)
PREMIUM FEATURE BETA
HelloRTTasksRTTaskFunctions folder included with the C++ examples
Before writing code, make sure you understand how the pieces fit together:
GlobalData definition. It cannot run on its own. The RTTaskManager loads and executes the functions from this library at runtime as RTTask's.GlobalData through methods in the RealTimeTasks API. All high-level logic remains here, outside the real-time environment. Keeping these boundaries clear prevents common mistakes and confusion. The RTTaskFunctions example project used in this guide defines the RTTaskFunctions library and the GlobalData. It will not be runnable on its own. You will need to start the RTTaskManager separately and submit the tasks to it. You can start the RTTaskManager through the RealTimeTasks API, rsiconfig, or the command line. For more information on using the RealTimeTasks API refer to the Real-Time Tasks concept page.
Before generating or building Real-Time Tasks, make sure your development environment meets these requirements:
build-essential on Debian/Ubuntu).For environment setup and Visual Studio generator scripts, see the C++ Sample Apps guide. For conceptual background and runtime behavior, refer to the Real-Time Tasks concept page.
To get up and running quickly, follow these steps:
RTTaskFunctions (found in the examples folder) into your projectsrc/rttaskglobals.hsrc/rttaskfunctions.cpp, using Increment as a templateFor a more detailed walkthrough, continue below.
The examples folder in your RMP installation includes a CMake project called RTTaskFunctions. This serves as a starting point for building a shared library containing RTTask functions. Copy this folder into your project directory and rename it appropriately.
Here is an overview of the key files:
The template project takes all the source files (.h, .cpp) in the src/ directory and compiles them into a shared library for the target platform (Windows/INtime or Linux). It automatically includes the RMP headers, links the RMP libraries, and applies necessary configuration for use with RTTasks. The output is a .dll/rsl/.so file that will be loaded by the RTTaskManager. There is no standalone executable produced by this project, so you must launch a manager instance separately.
By default, the resulting library is named RTTaskFunctions and is output to the default RMP install directory (e.g., /rsi or C:/RSI/X.X.X). If these paths or names are modified, you must specify them explicitly when submitting a task from your application. Otherwise, they will be discovered automatically.
If you want to change any of the default behavior or are interested in learning more about how it works, then look at the CMakeLists.txt located in the root of the project folder.
In this guide, you’ll build a simple application that moves an axis based on the value of an analog input. It will use one global variable and two RTTasks. The first task, CalculateTarget, reads the analog input and calculates a target position, storing it in the global variable targetPosition. The second task, FollowTarget, moves the axis to the specified target.
Open src/rttaskglobals.h. This file is where the global variables are defined and registered before being exposed to the host through RealTimeTasks API.
To add a new global of type double called targetPosition:
RSI_GLOBAL(double, targetPosition) to the GlobalData structREGISTER_GLOBAL(targetPosition) to the GlobalMetaDataMapThe result should be:
Open src/rttaskfunctions.cpp. This is the file where the task functions are defined. A task function must follow this template:
We will add a new task function called CalculateTarget that will read the analog input, scale it to a value between 0 and 1, and store it in the global created in the previous step.
Then we will add another task function called FollowTarget that will move the axis towards the target position.
Once the task functions have been added, build the project to produce a new shared library. Deploy the library to the target machine alongside the RMP runtime so that the RTTaskManager can discover it.
In your user application, written in C++, C#, or Python with RapidCode, or in any gRPC-capable language using RapidCodeRemote, create an RTTaskManager instance after configuring your RapidCode objects. Submit your tasks to the manager, which will load the shared library built in the previous steps. You can also start the manager from utilities like rsiconfig if you prefer to manage tasks outside of application code.
Next, run the Initialize task one time, to get access to RapidCode objects and initialize your global variables.
Then submit the tasks created in the previous section.
While your tasks are running, you can use RTTask::StatusGet and RTTaskManager::GlobalValueGet to monitor your tasks.
At the end of your program, stop the tasks and manager.