© Bueno Systems, Inc. • TSL1401-DB (2009.10.01)
Page 42 of 52
Here’s one way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
lft_edge VAR Byte
rgt_edge VAR Byte
area VAR Word
OWOUT owio, 1, [SETEXP, 30]
OWOUT owio, 0, [SETBIN, 128, 10, FIXED|LEVEL]
DO
OWOUT owio, 0, ["<", ACQBIN|XTRIG, FNDNXT|FWD|BRTEDG]
OWOUT owio, 0, [FNDNXT|BKWD|BRTEDG, ">"]
IF (rgt_edge) THEN
area = area + rgt_edge - lf 1
ELSEIF (area) THEN
DEBUG "Bagel area: ", DEC area, CR
area = 0
ENDIF
GOSUB Ready
OWOUT owio, 0, [DUMPADR, R 5]
OWIN owio, 2, [lft_edge, rgt_edge]
LOOP
This is very similar to the previous example, except that the statement order has been changed. We’ve
buffered the acquire and finds in the loop first, but then look what happens: we start computing with
data that hasn’t yet been read. In PBASIC, all variables are initialized to zero; So the first time through
the loop, neither the
IF
nor the
ELSEIF
conditions will be true, and that section, between lines 12 and
17, will simply be skipped.
Then
comes the call to
Ready
, because right then we want to read some new
data, which we do on lines 19 and 20. Now, when we loop back, we can prime the next exposure right
away,
and then
get on with our area calculations and, possibly, output. This has the effect of overlapping
the coprocessor’s work with the PBASIC program’s in the most efficient manner possible. Of course, in
any real application, you also have to make sure there’s enough time between encoder pulses to get
everything done! The illustration below shows the timeline of events:
Encoder (P0)
PBASIC
Driver
Acquire Image
Acquire Image
Find Edges
Find Edges
Compute
Compute & Output
Waiting in
Ready
In
Ready
Read
< ... >
>
Waiting
Waiting
P
ri
m
e
d
P
ri
m
e
d
T
ri
g
g
e
re
d
T
ri
g
g
e
re
d
L
O
O
P
D
O
N
o
t
B
u
s
y
Send Buffer
In the monitor program section, we looked at bottles – some full, some not-so full, some with caps, and
some without. What we want to do is pass the bottle if all of the following conditions prevail:
1. The liquid level is detected between pixels 111 and 129, and
2. The cap is detected, and it has a size of at least 36 pixels.
3. The top of the cap is no higher than pixel 205 (i.e. it’s pushed on all the way).