-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_test
More file actions
250 lines (201 loc) · 6.51 KB
/
final_test
File metadata and controls
250 lines (201 loc) · 6.51 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
#include <wiringPi.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <softPwm.h>
#include <wiringSerial.h>
#define SERVO_PIN 12 // 서보모터 핀
#define SERVO_RANGE 2000 // 서보 모터 PWM 범위
#define DC_START 19 // DC 모터 핀
#define DC_END 18 // DC 모터 핀
#define DC_START_SPEED 100 // 0 ~ 100
#define DC_END_SPEED 30
// 전역 변수 및 조건 변수
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t servo_at_position = PTHREAD_COND_INITIALIZER;
volatile int servo_reached = 0; // 서보 모터가 특정 위치에 도착
volatile float servo_current_angle = 0.0; // 서보 모터의 현재 각도
volatile int card_distributed = 0; // 전체 분배된 카드 수
volatile float *user_positions = NULL; // 사용자 위치
volatile int user_count;
volatile int card_count;
int fd_serial;
unsigned char dat;
#define BAUD_RATE 115200
static const char* UART1_DEV = "/dev/ttyAMA2"; // RPi5: UART1 장치 파일
unsigned char serialRead(const int fd); // 1Byte 데이터를 수신하는 함수
// 1Byte 데이터를 수신하는 함수
unsigned char serialRead(const int fd) {
unsigned char x;
if (read(fd, &x, 1) != 1) // 1바이트 읽기 실패 시 -1 반환
return -1;
return x;
}
// DC 모터: softPWM, 서보 모터: 기존의 하드웨어 PWM
void init_gpio()
{
pinMode(SERVO_PIN, PWM_OUTPUT);
pwmSetMode(PWM_MODE_MS);
pwmSetRange(SERVO_RANGE);
pwmSetClock(192);
// 초기값 0, 최댓값 100
softPwmCreate(DC_START, 0, 100);
softPwmCreate(DC_END, 0, 100);
}
void init_setting()
{
FILE *file = fopen("angles.txt", "r");
if (file == NULL)
{
perror("파일을 열 수 없습니다.");
exit(1);
}
char buffer[1024];
int cnt = 0;
if (fgets(buffer, sizeof(buffer), file) != NULL)
{
char *token = strtok(buffer, ",");
user_count = atoi(token);
user_positions = (float *)malloc(user_count * sizeof(float));
token = strtok(NULL, ",");
while (token != NULL && cnt < user_count)
{
user_positions[cnt] = (float)(90 - atof(token)); // 문자열을 float로 변환
cnt++;
token = strtok(NULL, ",");
}
}
fclose(file);
printf("user count: %d\n", user_count);
for(int i = 0; i < user_count; i++)
{
printf("사용자 %d의 위치: %.2f도\n", i + 1, user_positions[i]);
}
}
// 서보 모터
void *rotate_servo(void *arg)
{
int index = 0;
int direction = 1;
while (card_distributed < user_count * card_count)
{
servo_current_angle = -90.0 + 1.0 * index * direction;
if (servo_current_angle > 90.0 || servo_current_angle < -90.0)
{
direction *= -1;
index = 0;
continue;
}
int duty = 150 + (servo_current_angle * 100 / 90);
if (duty < 50)
duty = 50;
if (duty > 250)
duty = 250;
pwmWrite(SERVO_PIN, duty);
delay(30);
printf("\r[서보 모터] %.2f도 위치로 이동", servo_current_angle);
fflush(stdout);
for (int i = 0; i < user_count; i++)
{
if (fabs(servo_current_angle - user_positions[i]) < 1e-1)
{
printf("\n[서보 모터] 사용자 %d의 위치 (%.2f도)에 도달\n", i + 1, user_positions[i]);
pthread_mutex_lock(&mutex);
servo_reached = i + 1;
pthread_cond_signal(&servo_at_position);
pthread_mutex_unlock(&mutex);
}
}
index++;
}
printf("\n[서보 모터] 종료\n");
return NULL;
}
// DC 모터 동작
void *rotate_dc()
{
int *user_card_count = (int *)calloc(user_count, sizeof(int)); // 각 사용자가 받은 카드 수
while (card_distributed < user_count * card_count)
{
pthread_mutex_lock(&mutex);
while (!servo_reached)
{
pthread_cond_wait(&servo_at_position, &mutex);
}
int user = servo_reached - 1; // 배열에 저장하기 위해 -1
servo_reached = 0; // 다시 0으로 초기화하여 wait
pthread_mutex_unlock(&mutex);
if (user_card_count[user] < card_count)
{
user_card_count[user]++;
card_distributed++;
// softPWM
softPwmWrite(DC_START, DC_START_SPEED);
delay(400);
softPwmWrite(DC_START, 0);
softPwmWrite(DC_END, DC_START_SPEED);
delay(300);
softPwmWrite(DC_END, 0);
softPwmWrite(DC_START, DC_END_SPEED);
delay(30);
softPwmWrite(DC_START, 0);
printf("\n[DC 모터] 사용자 %d에게 카드 %d 분배 완료\n", user + 1, user_card_count[user]);
}
}
free(user_card_count);
return NULL;
}
int return_card_number(){
if ((fd_serial = serialOpen(UART1_DEV, BAUD_RATE)) < 0) {
printf("UART 포트를 열 수 없습니다: %s\n", UART1_DEV);
return 1;
}
while (1) {
if (serialDataAvail(fd_serial)) { // 읽을 데이터가 있는 경우
dat = serialRead(fd_serial); // 1바이트 데이터를 읽음
// 읽은 데이터가 2~6인 경우만 처리
if (dat >= '1' && dat <= '5') {
card_count = dat - '0'; // 아스키 값을 정수로 변환
printf("Input the card number in App: %d\n", card_count);
return card_count;
} else {
printf("무시된 데이터: %c\n", dat);
}
}
delay(10); // 짧은 대기 (10ms)
}
}
// 메인 함수
int main()
{
card_count = return_card_number();
int status = system("./embed/bin/python3 returnvalue.py");
printf("camera start\n");
if(status != 0){
printf("fail");
return 1;
}
if (wiringPiSetupGpio() == -1)
{
printf("GPIO 초기화 실패\n");
return 1;
}
init_gpio();
init_setting();
pthread_t servo_thread, dc_thread;
pthread_create(&servo_thread, NULL, rotate_servo, NULL);
pthread_create(&dc_thread, NULL, rotate_dc, NULL);
pthread_join(servo_thread, NULL);
pthread_join(dc_thread, NULL);
printf("\n[시스템] 모든 카드가 분배되었습니다.\n");
pwmWrite(SERVO_PIN, 0);
delay(1000);
softPwmWrite(DC_START, 0);
softPwmWrite(DC_END, 0);
delay(500);
return 0;
}
//