The RMP Motion Controller APIs
HelperFunctionsCpp.h
1
16#ifndef CPP_HELPER_FUNCTIONS
17#define CPP_HELPER_FUNCTIONS
18
19#include "rsi.h" // Import our RapidCode Library.
20#include <iostream>
21#include <string>
22#include <cstdlib>
23#include <algorithm>
24
25
26using namespace std;
27using namespace RSI::RapidCode;
28
34{
35public:
36
42 static void CheckErrors(RapidCodeObject* rsiObject)
43 {
44 bool hasErrors = false;
45 std::string errorStrings("");
46 while (rsiObject->ErrorLogCountGet() > 0)
47 {
48 const RsiError* err = rsiObject->ErrorLogGet();
49
50 errorStrings += err->what();
51
52 if (!err->isWarning)
53 {
54 hasErrors = true;
55 }
56 }
57
58 if (errorStrings.size() > 0)
59 {
60 printf("%s", errorStrings.c_str());
61 }
62
63 if (hasErrors)
64 {
65 throw std::runtime_error(errorStrings.c_str());
66 }
67 }
68
74 static void StartTheNetwork(MotionController* controller)
75 {
76 // Initialize the Network
77 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started already.
78 {
79 cout << "Starting Network.." << endl;
80 controller->NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
81 }
82
83 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started again.
84 {
85 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
86
87 for (int i = 0; i < messagesToRead; i++)
88 {
89 cout << controller->NetworkLogMessageGet(i) << endl; // Print all the messages to help figure out the problem
90 }
91 throw std::runtime_error("Expected OPERATIONAL state but the network did not get there.");
92 }
93 else // Else, of network is operational.
94 {
95 cout << "Network Started" << endl << endl;
96 }
97 }
98
104 static void ShutdownTheNetwork(MotionController* controller)
105 {
106 // Check if the network is already shutdown
107 if (controller->NetworkStateGet() == RSINetworkState::RSINetworkStateUNINITIALIZED ||
108 controller->NetworkStateGet() == RSINetworkState::RSINetworkStateSHUTDOWN)
109 {
110 return;
111 }
112
113 // Shutdown the network
114 cout << "Shutting down the network.." << endl;
115 controller->NetworkShutdown();
116
117 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
118 controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN) // Check if the netwrok is shutdown.
119 {
120 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error shutting down the network, read the network log messages
121
122 for (int i = 0; i < messagesToRead; i++)
123 {
124 cout << controller->NetworkLogMessageGet(i) << endl; // Print all the messages to help figure out the problem
125 }
126 throw std::runtime_error("Expected SHUTDOWN state but the network did not get there.");
127 }
128 else // Else, of network is shutdown.
129 {
130 cout << "Network Shutdown" << endl << endl;
131 }
132 }
133
139 {
140 ShutdownTheNetwork(controller);
141 controller->Reset(); // Reset the controller to ensure it is in a known state.
142 StartTheNetwork(controller);
143 }
144
150 {
151 // Clear the counts
152 controller->AxisCountSet(0);
153 controller->MotionCountSet(0);
154 controller->UserLimitCountSet(0);
155 }
156
163 static void SetupControllerForPhantoms(MotionController* controller, int axisCount, std::vector<int> axisNums)
164 {
165 ShutdownTheNetwork(controller);
166
167 ClearControllerCounts(controller);
168
169 controller->AxisCountSet(axisCount);
170 for (auto axisNum : axisNums)
171 {
172 ConfigurePhantomAxis(controller, axisNum);
173 }
174 }
175
182 static void ConfigurePhantomAxis(MotionController* controller, int axisNumber)
183 {
184 // Check if the axis number is valid
185 if (axisNumber < 0)
186 {
187 throw std::invalid_argument("Error: " + std::to_string(axisNumber) + " is invalid. Axis numbers cannot be negative.");
188 }
189
190 if (axisNumber >= controller->AxisCountGet())
191 {
192 throw std::invalid_argument("Error: " + std::to_string(axisNumber) + " is invalid. Axis numbers cannot be greater than the number of axes on the controller.");
193 }
194
195 // Configure the specified axis as a phantom axis
196 Axis* axis = controller->AxisGet(axisNumber); // Initialize Axis class
197 HelperFunctionsCpp::CheckErrors(axis); // [Helper Function] Check that the axis has been initialized correctly
198
199 // 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)
200 // Therefore, you must set all of their actions to "NONE".
201 axis->PositionSet(0); // Set the position to 0
202 axis->ErrorLimitActionSet(RSIAction::RSIActionNONE); // Set Error Limit Action.
203 axis->HardwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Negative Limit Action.
204 axis->HardwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Hardware Positive Limit Action.
205 axis->HomeActionSet(RSIAction::RSIActionNONE); // Set Home Action.
206 axis->SoftwareNegLimitActionSet(RSIAction::RSIActionNONE); // Set Software Negative Limit Action.
207 axis->SoftwarePosLimitActionSet(RSIAction::RSIActionNONE); // Set Software Positive Limit Action.
208
209 constexpr 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
210 axis->PositionToleranceCoarseSet(positionToleranceMax); // Set Settling Coarse Position Tolerance to max value
211 axis->PositionToleranceFineSet(positionToleranceMax); // Set Settling Fine Position Tolerance to max value (so Phantom axis will get immediate MotionDone when target is reached)
212
213 axis->MotorTypeSet(RSIMotorType::RSIMotorTypePHANTOM); // Set the MotorType to phantom
214 }
215};
216#endif
static void SetupControllerForHardware(MotionController *controller)
Sets up the controller for hardware use by resetting it and starting the network.
static void ConfigurePhantomAxis(MotionController *controller, int axisNumber)
Configures a specified axis as a phantom axis with custom settings.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void ShutdownTheNetwork(MotionController *controller)
Shuts down the network communication for the given MotionController.
static void SetupControllerForPhantoms(MotionController *controller, int axisCount, std::vector< int > axisNums)
Sets up the controller for phantom axes, including configuring specified axes as phantom.
static void ClearControllerCounts(MotionController *controller)
Clears count settings for axes, motion, and user limits on the controller.
static void StartTheNetwork(MotionController *controller)
Starts the network communication for the given MotionController.
HelperFunctionsCpp class provides static methods for common tasks in RMP applications.
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 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:5664
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
RSINetworkState NetworkStateGet()
void Reset()
Reset the controller which stops and restarts the RMP firmware.
void MotionCountSet(int32_t motionCount)
Set the number of processed Motion Supervisors in the controller.
const char *const NetworkLogMessageGet(int32_t messageIndex)
int32_t AxisCountGet()
Get the number of axes processing.
void UserLimitCountSet(int32_t userLimitCount)
Set the number of processed UserLimits in the MotionController.
void AxisCountSet(int32_t axisCount)
Set the number of allocated and processed axes in the controller.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:762
void NetworkShutdown()
Shutdown the network. For EtherCAT, this will kill the RMPNetwork process.
void NetworkStart()
Start the network with RSINetworkStartupMethodNORMAL.
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:178
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:105
bool isWarning
Whether the error is or is not a warning.
Definition rsi.h:114
const char * what() const noexcept
Returns a null terminated character sequence that may be used to identify the exception.
Definition rsi.h:167