GY-63_MS5611/libraries/SRF05/SRF05.h

104 lines
2.7 KiB
C
Raw Normal View History

2021-05-26 05:31:36 -04:00
#pragma once
//
// FILE: SRF05.h
// AUTHOR: Rob Tillaart
2024-02-10 07:14:07 -05:00
// VERSION: 0.3.0
2021-05-26 05:31:36 -04:00
// DATE: 2021-05-17
// PURPOSE: Arduino library for SRF05 distance sensor
// URL: https://github.com/RobTillaart/SRF05
#include "Arduino.h"
2024-02-10 07:14:07 -05:00
#define SRF05_LIB_VERSION (F("0.3.0"))
2022-11-25 13:11:37 -05:00
const uint8_t SRF05_MODE_SINGLE = 0;
const uint8_t SRF05_MODE_AVERAGE = 1;
const uint8_t SRF05_MODE_MEDIAN = 2;
const uint8_t SRF05_MODE_RUN_AVERAGE = 3;
2021-05-26 05:31:36 -04:00
class SRF05
{
public:
2024-02-10 07:14:07 -05:00
explicit SRF05(const uint8_t trigger, const uint8_t echo = 0);
2021-05-26 05:31:36 -04:00
2022-11-25 13:11:37 -05:00
// configuration
2024-01-30 04:29:00 -05:00
void setSpeedOfSound(float speedOfSound = 340); // meter/sec
2021-05-26 05:31:36 -04:00
float getSpeedOfSound();
2022-11-25 13:11:37 -05:00
// adjust timing
2023-03-16 06:50:16 -04:00
bool setCorrectionFactor(float factor = 1);
2022-11-25 13:11:37 -05:00
float getCorrectionFactor();
2021-05-26 05:31:36 -04:00
2022-11-25 13:11:37 -05:00
// operational mode
2023-03-16 06:50:16 -04:00
void setModeSingle(); // default
2021-11-18 07:52:59 -05:00
void setModeAverage(uint8_t count);
void setModeMedian(uint8_t count);
2021-05-26 05:31:36 -04:00
void setModeRunningAverage(float alpha);
uint8_t getOperationalMode();
2024-02-10 07:14:07 -05:00
// interval of average and median mode
void setSampleInterval(uint16_t microSeconds = 1000);
uint16_t getSampleInterval();
2021-05-26 05:31:36 -04:00
2022-11-25 13:11:37 -05:00
// get distance
uint32_t getTime(); // uSec
uint32_t getMillimeter(); // mm
float getCentimeter(); // cm
float getMeter(); // m
float getInch(); // inch = 2.54 cm
float getFeet(); // feet = 12 inch
2024-02-10 07:14:07 -05:00
float getYards(); // yard = 3 feet = 36 inch
2021-05-26 05:31:36 -04:00
2022-11-25 13:11:37 -05:00
// Experimental - calibration
2024-01-30 04:29:00 -05:00
// The distance is averaged over 64 measurements.
// blocks for 70-80 ms.
// distance in meters (1 meter = 3.333 feet)
// returns speed in m/s.
float determineSpeedOfSound(float distance, uint8_t count = 64);
2021-05-26 05:31:36 -04:00
2023-03-16 06:50:16 -04:00
// Experimental - adjust trigger length
// - gain is a few microseconds at best.
// - 10 microseconds is advised minimum
// - to be investigated.
void setTriggerLength(uint8_t length = 10);
uint8_t getTriggerLength();
2024-02-10 07:14:07 -05:00
2023-03-16 06:50:16 -04:00
// TIMING
uint32_t lastTime();
2021-05-26 05:31:36 -04:00
2024-01-30 04:29:00 -05:00
// helper function.
// temperature and humidity to be determined by a sensor e.g. DHT22 or SHT85
// returned value must be set explicitly by setSpeedOfSound().
float calculateSpeedOfSound(float temperature, float humidity);
2021-05-26 05:31:36 -04:00
private:
uint8_t _trigger;
uint8_t _echo;
2022-11-25 13:11:37 -05:00
uint8_t _mode = SRF05_MODE_SINGLE;
2021-11-18 07:52:59 -05:00
uint8_t _count = 1;
2021-05-26 05:31:36 -04:00
float _alpha = 1.0;
2021-11-18 07:52:59 -05:00
float _value = 0;
2021-05-26 05:31:36 -04:00
float _correctionFactor = 1;
2024-02-10 07:14:07 -05:00
uint8_t _triggerLength = 10; // microSeconds
2024-01-30 04:29:00 -05:00
float _speedOfSound = 340; // 15°C 0%RH Sea level
2023-03-16 06:50:16 -04:00
uint32_t _lastTime = 0;
2024-02-10 07:14:07 -05:00
uint16_t _sampleInterval = 1000; // microSeconds
2021-05-26 05:31:36 -04:00
uint32_t _read();
void _insertSort(uint32_t * array, uint8_t size);
};
2021-11-18 07:52:59 -05:00
2023-03-16 06:50:16 -04:00
// -- END OF FILE --
2021-11-18 07:52:59 -05:00