Tutorial 2-6: Calling a function from another module
The WMX3Api class contains functions that are central to the operation of the WMX3 library, but most WMX3 functions are organized into modules. Each module has a class that the functions in the module can be called from.
When instantiating a class object of one of these modules, a WMX3Api object must be passed to the constructor. The WMX3Api that is passed to the constructor must have successfully called the CreateDevice function before the class object of the module can call any functions (attempting to call functions before that will result in an error being returned). The WMX3Api object can call CreateDevice before or after it is passed to the constructor. After the WMX3Api object calls the CloseDevice function, the class object that the WMX3Api object was passed to will no longer be able to execute functions.
Many motion functions are organized into the CoreMotion module. Functions from this module can be called from the CoreMotion class.
The servo of an axis must be turned on before it can execute any motion commands. The SetServoOn function in the CoreMotion module can be called to turn an axis servo on or off. The servo must be turned off before communication can be stopped.
The following adds an instance of the CoreMotion class to the example code, and calls the SetServoOn function.
WMX3Api wmxlib = new WMX3Api();
CoreMotion wmxlib_cm = new CoreMotion(wmxlib);
int err;
Console.WriteLine("Starting application...");
err = wmxlib.CreateDevice("C:\\Program Files\\SoftServo\\WMX3", DeviceType.DeviceTypeNormal);
if (err != ErrorCode.None) {
Console.WriteLine("Failed to create device. Error={0} ({1})",
err, WMX3Api.ErrorToString(err));
goto exit;
}
err = wmxlib.StartCommunication(5000); //Wait for up to 5 seconds for communication to start
if (err != ErrorCode.None) {
Console.WriteLine("Failed to start communication. Error={0} ({1})",
err, WMX3Api.ErrorToString(err));
goto exit;
}
err = wmxlib_cm.AxisControl.SetServoOn(0, 1); //Set axis 0 servo on
if (err != ErrorCode.None) {
Console.WriteLine("Failed to set servo on. Error={0} ({1})",
err, CoreMotion.ErrorToString(err));
goto exit;
}
System.Threading.Thread.Sleep(5000); //Wait five seconds before stopping communication
err = wmxlib_cm.AxisControl.SetServoOn(0, 0); //Set axis 0 servo off
if (err != ErrorCode.None)
{
Console.WriteLine("Failed to set servo off. Error={0} ({1})",
err, CoreMotion.ErrorToString(err));
goto exit;
}
exit:
err = wmxlib.StopCommunication();
if (err != ErrorCode.None) {
Console.WriteLine("Failed to stop communication. Error={0} ({1})",
err, WMX3Api.ErrorToString(err));
}
err = wmxlib.CloseDevice();
if (err != ErrorCode.None) {
Console.WriteLine("Failed to close device. Error={0} ({1})",
err, WMX3Api.ErrorToString(err));
}
wmxlib_cm.Dispose();
wmxlib.Dispose();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();