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