Vous reconnaîtrez dans cet exemple l'utilisation des fonctions expliquées ci-dessus, cette fois
utilisées avec le décorum nécessaire à en faire un petit programme d'exemple concret.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,
sys
from
yocto_api
import
from
yocto_tilt
import
from
yocto_compass
from
yocto_gyro
import
from
yocto_accelerometer
def
usage():
scriptname
= os.path.basename(sys.argv[0])
print("Usage:")
print(scriptname +
print(scriptname +
print(scriptname +
sys.exit()
def
die(msg):
sys.exit(msg +
errmsg
= YRefParam()
if len(sys.argv) <
usage()
target
= sys.argv[1]
# Setup the API to use local USB devices
if YAPI.RegisterHub("usb", errmsg) !=
sys.exit("init error"
if
target
==
'any':
# retreive any tilt sensor
anytilt
= YTilt.FirstTilt()
if
anytilt
is
die('No module connected (check USB
m
= anytilt.get_module()
target
= m.get_serialNumber()
else:
anytilt
=
YTilt.FindTilt(target +
if not
(anytilt.isOnline()):
die('Module not connected (check identification and USB
serial
= anytilt.get_module().get_serialNumber()
tilt1
=
YTilt.FindTilt(serial +
tilt2
=
YTilt.FindTilt(serial +
compass
=
YCompass.FindCompass(serial +
accelerometer
=
YAccelerometer.FindAccelerometer(serial +
gyro
=
YGyro.FindGyro(serial +
count
=
0
if not
(tilt1.isOnline()):
die("Module not connected (check identification and USB
while
tilt1.isOnline():
if
count %
10
print("tilt1
print("%-7.1f "
"%-7.1f "
"%-7.1f "
"%-7.1f "
"%-7.1f"
count
+=
1
YAPI.Sleep(250, errmsg)
YAPI.FreeAPI()
www.yoctopuce.com
*
*
import
*
*
import
*
'
<serial_number>')
'
<logical_name>')
' any
')
' (check USB
cable)')
2:
YAPI.SUCCESS:
+
errmsg.value)
None:
".tilt1")
".tilt1")
".tilt2")
".gyro")
==
0:
tilt2
compass acc
%
tilt1.get_currentValue()
%
tilt2.get_currentValue()
%
compass.get_currentValue()
%
accelerometer.get_currentValue()
%
gyro.get_currentValue())
cable)')
".compass")
".accelerometer")
cable)")
gyro")
+ \
+ \
+ \
+ \
8. Utilisation du Yocto-3D-V2 en Python
cable)')
47