forked from GreenAppleSoda/Metaverse_IoT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartFan.c
More file actions
400 lines (331 loc) · 11.5 KB
/
SmartFan.c
File metadata and controls
400 lines (331 loc) · 11.5 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <wiringPi.h>
#include <softPwm.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wiringSerial.h>
// GPIO 핀 설정 (BCM 핀 번호)
#define PWM0 18 // GPIO18
#define PWM1 19 // GPIO19
#define LED1 16 // GPIO16
#define LED2 20 // GPIO20
#define LED3 21 // GPIO21
#define TRIG 23 // GPIO23
#define ECHO 24 // GPIO24
#define BUZZER 26 // GPIO26
#define SERVO 13 // GPIO13
// UART 블루투스 설정
#define BAUD_RATE 115200 //블루투스의 보율이 바뀔 경우 이 값을 변경해야함
static const char* UART1_DEV = "/dev/ttyAMA1"; //RPi5: UART1 연결을 위한 장치 파일
// UART 유선 설정
#define BAUD_RATE_UART 9600 // 유선 UART 보율
static const char* UART2_DEV = "/dev/ttyAMA2"; // 유선 UART 장치 파일
// 상태 변수
volatile int turn_enabled = 0; // 서보모터 회전 활성화
volatile int current_position = 0; // 서보모터 현재 위치
// Mutex
pthread_mutex_t mutex;
void initPWM(int gpio1, int gpio2, int gpio3) {
pinMode(gpio1, PWM_OUTPUT);
pinMode(gpio2, PWM_OUTPUT);
pinMode(gpio3, PWM_OUTPUT); // 하드웨어 PWM 사용
// PWM 모드를 마크-스페이스 모드로 설정
pwmSetMode(PWM_MODE_MS);
// PWM 범위 설정 (주파수 = 19.2 MHz / (pwmClock * pwmRange))
pwmSetClock(384); // PWM 클럭 분주 설정 (19.2 MHz / 192 = 100 kHz)
pwmSetRange(1000); // PWM 범위 설정 (100 kHz / 1000 = 100 Hz)
// 초기값 설정
pwmWrite(gpio1, 0);
pwmWrite(gpio2, 0);
pwmWrite(gpio3, 0);
}
//여러 바이트의 데이터를 씀
void serialWriteBytes (const int fd, const char *s)
{
write (fd, s, strlen (s)) ;
}
//1바이트 데이터를 읽음
unsigned char serialRead(const int fd)
{
unsigned char x;
if(read (fd, &x, 1) != 1) //read 함수를 통해 1바이트 읽어옴
return -1;
return x; //읽어온 데이터 반환
}
//1바이트 데이터를 씀
void serialWrite(const int fd, const unsigned char c)
{
write (fd, &c, 1); //write 함수를 통해 1바이트 씀
}
// LED 상태 업데이트
void update_leds(int speed) {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
if (speed > 0 && speed <= 30) {
digitalWrite(LED1, HIGH);
} else if (speed > 30 && speed <= 70) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
} else if (speed > 70) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
}
}
// 모터 회전
// void motor_rotate(int speed, int direction) {
// if (direction == 0) { // CW
// softPwmWrite(PWM0, 0);
// softPwmWrite(PWM1, speed);
// } else if (direction == 1) { // CCW
// softPwmWrite(PWM1, 0);
// softPwmWrite(PWM0, speed);
// }
// }
// 모터 회전 함수
void motor_rotate(int speed, int direction) {
if (speed < 0 || speed > 100) {
printf("속도는 0 ~ 100 사이로 설정해야 합니다.\n");
return;
}
// 속도를 PWM 값으로 변환 (0 ~ 100 -> 0 ~ 1000)
int pwm_value = speed * 5;
if (direction == 0) { // CW (시계 방향)
pwmWrite(PWM0, 0);
pwmWrite(PWM1, pwm_value);
printf("시계 방향으로 %d%% 속도로 회전합니다.\n", speed);
} else if (direction == 1) { // CCW (반시계 방향)
pwmWrite(PWM0, pwm_value);
pwmWrite(PWM1, 0);
printf("반시계 방향으로 %d%% 속도로 회전합니다.\n", speed);
} else {
printf("방향은 0 (CW) 또는 1 (CCW)로 설정해야 합니다.\n");
}
if (speed == 0 && direction == 0)
{
turn_enabled = 0;
}
}
// 서보모터 제어
void *servo_control(void *arg) {
int positions[] = {-45, 0, 45, 0}; // 각도 배열
//int positions[] = {-90, 90}; // 각도 배열
int index = 0;
while (1) {
pthread_mutex_lock(&mutex);
if (turn_enabled) {
current_position = positions[index];
pthread_mutex_unlock(&mutex);
// 각도를 PWM 신호로 변환 (-90도: 25, 0도: 75, 90도: 125)
int duty_cycle = (int)((current_position + 90) * 50 / 90 + 25);
pwmWrite(SERVO, duty_cycle); // 하드웨어 PWM 출력
printf("Servo moved to position: %d (duty: %d)\n", current_position, duty_cycle);
index = (index + 1) % 4; // 다음 위치로 이동
//index = (index + 1) % 2; // 다음 위치로 이동
sleep(1);
} else {
pthread_mutex_unlock(&mutex);
usleep(100000);
}
}
return NULL;
}
// 초음파 센서 제어
void *ultrasonic_sensor(void *arg) {
int uart_fd; // 유선 UART 파일 디스크립터
unsigned char dat;
char buffer[100];
buffer[0] = '0';
buffer[1] = ',';
buffer[2] = '5';
buffer[3] = '\0';
// 유선 UART 포트 열기
uart_fd = serialOpen(UART2_DEV, BAUD_RATE_UART);
if (uart_fd < 0) {
fprintf(stderr, "Unable to open UART device: %s\n", UART2_DEV);
return NULL;
}
while (1) {
digitalWrite(TRIG, HIGH);
usleep(10);
digitalWrite(TRIG, LOW);
while (digitalRead(ECHO) == LOW);
long start_time = micros();
while (digitalRead(ECHO) == HIGH);
long travel_time = micros() - start_time;
float distance = travel_time / 58.0; // 거리 계산
printf("Distance: %.1f cm\n", distance);
pthread_mutex_lock(&mutex);
if (distance <= 17.9) {
digitalWrite(BUZZER, HIGH);
pwmWrite(PWM0, 0);
pwmWrite(PWM1, 0);
turn_enabled = 0;
// UART를 통해 경고 메시지 전송
serialWriteBytes(uart_fd, buffer);
serialWriteBytes(uart_fd, "\n");
} else {
digitalWrite(BUZZER, LOW);
}
pthread_mutex_unlock(&mutex);
usleep(200000);
}
close(uart_fd);
return NULL;
}
// 문자열 양옆의 공백 및 줄 바꿈 문자를 제거하는 함수
void trim(char *str) {
int len = strlen(str);
int start = 0;
int end = len - 1;
// 앞쪽 공백 제거
while (start < len && (str[start] == ' ' || str[start] == '\n' || str[start] == '\r')) {
start++;
}
// 뒤쪽 공백 제거
while (end >= start && (str[end] == ' ' || str[end] == '\n' || str[end] == '\r')) {
end--;
}
// 잘린 문자열을 새로운 문자열로 변환
memmove(str, str + start, end - start + 1);
str[end - start + 1] = '\0'; // null terminator 추가
}
// 블루투스 명령 수신 처리
void *bluetooth_listener(void *arg) {
int fd_serial; //블루투스
int uart_fd; //유선
unsigned char dat;
char buffer[100];
// 시리얼 포트 열기
if ((fd_serial = serialOpen(UART1_DEV, BAUD_RATE)) < 0) {
printf("Unable to open serial device.\n");
return NULL;
}
// UART 포트 열기 (유선 UART)
if ((uart_fd = serialOpen(UART2_DEV, BAUD_RATE_UART)) < 0) {
fprintf(stderr, "Unable to open UART device: %s\n", UART2_DEV);
return NULL;
}
while (1) {
if (serialDataAvail(fd_serial)) {
int i = 0;
while (serialDataAvail(fd_serial) && i < sizeof(buffer)) {
dat = serialRead(fd_serial);
buffer[i++] = dat;
}
buffer[i] = '\0'; // 문자열 종료
// 문자열 트리밍
trim(buffer);
printf("Received data: '%s'\n", buffer); // 받은 데이터 출력
// 클라이언트로 회신
serialWriteBytes(fd_serial, "Echo: ");
serialWriteBytes(fd_serial, buffer);
serialWriteBytes(fd_serial, "\n");
// 유선 UART로 회신
//serialWriteBytes(uart_fd, "From. SmartPhone : ");
serialWriteBytes(uart_fd, buffer);
serialWriteBytes(uart_fd, "\n");
pthread_mutex_lock(&mutex);
// 명령 처리
if (strcasecmp(buffer, "0,1000") == 0) {
turn_enabled = 1;
printf("Servo rotation started.\n");
} else if (strcasecmp(buffer, "0,2000") == 0) {
turn_enabled = 0;
printf("Servo rotation stopped.\n");
}
else {
int speed, direction;
if (sscanf(buffer, "%d,%d", &direction, &speed) == 2) {
if (speed >= 0 && speed <= 100 && (direction == 0 || direction == 1)) {
motor_rotate(speed, direction);
update_leds(speed);
} else {
printf("Invalid speed or direction values.\n");
}
} else {
printf("Invalid command format.\n");
}
}
pthread_mutex_unlock(&mutex);
}
if (serialDataAvail(uart_fd)) {
int i = 0;
while (serialDataAvail(uart_fd) && i < sizeof(buffer)) {
dat = serialRead(uart_fd);
buffer[i++] = dat;
}
buffer[i] = '\0'; // 문자열 종료
// 문자열 트리밍
trim(buffer);
printf("Received data: '%s'\n", buffer); // 받은 데이터 출력
// 클라이언트로 회신
serialWriteBytes(fd_serial, "Echo: ");
serialWriteBytes(fd_serial, buffer);
serialWriteBytes(fd_serial, "\n");
// 유선 UART로 회신
//serialWriteBytes(uart_fd, "From. SmartPhone : ");
//serialWriteBytes(uart_fd, buffer);
//serialWriteBytes(uart_fd, "\n");
pthread_mutex_lock(&mutex);
// 명령 처리
if (strcasecmp(buffer, "0,1000") == 0) {
turn_enabled = 1;
printf("Servo rotation started.\n");
} else if (strcasecmp(buffer, "0,2000") == 0) {
turn_enabled = 0;
printf("Servo rotation stopped.\n");
} else {
int speed, direction;
if (sscanf(buffer, "%d,%d", &direction, &speed) == 2) {
if (speed >= 0 && speed <= 100 && (direction == 0)) {
motor_rotate(speed, direction);
update_leds(speed);
} else {
printf("Invalid speed or direction values.\n");
}
} else {
printf("Invalid command format.\n");
}
}
pthread_mutex_unlock(&mutex);
}
else {
usleep(100000); // 데이터가 없으면 잠시 대기
}
}
close(fd_serial);
close(uart_fd);
return NULL;
}
int main() {
// GPIO 초기화
wiringPiSetupGpio();
// Mutex 초기화
pthread_mutex_init(&mutex, NULL);
// 핀 설정
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BUZZER, OUTPUT);
initPWM(PWM0, PWM1, SERVO);
// softPwmCreate(PWM0, 0, 100);
// softPwmCreate(PWM1, 0, 100);
// 스레드 생성
pthread_t servo_thread, ultrasonic_thread, bluetooth_thread;
pthread_create(&servo_thread, NULL, servo_control, NULL);
pthread_create(&ultrasonic_thread, NULL, ultrasonic_sensor, NULL);
pthread_create(&bluetooth_thread, NULL, bluetooth_listener, NULL);
// 메인 루프
while (1) {
sleep(1);
}
// 자원 해제
pthread_mutex_destroy(&mutex);
return 0;
}