-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_arm.c
95 lines (76 loc) · 1.52 KB
/
robot_arm.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "serial.h"
static unsigned int get_distance(int fd)
{
int err;
unsigned char buf[2] = { 0 };
send_c(fd, 0x55);
err = recv_s(fd, buf, 2);
if (err < 0) {
printf("send_s() ret %d\n", err);
return -1;
}
return ((buf[0] << 8) + buf[1]) / 10;
}
static int is_say_hello_to_me(int fd)
{
unsigned int distance = 0;
clear_serial(fd);
distance = get_distance(fd);
clear_serial(fd);
if (distance > 20 && distance < 70) {
distance = get_distance(fd);
clear_serial(fd);
if (distance > 20 && distance < 70) {
distance = get_distance(fd);
clear_serial(fd);
if (distance > 20 && distance < 70)
return 1;
}
}
return 0;
}
static int is_touch_me(int fd)
{
unsigned int distance = 0;
clear_serial(fd);
distance = get_distance(fd);
clear_serial(fd);
if (distance > 2 && distance < 30) {
distance = get_distance(fd);
clear_serial(fd);
if (distance > 2 && distance < 30) {
distance = get_distance(fd);
clear_serial(fd);
if (distance > 2 && distance < 30)
return 1;
}
}
return 0;
}
int main(void)
{
int fd, err;
fd = serial_init("/dev/ttyS1", 9600, 8, 1, 'N');
if (fd < 0) {
printf("serial_init() ret %d\n", fd);
return -1;
}
err = set_serial_opt(fd, 5, 2);
if (err < 0) {
printf("set_serial_opt() ret %d\n", err);
return -2;
}
for (;;)
{
if (is_say_hello_to_me(fd))
system("/usr/bin/swing.sh");
if (is_touch_me(fd))
system("/usr/bin/handshake.sh");
usleep(200000);
}
serial_free(fd);
return 0;
}