SRF Ultrasonic Range Finder - Serial Mode
This library is a built-in class that provides a control function for the Devantech SRF series ultrasonic distance sensors in Serial mode.
Library usage requirements | |
---|---|
Type | UART |
Name | SRF_SERIAL |
Version | 1_00_00 |
Code size used | 3.9KB |
Resources used | UART × 1, Timer × 1 |
Related Documents
- SRF02 Ultrasonic Range Finder Technical Specification
- SRF02 Ultrasonic Range Finder Technical Specification - Serial Mode
Abstracts
Methods()/Properties | Summary | Note |
---|---|---|
new SRF_SERIAL() | Creates an SRF_SERIAL instance. |
{SRF_SERIAL} Instance
Methods()/Properties | Summary | Note |
---|---|---|
.write() | Executes the write command. | |
.read() | Executes the read command. | |
.isReady() | Checks if the sensor at the specified device address is ready. | |
.scanAddress() | Scans for all device addresses supported by the sensor. | |
.init() | Initializes the sensor. | |
.changeAddress() | Rewrites the device address of the sensor. | |
.startMeasurement() | Starts measurement. | |
.checkMeasComp() | Checks the measurement completion. | |
.getResult() | Retrieves the measurement results. | |
.startMeasAndGetResult() | Starts measurement, waits for measurement completion, and then retrieves the measurement result. | |
.VERSION | Version information maj : {number} Main Version min : {number} Minor Version rev : {number} Revision |
Details
new SRF_SERIAL(uart[,config])
Creates an SRF_SERIAL instance.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
uart | {UART} | mandatory | Specify a UART instance that has been opened in advance. The UART settings differ depending on the model number of the sensor. Confirm the technical specifications of the sensor to be used. | For using SRF02, the following settings are used. Baudrate: 9600bps Hardware flow: Invalid Parity bit: None Data bit: 8-bit Stop bit: 2-bit |
config | Object | optional | Configurations For details, refer to config. | |
return | {SRF_SERIAL} | - | {SRF_SERIAL} : Generated {SRF_SERIAL} | When an error occurs, an exception is raised. |
config
Name | Type | M/O | Description | Note |
---|---|---|---|---|
address | number | optional | Target device address Range: 0x00 - 0x0F The default value is 0x00. | |
rangingMode | number | optional | Ranging mode Specify the output unit of the measurement result. 0x50: In inches 0x51: In centimeters 0x52: In micro-seconds The default value is 0x51. |
.write(cmd)
Executes the write command.
This method is used to access the sensor directly.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
cmd | number | mandatory | Write command Range: 0x00 - 0xFF | |
return | boolean | - | true: Success false: Failure |
.read(cmd,len)
Executes the read command.
This method is used to access the sensor directly.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
cmd | number | mandatory | Read command Range: 0x00 - 0xFF | |
len | number | mandatory | Specify the byte length of the command response data. Range: 1 - 512 | |
return | ArrayBuffer | - | Command response data If the command fails, the returned size will be 0. |
.isReady([address])
Checks if the sensor at the specified device address is ready.
If the sensor at the specified device address does not exist, 'Not ready' will be returned.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
address | number | optional | Device Address Range: 0x00 - 0x0F If omitted, it will be the target device address that is currently set. | |
return | boolean | - | true: Ready false: Not ready | If a parameter is invalid, an exception is raised. |
.scanAddress()
Scans for all device addresses supported by the sensor.
Performs a status check with .isReady() for each device address.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | Array | - | Scan list The list of device addresses for which the ready status is detected is returned as an array type. The array elements contain device address values of type number. In case of no detection, the number of elements in the array will be 0. |
.init([address[,statusCheck]])
Initializes the sensor.
Performs a status check with .isReady() on the sensor at the target device address.
Specify the "switching destination device address" to be used for dynamically switching between multiple sensors with different device addresses on the same bus. In essence, switching is necessary when the sensor at the currently selected target device address has completed measurement.
In the default operation, the status check by .isReady() is performed for the sensor after switching. However, if the status of each sensor is always monitored, it is not necessary to check the status at each switching. In this case, it is possible to skip the status check process by setting statusCheck
to false.
Note that there must not be multiple sensors with the same device address on the same bus.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
address | number | optional | Switching destination device address (7bits) Range: 0x00 - 0x0F If omitted, initialization is performed for the sensor with the currently set target device address. | |
statusCheck | boolean | optional | Status check after sensor switching true: Enabled false: Disabled The default value is true. | This parameter is valid only when " Switching destination device address" is specified. |
return | boolean | - | true: Success false: Failure | If a parameter is invalid, an exception is raised. |
.changeAddress(address)
Rewrites the device address of the sensor.
Before executing this method, the device address of the sensor to be rewritten must be selected by .init() and become ready.
Issuing a rewrite of the device address to the sensor will rewrite the ROM in the sensor and reboot the sensor. Afterwards, the device address of the sensor becomes the new device address.
This method then switches the target device address to the new device address, executes .init(), and then returns the result. In doing so, it is possible to start measurement immediately after this method.
Rewriting the device address involves rewriting the ROM in the sensor. To avoid unexpected failures, frequent use of this method should be avoided.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
address | number | mandatory | Specify the new device address. Range: 0x00 - 0x0F | |
return | boolean | - | true: Success false: Failure | If a parameter is invalid, an exception is raised. |
.startMeasurement()
Starts measurement.
After executing this method, it is necessary to wait for the measurements to complete with .checkMeasComp().
After the measurement is completed, it is necessary to call this method again to start a new measurement.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.checkMeasComp()
Checks the measurement completion.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | boolean | - | true: Measurement completed false: Measurement in progress |
.getResult()
Retrieves the measurement results.
It is necessary to wait for measurement completion by using .checkMeasComp() in advance.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | number | - | Measurement results The measurement result will be output in the unit specified in the ranging mode. If the distance could not be measured, the value will be 0. If a sensor error occurred, the value will be -1. |
.startMeasAndGetResult()
Starts measurement, waits for measurement completion, and then retrieves the measurement result.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | number | - | Measurement results The measurement result will be output in the unit specified in the ranging mode. If the distance could not be measured, the value will be 0. If a sensor error occurred, the value will be -1. |
Usage Examples
Sample 1
This is a sample that measures distance at periodic intervals.
log.setLevel(0,2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE, 0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
if(!('SRF_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }
//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
throw new Error('UART open failed. Check the UART node number.');
}
//var srf = new SRF_SERIAL(uart, { address: 0x00 });
var srf = new SRF_SERIAL(uart);
ready = srf.init();
if(!ready) {
print('srf init failed');
while(1);
}
var actFlag = true;
var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);
while(actFlag) {
if(measFlag) {
var utime = Date.now();
var dist = srf.startMeasAndGetResult();
print(utime + ': ' + dist);
measFlag = false;
}
}
clearInterval(tout);
uart.close();
Sample 2
This is a sample that measures the distance while switching to multiple sensors with different device addresses in order.
First, .scanAddress() is used to find all the sensors that exist on the bus.
log.setLevel(0,2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE, 0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
if(!('SRF_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }
var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };
//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
throw new Error('UART open failed. Check the UART node number.');
}
var srf = new SRF_SERIAL(uart);
var list = srf.scanAddress();
if(!list.length) {
print('srf not found');
while(1);
}
print('List[' + list.length + ']: ' + list);
var actFlag = true;
var index = 0;
var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);
while(actFlag) {
if(measFlag) {
var utime = Date.now();
var addr = list[index];
srf.init(addr, false); //Skip status check
var dist = srf.startMeasAndGetResult();
print(utime + ': [' + toHexStr(addr) + '] '+ dist);
if(++index >= list.length) index = 0;
measFlag = false;
}
}
clearInterval(tout);
uart.close();
Sample 3
This is a sample that rewrites the device address of the target sensor to the new device address.
After the device address is rewritten, it will proceed to measure distance.
log.setLevel(0,2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE, 0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
if(!('SRF_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }
var TARGET_ADDRESS = 0x00;
var NEW_ADDRESS = 0x01;
var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };
//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
throw new Error('UART open failed. Check the UART node number.');
}
var srf = new SRF_SERIAL(uart);
ready = srf.init(TARGET_ADDRESS);
if(!ready) {
print('target srf not found');
while(1);
}
ready = srf.changeAddress(NEW_ADDRESS);
if(!ready) {
print('new address srf not found');
while(1);
}
print('Address change successful! [' + toHexStr(TARGET_ADDRESS) + ' to ' + toHexStr(NEW_ADDRESS) + ']');
var actFlag = true;
var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);
while(actFlag) {
if(measFlag) {
var utime = Date.now();
var dist = srf.startMeasAndGetResult();
print(utime + ': ' + dist);
measFlag = false;
}
}
clearInterval(tout);
uart.close();