Skip to content

Commit

Permalink
Add support for NewPing
Browse files Browse the repository at this point in the history
* Works with many different ultrasonic sensor models: HC-SR04, SRF05,
  SRF06, DYP-ME007, JSN-SR04T & Parallax PING)))™.
* Built-in digital filter method ping_median() for easy error correction
  (use SONAR_ITERATIONS build flag to configure).
* Allows setting of a maximum distance where pings beyond that distance
  are read as no ping or clear (use SONAR_MAX_DISTANCE build flag).
  • Loading branch information
ruimarinho committed Aug 9, 2018
1 parent 8240561 commit 43abc6b
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 53 deletions.
2 changes: 1 addition & 1 deletion code/espurna/config/arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
//#define EMON_ANALOG_SUPPORT 1
//#define EVENTS_SUPPORT 1
//#define GUVAS12SD_SUPPORT 1
//#define HCSR04_SUPPORT 1
//#define SONAR_SUPPORT 1
//#define HLW8012_SUPPORT 1
//#define MHZ19_SUPPORT 1
//#define NTC_SUPPORT 1
Expand Down
11 changes: 7 additions & 4 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
#define I2C_SDA_PIN 12 // D6
#define I2C_SCL_PIN 14 // D5

#define SONAR_TRIGGER 12 // D6
#define SONAR_ECHO 13 // D7

#elif defined(WEMOS_D1_TARPUNA_SHIELD)

// Info
Expand Down Expand Up @@ -2665,10 +2668,10 @@
#define EVENTS_SUPPORT 1
#define EVENTS_PIN 6

// HC-RS04
#define HCSR04_SUPPORT 1
#define HCSR04_TRIGGER 7
#define HCSR04_ECHO 8
// Sonar
#define SONAR_SUPPORT 1
#define SONAR_TRIGGER 7
#define SONAR_ECHO 8

// MHZ19
#define MHZ19_SUPPORT 1
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ PROGMEM const char espurna_sensors[] =
#if GUVAS12SD_SUPPORT
"GUVAS12SD "
#endif
#if HCSR04_SUPPORT
"HCSR04 "
#if SONAR_SUPPORT
"SONAR "
#endif
#if HLW8012_SUPPORT
"HLW8012 "
Expand Down
30 changes: 19 additions & 11 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,30 @@
#endif

//------------------------------------------------------------------------------
// HC-SR04
// Enable support by passing HCSR04_SUPPORT=1 build flag
// Sonar
// Enable support by passing SONAR_SUPPORT=1 build flag
//------------------------------------------------------------------------------

#ifndef HCSR04_SUPPORT
#define HCSR04_SUPPORT 0
#ifndef SONAR_SUPPORT
#define SONAR_SUPPORT 0
#endif

#ifndef HCSR04_TRIGGER
#define HCSR04_TRIGGER 12 // GPIO for the trigger pin (output)
#ifndef SONAR_TRIGGER
#define SONAR_TRIGGER 12 // GPIO for the trigger pin (output)
#endif

#ifndef HCSR04_ECHO
#define HCSR04_ECHO 14 // GPIO for the echo pin (input)
#ifndef SONAR_ECHO
#define SONAR_ECHO 14 // GPIO for the echo pin (input)
#endif

#ifndef SONAR_MAX_DISTANCE
#define SONAR_MAX_DISTANCE MAX_SENSOR_DISTANCE // Max sensor distance in cm
#endif

#ifndef SONAR_ITERATIONS
#define SONAR_ITERATIONS 5 // Number of iterations to ping for
#endif // error correction.

//------------------------------------------------------------------------------
// HLW8012 Energy monitor IC
// Enable support by passing HLW8012_SUPPORT=1 build flag
Expand Down Expand Up @@ -628,7 +636,7 @@
EVENTS_SUPPORT || \
GEIGER_SUPPORT || \
GUVAS12SD_SUPPORT || \
HCSR04_SUPPORT || \
SONAR_SUPPORT || \
HLW8012_SUPPORT || \
MHZ19_SUPPORT || \
NTC_SUPPORT || \
Expand Down Expand Up @@ -748,8 +756,8 @@
#include "../sensors/GUVAS12SDSensor.h"
#endif

#if HCSR04_SUPPORT
#include "../sensors/HCSR04Sensor.h"
#if SONAR_SUPPORT
#include "../sensors/SonarSensor.h"
#endif

#if HLW8012_SUPPORT
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
#define SENSOR_GUVAS12SD_ID 0x20
#define SENSOR_CSE7766_ID 0x21
#define SENSOR_TMP3X_ID 0x22
#define SENSOR_HCSR04_ID 0x23
#define SENSOR_SONAR_ID 0x23
#define SENSOR_SENSEAIR_ID 0x24
#define SENSOR_GEIGER_ID 0x25
#define SENSOR_NTC_ID 0x26
Expand Down
10 changes: 6 additions & 4 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,13 @@ void _sensorLoad() {
}
#endif

#if HCSR04_SUPPORT
#if SONAR_SUPPORT
{
HCSR04Sensor * sensor = new HCSR04Sensor();
sensor->setTrigger(HCSR04_TRIGGER);
sensor->setEcho(HCSR04_ECHO);
SonarSensor * sensor = new SonarSensor();
sensor->setEcho(SONAR_ECHO);
sensor->setIterations(SONAR_ITERATIONS);
sensor->setMaxDistance(SONAR_MAX_DISTANCE);
sensor->setTrigger(SONAR_TRIGGER);
_sensors.push_back(sensor);
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,46 @@
// Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------

#if SENSOR_SUPPORT && HCSR04_SUPPORT
#if SENSOR_SUPPORT && SONAR_SUPPORT

#pragma once

#include "Arduino.h"
#include "BaseSensor.h"
#include "NewPing.h"

class HCSR04Sensor : public BaseSensor {
class SonarSensor : public BaseSensor {

public:

// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------

HCSR04Sensor(): BaseSensor() {
SonarSensor(): BaseSensor() {
_count = 1;
_sensor_id = SENSOR_HCSR04_ID;
_sensor_id = SENSOR_SONAR_ID;
}

// ---------------------------------------------------------------------

// Echo pin.
void setEcho(unsigned char echo) {
_echo = echo;
}

// Number of iterations to ping in order to filter out erroneous readings
// using a digital filter.
void setIterations(unsigned int iterations) {
_iterations = iterations;
}

// Max sensor distance in centimeters.
void setMaxDistance(unsigned int distance) {
_max_distance = distance;
}

// Trigger pin.
void setTrigger(unsigned char trigger) {
_trigger = trigger;
}
Expand All @@ -43,22 +57,28 @@ class HCSR04Sensor : public BaseSensor {
return _trigger;
}

unsigned int getMaxDistance() {
return _max_distance;
}

unsigned int getIterations() {
return _iterations;
}

// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------

// Initialization method, must be idempotent
void begin() {
pinMode(_echo, INPUT);
pinMode(_trigger, OUTPUT);
digitalWrite(_trigger, LOW);
_sonar = new NewPing(getTrigger(), getEcho(), getMaxDistance());
_ready = true;
}

// Descriptive name of the sensor
String description() {
char buffer[24];
snprintf(buffer, sizeof(buffer), "HCSR04 @ GPIO(%u, %u)", _trigger, _echo);
char buffer[23];
snprintf(buffer, sizeof(buffer), "Sonar @ GPIO(%u, %u)", _trigger, _echo);
return String(buffer);
}

Expand All @@ -80,28 +100,13 @@ class HCSR04Sensor : public BaseSensor {

// Current value for slot # index
double value(unsigned char index) {
if (index != 0) return 0;

if (index == 0) {

// Trigger pulse
digitalWrite(_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(_trigger, LOW);

// Wait for echo pulse low-high-low
while ( digitalRead(_echo) == 0 ) yield();
unsigned long start = micros();
while ( digitalRead(_echo) == 1 ) yield();
unsigned long travel_time = micros() - start;

// Assuming a speed of sound of 340m/s
// Dividing by 2 since it is a round trip
return 340.0 * (double) travel_time / 1000000.0 / 2;

if (getIterations() > 0) {
return NewPing::convert_cm(_sonar->ping_median(getIterations())) / 100.0;
}

return 0;

return _sonar->ping_cm() / 100.0;
}


Expand All @@ -113,7 +118,10 @@ class HCSR04Sensor : public BaseSensor {

unsigned char _trigger;
unsigned char _echo;
unsigned int _max_distance;
unsigned int _iterations;
NewPing * _sonar = NULL;

};

#endif // SENSOR_SUPPORT && HCSR04_SUPPORT
#endif // SENSOR_SUPPORT && SONAR_SUPPORT
2 changes: 1 addition & 1 deletion code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function sensorName(id) {
"HLW8012", "V9261F", "ECH1560", "Analog", "Digital",
"Events", "PMSX003", "BMX280", "MHZ19", "SI7021",
"SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD",
"TMP3X", "HC-SR04", "SenseAir", "GeigerTicks", "GeigerCPM"
"TMP3X", "Sonar", "SenseAir", "GeigerTicks", "GeigerCPM"
];
if (1 <= id && id <= names.length) {
return names[id - 1];
Expand Down
1 change: 1 addition & 0 deletions code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ lib_deps =
rc-switch
https://github.com/LowPowerLab/RFM69#1.1.3
https://github.com/xoseperez/Time
NewPing
lib_ignore =

# ------------------------------------------------------------------------------
Expand Down

0 comments on commit 43abc6b

Please sign in to comment.