APIs, concepts, guides, and more
rttask.h
1#pragma once
2
3#ifndef RTTASK_H
4#define RTTASK_H
5
6#include <atomic> // for std::atomic
7#include <cstdint> // for int32_t, int64_t
8#include <cstddef> // for size_t
9#include <cstdio> // for std::snprintf
10#include <cstring> // for std::memset
11#include <limits> // for std::numeric_limits
12#include <stdexcept> // for std::invalid_argument
13
14#if defined(WIN32)
15#undef max
16#endif // defined(WIN32)
17
18#if !defined(RSI_INTERNAL)
19#include "rsi.h"
20#endif // defined(RSI_INTERNAL)
21
22#define RSI_GLOBAL(type, name) \
23 static_assert(sizeof(std::atomic<type>) == sizeof(type), "Expected size of "#type" to be equal to std::atomic<"#type">."); \
24 alignas(uint64_t) std::atomic<type> name
25
26#define REGISTER_GLOBAL(name) \
27 { #name, offsetof(GlobalData, name), sizeof(GlobalData::name), RSIDataTypeGet<decltype(GlobalData::name.load())>::type }
28
29namespace RSI
30{
31namespace RapidCode
32{
33namespace RealTimeTasks
34{
35 // Forward declaration of User's custom-defined structure of global data.
36 struct GlobalData;
37
38 class RTTaskImplementation;
39 class RTTaskManagerImplementation;
40
45
47 static constexpr int32_t RTTaskManagerCountMaximum = 8;
49 static constexpr int32_t RTTaskCountMaximum = 64;
51 static constexpr char RTTaskManagerExecutableName[] = "rttaskmanager";
53
56
61 enum class RTTaskState : int32_t
62 {
63 Dead = 0,
64 Disabled = 1,
65 Waiting = 2,
66 Running = 3,
67 };
68
73 enum class RTTaskManagerState : int32_t
74 {
75 Dead = 0,
76 Running = 1,
77 Stopped = 2,
78 };
79
84 enum class PlatformType : int32_t
85 {
86 Native = 0,
87 INtime = 1,
88 Linux = 2,
89 Windows = 3,
90 };
91
93
96
97 // Function pointer type for real-time task functions.
98 typedef void(*TaskFunction)(GlobalData* globalData);
99
105 {
107 static constexpr int32_t DirectoryLengthMaximum = 256;
108
110 static constexpr int32_t NameLengthMaximum = 64;
111
113 static constexpr const char* const LibraryNameDefault = "RTTaskFunctions";
114
116 static constexpr int32_t PriorityDefault = 0;
117
119 static constexpr int32_t RepeatForever = -1;
120
122 static constexpr int32_t RepeatNone = 0;
123
125 static constexpr int32_t PeriodDefault = 1;
126
128 static constexpr int32_t PhaseDefault = 0;
129
131 static constexpr bool EnableTimingDefault = false;
132
135
137 char LibraryName[NameLengthMaximum] = "RTTaskFunctions";
138
141
144
147
150
153
158
162
163 // Default constructor.
164 RTTaskCreationParameters() = default;
165
173 RTTaskCreationParameters(const char* const argFunctionName, const char* const argLibraryName = nullptr, const char* const argLibraryDirectory = nullptr)
179 {
180 if (argFunctionName == nullptr)
181 {
182 throw std::invalid_argument("Task function name must not be null.");
183 }
184 std::snprintf(FunctionName, NameLengthMaximum, "%s", argFunctionName);
185
186 if (argLibraryName != nullptr)
187 {
188 std::snprintf(LibraryName, NameLengthMaximum, "%s", argLibraryName);
189 }
190 else
191 {
192 std::snprintf(LibraryName, NameLengthMaximum, "%s", LibraryNameDefault);
193 }
194
195 if (argLibraryDirectory != nullptr)
196 {
197 std::snprintf(LibraryDirectory, DirectoryLengthMaximum, "%s", argLibraryDirectory);
198 }
199 else
200 {
202 }
203 }
204 };
205
213
218 {
221
224
227
229 uint64_t ExecutionTimeMin = std::numeric_limits<uint64_t>::max();
230
233
236
238 RTTaskStatus() = default;
239
241 static constexpr int64_t InvalidExecutionCount = -1;
242
244 static constexpr uint64_t InvalidExecutionTime = 0;
245 };
246
251 {
254
257
259 int64_t CycleCount;
260
262 uint64_t CycleTimeMax;
263
265 uint64_t CycleTimeMin;
266
269
272 };
273
278 {
280 static constexpr int32_t DirectoryLengthMaximum = 256;
281
283 static constexpr int32_t NameLengthMaximum = 64;
284
287
290
293
295 int32_t CpuCore = -1;
296
299 };
300
312
313 typedef int32_t (*GlobalMemberOffsetGetter)(const char* const name);
314 typedef int32_t (*GlobalNamesGetter)(const char* names[], int32_t capacity);
315 typedef int32_t (*GlobalMemberTypeGetter)(const char* const name);
316
317 inline constexpr int32_t GlobalMaxSize = 2048;
318
319#if !defined(RSI_INTERNAL)
327 class RSI_API RTTask : public virtual RapidCodeObject
328 {
329 public:
334
340 virtual void Stop() = 0;
341
349 virtual void TimingReset() = 0;
351
356
361 virtual RTTaskStatus StatusGet() = 0;
362
373 virtual int64_t ExecutionCountAbsoluteWait(int64_t count = ExecutionCountDefault, int32_t timeoutMs = ExecutionCountWaitTimeoutMillisecondsDefault) = 0;
374
385 virtual int64_t ExecutionCountRelativeWait(int64_t count = ExecutionCountDefault, int32_t timeoutMs = ExecutionCountWaitTimeoutMillisecondsDefault) = 0;
387
392
397 virtual int32_t IdGet() = 0;
398
403 virtual RTTaskInfo InfoGet() = 0;
405
410
412 static constexpr int64_t ExecutionCountWaitFailure = -1;
414 static constexpr int64_t ExecutionCountDefault = 1;
416 static constexpr int32_t ExecutionCountWaitTimeoutMillisecondsDefault = 250;
418
419 virtual ~RTTask() = default;
420 protected:
421 RTTask() = default;
422 }; // end class RTTask
423
430 class RSI_API RTTaskManager : public virtual RapidCodeObject
431 {
432 public:
437
443 static RapidVector<RTTaskManager*> Discover();
444
451 static RTTaskManager* Get(const int32_t managerId);
452
461
466
474 virtual RapidVector<const char*> GlobalNamesGet(const char* const libraryName = nullptr, const char* const libraryDirectory = nullptr) = 0;
475
481
488 virtual RSI::RapidCode::FirmwareValue GlobalValueGet(const char* const name, const char* const libraryName = nullptr, const char* const libraryDirectory = nullptr) = 0;
489
497 virtual void GlobalValueSet(const RSI::RapidCode::FirmwareValue& value, const char* const name, const char* const libraryName = nullptr, const char* const libraryDirectory = nullptr) = 0;
498
505 virtual RSI::RapidCode::RSIDataType GlobalTypeGet(const char* const name, const char* const libraryName = nullptr, const char* const libraryDirectory = nullptr) = 0;
506
508
513
520 virtual RTTask* TaskSubmit(const RTTaskCreationParameters& parameters) = 0;
521
528 virtual RTTask* TaskSubmit(RTTaskCreationParameters&& parameters) = 0;
529
538 virtual RTTask* TaskSubmit(const char* const functionName, const char* const libraryName = RTTaskCreationParameters::LibraryNameDefault, const char* const libraryDirectory = nullptr) = 0;
539
545 virtual void Shutdown() = 0;
547
548
553
558 virtual RapidVector<RTTask*> TasksGet() = 0;
559
565
567
572
578
583 virtual int32_t IdGet() = 0;
585
586 virtual ~RTTaskManager() = default;
587 protected:
588 RTTaskManager() = default;
589 }; // end class RTTaskManager
590
591 template <int32_t Capacity>
592 class GlobalMetadataMap {
593 public:
595 struct Metadata
596 {
598 const char* key;
599
601 int32_t offset;
602
604 int32_t size;
605
606 // const std::type_info* type;
608 };
609
610 constexpr GlobalMetadataMap() : data(), tail(0) {}
611
612 constexpr GlobalMetadataMap(std::initializer_list<Metadata> initData)
613 {
614 for (const auto& item : initData)
615 {
616 data[tail++] = item;
617 }
618 }
619
620 constexpr int32_t Size() const { return tail; }
621
622 constexpr Metadata operator[](int32_t index) const { return data[index]; }
623
624 constexpr Metadata operator[](const char* key) const
625 {
626 for (int32_t index = 0; index < tail; ++index)
627 {
628 auto& metadata = data[index];
629 if (std::strcmp(metadata.key, key)==0)
630 {
631 return metadata;
632 }
633 }
634 constexpr char errorMessageFormat[] = "Key '%s' not found in GlobalMetadata.";
635 constexpr int32_t bufferSize = 256;
636 char buffer[bufferSize] = {0};
637 std::snprintf(buffer, bufferSize, errorMessageFormat, key);
638 throw std::out_of_range(buffer);
639 }
640
641 private:
642 Metadata data[Capacity]{};
643 int32_t tail = 0;
644 };
645
646#if !defined(SWIG)
647 // Primary template for parsing the type of a global variable.
648 template<typename Type, typename = void>
649 struct RSIDataTypeGet { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeINVALID; };
650 template<typename Type>
651 struct RSIDataTypeGet<Type, typename std::enable_if<std::is_integral<Type>::value && std::is_signed<Type>::value , void>::type>
653 template<typename Type>
654 struct RSIDataTypeGet<Type, typename std::enable_if<std::is_integral<Type>::value && std::is_unsigned<Type>::value , void>::type>
656 template<typename Type>
657 struct RSIDataTypeGet<Type, typename std::enable_if<std::is_floating_point<Type>::value, void>::type>
659 template<>
660 struct RSIDataTypeGet<int16_t> { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeINT16; };
661 template<>
662 struct RSIDataTypeGet<uint16_t> { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeUINT16; };
663 template<>
664 struct RSIDataTypeGet<int32_t> { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeINT32; };
665 template<>
666 struct RSIDataTypeGet<uint32_t> { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeUINT32; };
667 template<>
668 struct RSIDataTypeGet<float> { static constexpr RSI::RapidCode::RSIDataType type = RSIDataType::RSIDataTypeFLOAT; };
669#endif // !defined(SWIG)
670
671#endif // !defined(RSI_INTERNAL)
672
673
675} // end namespace RealTimeTasks
676} // end namespace RapidCode
677} // end namespace RSI
678
679#endif // !defined(RTTASK_H)
The RapidCode base class. All non-error objects are derived from this class.
Definition rsi.h:184
RTTaskStatus StatusGet()=0
Get the current status of the task.
void Stop()=0
Stop the task from executing.
void TimingReset()=0
Reset the timing statistics for the task.
int32_t IdGet()=0
Get the ID of the task.
RTTaskInfo InfoGet()=0
Get information about the task.
int64_t ExecutionCountAbsoluteWait(int64_t count=ExecutionCountDefault, int32_t timeoutMs=ExecutionCountWaitTimeoutMillisecondsDefault)=0
Wait for the task to reach a specific execution count.
int64_t ExecutionCountRelativeWait(int64_t count=ExecutionCountDefault, int32_t timeoutMs=ExecutionCountWaitTimeoutMillisecondsDefault)=0
Wait for the task to execute a specific number of times relative to current count.
Interface for controlling and monitoring a single real-time task. See RTTaskManager::TaskSubmit and R...
Definition rttask.h:328
RTTaskManagerInfo InfoGet()=0
Get information about the manager.
RSI::RapidCode::FirmwareValue GlobalValueGet(const char *const name, const char *const libraryName=nullptr, const char *const libraryDirectory=nullptr)=0
Read a GlobalTag by its name.
RapidVector< RTTask * > TasksGet()=0
Get all tasks managed by this RTTaskManager.
RTTask * TaskSubmit(const RTTaskCreationParameters &parameters)=0
Submit a new task to the manager using creation parameters.
RSI::RapidCode::FirmwareValue GlobalValueGet(int32_t offset)=0
Read a GlobalTag by its offset. (internal use)
RTTask * TaskSubmit(const char *const functionName, const char *const libraryName=RTTaskCreationParameters::LibraryNameDefault, const char *const libraryDirectory=nullptr)=0
Submit a new task to the manager using function and library names.
void GlobalValueSet(const RSI::RapidCode::FirmwareValue &value, const char *const name, const char *const libraryName=nullptr, const char *const libraryDirectory=nullptr)=0
Set the value of a GlobalTag variable.
RTTaskManagerStatus StatusGet()=0
Get the current status of the manager.
int32_t IdGet()=0
Get the ID of the manager.
static RapidVector< RTTaskManager * > Discover()
Discover all active RTTaskManager instances.
RTTask * TaskSubmit(RTTaskCreationParameters &&parameters)=0
Submit a new task to the manager using move semantics.
RSI::RapidCode::RSIDataType GlobalTypeGet(const char *const name, const char *const libraryName=nullptr, const char *const libraryDirectory=nullptr)=0
Get the type of a GlobalTag variable.
static RTTaskManager * Get(const int32_t managerId)
Get an existing RTTaskManager by ID.
void Shutdown()=0
Shutdown the RTTaskManager firmware.
RapidVector< const char * > GlobalNamesGet(const char *const libraryName=nullptr, const char *const libraryDirectory=nullptr)=0
Get names of all global variables.
static RTTaskManager * Create(const RTTaskManagerCreationParameters &parameters)
Create a new RTTaskManager instance.
Interface for managing real-time tasks firmware. See Real-Time Tasks for more information.
Definition rttask.h:431
RTTaskManagerState
Enum representing the possible states of an RTTaskManager.
Definition rttask.h:74
@ Stopped
Manager firmware is initialized but not currently running.
RTTaskState
Enum representing the possible states of a real-time task.
Definition rttask.h:62
@ Dead
Task is not initialized or has been terminated.
@ Waiting
Task is active but waiting for its next scheduled execution time.
@ Running
Task is currently executing.
@ Disabled
Task is initialized but not currently executing.
PlatformType
Enum representing the platform type for an RTTaskManager.
Definition rttask.h:85
@ Native
Native platform (where the library is running, Windows or Linux).
@ Windows
Standard Windows (useful for debugging tasks before running on INtime).
RSIDataType
Data types for User Limits and other triggers.
Definition rsienums.h:654
@ RSIDataTypeUINT32
uint32 (unsigned 32-bit integer)
@ RSIDataTypeDOUBLE
double (64-bit floating point)
@ RSIDataTypeINT64
int64 (signed 64-bit integer)
@ RSIDataTypeINT16
signed 16-bit integer
@ RSIDataTypeUINT16
unsigned 16-bit integer
@ RSIDataTypeINT32
int32 (signed 32-bit integer)
@ RSIDataTypeUINT64
uint64 (unsigned 64-bit integer)
@ RSIDataTypeFLOAT
float (32-bit floating point, rarely used)
int32_t offset
Offset of the global variable within the GlobalData structure.
Definition rttask.h:601
const char * key
Name of the global variable.
Definition rttask.h:598
int32_t size
Size of the global variable in bytes.
Definition rttask.h:604
RTTaskCreationParameters(const char *const argFunctionName, const char *const argLibraryName=nullptr, const char *const argLibraryDirectory=nullptr)
Constructor with function name and library details.
Definition rttask.h:173
RTTaskCreationParameters specifies all the information required to create and configure a real-time t...
Definition rttask.h:105
static constexpr int32_t RepeatForever
Special value to indicate the task should repeat forever.
Definition rttask.h:119
char FunctionName[NameLengthMaximum]
Name of the task function to execute.
Definition rttask.h:134
char LibraryName[NameLengthMaximum]
Name of the library containing the task function.
Definition rttask.h:137
char UserLabel[NameLengthMaximum]
User-defined label for the task.
Definition rttask.h:143
int32_t Repeats
Number of times the task should execute (RepeatForever for infinite, 0 for none (one-shot)).
Definition rttask.h:149
static constexpr int32_t RepeatNone
Special value to indicate the task should not repeat.
Definition rttask.h:122
static constexpr int32_t PeriodDefault
Default execution period in RMP sample periods.
Definition rttask.h:125
static constexpr int32_t DirectoryLengthMaximum
Maximum length of the library directory path.
Definition rttask.h:107
char LibraryDirectory[DirectoryLengthMaximum]
Path to the directory containing the library with the task function.
Definition rttask.h:140
int32_t Period
Execution period of the task in RMP sample periods.
Definition rttask.h:152
int32_t TaskPriority
Priority of the task (coming soon).
Definition rttask.h:146
static constexpr int32_t PhaseDefault
Default phase offset for task execution.
Definition rttask.h:128
int32_t Phase
Phase offset for task execution. For example, if you have 4 tasks with a period of 4,...
Definition rttask.h:157
static constexpr bool EnableTimingDefault
Default setting for timing measurements.
Definition rttask.h:131
static constexpr int32_t NameLengthMaximum
Maximum length of name fields (library, function, user label).
Definition rttask.h:110
static constexpr int32_t PriorityDefault
Default priority for real-time tasks.
Definition rttask.h:116
static constexpr const char *const LibraryNameDefault
Default library name for the task function.
Definition rttask.h:113
bool EnableTiming
Whether to enable timing measurements for the task. Keep in mind, enabling timing does add a small am...
Definition rttask.h:161
RTTaskInfo provides information about a real-time task, including its creation parameters....
Definition rttask.h:209
RTTaskCreationParameters CreationParameters
Creation parameters used to create the task.
Definition rttask.h:211
RTTaskManagerCreationParameters specifies all the information required to create and configure an RTT...
Definition rttask.h:278
char UserLabel[NameLengthMaximum]
User-defined label for the manager.
Definition rttask.h:298
int32_t CpuCore
[Linux] CPU core to which the manager should be pinned (-1 for no pinning).
Definition rttask.h:295
static constexpr int32_t DirectoryLengthMaximum
Maximum length of the directory path.
Definition rttask.h:280
char NodeName[NameLengthMaximum]
[INtime] Name of the node on which the manager will run.
Definition rttask.h:292
char RTTaskDirectory[DirectoryLengthMaximum]
Path to the directory containing the real-time task libraries.
Definition rttask.h:286
static constexpr int32_t NameLengthMaximum
Maximum length of name fields (node name, user label).
Definition rttask.h:283
Information about RTTaskManager firmware, including its creation parameters and ID....
Definition rttask.h:305
RTTaskManagerCreationParameters CreationParameters
Creation parameters used to create the manager.
Definition rttask.h:307
RTTaskManagerStatus provides status information for RTTaskManager firmware, including its current sta...
Definition rttask.h:251
uint64_t CycleTimeMax
Maximum execution time of a cycle in nanoseconds.
Definition rttask.h:262
uint64_t TaskSubmissionCount
Number of tasks submitted to the manager.
Definition rttask.h:256
RTTaskManagerState State
Current state of the manager.
Definition rttask.h:253
uint64_t CycleTimeLast
Execution time of the last cycle in nanoseconds.
Definition rttask.h:271
double CycleTimeMean
Mean execution time of cycles in nanoseconds.
Definition rttask.h:268
uint64_t CycleTimeMin
Minimum execution time of a cycle in nanoseconds.
Definition rttask.h:265
int64_t CycleCount
Number of cycles executed by the manager.
Definition rttask.h:259
RTTaskStatus provides status information for a real-time task, including its current state,...
Definition rttask.h:218
RTTaskState State
Current state of the task.
Definition rttask.h:220
static constexpr uint64_t InvalidExecutionTime
Invalid value for execution time, indicating timing is not enabled or the task has not executed.
Definition rttask.h:244
uint64_t ExecutionTimeMin
Minimum execution time of the task in nanoseconds.
Definition rttask.h:229
uint64_t ExecutionTimeMax
Maximum execution time of the task in nanoseconds.
Definition rttask.h:226
int64_t ExecutionCount
Number of times the task has executed.
Definition rttask.h:223
double ExecutionTimeMean
Mean execution time of the task in nanoseconds.
Definition rttask.h:232
uint64_t ExecutionTimeLast
Last execution time of the task in nanoseconds.
Definition rttask.h:235
RTTaskStatus()=default
Default constructor.
static constexpr int64_t InvalidExecutionCount
Invalid value for execution count, indicating the task has not executed.
Definition rttask.h:241
Union representing a generic RMP firmware value with multiple data types, stored in 64-bits.
Definition rsi.h:468