This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thispark.c
170 lines (138 loc) · 3.52 KB
/
thispark.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <wiringPi.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#define PINECHO 21
#define PINTRIG 22
#define PINBEWEGUNG 23
#define PINTON 25
#define ROT 0
#define GELB 2
#define GREEN 3
void *threadOne();
void *threadTwo();
void *threadThree();
void *threadFour();
sem_t semA, semB, messdatenZugriff, semC, semD;
double distance = 0;
int main() {
static pthread_t threads[4] = {0};
sem_init(&semA,0,0);
sem_init(&semB,0,0);
sem_init(&messdatenZugriff,0,0);
sem_init(&semC,0,0);
sem_init(&semD,0,0);
sem_post(&messdatenZugriff);
wiringPiSetup();
pinMode(ROT, OUTPUT);
pinMode(GELB, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(PINTRIG, OUTPUT);
pinMode(PINECHO, INPUT);
pinMode(PINBEWEGUNG, INPUT);
pullUpDnControl(PINTRIG,PUD_OFF);
pullUpDnControl(PINECHO,PUD_OFF);
pullUpDnControl(23,PUD_OFF);
softToneCreate(25);
distance = 5;
pthread_create(&threads[0], NULL, threadOne, NULL);
pthread_create(&threads[1], NULL, threadTwo, NULL);
pthread_create(&threads[2], NULL, threadThree, NULL);
pthread_create(&threads[3], NULL, threadFour, NULL);
sleep(15);
pthread_cancel(threads[0]);
pthread_cancel(threads[1]);
pthread_cancel(threads[2]);
pthread_cancel(threads[3]);
digitalWrite(ROT, 0);
digitalWrite(GELB, 0);
digitalWrite(GREEN, 0);
digitalWrite(PINECHO, 0);
digitalWrite(PINTRIG, 0);
digitalWrite(23, 0);
softToneWrite(PINTON,0);
sem_destroy(&semA);
sem_destroy(&semB);
sem_destroy(&messdatenZugriff);
sem_destroy(&semC);
sem_destroy(&semD);
return 0;
}
void *threadOne() {
while (1) {
if (digitalRead(23) == 1) {
sem_post(&semB);
sem_wait(&semA);
sem_wait(&semA);
}
}
}
void *threadTwo() {
double myvar = 0.0;
double timediff;
struct timeval start, ende;
double sec, usec;
while (1) {
sem_wait(&semB);
//Messung der Daten
digitalWrite(PINTRIG, 1);
delay(10);
digitalWrite(PINTRIG, 0);
if (gettimeofday(&start,(struct timezone*)0)) {
printf("error\n");
exit(1);
}
while (digitalRead(PINECHO) == 0) {
if (gettimeofday(&start,(struct timezone *)0)) {
printf("error\n");
exit(1);
}
}
while (digitalRead(PINECHO) == 1) {
if (gettimeofday(&ende, (struct timezone *)0)) {
printf("error\n");
exit(1);
}
}
sec = ende.tv_sec - start.tv_sec;
usec = ende.tv_usec - start.tv_usec;
timediff =(double) sec + (double) (usec / 1000000.0);
myvar = (timediff * 34300) / 2;
distance = myvar;
sem_post(&semC);
sem_post(&semD);
}
}
void *threadThree() {
double myvar = 0;
while (1) {
sem_wait(&semC);
myvar = distance;
digitalWrite(ROT,0);
digitalWrite(GELB,0);
digitalWrite(GREEN,0);
if (myvar < 10)
digitalWrite(ROT,1);
else if (myvar < 60)
digitalWrite(GELB,1);
else
digitalWrite(GREEN,1);
sem_post(&semA);
}
}
void *threadFour() {
double myvar;
while (1) {
sem_wait(&semD);
myvar = distance;
if (myvar < 10) {
softToneWrite(PINTON, 100);
delay(300);
softToneWrite(PINTON, 0);
}
sem_post(&semA);
}
}