/
ultrasonic.c
156 lines (127 loc) · 3 KB
/
ultrasonic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
This one is to make ultrasonic working example.
Input: GPIO8(pin24)
Output: GPIO7(pin26)
3V3 5V
pin3: 5V
pin5: GND
pin7: pifm pin8:
GND pin10:
pin11: pin12: motor1
pin13: motor2 GND
pin15: motor3 pin16: motor4
3V3 pin18:
pin19: GND
pin21: pin22: OneWire(previous for DS18B20)
pin23: pin24: ultrasonic trig1
GND pin26: ultrasonic echo1
Outline:
Send a pulse to activate the sensor, and wait for a time to receive the signal.
use Distance = Speed_of_sound * timediff.
You need the wiringPi lib.
Compile:
gcc -lwiringPi $(this).c
Run:
sudo ./$(this.out)
Thanks the following persons developed the libs which this project used.
wiringPi lib from: Gordons Projects @ https://projects.drogon.net/raspberry-pi/wiringpi/
This project is created by @DaochenShi (shidaochen@live.com)
20150421 created.
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h> // for SIG_IGN etc.
#include <errno.h> // for errno
#define TRIG_GPIO_PIN 10
#define ECHO_GPIO_PIN 11
#define DEBUG
void break_program(int sig);
void myInterrupt(void);
static volatile int loopingStatus = 0;
static volatile struct timespec prevT;
static volatile struct timespec nowT;
int main(int argc, const char* argv[])
{
int result = 1;
// run this program in background when not in Debug mode
#ifndef DEBUG
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
// change file mode mask
umask(0);
// open logs,,, but I did not record any logs
// create SID for child process
sid = setsid();
if (sid < 0)
{
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
//close(STDERR_FILENO);
}
#endif
signal(SIGINT, break_program);
// First to setup wiringPi
if (wiringPiSetup() < 0)
{
fprintf(stderr, "Unable to setup wiringPi: %s \n", strerror(errno));
return 1; //exit(1);
}
pinMode(TRIG_GPIO_PIN, OUTPUT);
pinMode(ECHO_GPIO_PIN, INPUT);
pullUpDnControl(ECHO_GPIO_PIN, PUD_DOWN);
if (wiringPiISR (ECHO_GPIO_PIN, INT_EDGE_BOTH, &myInterrupt) < 0)
{
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}
loopingStatus = 1;
#ifdef DEBUG
printf("All initialized...\n");
#endif
while(loopingStatus)
{
#ifdef DEBUG
printf("waiting for signal...\n");
#endif
digitalWrite(TRIG_GPIO_PIN, LOW);
delayMicroseconds(10);
digitalWrite(TRIG_GPIO_PIN, HIGH);
usleep(10);
digitalWrite(TRIG_GPIO_PIN, LOW);
sleep(5);
}
return 0;
}
// Accept Ctrl+C command, this seems not work when main process is forked.
void break_program(int sig)
{
signal(sig, SIG_IGN);
loopingStatus = 0;
printf("Program end.\n");
signal(sig, SIG_DFL);
}
// Get the interrupt if we touched the button.
void myInterrupt()
{
long long diffns = 0;
gettimeofday(&nowT, NULL);
diffns = nowT.tv_nsec - prevT.tv_nsec;
diffns += (nowT.tv_sec - prevT.tv_sec)*1000000000;
if ( !digitalRead(ECHO_GPIO_PIN) )
printf("detected!%f m\n", diffns*340.0/2/1000000);
prevT = nowT;
}