Main:
call InitSensors(0,0,0,90) // set offsets
call Orientation(Forw,Side,Rot,Heading)
// display the entire array
for i=0 to 6
xyString 10,10+20*i,i; Sensors[i]
next
// display the processed readings
xyString 10,160,"Forward Tilt = ",Forw
xyString 10,180,"Side Tilt = ",Side
xyString 10,200,"Rotational Tilt = ",Rot
xyString 10,220,"Compass Heading =
",Head-ing
end
sub InitSensors(a,b,c,d)
dim Sensors[28] //max available from utility
for i=0 to 27
Sensors[i]=""
next
// allow setting certain angles to zero
dim Offsets[4]
Offsets[0]=a
Offsets[1]=b
Offsets[2]=c
Offsets[3]=d
Figure 2.
sub Orientation(&t1,&t2,&t3,&h)
// Request readings
SetCBtext("tilt")
filerename ("RBsTurn.txt","SensorsTurn.txt")
// and wait for it to respond
while !fileexists("RBsTurn.txt")
wend
// delay 10
// get the data
s=getcbtext()
// and parse it
mfromstring Sensors,s,","
t1 = round(Sensors[1])+Offsets[0]
t2 = round(Sensors[2])+Offsets[1]
t3 = round(Sensors[3])+Offsets[2]
h = round(Sensors[5])+Offsets[3]
// ensure heading within normal range
if h>359 then h-=360
if h<0 then h+=360
return
Orientation(). The initialization function dimensions two
arrays, and fills one of them with the four offset values that
are passed to it. More on these offsets shortly.
The Orientation function handles all the communication
with the clipboard and parses the data string obtained from
the sensor utility, placing the individual data elements into
the array Sensors[]. The individual elements of this array
can be accessed directly to obtain the desired data but we
wanted to make using the data even easier. In our example,
the tilt readings and compass heading are truncated to
integers and offset by the amounts specified during
initialization before being placed into the variable names
passed to the function.
utility program is a single string with each item in the string
separated by a comma. To make it easier to deal with the
data, headings are placed in the string that describe the
data within the string itself. Let’s look at an example.
When the tablet’s orientation data is requested (using
the command tilt), the first item in the string is the heading
Inclinometer with the next three items being the data
indicating the current tilt angles. Following the angle data is
the heading Compass, which is followed by the two data
parameters. The first reading is for magnetic north,
followed by the true north reading if it is available. In order
to make this data more easily accessible, we created two
functions: one called InitSensors() and the other called
0 Inclinometer
1 90.04
You will notice a 10 ms delay has been commented out
in the function just before the data is retrieved from the
clipboard. Windows performs clipboard operations in the
background, so it is possible that some systems might need
a small delay to ensure the data is ready. If your system
encounters an error while accessing the clipboard, add a
small delay at this point.
2 - 3. 34
3 270.34
4 Compass
5 89.6799979954958
6
Forward Tilt = 90
Side Tilt = - 3
Rotational Tilt = 270
Compass Heading = 180
Figure 3.
Our function provides integer readings because they
are often easier to use, but you can keep the floating-point
format if such accuracy is needed for your projects. The
offsets for the tilt parameters allow you to create a
“normal” orientation for your tablet. For example, the
forward tilt reading is normally zero (at least on the tablet
we used) if the tablet is lying flat like a piece of paper on a
desk. If your tablet is going to be used in a vertical position,
then applying an offset of -90 to the forward tilt parameter
can make the data more logical because a zero reading
would then mean the tablet is in a normal upright position.
Having the ability to add an offset to the compass
reading can also be valuable. Our tablet, for example, gives
a due north reading when the back of a vertical tablet
60 SERVO 12.2013