APIs, concepts, guides, and more
config.h
1#ifndef CONFIG_H
2#define CONFIG_H
3
4// These macros are defined as part of the build process in CMake. To avoid
5// build errors, we default them to empty strings if they are not defined.
6#ifndef RMP_INSTALL_PATH
7#define RMP_INSTALL_PATH ""
8#endif
9
10#include <sstream>
11#include <source_location>
12
13#include "rsi.h" // Import our RapidCode Library.
14
15#include "helpers.h"
16
17using namespace RSI::RapidCode; // Import our RapidCode namespace
18
24namespace Config
25{
26
32
33 // Motion Controller Creation Parameters
34 inline constexpr char RMP_PATH[] = RMP_INSTALL_PATH;
35 inline constexpr char NIC_PRIMARY[] = "";
36 inline constexpr char NODE_NAME[] = "";
37 inline constexpr int CPU_AFFINITY = 0;
38
39 // If you want to use hardware, then follow the instructions in the README before setting this to true.
40 inline constexpr bool USE_HARDWARE = false;
41
48 {
49 // If you have configured your axes using rsiconfig, RapidSetup, or RapidSetupX then you can comment out this error and return.
50 // IMPLEMENTATION REQUIRED: Configure the axes for hardware
51 std::ostringstream message;
52 message << "You must implement the " << __func__ << " function (found in " << __FILE__ << " line " << __LINE__ << ") to use hardware.";
53 throw std::runtime_error(message.str().c_str());
54
55 // Simple Axis Configuration Example:
56 // (Look at the Axis: Configuration section in the RapidCode API Reference for more advanced configurations)
57
58 // const int USER_UNITS = 1048576; // example for 20-bit encoder, 2^20 = 1048576
59 // Axis *axis = controller->AxisGet(0);
60 // Helpers::CheckErrors(axis);
61 // axis->UserUnitsSet(USER_UNITS);
62 // axis->PositionSet(0);
63 // axis->ErrorLimitTriggerValueSet(0.1 * USER_UNITS);
64 // axis->ErrorLimitActionSet(RSIAction::RSIActionABORT);
65 // axis->Abort();
66 // axis->ClearFaults();
67 }
68
74
80 #if _WIN32
82 {
83 // Create a controller with using defined parameters
85 strncpy(params.RmpPath, RMP_PATH, params.PathLengthMaximum);
86 strncpy(params.NicPrimary, NIC_PRIMARY, params.PathLengthMaximum);
87 strncpy(params.NodeName, NODE_NAME, params.PathLengthMaximum);
88 return params;
89 }
90 #elif __linux__
92 {
94 strncpy(params.RmpPath, RMP_PATH, params.PathLengthMaximum);
95 strncpy(params.NicPrimary, NIC_PRIMARY, params.PathLengthMaximum);
97 return params;
98 }
99 #endif
101
110 void ConfigurePhantomAxis(MotionController *controller, int axisIndex)
111 {
112 // Check that the axis has been initialized correctly
113 Axis* axis = controller->AxisGet(axisIndex);
115
116 // 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)
117 // Therefore, you must set all of their actions to "NONE".
118 axis->PositionSet(0); // Set the position to 0
119 axis->ErrorLimitActionSet(RSIAction::RSIActionNONE); // Set Error Limit Action.
120 axis->AmpFaultActionSet(RSIAction::RSIActionNONE); // Set Amp Fault Action.
121 axis->AmpFaultTriggerStateSet(1); // Set Amp Fault Trigger State.
122 axis->HardwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Negative Limit Action.
123 axis->HardwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Positive Limit Action.
124 axis->SoftwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Software Negative Limit Action.
125 axis->SoftwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Software Positive Limit Action.
126 axis->HomeActionSet(RSIAction::RSIActionNONE); // Set Home Action.
127
128 const double positionToleranceMax =
129 std::numeric_limits<double>::max() /
130 10.0; // Reduce from max slightly, so XML to string serialization and deserialization works without throwing System.OverflowException
131 axis->PositionToleranceCoarseSet(positionToleranceMax); // Set Settling Coarse Position Tolerance to max value
132 axis->PositionToleranceFineSet(positionToleranceMax
133 ); // Set Settling Fine Position Tolerance to max value (so Phantom axis will get immediate MotionDone when target is reached)
134
135 axis->MotorTypeSet(RSIMotorType::RSIMotorTypePHANTOM); // Set the MotorType to phantom
136 }
137
138
139 static int initialAxisCount = -1;
140 static int initialMotionCount = -1;
141
147 void SetupController(MotionController *controller, int numAxes = 0)
148 {
149 // Start or shutdown the network depending on the configuration
150 if (USE_HARDWARE)
151 {
152 Helpers::NetworkStart(controller);
153 }
154 else if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
155 controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN)
156 {
157 std::ostringstream message;
158 message << "The Sample Apps are configured to use Phantom Axes, but the network is not in the UNINITIALIZED or SHUTDOWN state.\n"
159 << "If you intended to run with hardware, then follow the steps in README.md and /src/config.h\n"
160 << "Otherwise, shutdown the network before running the sample apps with phantom axes.";
161
162 throw std::runtime_error(message.str().c_str());
163 }
164
165 // Check if we are using hardware or phantom axes
166 if (USE_HARDWARE)
167 {
168 // Let the user defined function configure the axes
169 ConfigureHardwareAxes(controller);
170
171 // Check that there are enough axes configured
172 int axisCount = controller->AxisCountGet();
173 if (axisCount < numAxes)
174 {
175 std::ostringstream message;
176 message << "Error! Not enough axes configured. Expected " << numAxes << " axes but only found " << axisCount << " axes."
177 << "Please configure the axes in the Config::ConfigureHardwareAxes function.";
178 throw std::runtime_error(message.str().c_str());
179 }
180 }
181 else
182 {
183 // Record the initial object counts to restore later
184 initialAxisCount = controller->AxisCountGet();
185 initialMotionCount = controller->MotionCountGet();
186
187 // Add phantom axes for the sample app if needed
188 if (numAxes > initialAxisCount)
189 {
190 controller->AxisCountSet(numAxes);
191 }
192
193 for (int i = 0; i < numAxes; i++)
194 {
195 ConfigurePhantomAxis(controller, i);
196 }
197 }
198 }
199
200
206 void Cleanup(MotionController* controller)
207 {
208 // Restore the object counts to the original values
209 if (initialAxisCount != -1) { controller->AxisCountSet(initialAxisCount); }
210 if (initialMotionCount != -1) { controller->MotionCountSet(initialMotionCount); }
211
212 // Reset the object counts
213 initialAxisCount = -1;
215 }
216
217};
218
219
220#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:1117
@ RSIMotorTypePHANTOM
Phantom motor (nothing physically connected).
Definition rsienums.h:1315
static int initialAxisCount
[ConfigurePhantomAxis]
Definition config.h:139
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:147
MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
Definition config.h:81
void ConfigurePhantomAxis(MotionController *controller, int axisIndex)
[GetCreationParameters]
Definition config.h:110
constexpr char RMP_PATH[]
Path to the RMP.rta file (usually the RapidSetup folder).
Definition config.h:34
void Cleanup(MotionController *controller)
[SetupController]
Definition config.h:206
void ConfigureHardwareAxes(MotionController *controller)
Configures the hardware axes according to user specified implementation.
Definition config.h:47
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:37
static int initialMotionCount
Initial motion count to restore later (-1 indicates it has not been set).
Definition config.h:140
constexpr char NIC_PRIMARY[]
Name of the NIC to use for the EtherCAT network (not needed for running on phantom axes).
Definition config.h:35
constexpr bool USE_HARDWARE
Flag for whether to use hardware or phantom axes.
Definition config.h:40
constexpr char NODE_NAME[]
(Windows only) INtime node name. Use "" for default node.
Definition config.h:36
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