Revision 1216
spline server and master
server.c | ||
---|---|---|
5 | 5 |
#include <sys/stat.h> |
6 | 6 |
#include <fcntl.h> |
7 | 7 |
|
8 |
#define LS 0 |
|
9 |
#define LD 1 |
|
10 |
#define RS 2 |
|
11 |
#define RD 3 |
|
12 |
#define FORWARD 1 |
|
13 |
#define BACKWARD 0 |
|
14 |
|
|
8 | 15 |
int main() |
9 | 16 |
{ |
10 |
int serialFile = open("/dev/ttyUSB0", O_RDONLY);
|
|
17 |
int serialFile = open("/dev/ttyUSB0", O_RDWR);
|
|
11 | 18 |
if(serialFile < 1) |
12 | 19 |
{ |
13 | 20 |
printf("Error opening serial\n"); |
14 | 21 |
return -1; |
15 | 22 |
} |
16 |
struct termios attributes; |
|
17 |
tcgetattr(serialFile, &attributes); |
|
23 |
|
|
24 |
struct termios attributes; |
|
25 |
tcgetattr(serialFile, &attributes); |
|
18 | 26 |
|
19 |
cfsetispeed(&attributes, 115200); |
|
20 |
cfsetospeed(&attributes, 115200); |
|
21 |
tcsetattr(serialFile, TCSANOW, &attributes); |
|
22 |
|
|
23 |
int encoders[2]; |
|
27 |
cfsetispeed(&attributes, B115200); |
|
28 |
cfsetospeed(&attributes, B115200); |
|
29 |
attributes.c_cflag &= ~PARENB; |
|
30 |
attributes.c_cflag &= ~CSTOPB; |
|
31 |
attributes.c_cflag &= ~CSIZE; |
|
32 |
attributes.c_cflag |= CS8; |
|
33 |
|
|
34 |
// attributes.c_cflag &= ~ICRNL; |
|
35 |
// attributes.c_cflag &= ~OCRNL; |
|
36 |
// attributes.c_cflag |= (CLOCAL | CREAD); |
|
37 |
// attributes.c_lflag &= ~ICANON; |
|
38 |
// attributes.c_cc[VMIN] = 1; |
|
39 |
// attributes.c_cc[VTIME] = 50; |
|
40 |
|
|
41 |
|
|
42 |
if (tcsetattr(serialFile, TCSANOW, &attributes) < 0){ |
|
43 |
perror("tcsetattr failed"); |
|
44 |
exit(-1); |
|
45 |
} |
|
46 |
|
|
47 |
unsigned char encoders[4] = {1,1,1,1}; |
|
48 |
/// while(1) |
|
49 |
// write(serialFile, encoders, 1); |
|
50 |
//Sending velocity as LS LD RS RD |
|
24 | 51 |
while(1) |
25 | 52 |
{ |
26 |
read(serialFile, encoders, sizeof(encoders)); |
|
27 |
printf("%d %d\n", encoders[0], encoders[1]); |
|
53 |
printf("SENDING DATA\n"); |
|
54 |
encoders[LS] = 170; |
|
55 |
encoders[LD] = 1; |
|
56 |
encoders[RS] = 170; |
|
57 |
encoders[RD] = 1; |
|
58 |
int temp = 0; |
|
59 |
int count = 0; |
|
60 |
do |
|
61 |
{ |
|
62 |
// tcflush(serialFile, TCIOFLUSH |
|
63 |
temp = write(serialFile, encoders + count, 1); |
|
64 |
if(temp < 0) |
|
65 |
perror("Write Error"); |
|
66 |
count += temp; |
|
67 |
// tcflush(serialFile, TCIOFLUSH); |
|
68 |
usleep(10000); |
|
69 |
}while(count < 4); |
|
70 |
count = 0; |
|
71 |
printf("READING DATA\n"); |
|
72 |
do |
|
73 |
{ |
|
74 |
count += read(serialFile, encoders, 4); |
|
75 |
}while(count < 4); |
|
76 |
|
|
77 |
int leftEncoder = (encoders[0] << 8) | encoders[1]; |
|
78 |
int rightEncoder = (encoders[2] << 8) | encoders[3]; |
|
79 |
printf("%d %d\n", leftEncoder, rightEncoder); |
|
80 |
|
|
28 | 81 |
} |
29 | 82 |
|
30 | 83 |
} |
31 |
|
Also available in: Unified diff