EN
Raspberry PI - how to use ultrasonic rangefinder with c++ (cpp)?
4 points
xxxxxxxxxx
1
$ wget abyz.me.uk/rpi/pigpio/pigpio.zip
2
$ unzip pigpio.zip
3
$ cd PIGPIO
4
$ make
5
$ sudo make install
6
$ rm pigpio.zip
7
$ sudo rm -rf PIGPIO
Note: for more details read official webpage: http://abyz.me.uk/rpi/pigpio/download.html
xxxxxxxxxx
1
2
3
4
5
6
7
8
9
using namespace std;
10
11
12
struct timeval tv;
13
14
double getTime()
15
{
16
gettimeofday(&tv, NULL);
17
18
return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
19
}
20
21
// https://en.wikipedia.org/wiki/Speed_of_sound
22
// https://www.google.com/search?q=343+m%2Fs+to+cm%2Fs&oq=343+m%2Fs+to+cm%2Fs&aqs=chrome..69i57j6.443j0j9&sourceid=chrome&ie=UTF-8
23
int coefficient = 17150; // it should be selected experimentally - it is half of speed of sound in air in cm/s
24
25
int TRIG = 23; // BCM/GPIO pin number
26
int ECHO = 24; // BCM/GPIO pin number
27
28
void initializePins()
29
{
30
gpioSetMode(TRIG, PI_OUTPUT);
31
gpioSetMode(ECHO, PI_INPUT);
32
}
33
34
bool waitValue(int value, int limit = 10000) // use 1000000 to increase measurement distance
35
{
36
for(int i = 0; gpioRead(ECHO) == value; ++i)
37
{
38
if(i >= limit)
39
return false;
40
}
41
42
return true;
43
}
44
45
double detectDistance()
46
{
47
gpioWrite(TRIG, 0); // reset rangefinder state
48
usleep(500000); // wait for reset state - use 100000 to speedup program
49
gpioWrite(TRIG, 1); // hi state runs rangefinder measurement
50
usleep(10); // 10 us of hi level is necessary to start measurement - depended on rangefinder model
51
gpioWrite(TRIG, 0); // prepare rangefinder for measurement
52
53
if(waitValue(0)) // wait until lo level is achieved on echo pin
54
{
55
double pulseStart = getTime();
56
57
if(waitValue(1)) // wait until hi level is achieved on echo pin - hi level duration is proportional to distance
58
{
59
double pulseEnd = getTime();
60
61
double duration = pulseEnd - pulseStart;
62
double distance = duration * coefficient;
63
64
return distance;
65
}
66
}
67
68
cout << "Measurement error!";
69
70
// Troubleshooting:
71
// - check correctness of pin connection,
72
// - check supply in case of many devices connected,
73
// - check maximal supported distance by rangefinder
74
75
return 0.0 / 0.0;
76
}
77
78
int main ()
79
{
80
if(gpioInitialise() < 0)
81
cout << "PIGPIO initialisation failure!" << endl;
82
83
else
84
{
85
cout << "PIGPIO initialisation success!" << endl;
86
87
initializePins();
88
89
for(int i = 0; i < 100; ++i) // for example: 100 measurements
90
{
91
double distance = detectDistance();
92
93
cout << "Distance: " << distance << "cm" << endl;
94
}
95
96
gpioTerminate();
97
}
98
99
return 0;
100
}
xxxxxxxxxx
1
$ gcc -o program -lpigpio -lpthread -lrt program.cpp
xxxxxxxxxx
1
$ ./program
Note: be sure that program has execution permissions; otherwise run:
xxxxxxxxxx
1$ chmod +x program
xxxxxxxxxx
1
TEMPLATE = app
2
CONFIG += console
3
CONFIG -= qt
4
5
LIBS += -lpigpio -lpthread -lrt
6
7
SOURCES += program.cpp