APIs, concepts, guides, and more
helpers.h
1#ifndef HELPERS
2#define HELPERS
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 "rsi.h" // Import our RapidCode Library.
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <source_location>
16
17using namespace RSI::RapidCode; // Import our RapidCode namespace
18
20namespace Helpers
21{
22 const char* NetworkStartErrorName(RSINetworkStartError error)
23 {
24 switch (error)
25 {
27 return "RSINetworkStartErrorNETWORK_START_OK";
29 return "RSINetworkStartErrorNIC_PASSED";
31 return "RSINetworkStartErrorDEVICES_CONNECTED_POWERED";
33 return "RSINetworkStartErrorDISCOVERED_ZERO_NODES";
35 return "RSINetworkStartErrorGENERAL_DISCOVERY_FAILURE";
37 return "RSINetworkStartErrorNETWORK_INIT_FAILURE";
39 return "RSINetworkStartErrorINCORRECT_ENI_FORMAT";
41 return "RSINetworkStartErrorSLAVE_COMM_FAILED";
43 return "RSINetworkStartErrorMEMORY_CONFIG";
45 return "RSINetworkStartErrorETHERCAT_MASTER_STARTUP_ERROR";
47 return "RSINetworkStartErrorINTERNAL_RMP_ERROR";
49 return "RSINetworkStartErrorINTIME_OPEN_ERROR";
51 return "RSINetworkStartErrorFAILED_TO_READ_PRODUCT_CODE";
53 return "RSINetworkStartErrorFAILED_TO_READ_VENDOR_ID";
55 return "RSINetworkStartErrorFAILED_TO_READ_REVISION";
57 return "RSINetworkStartErrorFAILED_TO_READ_SERIAL_NO";
59 return "RSINetworkStartErrorFAILED_TO_READ_STATION_ALIAS";
61 return "RSINetworkStartErrorTHREAD_SCHEDULING_OR_PRIORITY";
63 return "RSINetworkStartErrorETHERCAT_SAFETY_NOT_LICENSED";
65 return "RSINetworkStartErrorETHERCAT_SAFETY_MAIN_DEVICE_NOT_DETECTED";
67 return "RSINetworkStartErrorUNSUPPORTED_NODE_DETECTED";
69 return "RSINetworkStartErrorENI_SAMPLE_RATE_MISMATCH";
71 return "RSINetworkStartErrorCPU_AFFINITY";
72 default:
73 return "RSINetworkStartErrorUNKNOWN";
74 }
75 }
76
87 void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location = std::source_location::current())
88 {
89 bool hasErrors = false;
90 std::string errorStrings("");
91 while (rsiObject->ErrorLogCountGet() > 0)
92 {
93 const RsiError *err = rsiObject->ErrorLogGet();
94
95 errorStrings += err->what();
96 errorStrings += "\n";
97
98 if (!err->isWarning)
99 {
100 hasErrors = true;
101 }
102 }
103
104 if (hasErrors)
105 {
106 std::ostringstream message;
107 message << "Error! In "
108 << location.file_name() << '('
109 << location.line() << ':'
110 << location.column() << ") `"
111 << location.function_name() << "`:\n"
112 << errorStrings;
113
114 throw std::runtime_error(message.str().c_str());
115 }
116 }
117
118
129 {
130 // Initialize the Network
131 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started already.
132 {
133 std::cout << "Starting Network.." << std::endl;
134 controller->NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
135 }
136
137 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started again.
138 {
139 RSINetworkStartError startError = controller->LastNetworkStartErrorGet();
140 std::cout << "Network start error: " << NetworkStartErrorName(startError)
141 << " (" << static_cast<int>(startError) << ")" << std::endl;
142
143 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
144
145 for (int i = 0; i < messagesToRead; i++)
146 {
147 std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
148 }
149 throw std::runtime_error("Expected OPERATIONAL state but the network did not get there.");
150 }
151 else // Else, of network is operational.
152 {
153 std::cout << "Network Started" << std::endl << std::endl;
154 }
155 }
156
157
168 {
169 // Check if the network is already shutdown
170 if (controller->NetworkStateGet() == RSINetworkState::RSINetworkStateUNINITIALIZED ||
171 controller->NetworkStateGet() == RSINetworkState::RSINetworkStateSHUTDOWN)
172 {
173 return;
174 }
175
176 // Shutdown the network
177 std::cout << "Shutting down the network.." << std::endl;
178 controller->NetworkShutdown();
179
180 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
181 controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN) // Check if the network is shutdown.
182 {
183 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error shutting down the network, read the network log messages
184
185 for (int i = 0; i < messagesToRead; i++)
186 {
187 std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
188 }
189 throw std::runtime_error("Expected SHUTDOWN state but the network did not get there.");
190 }
191 else // Else, of network is shutdown.
192 {
193 std::cout << "Network Shutdown" << std::endl << std::endl;
194 }
195 }
196
197
205 void PrintHeader(std::string sampleAppName)
206 {
207 std::cout << "----------------------------------------------------------------------------------------------------\n";
208 std::cout << "Running " << sampleAppName << " Sample App\n";
209 std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
210 }
211
212
221 void PrintFooter(std::string sampleAppName, int exitCode)
222 {
223 std::cout << "\n----------------------------------------------------------------------------------------------------\n";
224 if (exitCode == 0)
225 {
226 std::cout << sampleAppName << " Sample App Completed Successfully\n";
227 }
228 else
229 {
230 std::cout << sampleAppName << " Sample App Failed with Exit Code: " << exitCode << "\n";
231 }
232 std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
233 }
234
235} // namespace Helpers
236
237#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