SRF Ultrasonic Range Finder - I2C Mode
This library is a built-in class that provides a control function for the Devantech SRF series ultrasonic distance sensors in I2C mode.
Library usage requirements | |
---|---|
Type | I2C |
Name | SRF_I2C |
Version | 1_00_00 |
Code size used | 4.1KB |
Resources used | I2C × 1, Timer × 1 |
Related Documents
- SRF02 Ultrasonic Range Finder Technical Specification
- SRF02 Ultrasonic Range Finder Technical Specification - I2C Mode
Abstracts
Methods()/Properties | Summary | Note |
---|---|---|
new SRF_I2C() | Creates an SRF_I2C instance. |
{SRF_I2C} Instance
Methods()/Properties | Summary | Note |
---|---|---|
.write() | Writes 1 byte of data to the specified sensor register. | |
.read() | Reads the specified byte length of data from the specified sensor register. | |
.isReady() | Checks if the sensor at the specified slave address is ready. | |
.scanSlaveAddress() | Scans for all slave addresses supported by the sensor. | |
.init() | Initializes the sensor. | |
.changeSlaveAddress() | Rewrites the slave 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_I2C(i2c[,config])
Creates an SRF_I2C instance.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
i2c | {I2C} | mandatory | Specify an I2C instance that has been opened in advance. | |
config | Object | optional | Configurations For details, refer to config. | |
return | {SRF_I2C} | - | {SRF_I2C} : Generated {SRF_I2C} | When an error occurs, an exception is raised. |
config
Name | Type | M/O | Description | Note |
---|---|---|---|---|
slaveAddress | number | optional | Target slave address (7 bits) Range: 0x70 - 0x7F The default value is 0x70. | |
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(reg,val)
Writes 1 byte of data to the specified sensor register.
This method is used to access the sensor directly.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
reg | number | mandatory | Target register Range: 0x00 - 0xFF | |
val | number | mandatory | Write data Range: 0x00 - 0xFF | |
return | boolean | - | true: Success false: Failure |
.read(reg,len)
Reads the specified byte length of data from the specified sensor register.
This method is used to access the sensor directly.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
reg | number | mandatory | Target register Range: 0x00 - 0xFF | |
len | number | mandatory | Specify the read byte length. Range: 1- 255 | |
return | ArrayBuffer | - | Read data If reading fails, the returned size will be 0. |
.isReady([slaveAddress])
Checks if the sensor at the specified slave address is ready.
If the sensor at the specified slave address does not exist or if the sensor is busy measuring, 'Not ready' will be returned.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
slaveAddress | number | optional | Slave address (7 bits) Range: 0x70 - 0x7F If omitted, it will be the target slave address that is currently set. | |
return | boolean | - | true: Ready false: Not ready | If a parameter is invalid, an exception is raised. |
.scanSlaveAddress()
Scans for all slave addresses supported by the sensor.
Performs a status check with .isReady() for each slave address.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
return | Array | - | Scan list The list of slave addresses for which a ready status is detected is returned as an array type. The array elements contain slave address values of type number. In case of no detection, the number of elements in the array will be 0. |
.init([slaveAddress[,statusCheck]])
Initializes the sensor.
Performs a status check with .isReady() on the sensor at the target slave address.
Specify the "switching destination slave address" to be used for dynamically switching between multiple sensors with different slave addresses on the same bus. In essence, switching is necessary when the sensor at the currently selected target slave 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 slave address on the same bus.
Name | Type | M/O | Description | Note |
---|---|---|---|---|
slaveAddress | number | optional | Switching destination slave address (7bits) Range: 0x70 - 0x7F If omitted, initialization is performed for the sensor with the currently set target slave 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 slave address" is specified. |
return | boolean | - | true: Success false: Failure | If a parameter is invalid, an exception is raised. |
.changeSlaveAddress(slaveAddress)
Rewrites the slave address of the sensor.
Before executing this method, the slave address of the sensor to be rewritten must be selected by .init() and become ready.
Issuing a rewrite of the slave address to the sensor will rewrite the ROM in the sensor and reboot the sensor. Afterwards, the slave address of the sensor becomes the new slave address.
This method then switches the target slave address to the new slave address, executes .init(), and then returns the result. In doing so, it is possible to start measurement immediately after this method.
Rewriting the slave 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 |
---|---|---|---|---|
slaveAddress | number | mandatory | Specify the new slave address (7 bits). Range: 0x70 - 0x7F | |
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_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }
//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
throw new Error('I2C open failed. Check the I2C node number.');
}
//var srf = new SRF_I2C(i2c, { slaveAddress: 0x70 });
var srf = new SRF_I2C(i2c);
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);
i2c.close();
Sample 2
This is a sample that measures the distance while switching to multiple sensors with different slave addresses in order.
First, .scanSlaveAddress() 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_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }
var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };
//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
throw new Error('I2C open failed. Check the I2C node number.');
}
var srf = new SRF_I2C(i2c);
var list = srf.scanSlaveAddress();
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);
i2c.close();
Sample 3
This is a sample that rewrites the slave address of the target sensor to the new slave address.
After the slave 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_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }
var TARGET_ADDRESS = 0x70;
var NEW_ADDRESS = 0x71;
var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };
//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
throw new Error('I2C open failed. Check the I2C node number.');
}
var srf = new SRF_I2C(i2c);
ready = srf.init(TARGET_ADDRESS);
if(!ready) {
print('target srf not found');
while(1);
}
ready = srf.changeSlaveAddress(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);
i2c.close();