[MAT logo][Prev][Up][Next]

Custom modules - Python

Introduction

The Python language was designed to be powerful yet have very clear syntax. It has modern features like modularity, OO, exceptions, dynamic data types, and dynamic typing. For more details, visit its homepage.

User interfaces can be easily built for python scripts using tools such as glade.

Installing into python

The python extension must be installed before it can be used.
  1. change to the lang/python directory
  2. (optional) ./setup.py build
  3. as root: ./setup.py install

If you don't have the MatPLC shared library installed in the usual place you'll also have to set the environment variable LD_LIBRARY_PATH to point to the MatPLC lib/.libs directory before starting python or the python script.

Example

import matplc

matplc.init("example")

foo = matplc.point("foo")
bar = matplc.point("bar")
es_out = matplc.point("es_out")

try:
	while 1:
		matplc.scan_beg()
		matplc.update()

		if foo.get():
			bar.set(1)
		else:
			bar.set(0)

		matplc.update()
		matplc.scan_end()
finally:
	es_out.set(1)
	matplc.update()

matplc.done()

Commentary

The first two and last two lines inside the loop are boiler-plate. Most programs will have them, since most programs will want a main loop. However, for instance GUI-oriented programs will not, and the interface supports that (ignore the scan_beg() and scan_end(), and just do an update() before reading points or after setting them).

The logic would presumably be more complicated than that, of course, but this is just a small example.

Note the use of the try/finally block - if there's any problem in the main body of the program, this will ensure that the module sets the output es_out (presumably connected to the ES circuit) before it exits.

Other functionality

You can use the built-in len(foo) function to get the length of the point foo in bits and foo[0] or foo[0:3] to get subpoints that access particular bit(s) of the point foo.

Instead of foo.set(1) and foo.set(0), you can also use foo.set() and foo.reset() respectively.

Null points can be obtained using matplc.point()

Floating-point points (f32, as used by the DSP module) can be obtained using matplc.point_f(name)

[Prev][Up][Next]

$Date: 2004/12/28 05:32:11 $