Skip to content

Commit d0a5707

Browse files
committed
Test SharpIR and QTRSensor, change max controllers to 1
1 parent 8a2df44 commit d0a5707

File tree

11 files changed

+670
-32
lines changed

11 files changed

+670
-32
lines changed

components/arduino/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ set(CORE_SRCS
7373

7474
set(LIBRARY_SRCS
7575
libraries/APDS9960/src/Arduino_APDS9960.cpp
76+
libraries/ESP32-SharpIR/src/ESP32SharpIR.cpp
7677
libraries/ESP32Servo/src/ESP32PWM.cpp
7778
libraries/ESP32Servo/src/ESP32Servo.cpp
7879
libraries/QTRSensors/src/QTRSensors.cpp
79-
libraries/SharpIR/src/SharpIR.cpp
8080
# libraries/ArduinoOTA/src/ArduinoOTA.cpp
8181
# libraries/AsyncUDP/src/AsyncUDP.cpp
8282
# libraries/BluetoothSerial/src/BluetoothSerial.cpp
@@ -178,8 +178,8 @@ set(includedirs
178178
cores/esp32/
179179
libraries/APDS9960/src
180180
libraries/ESP32Servo/src
181+
libraries/ESP32-SharpIR/src
181182
libraries/QTRSensors/src
182-
libraries/SharpIR/src
183183
# libraries/ArduinoOTA/src
184184
# libraries/AsyncUDP/src
185185
# libraries/BLE/src
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
5+
6+
7+
## [1.0.0] - 2019-04-28 ##
8+
### First Release ###
9+
- Support ESP32 development board
10+
- For only GP2Y0A21YK0F

components/arduino/libraries/ESP32-SharpIR/LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)
2+
3+
# ESP32SharpIR
4+
An Arduino library for ESP32 development board that can measure distance from an analog Sharp IR sensor.
5+
6+
## Currently supported sensor models
7+
- GP2Y0A21YK0F
8+
9+
## Usage
10+
- Adding the library to the sketch
11+
12+
~~~c++
13+
#include <ESP32SharpIR.h>
14+
~~~
15+
16+
- Creating an instance
17+
18+
~~~c++
19+
SharpIR sensorName(sensorModel, sensorPin);
20+
~~~
21+
~~~
22+
sensorName : the name of the object
23+
sensorModel : the model code of the sensor (e.g. GP2YA41SK0F)
24+
sensorPin : the analog pin where the sensor Vout pin is attached
25+
~~~
26+
27+
- Acquiring data
28+
29+
~~~c++
30+
sensorName.getDistance();
31+
sensorName.getDistance( avoidBurstRead );
32+
~~~
33+
~~~
34+
avoidBurstRead : can be true or false
35+
~~~
36+
37+
The above method returns an uint8_t type value that is the distance in centimeters from the sensor and the object in front of it.
38+
By default burst reads are avoided but this causes a delay of 20ms on every call.
39+
In order to speed up the sketch is possible to allow bust reads by setting the parameter **avoidBurstRead** to **false**, in this way:
40+
41+
42+
~~~c++
43+
sensorName.getDistanceFloat();
44+
sensorName.getDistanceFloat( avoidBurstRead );
45+
~~~
46+
47+
Above method returns the distance in cm, as a float value filtered by exponent filter.
48+
49+
~~~c++
50+
sensorName.getRawDistance();
51+
sensorName.getRawDistance( avoidBurstRead );
52+
~~~
53+
54+
Above method returns just the raw distance read by the sensor, without applying any filter in cm.
55+
56+
~~~c++
57+
sensorName.setFilterRate(float rate);
58+
~~~
59+
60+
~~~
61+
rate : a value from the range from 0.01 to 0.99.
62+
0.01 equal to sampling 100 values and getting the average.
63+
~~~
64+
65+
66+
## License ##
67+
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">
68+
<img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" />
69+
</a>
70+
<br />
71+
<span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">The SharpIR Library</span>
72+
by <a xmlns:cc="http://creativecommons.org/ns#" href="https://nuwanjaliyagoda.com" property="cc:attributionName" rel="cc:attributionURL">Nuwan Jaliyagoda</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>
73+
74+
The library is based on the library 'Arduino_SharpIR', written by <a xmlns:cc="http://creativecommons.org/ns#" href="https://facebook.com/dev.giuseppemasino" property="cc:attributionName" rel="cc:attributionURL">Giuseppe Masinos</a>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#include <ESP32SharpIR.h>
3+
4+
ESP32SharpIR sensor( ESP32SharpIR::GP2Y0A21YK0F, 27);
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
9+
// Setting the filter resolution to 0.1
10+
sensor.setFilterRate(0.1f);
11+
}
12+
13+
void loop() {
14+
15+
// Print distance in cm
16+
//Serial.println(sensor.getDistance());
17+
18+
// Print distance in cm, as a float value
19+
Serial.println(sensor.getDistanceFloat());
20+
21+
delay(100);
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ESP32SharpIR KEYWORD1 DATA_TYPE
2+
getDistance KEYWORD2
3+
getDistanceFloat KEYWORD2
4+
getRawDistance KEYWORD2
5+
setFilterRate KEYWORD2
6+
GP2YA41SK0F LITERAL1 RESERVED_WORD_2
7+
GP2Y0A21YK0F LITERAL1 RESERVED_WORD_2
8+
GP2Y0A02YK0F LITERAL1 RESERVED_WORD_2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name=ESP32-SharpIR
2+
version=1.0.1
3+
author=Nuwan Jaliyagoda (NuwanJ)
4+
maintainer=Nuwan Jaliyagoda (NuwanJ)
5+
sentence=Can measure distance from SharpIR sensors using this library
6+
paragraph=
7+
category=Sensors
8+
url=https://github.com/NuwanJ/ESP32-SharpIR.git
9+
architectures=esp32
10+
dot_a_linkage=false
11+
includes=ESP32SharpIR.h
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "ESP32SharpIR.h"
2+
3+
ESP32SharpIR::ESP32SharpIR( sensorCode _sensorType , uint8_t _sensorPin ) {
4+
5+
// Assign sensor type and pin number
6+
sensorType = _sensorType;
7+
pin = _sensorPin;
8+
9+
// Set ADC unit resolution on ESP32
10+
analogReadResolution(ANALOG_RESOLUTION);
11+
12+
}
13+
14+
void ESP32SharpIR::setFilterRate(float rate) {
15+
if (rate > 0 && rate < 1) alpha = rate;
16+
}
17+
18+
float ESP32SharpIR::getDistanceFloat(bool avoidBurstRead ) {
19+
if ( !avoidBurstRead ) while ( millis() <= lastTime + 20 ) {} //wait for sensor's sampling time
20+
21+
rawDistance = getRawDistance(avoidBurstRead);
22+
average = (alpha * rawDistance) + (1 - alpha) * average; // exponential smoothing
23+
24+
#ifdef DEBUG
25+
Serial.printf("%f %f\n", rawDistance, average);
26+
#endif
27+
28+
return average;
29+
}
30+
31+
uint8_t ESP32SharpIR::getDistance(bool avoidBurstRead ) {
32+
return (uint8_t)getDistanceFloat(avoidBurstRead );
33+
}
34+
35+
36+
uint8_t ESP32SharpIR::getRawDistance( bool avoidBurstRead ) {
37+
uint8_t distance ;
38+
39+
if (!avoidBurstRead) while ( millis() <= lastTime + 20 ) {} //wait for sensor's sampling time
40+
41+
lastTime = millis();
42+
43+
switch ( sensorType )
44+
{
45+
case GP2Y0A41SK0F :
46+
// TODO: Implement
47+
distance = 0;// 2076/(analogRead(pin)-11);
48+
49+
if (distance > 30) return 31;
50+
else if (distance < 4) return 3;
51+
else return distance;
52+
53+
break;
54+
55+
case GP2Y0A21YK0F :
56+
57+
distance = (28400) / (analogRead(pin));
58+
if (distance > 80) return 81;
59+
else if (distance < 10) return 9;
60+
else return distance;
61+
62+
break;
63+
64+
case GP2Y0A02YK0F :
65+
// TODO: Implement
66+
distance = 0;// 9462/(analogRead(pin)-16.92);
67+
68+
if (distance > 150) return 151;
69+
else if (distance < 20) return 19;
70+
else return distance;
71+
}
72+
73+
// Error, returns -1
74+
return -1;
75+
}
76+
/*
77+
// White Perpendicular Block, r [0:511]
78+
79+
d r d*r
80+
10 330 3300
81+
12 285 3420
82+
14 250 3500
83+
16 230 3680
84+
18 190 3420
85+
20 180 3600
86+
22 160 3520
87+
24 150 3600
88+
26 140 3640
89+
28 130 3640
90+
30 125 3750
91+
92+
avg = 3550
93+
94+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
SEP32-SharpIR
3+
Library for the acquisition of distance data from Sharp IR sensors
4+
*/
5+
6+
#pragma once
7+
8+
#include <Arduino.h>
9+
10+
#define ANALOG_RESOLUTION 12
11+
12+
//#define DEBUG
13+
14+
class ESP32SharpIR
15+
{
16+
public:
17+
18+
using sensorCode = const uint8_t ;
19+
20+
ESP32SharpIR( sensorCode _sensorType , uint8_t _sensorPin );
21+
22+
float getDistanceFloat(bool avoidBurstRead = true);
23+
24+
uint8_t getRawDistance( bool avoidBurstRead = true );
25+
uint8_t getDistance(bool avoidBurstRead = true);
26+
27+
void setFilterRate(float rate);
28+
29+
static sensorCode GP2Y0A41SK0F = 0;
30+
static sensorCode GP2Y0A21YK0F = 1;
31+
static sensorCode GP2Y0A02YK0F = 3;
32+
33+
protected:
34+
uint8_t sensorType, pin ;
35+
36+
private:
37+
float alpha = 0.2f;
38+
float average = 25.0f;
39+
float rawDistance = 0.0f;
40+
uint32_t rawReading = 0;
41+
uint32_t lastTime = 0 ;
42+
};

0 commit comments

Comments
 (0)