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{
32 void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location = std::source_location::current())
33 {
34 bool hasErrors = false;
35 std::string errorStrings("");
36 while (rsiObject->ErrorLogCountGet() > 0)
37 {
38 const RsiError *err = rsiObject->ErrorLogGet();
39
40 errorStrings += err->what();
41 errorStrings += "\n";
42
43 if (!err->isWarning)
44 {
45 hasErrors = true;
46 }
47 }
48
49 if (hasErrors)
50 {
51 std::ostringstream message;
52 message << "Error! In "
53 << location.file_name() << '('
54 << location.line() << ':'
55 << location.column() << ") `"
56 << location.function_name() << "`:\n"
57 << errorStrings;
58
59 throw std::runtime_error(message.str().c_str());
60 }
61 }
62
63
74 {
75 // Initialize the Network
76 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started already.
77 {
78 std::cout << "Starting Network.." << std::endl;
79 controller->NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
80 }
81
82 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateOPERATIONAL) // Check if network is started again.
83 {
84 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
85
86 for (int i = 0; i < messagesToRead; i++)
87 {
88 std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
89 }
90 throw std::runtime_error("Expected OPERATIONAL state but the network did not get there.");
91 }
92 else // Else, of network is operational.
93 {
94 std::cout << "Network Started" << std::endl << std::endl;
95 }
96 }
97
98
109 {
110 // Check if the network is already shutdown
111 if (controller->NetworkStateGet() == RSINetworkState::RSINetworkStateUNINITIALIZED ||
112 controller->NetworkStateGet() == RSINetworkState::RSINetworkStateSHUTDOWN)
113 {
114 return;
115 }
116
117 // Shutdown the network
118 std::cout << "Shutting down the network.." << std::endl;
119 controller->NetworkShutdown();
120
121 if (controller->NetworkStateGet() != RSINetworkState::RSINetworkStateUNINITIALIZED &&
122 controller->NetworkStateGet() != RSINetworkState::RSINetworkStateSHUTDOWN) // Check if the network is shutdown.
123 {
124 int messagesToRead = controller->NetworkLogMessageCountGet(); // Some kind of error shutting down the network, read the network log messages
125
126 for (int i = 0; i < messagesToRead; i++)
127 {
128 std::cout << controller->NetworkLogMessageGet(i) << std::endl; // Print all the messages to help figure out the problem
129 }
130 throw std::runtime_error("Expected SHUTDOWN state but the network did not get there.");
131 }
132 else // Else, of network is shutdown.
133 {
134 std::cout << "Network Shutdown" << std::endl << std::endl;
135 }
136 }
137
138
146 void PrintHeader(std::string sampleAppName)
147 {
148 std::cout << "----------------------------------------------------------------------------------------------------\n";
149 std::cout << "Running " << sampleAppName << " Sample App\n";
150 std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
151 }
152
153
162 void PrintFooter(std::string sampleAppName, int exitCode)
163 {
164 std::cout << "\n----------------------------------------------------------------------------------------------------\n";
165 if (exitCode == 0)
166 {
167 std::cout << sampleAppName << " Sample App Completed Successfully\n";
168 }
169 else
170 {
171 std::cout << sampleAppName << " Sample App Failed with Exit Code: " << exitCode << "\n";
172 }
173 std::cout << "----------------------------------------------------------------------------------------------------\n" << std::endl;
174 }
175
176} // namespace Helpers
177
178#endif // HELPERS
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
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:184
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:173
@ RSINetworkStateSHUTDOWN
EtherCAT was shutdown or stopped, must restart.
Definition rsienums.h:575
@ RSINetworkStateOPERATIONAL
EtherCAT operational, good state.
Definition rsienums.h:573
@ RSINetworkStateUNINITIALIZED
EtherCAT not yet started.
Definition rsienums.h:569
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
void PrintHeader(std::string sampleAppName)
[NetworkShutdown]
Definition helpers.h:146
void NetworkShutdown(MotionController *controller)
[NetworkStart]
Definition helpers.h:108
void PrintFooter(std::string sampleAppName, int exitCode)
[PrintHeader]
Definition helpers.h:162
Helpers namespace provides utility functions for common tasks in RMP applications.
Definition helpers.h:21