APIs, concepts, guides, and more
helpers.h
Note
See ⚙️ Helpers 📜 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.
#ifndef HELPERS
#define HELPERS
// These macros are defined as part of the build process in CMake. To avoid
// build errors, we default them to empty strings if they are not defined.
#ifndef RMP_INSTALL_PATH
#define RMP_INSTALL_PATH ""
#endif
#include "rsi.h" // Import our RapidCode Library.
#include <iostream>
#include <string>
#include <sstream>
#include <source_location>
using namespace RSI::RapidCode; // Import our RapidCode namespace
namespace Helpers
{
const char* NetworkStartErrorName(RSINetworkStartError error)
{
switch (error)
{
return "RSINetworkStartErrorNETWORK_START_OK";
return "RSINetworkStartErrorNIC_PASSED";
return "RSINetworkStartErrorDEVICES_CONNECTED_POWERED";
return "RSINetworkStartErrorDISCOVERED_ZERO_NODES";
return "RSINetworkStartErrorGENERAL_DISCOVERY_FAILURE";
return "RSINetworkStartErrorNETWORK_INIT_FAILURE";
return "RSINetworkStartErrorINCORRECT_ENI_FORMAT";
return "RSINetworkStartErrorSLAVE_COMM_FAILED";
return "RSINetworkStartErrorMEMORY_CONFIG";
return "RSINetworkStartErrorETHERCAT_MASTER_STARTUP_ERROR";
return "RSINetworkStartErrorINTERNAL_RMP_ERROR";
return "RSINetworkStartErrorINTIME_OPEN_ERROR";
return "RSINetworkStartErrorFAILED_TO_READ_PRODUCT_CODE";
return "RSINetworkStartErrorFAILED_TO_READ_VENDOR_ID";
return "RSINetworkStartErrorFAILED_TO_READ_REVISION";
return "RSINetworkStartErrorFAILED_TO_READ_SERIAL_NO";
return "RSINetworkStartErrorFAILED_TO_READ_STATION_ALIAS";
return "RSINetworkStartErrorTHREAD_SCHEDULING_OR_PRIORITY";
return "RSINetworkStartErrorETHERCAT_SAFETY_NOT_LICENSED";
return "RSINetworkStartErrorETHERCAT_SAFETY_MAIN_DEVICE_NOT_DETECTED";
return "RSINetworkStartErrorUNSUPPORTED_NODE_DETECTED";
return "RSINetworkStartErrorENI_SAMPLE_RATE_MISMATCH";
return "RSINetworkStartErrorCPU_AFFINITY";
default:
return "RSINetworkStartErrorUNKNOWN";
}
}
void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location = std::source_location::current())
{
bool hasErrors = false;
std::string errorStrings("");
while (rsiObject->ErrorLogCountGet() > 0)
{
const RsiError *err = rsiObject->ErrorLogGet();
errorStrings += err->what();
errorStrings += "\n";
if (!err->isWarning)
{
hasErrors = true;
}
}
if (hasErrors)
{
std::ostringstream message;
message << "Error! In "
<< location.file_name() << '('
<< location.line() << ':'
<< location.column() << ") `"
<< location.function_name() << "`:\n"
<< errorStrings;
throw std::runtime_error(message.str().c_str());
}
}
void NetworkStart(MotionController *controller)
{
// Initialize the Network
if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started already.
{
std::cout << "Starting Network.." << std::endl;
controller->NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
}
if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started again.
{
RSINetworkStartError startError = controller->LastNetworkStartErrorGet();
std::cout << "Network start error: " << NetworkStartErrorName(startError)
<< " (" << static_cast<int>(startError) << ")" << std::endl;
int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
for (int i = 0; i < messagesToRead; i++)
{
std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
}
throw std::runtime_error("Expected OPERATIONAL state but the network did not get there.");
}
else // Else, of network is operational.
{
std::cout << "Network Started" << std::endl << std::endl;
}
}
{
// Check if the network is already shutdown
if (controller->NetworkStateGet() == RSINetworkState::RSINetworkStateUNINITIALIZED ||
controller->NetworkStateGet() == RSINetworkState::RSINetworkStateSHUTDOWN)
{
return;
}
// Shutdown the network
std::cout << "Shutting down the network.." << std::endl;
controller->NetworkShutdown();
if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN) // Check if the network is shutdown.
{
int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error shutting down the network, read the network log messages
for (int i = 0; i < messagesToRead; i++)
{
std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
}
throw std::runtime_error("Expected SHUTDOWN state but the network did not get there.");
}
else // Else, of network is shutdown.
{
std::cout << "Network Shutdown" << std::endl << std::endl;
}
}
void PrintHeader(std::string sampleAppName)
{
std::cout << "----------------------------------------------------------------------------------------------------\n";
std::cout << "Running " << sampleAppName << " Sample App\n";
std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
}
void PrintFooter(std::string sampleAppName, int exitCode)
{
std::cout << "\n----------------------------------------------------------------------------------------------------\n";
if (exitCode == 0)
{
std::cout << sampleAppName << " Sample App Completed Successfully\n";
}
else
{
std::cout << sampleAppName << " Sample App Failed with Exit Code: " << exitCode << "\n";
}
std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
}
} // namespace Helpers
#endif // HELPERS
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:796
const RsiError *const ErrorLogGet()
Get the next RsiError in the log.
int32_t ErrorLogCountGet()
Get the number of software errors in the error log.
The RapidCode base class. All non-error objects are derived from this class.
Definition rsi.h:180
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:111
bool isWarning
Whether the error is or is not a warning.
Definition rsi.h:120
const char * what() const noexcept
Returns a null terminated character sequence that may be used to identify the exception.
Definition rsi.h:169
@ RSINetworkStateSHUTDOWN
EtherCAT was shutdown or stopped, must restart.
Definition rsienums.h:616
@ RSINetworkStateOPERATIONAL
EtherCAT operational, good state.
Definition rsienums.h:614
@ RSINetworkStateUNINITIALIZED
EtherCAT not yet started.
Definition rsienums.h:610
RSINetworkStartError
Network start errors.
Definition rsienums.h:635
@ RSINetworkStartErrorNIC_PASSED
Invalid or unusable NIC/interface selection. Verify NIC name, interface state, and permissions.
Definition rsienums.h:637
@ RSINetworkStartErrorINCORRECT_ENI_FORMAT
ENI data is invalid or unreadable. Regenerate or replace the ENI file.
Definition rsienums.h:642
@ RSINetworkStartErrorETHERCAT_SAFETY_MAIN_DEVICE_NOT_DETECTED
Safety configuration was detected but no supported Safety MainDevice was found.
Definition rsienums.h:662
@ RSINetworkStartErrorMEMORY_CONFIG
Memory/configuration allocation failed during network startup. Restart and review network logs.
Definition rsienums.h:646
@ RSINetworkStartErrorENI_SAMPLE_RATE_MISMATCH
The ENI cycle time does not match the configured SampleRate. Regenerate the ENI after changing the sa...
Definition rsienums.h:664
@ RSINetworkStartErrorSLAVE_COMM_FAILED
Startup communication with one or more EtherCAT slaves failed. Verify wiring and slave health.
Definition rsienums.h:645
@ RSINetworkStartErrorFAILED_TO_READ_STATION_ALIAS
Failed to read Station Alias from a slave device. Verify slave communication stability.
Definition rsienums.h:656
@ RSINetworkStartErrorFAILED_TO_READ_PRODUCT_CODE
Failed to read Product Code from a slave device. Verify slave communication stability.
Definition rsienums.h:652
@ RSINetworkStartErrorINTIME_OPEN_ERROR
INtime open/init operation failed. Verify INtime runtime status and platform setup.
Definition rsienums.h:649
@ RSINetworkStartErrorDISCOVERED_ZERO_NODES
Discovery completed with zero nodes found. Verify topology and that slaves are powered.
Definition rsienums.h:639
@ RSINetworkStartErrorDEVICES_CONNECTED_POWERED
EtherCAT devices could not be reached. Verify cabling, power, and link status.
Definition rsienums.h:638
@ RSINetworkStartErrorCPU_AFFINITY
Linux CPU affinity is not set to a single core.
Definition rsienums.h:665
@ RSINetworkStartErrorNETWORK_INIT_FAILURE
Network startup failed after initial setup. Review network logs and configuration.
Definition rsienums.h:641
@ RSINetworkStartErrorETHERCAT_MASTER_STARTUP_ERROR
EtherCAT master failed to reach the required startup state. Check logs and network configuration.
Definition rsienums.h:647
@ RSINetworkStartErrorFAILED_TO_READ_VENDOR_ID
Failed to read Vendor ID from a slave device. Verify slave communication stability.
Definition rsienums.h:653
@ RSINetworkStartErrorFAILED_TO_READ_SERIAL_NO
Failed to read Serial Number from a slave device. Verify slave communication stability.
Definition rsienums.h:655
@ RSINetworkStartErrorNETWORK_START_OK
No startup error. Network start completed successfully.
Definition rsienums.h:636
@ RSINetworkStartErrorFAILED_TO_READ_REVISION
Failed to read Revision from a slave device. Verify slave communication stability.
Definition rsienums.h:654
@ RSINetworkStartErrorUNSUPPORTED_NODE_DETECTED
An unsupported node on the network is blocking EtherCAT startup.
Definition rsienums.h:663
@ RSINetworkStartErrorTHREAD_SCHEDULING_OR_PRIORITY
Failed to set required thread scheduling/priority. Verify RT kernel support and privileges.
Definition rsienums.h:659
@ RSINetworkStartErrorETHERCAT_SAFETY_NOT_LICENSED
Safety configuration was detected but the required safety license is not present.
Definition rsienums.h:661
@ RSINetworkStartErrorGENERAL_DISCOVERY_FAILURE
Discovery failed for an unspecified reason. Check network logs and retry discovery/start.
Definition rsienums.h:640
@ RSINetworkStartErrorINTERNAL_RMP_ERROR
Internal controller/runtime error occurred while starting the network. Restart and collect logs.
Definition rsienums.h:648
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:87
void NetworkStart(MotionController *controller)
[CheckErrors]
Definition helpers.h:128
void PrintHeader(std::string sampleAppName)
[NetworkShutdown]
Definition helpers.h:205
void NetworkShutdown(MotionController *controller)
[NetworkStart]
Definition helpers.h:167
void PrintFooter(std::string sampleAppName, int exitCode)
[PrintHeader]
Definition helpers.h:221
Helpers namespace provides utility functions for common tasks in RMP applications.
Definition helpers.h:21