APIs, concepts, guides, and more
HelperFunctionsCS.cs
1
4using RSI.RapidCode.dotNET; // Import our RapidCode Library
5using System;
6
7#if DOXYGEN // RSI internal documentation use only
8using RSI.RapidCode;
10#endif
11
16public static class HelperFunctionsCS
17{
26 public static void CheckErrors(RapidCodeObject rsiObject)
27 {
28 bool hasErrors = false;
29 System.Text.StringBuilder errorStringBuilder = new System.Text.StringBuilder();
30 while (rsiObject.ErrorLogCountGet() > 0)
31 {
32 RsiError error = rsiObject.ErrorLogGet();
33
34 if (error.isWarning)
35 {
36 errorStringBuilder.AppendLine("WARNING: " + error.Message);
37 }
38 else
39 {
40 hasErrors = true;
41 errorStringBuilder.AppendLine("ERROR: " + error.Message);
42 }
43 }
44
45 if (hasErrors)
46 {
47 throw new Exception(errorStringBuilder.ToString());
48 }
49 }
52
61 public static void StartTheNetwork(MotionController controller)
62 {
63 // Initialize the Network
64 if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL) // Check if network is started already.
65 {
66 Console.WriteLine("Starting Network..");
67 controller.NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
68 }
69
70 if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL) // Check if network is started again.
71 {
72 int messagesToRead = controller.NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
73
74 for (int i = 0; i < messagesToRead; i++)
75 {
76 Console.WriteLine(controller.NetworkLogMessageGet(i)); // Print all the messages to help figure out the problem
77 }
78 throw new SystemException("Expected OPERATIONAL state but the network did not get there.");
79 }
80 else // Else, of network is operational.
81 {
82 Console.WriteLine("Network Started");
83 }
84 }
87
96 public static void ShutdownTheNetwork(MotionController controller)
97 {
98 // Check if the network is already shutdown
99 if (controller.NetworkStateGet() == RSINetworkState.RSINetworkStateUNINITIALIZED ||
100 controller.NetworkStateGet() == RSINetworkState.RSINetworkStateSHUTDOWN)
101 {
102 return;
103 }
104
105 // Shutdown the network
106 Console.WriteLine("Shutting down the network..");
107 controller.NetworkShutdown();
108 if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateUNINITIALIZED &&
109 controller.NetworkStateGet() != RSINetworkState.RSINetworkStateSHUTDOWN) // Check if the network is shutdown.
110 {
111 int messagesToRead = controller.NetworkLogMessageCountGet(); // Some kind of error shutting down the network, read the network log messages
112
113 for (int i = 0; i < messagesToRead; i++)
114 {
115 Console.WriteLine(controller.NetworkLogMessageGet(i)); // Print all the messages to help figure out the problem
116 }
117 throw new SystemException("Expected SHUTDOWN state but the network did not get there.");
118 }
119 else // Else, of network is shutdown.
120 {
121 Console.WriteLine("Network Shutdown");
122 }
123 }
126
134 public static void SetupControllerForHardware(MotionController controller)
135 {
136 ShutdownTheNetwork(controller);
137 controller.Reset(); // Reset the controller to ensure it is in a known state.
138 StartTheNetwork(controller);
139 }
142
149 public static void ClearControllerCounts(MotionController controller)
150 {
151 // Clear the counts
152 controller.AxisCountSet(0);
153 controller.MotionCountSet(0);
154 controller.UserLimitCountSet(0);
155 }
158
168 public static void SetupControllerForPhantoms(MotionController controller, int axisCount, int[] axisNums)
169 {
170 ShutdownTheNetwork(controller);
171
172 ClearControllerCounts(controller);
173
174 controller.AxisCountSet(axisCount);
175 foreach (int axisNum in axisNums)
176 {
177 ConfigurePhantomAxis(controller, axisNum);
178 }
179 }
182
189 {
190 MotionController.CreationParameters creationParameters = App.GetCreationParameters();
191 return MotionController.Create(creationParameters);
192 }
193
194 public static MotionController CreateController(string rmpPath)
195 {
196 MotionController.CreationParameters creationParameters = App.GetCreationParameters();
197 creationParameters.RmpPath = rmpPath;
198 return MotionController.Create(creationParameters);
199 }
202
212 public static void ConfigurePhantomAxis(MotionController controller, int axisNumber)
213 {
214 // Check if the axis number is valid
215 if (axisNumber < 0)
216 {
217 throw new SystemException("Error: " + axisNumber.ToString() + " is invalid. Axis numbers cannot be negative.");
218 }
219
220 if (axisNumber >= controller.AxisCountGet())
221 {
222 throw new SystemException("Error: " + axisNumber.ToString() + " is invalid. Axis numbers cannot be greater than the number of axes on the controller.");
223 }
224
225 // Configure the specified axis as a phantom axis
226 Axis axis = controller.AxisGet(axisNumber); // Initialize Axis class
227 HelperFunctionsCS.CheckErrors(axis); // [Helper Function] Check that the axis has been initialized correctly
228
229 // 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)
230 // Therefore, you must set all of their actions to "NONE".
231 axis.PositionSet(0); // Set the position to 0
232 axis.ErrorLimitActionSet(RSIAction.RSIActionNONE); // Set Error Limit Action.
233 axis.HardwareNegLimitActionSet(RSIAction.RSIActionNONE); // Set Hardware Negative Limit Action.
234 axis.HardwarePosLimitActionSet(RSIAction.RSIActionNONE); // Set Hardware Positive Limit Action.
235 axis.HomeActionSet(RSIAction.RSIActionNONE); // Set Home Action.
236 axis.SoftwareNegLimitActionSet(RSIAction.RSIActionNONE); // Set Software Negative Limit Action.
237 axis.SoftwarePosLimitActionSet(RSIAction.RSIActionNONE); // Set Software Positive Limit Action.
238
239 const double positionToleranceMax = Double.MaxValue / 10.0; // Reduce from max slightly, so XML to string serialization and deserialization works without throwing System.OverflowException
240 axis.PositionToleranceCoarseSet(positionToleranceMax); // Set Settling Coarse Position Tolerance to max value
241 axis.PositionToleranceFineSet(positionToleranceMax); // Set Settling Fine Position Tolerance to max value (so Phantom axis will get immediate MotionDone when target is reached)
242
243 axis.MotorTypeSet(RSIMotorType.RSIMotorTypePHANTOM); // Set the MotorType to phantom
244 }
247}
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 MotionController CreateController()
Creates the MotionController object, using creation parameters.
static void StartTheNetwork(MotionController controller)
Starts the network communication for the given MotionController.
static void SetupControllerForPhantoms(MotionController controller, int axisCount, int[] axisNums)
Sets up the controller for phantom axes, including configuring specified axes as phantom.
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 ClearControllerCounts(MotionController controller)
Clears count settings for axes, motion, and user limits on the controller.
HelperFunctionsCS 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:5643
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.
static MotionController * Create(CreationParameters *creationParameters)
Initialize and start the RMP EtherCAT 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:796
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:180
Represents the error details thrown as an exception by all RapidCode classes. This class contains an ...
Definition rsi.h:107
bool isWarning
Whether the error is or is not a warning.
Definition rsi.h:116
RSINetworkState
State of network.
Definition rsienums.h:567
RSIAction
Action to perform on an Axis.
Definition rsienums.h:1073
RSIMotorType
Motor Type.
Definition rsienums.h:1269
The Cartesian namespace.
CreationParameters for MotionController::Create.
Definition rsi.h:857