APIs, concepts, guides, and more
random-walk.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 a Real-Time Task that performs a random walk
motion on an axis using Real-Time Tasks.
*/
#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: Random Walk";
// print a start message to indicate that the sample app has started
Helpers::PrintHeader(SAMPLE_APP_NAME);
/* CONSTANTS */
// *NOTICE* The following constants must be configured before attempting to run with hardware.
const int NUM_AXES = 1; // The number of axes to configure, default is 0 for this template
const int TIMEOUT_MS = 5000; // Timeout for waiting for the task to execute once
/* RAPIDCODE INITIALIZATION */
// 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);
Config::SetupController(controller, NUM_AXES);
/* SET CONTROLLER OBJECT COUNTS HERE*/
// normally you would set the number of axes here, but for samples that is handled in the Config::SetupController function
// controller->AxisCountSet(NUM_AXES);
// configure the axis
Axis *axis = controller->AxisGet(0);
axis->PositionSet(0);
axis->AmpEnableSet(true);
// create the task manager
RTTaskManagerCreationParameters parameters = RTTaskHelper::GetTaskManagerCreationParameters();
std::cout << "Creating task manager..." << std::endl;
manager = RTTaskManager::Create(parameters);
Helpers::CheckErrors(&manager.value());
// call the initialization task to initialize the global variables and get pointers to RapidCode objects
// in the RTTaskFunctions library. This needs to be called before any task that uses RapidCode objects.
RTTaskHelper::SubmitInitializationTask(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 RandomWalk function is defined in the RTTaskFunctions library.
// it moves the axis back and forth based on std::rand().
std::cout << "Submitting task..." << std::endl;
RTTaskCreationParameters params("RandomWalk");
params.Period = 5;
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;
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;
}
// the average global tag is the position the axis should be at if the RandomWalk task is working correctly
FirmwareValue average = manager->GlobalValueGet("average");
std::cout << "Average: " << average.Double << std::endl;
// Print the final axis position
std::cout << "Axis position: " << axis->CommandPositionGet() << std::endl;
task.Stop();
axis->MotionDoneWait(TIMEOUT_MS); // The axis might still be moving from the task, so wait for it to finish
axis->AmpEnableSet(false);
}
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;
}
double CommandPositionGet()
Get the current command position.
void PositionSet(double position)
Set the Command and Actual positions.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5863
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
int32_t MotionDoneWait()
Waits for a move to complete.
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
void Stop()
Stop the task from executing.
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
int32_t Period
Execution period of the task in RMP sample periods.
Definition rttask.h:173
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
double Double
Double precision (64-bit) floating-point.
Definition rsi.h:477