Introduction
This article provides a more advanced introduction to the Galil COM library. Specifically event based execution will be introduced using the three COM library events, onRecord, onMessage, and onInterrupt. The example will guide users, with minimal experience, to build the following application.
Application Description
The three columns of the interface correspond to the three types of events in the COM library.
- Data records are sent out from the controller at a time pitch. When a data record is received by the host, the time value is taken from data record packet and printed with “DR->” in column 1.
- Embedded DMC code sends unsolicited messages to the controller at a particular time pitch. When an unsolicited message is received by the host, it is printed with “MG->” in column 2.
- Embedded DMC code generates each of 16 user interrupts sequentially at a particular time pitch. When an interrupt is received by the host, its value is checked. Special cases 1, 2 and 3 have been defined for user interrupts 240, 241, and 242, accordingly. All other interrupts (243-255) will be handled by the default case which prints “IR-> Undefined case ” and the interrupt number.
Motivation
Event driven programming takes advantage of .NET resource management. When practiced, event driven programming allows the host machine to take processing time away from a particular part of application code to service events as they happen.
The onRecord Event
An example of an application that would take advantage of the onRecord event is one that constantly prints out the encoder position to the GUI (graphical user interface). Rather than executing a loop that polls the encoder position constantly by issuing the TPA command, the program would access the data record on the fly. Each time a new data record packet comes in, the TPA value from the data record is written to a text label in the GUI.
This is a much better solution because it allows for the application resources to perform other tasks such as updating the GUI and making calculations. This is critical because it allows the primary application code to run on the host all while the encoder position is updated in real time. Application code can be written to utilize any data in the data record on each new packet when the onRecord event is used.
The onMessage Event
There are two types of messages in the Galil COM library. Solicited and unsolicited messages. A solicited message involves the application asking the controller for data and the controller responding with the requested data. An unsolicited message is data that is sent by the controller without the application specifically requesting it. The difference is the context that a request occurs in.
This command will generate a solicited Message. The application asks what is the encoder position of the x-axis and the controller responds. The controller will immediately respond, just as the application code expects it to.
g.command("TPX");
When the following DMC program is downloaded to the controller and run, the program will report the value of the TIME register after each 500ms delay. Since the host has not queried the data directly, the message is not expected. This message type is known as an unsolicited message.
g.programDownload("#A\rMGTIME\rWT500\rJP#A\rEN");
g.command(“XQ #A”)
Since the unsolicited message is unexpected by the host, the onMessage event must be used to allow the application program to receive the unsolicited data. Once an onMessage routine has been prepared, each time an unsolicited message is received the routine is run to handle it. This is is useful for transferring specialized data to the host application.
The onInterrupt Event
The final event is the onInterrupt event. This event is very useful. From the get-go, the controller is set up to provide interrupts on many useful events such as profiled motion complete, limit switch activated, application program stopped, digital input triggered and more. For a complete list, please see the documentation for the EI command in your controller command reference. The onInterrupt event code should utilize a switch statement or other form of branching to handle each interrupt separately and appropriately.
Another benefit to the onInterrupt event is that it captures user interrupts. In embedded code, a user can cause up to 16 unique interrupts to occur by taking advantage of the UI command. As in the example program, the user defined interrupt can be differentiated and handled by a switch statement (as discussed for the EI interrupt routines). For more information on how to use the UI command consult your controller command reference.
Conclusions
Resource management is relatively simple to implement through the use of event driven programming. Sharing resources allows for application critical tasks to run at the same time as general application code. The onRecord event allows access to the entire data record as soon as it is received. The onMessage event is useful for transferring specific data back to the application code for utilization by the host. The onInterrupt event provides easy access to flags that give insight into changes on the controller (such as a completion of motion on an axis). Taking advantage of the COM library's events both expands the possibilities of what can be done with application code and simplifies programming.
Download Example
The event driven programming guide is now part of the 2012 VB and 2012 C# COM library Hello Galil example files. These files are available for download at the API Examples page.