APIs, concepts, guides, and more
HelloRTTasks.cpp
Attention
See the following Concept pages for a detailed explanation of this sample: Real-Time Tasks.
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.
#include "SampleAppsHelper.h" // Import our helper functions.
#include "rsi.h" // Import our RapidCode Library.
#include "RTTasksHelpers.h" // Import our helper functions for RTTasks
#include "rttask.h" // Import the RTTask library
#include <iostream>
#include <memory>
#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
// Create the controller
MotionController *controller = MotionController::Create(&params);
RTTaskManagerCreationParameters parameters = RTTaskHelper::GetTaskManagerCreationParameters();
std::cout << "Creating task manager..." << std::endl;
std::unique_ptr<RTTaskManager> manager(RTTaskManager::Create(parameters));
int exitCode = -1; // Set the exit code to an error value.
try
{
// Prepare the controller as defined in SampleAppsHelper.h depending on the configuration
// 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");
params.Repeats = RTTaskCreationParameters::RepeatForever;
std::unique_ptr<RTTask> task(manager->TaskSubmit(params));
// Wait for the task to run for a bit
task->ExecutionCountAbsoluteWait(50, 500);
// 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 != nullptr)
{
manager->Shutdown();
}
// Clean up the controller and any other objects as needed
// 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
SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
return exitCode;
}
void Delete(void)
Delete the MotionController and all its objects.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
static void PrintFooter(std::string sampleAppName, int exitCode)
Print a message to indicate the sample app has finished and if it was successful or not.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void PrintHeader(std::string sampleAppName)
Print a start message to indicate that the sample app has started.
static void Cleanup(MotionController *controller)
Cleanup the controller and restore the object counts to the original values.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
static void SetupController(MotionController *controller, int numAxes=0)
Setup the controller with user defined axis counts and configuration.
CreationParameters for MotionController::Create.
Definition rsi.h:861
RTTaskCreationParameters specifies all the information required to create and configure a real-time t...
Definition rttask.h:124
int32_t Repeats
Number of times the task should execute (RepeatForever for infinite, 0 for none (one-shot)).
Definition rttask.h:168
RTTaskManagerCreationParameters specifies all the information required to create and configure an RTT...
Definition rttask.h:318
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