commit | author | age
|
df1f8e
|
1 |
/* |
A |
2 |
____ _____ _ _ |
|
3 |
| __ )| ____| | / \ |
|
4 |
| _ \| _| | | / _ \ |
|
5 |
| |_) | |___| |___ / ___ \ |
|
6 |
|____/|_____|_____/_/ \_\ |
|
7 |
http://bela.io |
|
8 |
*/ |
|
9 |
/** |
|
10 |
\example Sensors/ultrasonic-distance-sensor/render.cpp |
|
11 |
|
|
12 |
Measuring distances with ultrasonic sensors |
|
13 |
--------------------------- |
|
14 |
|
|
15 |
Ultrasonic sensors can detect distances to a high degree of accuracy, even |
|
16 |
in many conditions where infrared sensors would not be suitable (e.g.: when natural |
|
17 |
light is a problem. |
|
18 |
For this example we used the [HC-SR04](https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf). |
|
19 |
|
|
20 |
After it receives a short pulse (> 10us) at its TRIGGER input, the module outputs from the |
|
21 |
ECHO output a pulse whose length is proportional to the distance of the object that is in |
|
22 |
front of the sensor. When a trigger is received the module emits an ultrasonic wave |
|
23 |
from its emitter and then receives the signal back in its receiver, measuring the time |
|
24 |
difference between the two. |
|
25 |
|
|
26 |
According to the datasheet of this sensor, the following relation stands: |
|
27 |
time[us] / 58 = distance[cm] |
|
28 |
The dataseheet recommends a minimum interval of 60ms between triggers, in order to be able to |
|
29 |
read the result appropriately. |
|
30 |
|
|
31 |
Bela sends out the TRIGGER event every 2646 samples(60ms) and then waits for a pulse to come |
|
32 |
appear on the ECHO pin. The `PulseIn` class is used here to monitor a digital pin for an HIGH |
|
33 |
pulse and once the pulse termiantes, it returnes the duration ( in samples ) of the pulse. |
|
34 |
|
|
35 |
The module requires a 5V power supply and its digital inputs and outputs are low at 0V and |
|
36 |
HIGH at 5V. Check the pin diagram in the IDE to see where to find the pins you need. |
|
37 |
It is important that the 5V ECHO output from the module *is not* connected |
|
38 |
straight to Bela's digital inputs, as that would most likely kill the Bela board |
|
39 |
(digital I/Os are 3.3V tolerant). You will need to use a passive resistor divider from the |
|
40 |
HC-SR04's ECHO output to scale down the voltage before connecting it to the digital input |
|
41 |
on `gEchoDigitalInPin`. |
|
42 |
|
|
43 |
On the scope you should see the pulses coming in from the trigger and the distance. The closer |
|
44 |
the object, the shorter the pulses. Make sure you set the trigger to "channel 1" (the pulse) and |
|
45 |
you set the horizontal zoom appropriately. |
|
46 |
*/ |
|
47 |
|
|
48 |
#include <Bela.h> |
|
49 |
#include <stdlib.h> |
|
50 |
#include <libraries/Scope/Scope.h> |
|
51 |
#include <libraries/PulseIn/PulseIn.h> |
|
52 |
|
|
53 |
PulseIn pulseIn; |
|
54 |
Scope scope; |
|
55 |
int gTriggerInterval = 2646; // how often to send out a trigger. 2646 samples are 60ms |
|
56 |
int gMinPulseLength = 7; //to avoid spurious readings |
|
57 |
float gRescale = 58; // taken from the datasheet |
|
58 |
unsigned int gTrigDigitalOutPin = 7; //channel to be connected to the module's TRIGGER pin - check the pin diagram in the IDE |
|
59 |
unsigned int gEchoDigitalInPin = 7; //channel to be connected to the modules's ECHO pin via resistor divider. Check the pin diagram in the IDE |
|
60 |
int gTriggerCount = 0; |
|
61 |
int gPrintfCount = 0; |
|
62 |
|
|
63 |
bool setup(BelaContext *context, void *userData) |
|
64 |
{ |
|
65 |
// Set the mode of digital pins |
|
66 |
// pinMode(context, 0, gTrigDigitalOutPin, OUTPUT); // writing to TRIGGER pin |
|
67 |
// pinMode(context, 0, gEchoDigitalInPin, INPUT); // reading from ECHO pin |
|
68 |
pulseIn.setup(context, gEchoDigitalInPin, HIGH); //detect HIGH pulses on the ECHO pin |
|
69 |
scope.setup(2, context->digitalSampleRate); |
|
70 |
return true; |
|
71 |
} |
|
72 |
|
|
73 |
void render(BelaContext *context, void *userData) |
|
74 |
{ |
|
75 |
for(unsigned int n = 0; n < context->digitalFrames; ++n){ |
|
76 |
gTriggerCount++; |
|
77 |
bool state; |
|
78 |
if(gTriggerCount == gTriggerInterval){ |
|
79 |
pinMode(context, 0, gTrigDigitalOutPin, OUTPUT); // writing to TRIGGER pin |
|
80 |
state = LOW; |
|
81 |
digitalWriteOnce(context, n, gTrigDigitalOutPin, state); |
|
82 |
} else if (gTriggerCount == gTriggerInterval+1) { |
|
83 |
pinMode(context, 0, gTrigDigitalOutPin, OUTPUT); // writing to TRIGGER pin |
|
84 |
state = HIGH; |
|
85 |
digitalWriteOnce(context, n, gTrigDigitalOutPin, state); |
|
86 |
} else if (gTriggerCount == gTriggerInterval+2) { |
|
87 |
gTriggerCount = 0; |
|
88 |
state = LOW; |
|
89 |
digitalWriteOnce(context, n, gTrigDigitalOutPin, state); |
|
90 |
} else { |
|
91 |
pinMode(context, 0, gTrigDigitalOutPin, INPUT); // writing to TRIGGER pin |
|
92 |
} |
|
93 |
|
|
94 |
auto dRead = digitalRead(context, n, gEchoDigitalInPin); |
|
95 |
int pulseLength = pulseIn.hasPulsed(context, n); // will return the pulse duration(in samples) if a pulse just ended |
|
96 |
|
|
97 |
float duration = 1e6 * pulseLength / context->digitalSampleRate; // pulse duration in microseconds |
|
98 |
|
|
99 |
static float distance = 0; |
|
100 |
if(pulseLength >= gMinPulseLength){ |
|
101 |
// rescaling according to the datasheet |
|
102 |
distance = duration / gRescale; |
|
103 |
} |
|
104 |
static int count = 0; |
|
105 |
if(count > 5000){ // we do not want to print the value every time we read it |
|
106 |
rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance); |
|
107 |
count = 0; |
|
108 |
} |
|
109 |
++count; |
|
110 |
// Logging to the scope the pulse inputs (gEchoDigitalInPin) and the distance |
|
111 |
scope.log(dRead * 0.5, distance/100); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
void cleanup(BelaContext *context, void *userData) |
|
116 |
{ |
|
117 |
} |