summaryrefslogtreecommitdiff
path: root/docs/src/hal/halmodule.txt
blob: 9f8a3166eed30ab82fa147a57e72f425a063e71d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
= Creating Userspace Python Components

== Basic usage

A userspace 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.

[source,c]
----
#!/usr/bin/python
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
----

Copy the above listing into a file named "passthrough", make it
executable ('chmod +x'), and place it on your '$PATH'. Then try it out:

----
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 
----

== Userspace 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 userspace component, the actual
delay between assignments can be much longer if the
memory used by the passthrough component is swapped to disk, the
assignment could be delayed until that memory is swapped back in.

Thus, userspace 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).

== 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.

.HAL Option Names[[cap:HAL-Option-Names]]
[width="100%",cols="<3s,4*<"]
|===========================================================
|Pin and Parameter Types: |HAL_BIT |HAL_FLOAT |HAL_S32 |HAL_U32
|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.

=== Changing the prefix

The prefix can be changed by calling the '.setprefix()' method. The
current prefix can be retrieved by calling the '.getprefix()' method.

== 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']
----

=== 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.

=== 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. 

== 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.

== Project ideas

* Create an external control panel with buttons, switches, and
   indicators. Connect everything to a microcontroller, and connect the
   microcontroller to the PC using a serial interface. Python has a very
   capable serial interface module called
   http://pyserial.sourceforge.net/[pyserial] 
   (Ubuntu package name “python-serial”, in the universe repository)
* Attach a http://lcdproc.omnipotent.net/[LCDProc]-compatible LCD module
   and use it to display a digital readout with information of your choice
   (Ubuntu package name “lcdproc”, in the universe repository)
* Create a virtual control panel using any GUI library supported by
   Python (gtk, qt, wxwindows, etc)