This section explains principles behind the implementation of HAL components with the Python programming language.
1. Basic usage example
A non-realtime component begins by creating its pins and parameters, then enters a loop which will periodically drive all the outputs from the inputs. The following component copies the value seen on its input pin (passthrough.in) to its output pin (passthrough.out) approximately once per second.
#!/usr/bin/env python3 import hal, time h = hal.component("passthrough") h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN) h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT) h.ready() try: while 1: time.sleep(1) h['out'] = h['in'] except KeyboardInterrupt: raise SystemExit
Kopier overnevnte utlisting inn i en fil ved navn «passthrough», gjør den kjørbar (chmod +x), og plasser den i din $PATH. Test den deretter:
$ halrun
halcmd: loadusr passthrough
halcmd: show pin
Component Pins:
Owner Type Dir Value Name
03 float IN 0 passthrough.in
03 float OUT 0 passthrough.out
halcmd: setp passthrough.in 3.14
halcmd: show pin
Component Pins:
Owner Type Dir Value Name
03 float IN 3.14 passthrough.in
03 float OUT 3.14 passthrough.out
2. Non-realtime components and delays
If you typed "show pin" quickly, you may see that passthrough.out still had its old value of 0. This is because of the call to time.sleep(1), which makes the assignment to the output pin occur at most once per second. Because this is a non-realtime component, the actual delay between assignments can be much longer if the memory used by the passthrough component is swapped to disk, as the assignment could be delayed until that memory is swapped back in.
Thus, non-realtime components are suitable for user-interactive elements such as control panels (delays in the range of milliseconds are not noticed, and longer delays are acceptable), but not for sending step pulses to a stepper driver board (delays must always be in the range of microseconds, no matter what).
3. Create pins and parameters
h = hal.component("passthrough")
The component itself is created by a call to the constructor hal.component. The arguments are the HAL component name and (optionally) the prefix used for pin and parameter names. If the prefix is not specified, the component name is used.
h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN)
Then pins are created by calls to methods on the component object. The arguments are: pin name suffix, pin type, and pin direction. For parameters, the arguments are: parameter name suffix, parameter type, and parameter direction.
Pin and Parameter Types: |
HAL_BIT |
HAL_FLOAT |
HAL_S32 |
HAL_U32 |
HAL_S64 |
HAL_U64 |
Pin Directions: |
HAL_IN |
HAL_OUT |
HAL_IO |
Parameter Directions: |
|||
HAL_RO |
HAL_RW |
The full pin or parameter name is formed by joining the prefix and the suffix with a ".", so in the example the pin created is called passthrough.in.
h.ready()
Once all the pins and parameters have been created, call the .ready() method.
3.1. Changing the prefix
The prefix can be changed by calling the .setprefix() method. The current prefix can be retrieved by calling the .getprefix() method.
4. Reading and writing pins and parameters
For pins and parameters which are also proper Python identifiers, the value may be accessed or set using the attribute syntax:
h.out = h.in
For all pins, whether or not they are also proper Python identifiers, the value may be accessed or set using the subscript syntax:
h['out'] = h['in']
To see all pins with their values, getpins returns all values in a dictionary of that component.
h.getpins()
>>>{'in': 0.0, 'out': 0.0}
4.1. Driving output (HAL_OUT) pins
Periodically, usually in response to a timer, all HAL_OUT pins should be "driven" by assigning them a new value. This should be done whether or not the value is different than the last one assigned. When a pin is connected to a signal, its old output value is not copied into the signal, so the proper value will only appear on the signal once the component assigns a new value.
4.2. Driving bidirectional (HAL_IO) pins
The above rule does not apply to bidirectional pins. Instead, a bidirectional pin should only be driven by the component when the component wishes to change the value. For instance, in the canonical encoder interface, the encoder component only sets the index-enable pin to FALSE (when an index pulse is seen and the old value is TRUE), but never sets it to TRUE. Repeatedly driving the pin FALSE might cause the other connected component to act as though another index pulse had been seen.
5. Exiting
A halcmd unload request for the component is delivered as a KeyboardInterrupt exception. When an unload request arrives, the process should either exit in a short time, or call the .exit() method on the component if substantial work (such as reading or writing files) must be done to complete the shutdown process.
6. Helpful Functions
See Python HAL Interface for an overview of available functions.
7. Constants
Use these to specify details rather then the value they hold.
-
HAL_BIT
-
HAL_FLOAT
-
HAL_S32
-
HAL_U32
-
HAL_S64
-
HAL_U64
-
HAL_IN
-
HAL_OUT
-
HAL_RO
-
HAL_RW
-
MSG_NONE
-
MSG_ALL
-
MSG_DBG
-
MSG_ERR
-
MSG_INFO
-
MSG_WARN
8. System Information
Read these to acquire information about the realtime system.
-
is_kernelspace
-
is_rt
-
is_sim
-
is_userspace