APIs, concepts, guides, and more
_helpers.cs
1using RSI.RapidCode; // RSI.RapidCode.dotNET;
2
4public static class Helpers
5{
15 public static void CheckErrors(RapidCodeObject rsiObject)
16 {
17 var hasErrors = false;
18 var errorStrBuilder = new System.Text.StringBuilder();
19
20 while (rsiObject.ErrorLogCountGet() > 0)
21 {
22 // get error
23 RsiError rsiError = rsiObject.ErrorLogGet();
24
25 // get message
26 var errorMsg = rsiError.isWarning ? $"WARNING: {rsiError.Message}" : $"ERROR: {rsiError.Message}";
27 errorStrBuilder.AppendLine(errorMsg);
28
29 // set flag
30 hasErrors = !rsiError.isWarning;
31 }
32
33 if (hasErrors)
34 throw new Exception(errorStrBuilder.ToString());
35 }
36
37
47 public static void NetworkStart(MotionController controller)
48 {
49 // check if network is started already
50 if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL)
51 {
52 Console.WriteLine("Starting Network..");
53
54 // if not started, initialize the network
55 controller.NetworkStart();
56 }
57
58 // check if network is started again
59 if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL)
60 {
61 RSINetworkStartError startError = controller.LastNetworkStartErrorGet();
62 Console.WriteLine($"Network start error: {startError} ({(int)startError})");
63
64 // read network log messages
65 int messagesToRead = controller.NetworkLogMessageCountGet();
66
67 // print all the messages to help figure out the problem
68 for (int i = 0; i < messagesToRead; i++)
69 Console.WriteLine(controller.NetworkLogMessageGet(i));
70
71 throw new SystemException("Expected OPERATIONAL state but the network did not get there.");
72 }
73 else
74 {
75 Console.WriteLine("Network Started");
76 }
77 }
78
79
89 public static void NetworkShutdown(MotionController controller)
90 {
91 // check if the network is already shutdown
92 if (controller.NetworkStateGet() == RSINetworkState.RSINetworkStateUNINITIALIZED ||
93 controller.NetworkStateGet() == RSINetworkState.RSINetworkStateSHUTDOWN)
94 return;
95
96 // shutdown the network
97 Console.WriteLine("Shutting down the network..");
98 controller.NetworkShutdown();
99
100 // check if the network is shutdown
101 var networkState = controller.NetworkStateGet();
102 if (networkState != RSINetworkState.RSINetworkStateUNINITIALIZED &&
103 networkState != RSINetworkState.RSINetworkStateSHUTDOWN)
104 {
105 // some kind of error shutting down the network, read the network log messages
106 int messagesToRead = controller.NetworkLogMessageCountGet();
107
108 // print all the messages to help figure out the problem
109 for (int i = 0; i < messagesToRead; i++)
110 Console.WriteLine(controller.NetworkLogMessageGet(i));
111
112 throw new SystemException("Expected SHUTDOWN state but the network did not get there.");
113 }
114 else
115 {
116 Console.WriteLine("Network Shutdown");
117 }
118 }
119
120
129 public static void ControllerSetup(MotionController controller)
130 {
131 NetworkShutdown(controller);
132
133 // reset the controller to ensure it is in a known state
134 controller.Reset();
135
136 NetworkStart(controller);
137 }
138
139
147 public static void PhantomAxisReset(Axis phantomAxis)
148 {
149 if (phantomAxis.MotorTypeGet() != RSIMotorType.RSIMotorTypePHANTOM)
150 {
151 throw new Exception(@$"Axis {phantomAxis.NumberGet()} is not configured as a phantom axis.
152 Please ensure the axis is set to phantom before calling PhantomAxisConfigure.");
153 }
154
155 // abort any motion
156 AbortMotionObject(phantomAxis);
157
158 // disable all limits (not used for phantom axes)
159 phantomAxis.ErrorLimitActionSet(RSIAction.RSIActionNONE);
160 phantomAxis.HardwareNegLimitActionSet(RSIAction.RSIActionNONE);
161 phantomAxis.HardwarePosLimitActionSet(RSIAction.RSIActionNONE);
162 phantomAxis.HomeActionSet(RSIAction.RSIActionNONE);
163 phantomAxis.SoftwareNegLimitActionSet(RSIAction.RSIActionNONE);
164 phantomAxis.SoftwarePosLimitActionSet(RSIAction.RSIActionNONE);
165
166 // set position tolerances to max value for immediate MotionDone
167 double POSITION_TOLERANCE_MAX = Double.MaxValue / 10.0;
168 phantomAxis.PositionToleranceCoarseSet(POSITION_TOLERANCE_MAX);
169 phantomAxis.PositionToleranceFineSet(POSITION_TOLERANCE_MAX);
170
171 // set sample apps default motion parameters
172 phantomAxis.UserUnitsSet(1);
173 phantomAxis.DefaultVelocitySet(100);
174 phantomAxis.DefaultAccelerationSet(1000);
175 phantomAxis.DefaultDecelerationSet(1000);
176 phantomAxis.DefaultJerkPercentSet(0);
177 phantomAxis.PositionSet(0);
178 }
180
181
189 /// @internal @[AbortMotionObject]
190 public static void AbortMotionObject(RapidCodeMotion motionObject)
191 {
192 motionObject.EStopAbort();
193 motionObject.MotionDoneWait();
194 motionObject.ClearFaults();
195
196 // check for idle state
197 if (motionObject.StateGet() != RSIState.RSIStateIDLE)
198 {
199 RSISource source = motionObject.SourceGet(); // get state source enum
200 string errorMsg = @$"Axis or MultiAxis {motionObject.NumberGet()} failed to enter IDLE state after aborting.
201 The source of the axis error is: {motionObject.SourceNameGet(source)}";
202 throw new Exception(errorMsg);
203 }
204 }
206
207 /*
208 @brief Ensures relationship between USE_HARDWARE constant and network status and throws if mismatch found.
209 @param controller Pointer to the MotionController to check.
210 @exception Exception Thrown if mismatch found between USE_HARDWARE constant and network status, or user cancels on warning.
211 @par Code Snippet
212 @snippet _helpers.cs VerifyHardwareUsage
213 */
215 public static void VerifyHardwareUsage(MotionController controller)
216 {
217 const bool USE_HARDWARE = Constants.USE_HARDWARE; // User must set this in _constants.cs
218
219 bool networkIsOperational = controller.NetworkStateGet() == RSINetworkState.RSINetworkStateOPERATIONAL;
220
221 if (USE_HARDWARE && !networkIsOperational)
222 {
223 throw new Exception("USE_HARDWARE is set to true, but the network is not operational. " +
224 "Please properly configure your hardware or run _setup.cs.");
225 }
226 else if (!USE_HARDWARE && networkIsOperational)
227 {
228 throw new Exception("USE_HARDWARE is set to false, but the network is operational. " +
229 "Please ensure you are not connected to real hardware, or set USE_HARDWARE in _constants.cs to true.");
230 }
231 else if (!USE_HARDWARE && !networkIsOperational)
232 {
233 Console.WriteLine("Running with phantom axes as expected.");
234 }
235 else
236 {
237 // State running with hardware and require 'y' input from user to continue or any other key to abort
238 Console.WriteLine("Caution: You are currently running with hardware. " +
239 "Do not continue unless you have configured your axes through _setup.cs or another tool!");
240 Console.Write("Press 'y' to continue: ");
241 var input = Console.ReadKey();
242 Console.WriteLine(); // Move to next line after key press
243
244 if (input.KeyChar != 'y' && input.KeyChar != 'Y')
245 {
246 throw new Exception("User chose not to continue with hardware operation.");
247 }
248 }
249 }
251
252 /*
253 @brief Verifies that the AXIS_COUNT constant matches the controller's axis count, then validates minimum axis and MultiAxis requirements. Outputs comparison information to console.
254 @param controller The MotionController object to verify.
255 @param minRequiredSampleAxisCount Minimum number of axes required by the sample application (default: 1).
256 @param minRequiredSampleMultiAxisCount Minimum number of MultiAxis objects required by the sample application (default: 0).
257 @exception Exception Thrown if AXIS_COUNT doesn't match controller axis count, if minimum axis count is not met, or if minimum MultiAxis count (calculated as MotionCount minus AxisCount) is not met.
258 @par Code Snippet
259 @snippet _helpers.cs VerifyAxisCount
260 */
262 public static void VerifyAxisCount(MotionController controller, int minRequiredSampleAxisCount = 1, int minRequiredSampleMultiAxisCount = 0)
263 {
264 int controllerAxisCount = controller.AxisCountGet();
265 int AXIS_COUNT = Constants.AXIS_COUNT;
266
267 // Verify AXIS_COUNT matches controller AxisCountGet()
268 if (controllerAxisCount != AXIS_COUNT)
269 {
270 // Axis count mismatch. Throw exception to stop execution
271 throw new Exception($"Controller axis count ({controllerAxisCount}) does not match intended AXIS_COUNT ({AXIS_COUNT}). " +
272 "Please ensure AXIS_COUNT in _constants.cs matches your number of hardware axes, then run _setup.cs or another tool " +
273 "to configure your hardware.");
274 }
275
276 // Verify minimum axis count
277 // Check against AXIS_COUNT here because if we get here we know controllerAxisCount == AXIS_COUNT
278 if (AXIS_COUNT < minRequiredSampleAxisCount)
279 {
280 throw new Exception($"This sample requires at least {minRequiredSampleAxisCount} " +
281 $"{(minRequiredSampleAxisCount == 1 ? "axis" : "axes")}, but AXIS_COUNT is only {AXIS_COUNT}. " +
282 $"Update your AXIS_COUNT in _constants.cs to meet the minimum requirement.");
283 }
284
285 // Verify minimum MultiAxis count
286 if (controller.MotionCountGet() - controllerAxisCount < minRequiredSampleMultiAxisCount)
287 {
288 throw new Exception($"This sample requires at least {minRequiredSampleMultiAxisCount} " +
289 $"MultiAxis object{(minRequiredSampleMultiAxisCount == 1 ? "" : "s")}. " +
290 "Please add more motion supervisors.");
291 }
292 }
293
294}
Constants used in the C# sample apps.
Definition _constants.cs:3
const bool USE_HARDWARE
Default: false.
Definition _constants.cs:10
static void AbortMotionObject(RapidCodeMotion motionObject)
Aborts motion on the given RapidCodeMotion object (Axis or MultiAxis), waits for motion to stop,...
Definition _helpers.cs:189
static void NetworkShutdown(MotionController controller)
Shuts down the network communication for the given MotionController.
Definition _helpers.cs:89
static void CheckErrors(RapidCodeObject rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
Definition _helpers.cs:15
static void PhantomAxisReset(Axis phantomAxis)
Configures a phantom axis on the controller.
Definition _helpers.cs:147
static void NetworkStart(MotionController controller)
Starts the network communication for the given MotionController.
Definition _helpers.cs:47
static void ControllerSetup(MotionController controller)
Sets up the controller for hardware use by resetting it and starting the network.
Definition _helpers.cs:129
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.
RSIMotorType MotorTypeGet()
Get the motor type.
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 UserUnitsSet(double countsPerUserUnit)
Sets the number of counts per User Unit.
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 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:5963
void DefaultVelocitySet(double velocity)
Set the default velocity in UserUnits.
void DefaultAccelerationSet(double acceleration)
Set the default acceleration in UserUnits.
void DefaultDecelerationSet(double deceleration)
Set the default deceleration in UserUnits.
void DefaultJerkPercentSet(double jerkPercent)
Set the default jerk percent.
int32_t AxisCountGet()
Get the number of axes processing.
int32_t MotionCountGet()
Get the number of Motion Supervisors available in the firmware.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:796
void ClearFaults()
Clear all faults for an Axis or MultiAxis.
int32_t MotionDoneWait(int32_t waitTimeoutMilliseconds=WaitForever)
Waits for a move to complete.
RSIState StateGet()
Get the Axis or MultiAxis state.
RSISource SourceGet()
Get the source of an error state for an Axis or MultiAxis.
void EStopAbort()
E-Stop, then abort an axis.
The RapidCodeMotion interface is implemented by Axis and MultiAxis .
Definition rsi.h:4421
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
RSINetworkState
State of network.
Definition rsienums.h:609
RSIAction
Action to perform on an Axis.
Definition rsienums.h:1162
RSIMotorType
Motor Type.
Definition rsienums.h:1358
RSINetworkStartError
Network start errors.
Definition rsienums.h:635
RSISource
Possible sources that have caused an Error state.
Definition rsienums.h:1061
void NetworkStart(MotionController *controller)
[CheckErrors]
Definition helpers.h:128
void NetworkShutdown(MotionController *controller)
[NetworkStart]
Definition helpers.h:167
Helpers namespace provides utility functions for common tasks in RMP applications.
Definition helpers.h:21