APIs, concepts, guides, and more
config.h
#ifndef CONFIG_H
#define CONFIG_H
// 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 <sstream>
#include <source_location>
#include "rsi.h" // Import our RapidCode Library.
#include "helpers.h"
using namespace RSI::RapidCode; // Import our RapidCode namespace
namespace Config
{
// Motion Controller Creation Parameters
inline constexpr char RMP_PATH[] = RMP_INSTALL_PATH;
inline constexpr char NIC_PRIMARY[] = "";
inline constexpr char NODE_NAME[] = "";
inline constexpr int CPU_AFFINITY = 0;
// If you want to use hardware, then follow the instructions in the README before setting this to true.
inline constexpr bool USE_HARDWARE = false;
{
// If you have configured your axes using rsiconfig, RapidSetup, or RapidSetupX then you can comment out this error and return.
// IMPLEMENTATION REQUIRED: Configure the axes for hardware
std::ostringstream message;
message << "You must implement the " << __func__ << " function (found in " << __FILE__ << " line " << __LINE__ << ") to use hardware.";
throw std::runtime_error(message.str().c_str());
// Simple Axis Configuration Example:
// (Look at the Axis: Configuration section in the RapidCode API Reference for more advanced configurations)
// const int USER_UNITS = 1048576; // example for 20-bit encoder, 2^20 = 1048576
// Axis *axis = controller->AxisGet(0);
// Helpers::CheckErrors(axis);
// axis->UserUnitsSet(USER_UNITS);
// axis->PositionSet(0);
// axis->ErrorLimitTriggerValueSet(0.1 * USER_UNITS);
// axis->ErrorLimitActionSet(RSIAction::RSIActionABORT);
// axis->Abort();
// axis->ClearFaults();
}
#if _WIN32
{
// Create a controller with using defined parameters
strncpy(params.RmpPath, RMP_PATH, params.PathLengthMaximum);
strncpy(params.NicPrimary, NIC_PRIMARY, params.PathLengthMaximum);
strncpy(params.NodeName, NODE_NAME, params.PathLengthMaximum);
return params;
}
#elif __linux__
{
strncpy(params.RmpPath, RMP_PATH, params.PathLengthMaximum);
strncpy(params.NicPrimary, NIC_PRIMARY, params.PathLengthMaximum);
return params;
}
#endif
void ConfigurePhantomAxis(MotionController *controller, int axisIndex)
{
// Check that the axis has been initialized correctly
Axis* axis = controller->AxisGet(axisIndex);
// These limits are not meaningful for a Phantom Axis (e.g., a phantom axis has no actual position so a position error trigger is not necessary)
// Therefore, you must set all of their actions to "NONE".
axis->PositionSet(0); // Set the position to 0
axis->ErrorLimitActionSet(RSIAction::RSIActionNONE); // Set Error Limit Action.
axis->AmpFaultActionSet(RSIAction::RSIActionNONE); // Set Amp Fault Action.
axis->AmpFaultTriggerStateSet(1); // Set Amp Fault Trigger State.
axis->HardwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Negative Limit Action.
axis->HardwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Positive Limit Action.
axis->SoftwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Software Negative Limit Action.
axis->SoftwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Software Positive Limit Action.
axis->HomeActionSet(RSIAction::RSIActionNONE); // Set Home Action.
const double positionToleranceMax =
std::numeric_limits<double>::max() /
10.0; // Reduce from max slightly, so XML to string serialization and deserialization works without throwing System.OverflowException
axis->PositionToleranceCoarseSet(positionToleranceMax); // Set Settling Coarse Position Tolerance to max value
axis->PositionToleranceFineSet(positionToleranceMax
); // Set Settling Fine Position Tolerance to max value (so Phantom axis will get immediate MotionDone when target is reached)
axis->MotorTypeSet(RSIMotorType::RSIMotorTypePHANTOM); // Set the MotorType to phantom
}
static int initialAxisCount = -1;
static int initialMotionCount = -1;
void SetupController(MotionController *controller, int numAxes = 0)
{
// Start or shutdown the network depending on the configuration
{
Helpers::NetworkStart(controller);
}
else if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN)
{
std::ostringstream message;
message << "The Sample Apps are configured to use Phantom Axes, but the network is not in the UNINITIALIZED or SHUTDOWN state.\n"
<< "If you intended to run with hardware, then follow the steps in README.md and /src/config.h\n"
<< "Otherwise, shutdown the network before running the sample apps with phantom axes.";
throw std::runtime_error(message.str().c_str());
}
// Check if we are using hardware or phantom axes
{
// Let the user defined function configure the axes
ConfigureHardwareAxes(controller);
// Check that there are enough axes configured
int axisCount = controller->AxisCountGet();
if (axisCount < numAxes)
{
std::ostringstream message;
message << "Error! Not enough axes configured. Expected " << numAxes << " axes but only found " << axisCount << " axes."
<< "Please configure the axes in the Config::ConfigureHardwareAxes function.";
throw std::runtime_error(message.str().c_str());
}
}
else
{
// Record the initial object counts to restore later
initialAxisCount = controller->AxisCountGet();
initialMotionCount = controller->MotionCountGet();
// Add phantom axes for the sample app if needed
if (numAxes > initialAxisCount)
{
controller->AxisCountSet(numAxes);
}
for (int i = 0; i < numAxes; i++)
{
ConfigurePhantomAxis(controller, i);
}
}
}
void Cleanup(MotionController* controller)
{
// Restore the object counts to the original values
if (initialAxisCount != -1) { controller->AxisCountSet(initialAxisCount); }
if (initialMotionCount != -1) { controller->MotionCountSet(initialMotionCount); }
// Reset the object counts
}
};
#endif // CONFIG_H
void HardwareNegLimitActionSet(RSIAction action)
Set the action that will occur when the Hardware Negative Limit Event triggers.
void HardwarePosLimitActionSet(RSIAction action)
Set the action that will occur when the Hardware Positive Limit Event triggers.
void PositionToleranceCoarseSet(double tolerance)
Set the Coarse Position Tolerance for Axis settling.
void SoftwareNegLimitActionSet(RSIAction action)
Set the action that will occur when the Software Negative Limit Event triggers.
void AmpFaultActionSet(RSIAction action)
Set the Amp Fault action.
void AmpFaultTriggerStateSet(bool state)
Set the trigger state of the Amp Fault input.
void HomeActionSet(RSIAction action)
Set the action that will occur when the Home Event triggers.
void PositionToleranceFineSet(double tolerance)
Set the Fine Position Tolerance for Axis settling.
void ErrorLimitActionSet(RSIAction action)
Set the action that will occur when the Error Limit Event triggers.
void MotorTypeSet(RSIMotorType type)
Set the motor type.
void PositionSet(double position)
Set the Command and Actual positions.
void SoftwarePosLimitActionSet(RSIAction action)
Set the action that will occur when the Software Positive Limit Event triggers.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
Definition rsi.h:5863
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
@ RSINetworkStateSHUTDOWN
EtherCAT was shutdown or stopped, must restart.
Definition rsienums.h:575
@ RSINetworkStateUNINITIALIZED
EtherCAT not yet started.
Definition rsienums.h:569
@ RSIActionNONE
None - do not perform any action.
Definition rsienums.h:1116
@ RSIMotorTypePHANTOM
Phantom motor (nothing physically connected).
Definition rsienums.h:1314
static int initialAxisCount
[ConfigurePhantomAxis]
Definition config.h:126
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 ConfigurePhantomAxis(MotionController *controller, int axisIndex)
[GetCreationParameters]
Definition config.h:97
void Cleanup(MotionController *controller)
[SetupController]
Definition config.h:193
void ConfigureHardwareAxes(MotionController *controller)
Configures the hardware axes according to user specified implementation.
Definition config.h:40
This namespace provides static methods and constants for user configuration in RMP applications....
Definition config.h:25
constexpr int CPU_AFFINITY
(Linux only) CPU core to use. This should be an isolated core.
Definition config.h:30
static int initialMotionCount
Initial motion count to restore later (-1 indicates it has not been set).
Definition config.h:127
constexpr char NIC_PRIMARY[]
Name of the NIC to use for the EtherCAT network (not needed for running on phantom axes).
Definition config.h:28
constexpr bool USE_HARDWARE
Flag for whether to use hardware or phantom axes.
Definition config.h:33
constexpr char RMP_PATH[]
Path to the RMP.rta file (usually the RapidSetup folder).
Definition config.h:27
constexpr char NODE_NAME[]
(Windows only) INtime node name. Use "" for default node.
Definition config.h:29
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 NetworkStart(MotionController *controller)
[CheckErrors]
Definition helpers.h:73
char NodeName[PathLengthMaximum]
[Windows/INtime] Indicate the INtime node on which the RMP and RMPNetwork processes run.
Definition rsi.h:994
char RmpPath[PathLengthMaximum]
Location of the RMP firmware executable, license, and associated binaries and resources.
Definition rsi.h:962
int32_t CpuAffinity
[Linux] Indicate the CPU core on which the RMP and RMPNetwork processes run.
Definition rsi.h:1014
char NicPrimary[PathLengthMaximum]
Primary EtherCAT Network Interface (NIC) name.
Definition rsi.h:968
static constexpr uint32_t PathLengthMaximum
MotionController::CreationParameters Maximum string buffer length.
Definition rsi.h:882
CreationParameters for MotionController::Create.
Definition rsi.h:866