root / branches / encoders / code / behaviors / spline / server / server.c @ 1224
History | View | Annotate | Download (2.03 KB)
1 |
#include <termios.h> |
---|---|
2 |
#include <stdio.h> |
3 |
#include <stdlib.h> |
4 |
#include <sys/types.h> |
5 |
#include <sys/stat.h> |
6 |
#include <fcntl.h> |
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 |
#define SENDER 1 |
15 |
#define RECEIVER 0 |
16 |
|
17 |
void setAttrib(int file) |
18 |
{ |
19 |
struct termios attributes;
|
20 |
tcgetattr(file, &attributes); |
21 |
|
22 |
cfsetispeed(&attributes, B115200); |
23 |
cfsetospeed(&attributes, B115200); |
24 |
attributes.c_cflag &= ~PARENB; |
25 |
attributes.c_cflag &= ~CSTOPB; |
26 |
attributes.c_cflag &= ~CSIZE; |
27 |
attributes.c_cflag |= CS8; |
28 |
|
29 |
// attributes.c_cflag &= ~ICRNL;
|
30 |
// attributes.c_cflag &= ~OCRNL;
|
31 |
// attributes.c_cflag |= (CLOCAL | CREAD);
|
32 |
// attributes.c_lflag &= ~ICANON;
|
33 |
// attributes.c_cc[VMIN] = 1;
|
34 |
// attributes.c_cc[VTIME] = 50;
|
35 |
|
36 |
|
37 |
if (tcsetattr(file, TCSANOW, &attributes) < 0){ |
38 |
perror("tcsetattr failed");
|
39 |
exit(-1);
|
40 |
} |
41 |
} |
42 |
|
43 |
int main()
|
44 |
{ |
45 |
int serialFileIn = open("/dev/ttyUSB0", O_RDWR); |
46 |
int serialFileOut = open("/dev/ttyUSB1", O_RDWR); |
47 |
if(serialFileIn < 1 || serialFileOut < 1) |
48 |
{ |
49 |
printf("Error opening serial\n");
|
50 |
return -1; |
51 |
} |
52 |
|
53 |
setAttrib(serialFileOut); |
54 |
setAttrib(serialFileIn); |
55 |
|
56 |
unsigned char encoders[4] = {1,1,1,1}; |
57 |
|
58 |
char senderNum = SENDER;
|
59 |
char receiverNum = RECEIVER;
|
60 |
|
61 |
write(serialFileIn, &receiverNum, 1);
|
62 |
sleep(1);
|
63 |
write(serialFileOut, &senderNum, 1);
|
64 |
|
65 |
//Sending velocity as LS LD RS RD
|
66 |
int tempCount = 0; |
67 |
|
68 |
while(1) |
69 |
{ |
70 |
printf("SENDING DATA\n");
|
71 |
encoders[LS] = tempCount; |
72 |
encoders[LD] = 1;
|
73 |
encoders[RS] = tempCount; |
74 |
encoders[RD] = 0;
|
75 |
tempCount += 50;
|
76 |
int temp = 0; |
77 |
int count = 0; |
78 |
do
|
79 |
{ |
80 |
// tcflush(serialFile, TCIOFLUSH);
|
81 |
temp = write(serialFileOut, encoders + count, 1);
|
82 |
if(temp < 0) |
83 |
perror("Write Error");
|
84 |
count += temp; |
85 |
printf("sent: %d\n", count);
|
86 |
usleep(100);
|
87 |
}while(count < 4); |
88 |
count = 0;
|
89 |
printf("READING DATA\n");
|
90 |
do
|
91 |
{ |
92 |
count += read(serialFileIn, encoders, 4);
|
93 |
}while(count < 4); |
94 |
|
95 |
int leftEncoder = (encoders[0] << 8) | encoders[1]; |
96 |
int rightEncoder = (encoders[2] << 8) | encoders[3]; |
97 |
printf("%d %d\n", leftEncoder, rightEncoder);
|
98 |
|
99 |
} |
100 |
|
101 |
} |