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 main_axis;
188  public Axis driven_axis;
189  public MultiAxis jointsMultiAxis;
190  public Robot robot;
191 
193  public Axis CreateAndReadyAxis(int AxisNumber)
194  {
195  Axis axis = controller.AxisGet(AxisNumber); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
196  HelperFunctions.CheckErrors(axis); // [Helper Function] Check that the axis has been initialize correctly.
197  ResetAxis(axis);
198  return axis;
199  }
201 
202  public void InitializeAllAxes()
203  {
204  axis = controller.AxisGet(Constants.AXIS_NUMBER); // Initialize Axis Class. (Use RapidSetup Tool to see what is your axis number)
206  x_axis = controller.AxisGet(Constants.X_AXIS_NUMBER);
208  y_axis = controller.AxisGet(Constants.Y_AXIS_NUMBER);
210  z_axis = controller.AxisGet(Constants.Z_AXIS_NUMBER);
212  a_axis = controller.AxisGet(Constants.A_AXIS_NUMBER);
214  b_axis = controller.AxisGet(Constants.B_AXIS_NUMBER);
216  c_axis = controller.AxisGet(Constants.C_AXIS_NUMBER);
218  main_axis = controller.AxisGet(Constants.MAIN_AXIS_NUMBER);
219  HelperFunctions.CheckErrors(main_axis);
220  driven_axis = controller.AxisGet(Constants.DRIVEN_AXIS_NUMBER);
221  HelperFunctions.CheckErrors(driven_axis);
222  }
223 
224  public void DisableAllAxes()
225  {
226  for (int i = 0; i < controller.AxisCountGet(); i++)
227  {
228  Axis axis = controller.AxisGet(i);
230  axis.Abort();
231  axis.AmpEnableSet(false);
232  }
233  }
234 
236  public void ResetAxis(Axis myAxis)
237  {
238  myAxis.ErrorLimitTriggerValueSet(1000);
239  myAxis.UserUnitsSet(Constants.USER_UNITS); // Specify the counts per Unit.
240  myAxis.PositionSet(0); // Sets the current position as 0 effectively 'homing' it.
241  myAxis.Abort(); // If there is any motion happening, abort it (creates a fault).
242  myAxis.DefaultAccelerationSet(Constants.ACCELERATION);
243  myAxis.DefaultDecelerationSet(Constants.ACCELERATION);
244  myAxis.DefaultVelocitySet(Constants.VELOCITY);
245 
246  EnableAmp(myAxis);
247  }
249 
251  public void EnableAmp(Axis myAxis)
252  {
253  myAxis.ClearFaults(); // To enable after an abort faults must be cleared.
254  myAxis.AmpEnableSet(true); // Enable the motor.
255  }
257 
258  public void TearDownFixture()
259  {
260  DisableAllAxes();
261  controller.Shutdown();
262  controller.Delete();
263  }
264 }
265 
267 public class SampleAppTestBase : TestBase
268 {
269  [SetUp]
270  public void SetUp()
271  {
272  for (int i = 0; i < controller.AxisCountGet(); i++)
273  {
274  //@[AxisGet]
275  Axis axis = controller.AxisGet(i);
277  //@[AxisGet]
278  ResetAxis(axis);
279  }
280  }
281 
282  [OneTimeSetUp]
283  public void OneTimeSetUp()
284  {
285  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);// Insert the path location of the RMP.rta (usually the RapidSetup folder)
286  HelperFunctions.CheckErrors(controller); // [Helper Function] Check that the controller has been initialize correctly.
287  controller.AxisCountSet(Constants.AXIS_COUNT);
288 
289  InitializeAllAxes();
290  }
291 
292  [TearDown]
293  public void TearDown()
294  {
295  DisableAllAxes();
296  }
297 
298  [OneTimeTearDown]
299  public void OneTimeTearDown()
300  {
301  controller.Shutdown();
302  controller.Delete();
303  }
304 }
305 
307 [TestFixture]
308 [Category("Software")]
309 public class StaticMemoryTestBase : TestBase
310 {
311  [OneTimeSetUp]
312  public void OneTimeSetUp()
313  {
314  //@[ControllerReset]
315  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);
316  controller.Reset(); // reboot the RMP controller firmware
317  //@[ControllerReset]
318  }
319 
320  [SetUp]
321  public void Setup()
322  {
323  controller = MotionController.CreateFromSoftware(TestContext.CurrentContext.TestDirectory);
324  HelperFunctions.CheckErrors(controller);
325  }
326 
327  [TearDown]
328  public void TearDown()
329  {
330  TearDownFixture(); // Teardown the entire fixture every time as the controller needs to be restarted between different static memory allocations
331  }
332 }
333 
335 [TestFixture]
336 [Category("Software")]
337 public class HelperFunctionsTests : SampleAppTestBase
338  {
339  [Test]
340  public void CheckErrorsTest()
341  {
342  string expectedErrorSubstring = "Motion: MPIStateERROR"; // we expect this to be in the message
343  axis.ThrowExceptions(false); // exceptions will be logged, not thrown
344  axis.Abort(); // put into RSIStateERROR
345  axis.MoveSCurve(1); // command a move while in ERROR state - this will log an exception
346  Assert.Throws(Is.TypeOf<Exception>().And.Message.Contains(expectedErrorSubstring), () => HelperFunctions.CheckErrors(axis));
347  }
348 
349  [Test]
350  public void CheckErrorsWarningTest()
351  {
352  const int BAD_AXIS_NUMBER = 777;
353  Axis badAxis = controller.AxisGet(BAD_AXIS_NUMBER); // try to get an illegal axis
354  Assert.Throws(Is.TypeOf<Exception>().And.Message.Contains(BAD_AXIS_NUMBER.ToString()), () => HelperFunctions.CheckErrors(badAxis));
355  }
356  }
RSI::RapidCode
RSI::RapidCode::RSINetworkState
RSINetworkState
State of network.
Definition: rsienums.h:550
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