2021-05-26 05:31:36 -04:00
|
|
|
//
|
|
|
|
// FILE: SRF05.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-25 13:11:37 -05:00
|
|
|
// VERSION: 0.1.3
|
2021-05-26 05:31:36 -04:00
|
|
|
// DATE: 2021-05-17
|
|
|
|
// PURPOSE: Arduino library for the SRF05 distance sensor (and compatibles)
|
|
|
|
// URL: https://github.com/RobTillaart/SRF05
|
|
|
|
|
|
|
|
|
|
|
|
#include "SRF05.h"
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2022-11-25 13:11:37 -05:00
|
|
|
// CONSTRUCTORS
|
2021-05-26 05:31:36 -04:00
|
|
|
//
|
|
|
|
SRF05::SRF05(const uint8_t trigger, const uint8_t echo, const uint8_t out)
|
|
|
|
{
|
|
|
|
_trigger = trigger;
|
|
|
|
_echo = echo;
|
|
|
|
_out = out;
|
|
|
|
_mode = 0;
|
|
|
|
pinMode(_trigger, OUTPUT);
|
|
|
|
digitalWrite(_trigger, LOW);
|
|
|
|
pinMode(_echo, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SRF05::setSpeedOfSound(float sos)
|
|
|
|
{
|
|
|
|
_speedOfSound = sos;
|
|
|
|
}
|
|
|
|
|
|
|
|
float SRF05::getSpeedOfSound()
|
|
|
|
{
|
|
|
|
return _speedOfSound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 13:11:37 -05:00
|
|
|
void SRF05::setCorrectionFactor(float factor)
|
|
|
|
{
|
|
|
|
_correctionFactor = factor;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float SRF05::getCorrectionFactor()
|
|
|
|
{
|
|
|
|
return _correctionFactor;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MODE
|
|
|
|
//
|
2021-05-26 05:31:36 -04:00
|
|
|
void SRF05::setModeSingle()
|
|
|
|
{
|
2021-11-18 07:52:59 -05:00
|
|
|
_mode = 0x00;
|
|
|
|
_count = 1;
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
|
|
|
void SRF05::setModeAverage(uint8_t count)
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
2021-11-18 07:52:59 -05:00
|
|
|
_mode = 0x01;
|
|
|
|
_count = count;
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
|
|
|
void SRF05::setModeMedian(uint8_t count)
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
2021-11-18 07:52:59 -05:00
|
|
|
_mode = 0x02;
|
|
|
|
_count = count;
|
|
|
|
if (_count < 3) _count = 3;
|
|
|
|
if (_count > 15) _count = 15;
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
void SRF05::setModeRunningAverage(float alpha)
|
|
|
|
{
|
|
|
|
_mode = 0x03;
|
2021-11-18 07:52:59 -05:00
|
|
|
_count = 1;
|
2021-05-26 05:31:36 -04:00
|
|
|
_alpha = alpha;
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
uint8_t SRF05::getOperationalMode()
|
|
|
|
{
|
|
|
|
return _mode;
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
//////////////////////////////////////////////////
|
2022-11-25 13:11:37 -05:00
|
|
|
//
|
|
|
|
// MEASUREMENTS
|
|
|
|
//
|
2021-05-26 05:31:36 -04:00
|
|
|
uint32_t SRF05::getTime()
|
|
|
|
{
|
|
|
|
switch(_mode)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 0x00:
|
|
|
|
return _read();
|
|
|
|
|
|
|
|
case 0x01:
|
|
|
|
{
|
|
|
|
float sum = 0;
|
2021-11-18 07:52:59 -05:00
|
|
|
for (uint8_t s = 0; s < _count; s++)
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
|
|
|
sum += _read();
|
|
|
|
delay(1);
|
|
|
|
}
|
2021-11-18 07:52:59 -05:00
|
|
|
return round(sum / _count);
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
case 0x02:
|
|
|
|
{
|
|
|
|
uint32_t samples[15];
|
2021-11-18 07:52:59 -05:00
|
|
|
for (uint8_t s = 0; s < _count; s++)
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
|
|
|
samples[s] = _read();
|
|
|
|
delay(1);
|
|
|
|
}
|
2021-11-18 07:52:59 -05:00
|
|
|
_insertSort(samples, _count);
|
|
|
|
if (_count & 0x01) return samples[_count / 2];
|
|
|
|
return (samples[(_count + 1) / 2] + samples[_count / 2]) / 2;
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
case 0x03:
|
2021-11-18 07:52:59 -05:00
|
|
|
_value = (1 - _alpha) * _value + _alpha * _read();
|
|
|
|
return _value;
|
2021-05-26 05:31:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t SRF05::getMillimeter()
|
|
|
|
{
|
|
|
|
return _speedOfSound * getTime() * 0.5e-3;
|
|
|
|
}
|
2021-11-18 07:52:59 -05:00
|
|
|
|
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
float SRF05::getCentimeter()
|
|
|
|
{
|
|
|
|
return _speedOfSound * getTime() * 0.5e-4;
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
float SRF05::getMeter()
|
|
|
|
{
|
|
|
|
return _speedOfSound * getTime() * 0.5e-6;
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
float SRF05::getInch()
|
|
|
|
{
|
|
|
|
return _speedOfSound * getTime() * 1.9685e-5;
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:52:59 -05:00
|
|
|
|
2021-05-26 05:31:36 -04:00
|
|
|
float SRF05::getFeet()
|
|
|
|
{
|
|
|
|
return _speedOfSound * getTime() * 1.64042e-6;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 13:11:37 -05:00
|
|
|
// assumes a distance of 1.00 meter
|
|
|
|
// typically use 100 or 500 meter for distance to calibrate
|
2021-05-26 05:31:36 -04:00
|
|
|
float SRF05::determineSpeedOfSound(uint16_t distance)
|
|
|
|
{
|
|
|
|
float sum = 0;
|
|
|
|
for (uint16_t i = 0; i < distance; i++)
|
|
|
|
{
|
|
|
|
sum += _read();
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
float sos = 2e6 * distance / sum;
|
|
|
|
return sos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// private
|
|
|
|
//
|
|
|
|
uint32_t SRF05::_read()
|
|
|
|
{
|
|
|
|
digitalWrite(_trigger, HIGH);
|
|
|
|
delayMicroseconds(_triggerLength);
|
|
|
|
digitalWrite(_trigger, LOW);
|
|
|
|
uint32_t duration = pulseIn(_echo, HIGH, 300000);
|
|
|
|
if (_correctionFactor == 1)
|
|
|
|
{
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
return round(duration * _correctionFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SRF05::_insertSort(uint32_t * array, uint8_t size)
|
|
|
|
{
|
|
|
|
uint8_t t, z;
|
|
|
|
uint32_t temp;
|
2022-11-25 13:11:37 -05:00
|
|
|
for (t = 1; t < size; t++)
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
|
|
|
z = t;
|
|
|
|
temp = array[z];
|
2022-11-25 13:11:37 -05:00
|
|
|
while( (z > 0) && (temp < array[z - 1] ))
|
2021-05-26 05:31:36 -04:00
|
|
|
{
|
|
|
|
array[z] = array[z - 1];
|
|
|
|
z--;
|
|
|
|
}
|
|
|
|
array[z] = temp;
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-11-18 07:52:59 -05:00
|
|
|
|