The RMP Motion Controller APIs
HelperFunctions.cs
1 
42 using RSI.RapidCode.dotNET; // Import our RapidCode Library
43 using System;
44 using NUnit.Framework;
45 
49 public static class HelperFunctions
50 {
62  public static void CheckErrors(RapidCodeObject rsiObject)
65  {
66  bool hasErrors = false;
67  System.Text.StringBuilder errorStringBuilder = new System.Text.StringBuilder();
68  while (rsiObject.ErrorLogCountGet() > 0)
69  {
70  RsiError error = rsiObject.ErrorLogGet();
71 
72  if (error.isWarning)
73  {
74  errorStringBuilder.AppendLine("WARNING: " + error.Message);
75  }
76  else
77  {
78  hasErrors = true;
79  errorStringBuilder.AppendLine("ERROR: " + error.Message);
80  }
81  }
82 
83  if (errorStringBuilder.Length > 0)
84  {
85  Console.WriteLine(errorStringBuilder.ToString());
86  }
87 
88  if (hasErrors)
89  {
90  throw new Exception(errorStringBuilder.ToString());
91  }
92  }
95 
107  public static void StartTheNetwork(MotionController controller)
110  {
111  // Initialize the Network
112  if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL) // Check if network is started already.
113  {
114  Console.WriteLine("Starting Network..");
115  controller.NetworkStart(); // If not. Initialize The Network. (This can also be done from RapidSetup Tool)
116  }
117 
118  if (controller.NetworkStateGet() != RSINetworkState.RSINetworkStateOPERATIONAL) // Check if network is started again.
119  {
120  int messagesToRead = controller.NetworkLogMessageCountGet(); // Some kind of error starting the network, read the network log messages
121 
122  for (int i = 0; i < messagesToRead; i++)
123  {
124  Console.WriteLine(controller.NetworkLogMessageGet(i)); // Print all the messages to help figure out the problem
125  }
126  Console.WriteLine("Expected OPERATIONAL state but the network did not get there.");
127  //throw new RsiError(); // Uncomment if you want your application to exit when the network isn't operational. (Comment when using phantom axis)
128  }
129  else // Else, of network is operational.
130  {
131  Console.WriteLine("Network Started");
132  }
133  }
136 }
137 
139 public static class Constants
140 {
141  public const int SAMPLES = 2;
142  public const int AXIS_NUMBER = 0;
143  public const int X_AXIS_NUMBER = 0;
144  public const int Y_AXIS_NUMBER = 1;
145  public const int Z_AXIS_NUMBER = 2;
146  public const int A_AXIS_NUMBER = 3;
147  public const int B_AXIS_NUMBER = 4;
148  public const int C_AXIS_NUMBER = 5;
149  public const int MAIN_AXIS_NUMBER = 0;
150  public const int DRIVEN_AXIS_NUMBER = 1;
151  public const int HOLDING_AXIS_INDEX = 0;
152  public const int MOVING_AXIS_INDEX = 1;
153  public const int AXIS_COUNT = 6;
154  public const int FIRST_AXIS_NUMBER = 0;
155  public const int SECOND_AXIS_NUMBER = 1;
156  public const int THIRD_AXIS_NUMBER = 2;
157  public const double USER_UNITS = 1;
158  public const double AKD_USER_UNITS = 1048576; // USER UNITS for an AKD drive 1 rev (the motor used in this sample app has 1048576 encoder pulses per revolution)
159 
161  public const double POSITION = 2; // Specify the position to travel to.
162  public const double VELOCITY = 200; // Specify your velocity. - units: UserUnits/Sec
163  public const double ACCELERATION = 100; // Specify your acceleration. - units: UserUnits/Sec^2
164  public const double DECELERATION = 100; // Specify your deceleration. - units: UserUnits/Sec^2
165  public const double JERK_PERCENT = 50; // Specify your jerk percent (0.0 to 100.0)
167 
168  public const double POSITION_A = 5;
169  public const double POSITION_B = 10;
170  public const int MAX_TEST_TIME = 3000;
171  public const int COMP_NUM_ZERO = 0;
172  public const int COMP_NUM_ONE = 1;
173  public const int COMP_NUM_TWO = 2;
174 }
175 
177 public class TestBase
178 {
179  public MotionController controller;
180  public Axis axis;
181  public Axis x_axis;
182  public Axis y_axis;
183  public Axis z_axis;
184  public Axis a_axis;
185  public Axis b_axis;
186  public Axis c_axis;
187  public Axis prime_axis;
188  public Axis main_axis;
189  public Axis driven_axis;
190  public MultiAxis jointsMultiAxis;
191  public Robot robot;
192 
194  public Axis CreateAndReadyAxis(int AxisNumber)
195  {
196  Axis axis = controller.AxisGet(AxisNumber); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
197  HelperFunctions.CheckErrors(axis); // [Helper Function] Check that the axis has been initialize correctly.
198  ResetAxis(axis);
199  return axis;
200  }
202 
203  public void InitializeAllAxes()
204  {
205  axis = controller.AxisGet(Constants.AXIS_NUMBER); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
207  x_axis = controller.AxisGet(Constants.X_AXIS_NUMBER);
209  y_axis = controller.AxisGet(Constants.Y_AXIS_NUMBER);
211  z_axis = controller.AxisGet(Constants.Z_AXIS_NUMBER);
213  a_axis = controller.AxisGet(Constants.A_AXIS_NUMBER);
215  b_axis = controller.AxisGet(Constants.B_AXIS_NUMBER);
217  c_axis = controller.AxisGet(Constants.C_AXIS_NUMBER);
219  main_axis = controller.AxisGet(Constants.MAIN_AXIS_NUMBER);
220  HelperFunctions.CheckErrors(main_axis);
221  driven_axis = controller.AxisGet(Constants.DRIVEN_AXIS_NUMBER);
222  HelperFunctions.CheckErrors(driven_axis);
223  jointsMultiAxis = controller.MultiAxisGet(Constants.AXIS_COUNT);
224  HelperFunctions.CheckErrors(jointsMultiAxis);
225  }
226 
227  public void DisableAllAxes()
228  {
229  for (int i = 0; i < controller.AxisCountGet(); i++)
230  {
231  Axis axis = controller.AxisGet(i);
233  axis.Abort();
234  axis.AmpEnableSet(false);
235  }
236  }
237 
239  public void ResetAxis(Axis myAxis)
240  {
241  myAxis.ErrorLimitTriggerValueSet(1000);
242  myAxis.UserUnitsSet(Constants.USER_UNITS); // Specify the counts per Unit.
243  myAxis.PositionSet(0); // Sets the current position as 0 effectively 'homing' it.
244  myAxis.Abort(); // If there is any motion happening, abort it (creates a fault).
245  myAxis.DefaultAccelerationSet(Constants.ACCELERATION);
246  myAxis.DefaultDecelerationSet(Constants.ACCELERATION);
247  myAxis.DefaultVelocitySet(Constants.VELOCITY);
248 
249  EnableAmp(myAxis);
250  }
252 
254  public void EnableAmp(Axis myAxis)
255  {
256  myAxis.ClearFaults(); // To enable after an abort faults must be cleared.
257  myAxis.AmpEnableSet(true); // Enable the motor.
258  }
260 
261  public void TearDownFixture()
262  {
263  DisableAllAxes();
264  controller.Shutdown();
265  controller.Delete();
266  }
267 }
268 
270 public class SampleAppTestBase : TestBase
271 {
272  [SetUp]
273  public void SetUp()
274  {
275  for (int i = 0; i < controller.AxisCountGet(); i++)
276  {
277  //@[AxisGet]
278  Axis axis = controller.AxisGet(i);
280  //@[AxisGet]
281  ResetAxis(axis);
282  }
283  }
284 
285  [OneTimeSetUp]
286  public void OneTimeSetUp()
287  {
288  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);// Insert the path location of the RMP.rta (usually the RapidSetup folder)
289  HelperFunctions.CheckErrors(controller); // [Helper Function] Check that the controller has been initialize correctly.
290  controller.AxisCountSet(Constants.AXIS_COUNT);
291  controller.MotionCountSet(Constants.AXIS_COUNT + 1); // Create a MultiAxis
292 
293  InitializeAllAxes();
294  }
295 
296  [TearDown]
297  public void TearDown()
298  {
299  DisableAllAxes();
300  }
301 
302  [OneTimeTearDown]
303  public void OneTimeTearDown()
304  {
305  controller.Shutdown();
306  controller.Delete();
307  }
308 }
309 
311 [TestFixture]
312 [Category("Software")]
313 public class StaticMemoryTestBase : TestBase
314 {
315  [OneTimeSetUp]
316  public void OneTimeSetUp()
317  {
318  //@[ControllerReset]
319  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);
320  controller.Reset(); // reboot the RMP controller firmware
321  //@[ControllerReset]
322  }
323 
324  [SetUp]
325  public void Setup()
326  {
327  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);
328  HelperFunctions.CheckErrors(controller);
329  }
330 
331  [TearDown]
332  public void TearDown()
333  {
334  TearDownFixture(); // Teardown the entire fixture every time as the controller needs to be restarted between different static memory allocations
335  }
336 }
337 
339 [TestFixture]
340 [Category("Software")]
341 public class HelperFunctionsTests : SampleAppTestBase
342  {
343  [Test]
344  public void CheckErrorsTest()
345  {
346  string expectedErrorSubstring = "Motion: MPIStateERROR"; // we expect this to be in the message
347  axis.ThrowExceptions(false); // exceptions will be logged, not thrown
348  axis.Abort(); // put into RSIStateERROR
349  axis.MoveSCurve(1); // command a move while in ERROR state - this will log an exception
350  Assert.Throws(Is.TypeOf<Exception>().And.Message.Contains(expectedErrorSubstring), () => HelperFunctions.CheckErrors(axis));
351  }
352 
353  [Test]
354  public void CheckErrorsWarningTest()
355  {
356  const int BAD_AXIS_NUMBER = 777;
357  Axis badAxis = controller.AxisGet(BAD_AXIS_NUMBER); // try to get an illegal axis
358  Assert.Throws(Is.TypeOf<Exception>().And.Message.Contains(BAD_AXIS_NUMBER.ToString()), () => HelperFunctions.CheckErrors(badAxis));
359  }
360  }
RSI::RapidCode
RSI::RapidCode::RSINetworkState
RSINetworkState
State of network.
Definition: rsienums.h:556
RSI
HelperFunctions.CheckErrors
static void CheckErrors(RapidCodeObject rsiObject)
Check if the RapidCode Object has any errors.
Definition: HelperFunctions.cs:64
HelperFunctions.StartTheNetwork
static void StartTheNetwork(MotionController controller)
Start the controller communication/network.
Definition: HelperFunctions.cs:109
HelperFunctions
Helper Functions for checking logged creation errors, starting the network, etc.
Definition: HelperFunctions.cs:50