}
Let us look at these lines in more details.
yocto_api and yocto_rangefinder import
These two import provide access to functions allowing you to manage Yoctopuce modules.
yocto_api
is always needed,
yocto_rangefinder
is necessary to manage modules
containing a range finder, such as Yocto-RangeFinder. Other imports can be useful in other cases,
such as
YModule
which can let you enumerate any type of Yoctopuce device.
YAPI.RegisterHub
The
RegisterHub
method allows you to indicate on which machine the Yoctopuce modules are
located, more precisely on which machine the VirtualHub software is running. In our case, the
127.0.0.1:4444
address indicates the local machine, port
4444
(the standard port used by
Yoctopuce). You can very well modify this address, and enter the address of another machine on
which the VirtualHub software is running, or of a YoctoHub. If the host cannot be reached, this
function will trigger an exception.
YRangeFinder.FindRangeFinder
The
FindRangeFinder
method allows you to find a range finder from the serial number of the
module on which it resides and from its function name. You can also use logical names, as long as
you have initialized them. Let us imagine a Yocto-RangeFinder module with serial number
YRNGFND1-123456
which you have named "
MyModule
", and for which you have given the
rangeFinder
function the name "
MyFunction
". The following five calls are strictly equivalent, as long
as "
MyFunction
" is defined only once.
rangefinder = YRangeFinder.
FindRangeFinder
(
"YRNGFND1-123456.rangeFinder"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"YRNGFND1-123456.MaFonction"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MonModule.rangeFinder"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MonModule.MaFonction"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MaFonction"
)
YRangeFinder.FindRangeFinder
returns an object which you can then use at will to control
the range finder.
isOnline
The
isOnline()
method of the object returned by
FindRangeFinder
allows you to know if the
corresponding module is present and in working order.
get_currentValue
The
get_currentValue()
method
of
the
object
returned
by
YRangeFinder.FindRangeFinder
allows you to obtain the distance measured by the sensor,
in mm or in inches depending on the sensor configuration.
A real example, for Node.js
Open a command window (a terminal, a shell...) and go into the directory
example_nodejs/Doc-
GettingStarted-Yocto-RangeFinder
within Yoctopuce library for JavaScript / EcmaScript 2017. In
there, you will find a file named
demo.js
with the sample code below, which uses the functions
explained above, but this time used with all side materials needed to make it work nicely as a small
demo.
If your Yocto-RangeFinder is not connected on the host running the browser, replace in the example
the address
127.0.0.1
by the IP address of the host on which the Yocto-RangeFinder is
connected and where you run the VirtualHub.
"use strict"
;
require
(
'yoctolib-es2017/yocto_api.js'
)
;
require
(
'yoctolib-es2017/yocto_lightsensor.js'
)
;
8. Using Yocto-RangeFinder with JavaScript / EcmaScript
www.yoctopuce.com
41
Содержание Yocto-RangeFinder
Страница 1: ...Yocto RangeFinder User s guide...
Страница 2: ......
Страница 18: ...12 www yoctopuce com...
Страница 26: ...20 www yoctopuce com...
Страница 42: ...36 www yoctopuce com...
Страница 70: ...gcc lyocto lm lpthread lusb 1 0 lstdc 10 Using Yocto RangeFinder with C 64 www yoctopuce com...
Страница 90: ...84 www yoctopuce com...
Страница 120: ...114 www yoctopuce com...
Страница 130: ...124 www yoctopuce com...
Страница 158: ...21 High level API Reference On failure throws an exception or returns a negative error code 152 www yoctopuce com...
Страница 484: ...478 www yoctopuce com...
Страница 488: ...482 www yoctopuce com...
Страница 490: ...23 Characteristics 484 www yoctopuce com...
Страница 491: ......
Страница 492: ......