Stand Alone GUI
The Python in Detail
The first important thing we did to be able to use the Python Interface to LinuxCNC was to import the library linuxcnc. For more information on the Python Interface see the LinuxCNC Integrator Manual.
import linuxcnc
Getting Status Updates
To get status updates from linuxcnc we have to poll it at regular intervals. This is done using a gobject.timeout_add(). We also have to import the gobject library like the linuxcnc library. The second thing we do is create a periodic function. The periodic function must return True or the gobject will stop executing the function.
import linuxcnc
import gobject
def __init__(self):
self.emc = linuxcnc # create an instance of the linuxcnc python interface
self.status = self.emc.stat() # create an instance of the .stat() function
# add a timeout to the periodic function so the periodic function is executed
# once every 100 milliseconds
gobject.timeout_add(100, self.periodic) # time between calls in milliseconds
def periodic(self): # fetch status items and update screen
self.status.poll() # poll the linuxcnc status to update any status info
data = self.status.actual_position[0] # get the position of axis 0
text = "% 9.4f"% (data) # convert the data to text
self.builder.get_object("dro_x").set_text(text) # update the GUI data
return True # must return True to keep running
|
Tip
|
If you have an error in the periodic function it will return false and stop running. |
Getting and displaying status updates is a pretty safe operation as compared to issuing MDI or command. Worst case for status is you made a mistake in your periodic function and it just won’t display anything.
All the actions so far have been provided by the GladeVCP Action widgets which provide any checks to make sure an action can be carried out.