APIs, concepts, guides, and more
hello-rttasks.cpp
Note
See Real-Time Tasks 📜 for a detailed explanation of this sample code.
Warning
This is a sample program to assist in the integration of the RMP motion controller with your application. It may not contain all of the logic and safety features that your application requires. We recommend that you wire an external hardware emergency stop (e-stop) button for safety when using our code sample apps. Doing so will help ensure the safety of you and those around you and will prevent potential injury or damage.

The sample apps assume that the system (network, axes, I/O) are configured prior to running the code featured in the sample app. See the Configuration page for more information.
/*
This sample demonstrates how to create and submit a simple Real-Time Task
that increments a global counter variable.
*/
#include "helpers.h" // import our helper functions.
#include "config.h" // import our configuration.
#include "rsi.h" // import our RapidCode Library.
#include "rttasks-helpers.h" // import our helper functions for RTTasks
#include "rttask.h" // import the RTTask library
#include <iostream>
#include <memory>
#include <optional>
#include <thread>
#include <chrono>
using namespace RSI::RapidCode; // Import the RapidCode namespace
using namespace RSI::RapidCode::RealTimeTasks; // Import the RealTimeTasks namespace
int main()
{
const std::string SAMPLE_APP_NAME = "📜 Real-Time Tasks: Hello RTTasks";
// print a start message to indicate that the sample app has started
Helpers::PrintHeader(SAMPLE_APP_NAME);
// create the controller
std::optional<RTTaskManager> manager;
int exitCode = -1; // Set the exit code to an error value.
try
{
// prepare the controller as defined in config.h depending on the configuration
Helpers::CheckErrors(controller);
RTTaskManagerCreationParameters parameters = RTTaskHelper::GetTaskManagerCreationParameters();
std::cout << "Creating task manager..." << std::endl;
manager = RTTaskManager::Create(parameters);
Helpers::CheckErrors(&manager.value());
// tell the task manager the name of the function to run as a task, the name
// of the library that contains the function, and the directory where the
// library is located. The Increment function is defined in the RTTaskFunctions library.
// it increments a global counter variable.
std::cout << "Submitting task..." << std::endl;
RTTaskCreationParameters params("Increment");
RTTask task = manager->TaskSubmit(params);
// wait for the task to run for a bit
// get the counter global tag to see if the task ran correctly
std::cout << "Getting counter global tag..." << std::endl;
RSI::RapidCode::FirmwareValue counter = manager->GlobalValueGet("counter");
if (counter.Int64 <= 0)
{
// the task did not run correctly
exitCode = -1;
std::cout << "Counter is not greater than 0. The task did not run correctly." << std::endl;
}
else
{
// the task ran correctly
exitCode = 0;
std::cout << "Counter: " << counter.Int64 << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
// shutdown the task manager firmware process
if (manager.has_value())
{
manager->Shutdown();
}
// clean up the controller and any other objects as needed
Config::Cleanup(controller);
// delete the controller as the program exits to ensure memory is deallocated in the correct order
controller->Delete();
// print a message to indicate the sample app has finished and if it was successful or not
Helpers::PrintFooter(SAMPLE_APP_NAME, exitCode);
return exitCode;
}
static MotionController * Create(CreationParameters *creationParameters)
Initialize and start the RMP EtherCAT controller.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
int64_t ExecutionCountAbsoluteWait(int64_t count=ExecutionCountDefault, int32_t timeoutMs=ExecutionCountWaitTimeoutMillisecondsDefault)
Wait for the task to reach a specific execution count.
Interface for controlling and monitoring a single real-time task. See RTTaskManager::TaskSubmit and R...
Definition rttask.h:435
static RTTaskManager Create(const RTTaskManagerCreationParameters &parameters)
Create a new RTTaskManager instance.
void SetupController(MotionController *controller, int numAxes=0)
Setup the controller and check if the network is in the correct state for the configuration.
Definition config.h:134
MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
Definition config.h:68
void Cleanup(MotionController *controller)
[SetupController]
Definition config.h:193
void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location=std::source_location::current())
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
Definition helpers.h:32
void PrintHeader(std::string sampleAppName)
[NetworkShutdown]
Definition helpers.h:146
void PrintFooter(std::string sampleAppName, int exitCode)
[PrintHeader]
Definition helpers.h:162
The RealTimeTasks namespace.
Definition rttask.h:36
CreationParameters for MotionController::Create.
Definition rsi.h:866
RTTaskCreationParameters specifies all the information required to create and configure a real-time t...
Definition rttask.h:126
static constexpr int32_t RepeatForever
Special value to indicate the task should repeat forever.
Definition rttask.h:140
int32_t Repeats
Number of times the task should execute (RepeatForever for infinite, 0 for none (one-shot)).
Definition rttask.h:170
RTTaskManagerCreationParameters specifies all the information required to create and configure an RTT...
Definition rttask.h:320
Union representing a generic RMP firmware value with multiple data types, stored in 64-bits.
Definition rsi.h:468
int64_t Int64
64-bit signed integer.
Definition rsi.h:478