Project

General

Profile

Statistics
| Revision:

root / branches / encoders / code / behaviors / spline / server / server.c @ 1241

History | View | Annotate | Download (2.85 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
#include <math.h>
9

    
10
#define LS  0
11
#define LD  1
12
#define RS  2
13
#define RD  3
14
#define FORWARD 1
15
#define BACKWARD 0
16
#define SENDER 1
17
#define RECEIVER 0  
18

    
19
#define INFINITE -1
20
#define L 10.0
21

    
22
double position[3] = {0,0,0};
23

    
24
void updatePosition(int vl, int vr, double dt);
25

    
26
void setAttrib(int file)
27
{
28
        struct termios attributes;
29
            tcgetattr(file, &attributes);
30

    
31
        cfsetispeed(&attributes, B115200);
32
        cfsetospeed(&attributes, B115200);
33
        attributes.c_cflag &= ~PARENB;
34
        attributes.c_cflag &= ~CSTOPB;
35
        attributes.c_cflag &= ~CSIZE;
36
        attributes.c_cflag |= CS8;
37
        
38
        attributes.c_cflag &= ~ICRNL;
39
        attributes.c_cflag &= ~OCRNL;
40
        attributes.c_cflag |= (CLOCAL | CREAD);
41
        attributes.c_lflag &= ~ICANON;
42

    
43

    
44
        if (tcsetattr(file, TCSANOW, &attributes) < 0){
45
                perror("tcsetattr failed");
46
                exit(-1);
47
        }
48
}
49

    
50
int main()
51
{
52
        int serialFileIn = open("/dev/ttyUSB0", O_RDWR);
53
        int serialFileOut = open("/dev/ttyUSB1", O_RDWR);
54
        if(serialFileIn < 1 || serialFileOut < 1)
55
        {
56
                printf("Error opening serial\n");
57
                return -1;
58
        }
59
            
60
        setAttrib(serialFileOut);
61
        setAttrib(serialFileIn);
62
        
63
        unsigned char encoders[4] = {1,1,1,1};
64
        
65
        char senderNum = SENDER;
66
        char receiverNum = RECEIVER;
67
        
68
        write(serialFileIn, &receiverNum, 1);
69
        sleep(1);
70
        write(serialFileOut, &senderNum, 1);
71
        
72
        //Sending velocity as LS LD RS RD
73
        int tempCount = 190;
74

    
75
        int dx, left_v;
76
        
77
        while(1)
78
        {
79
                printf("SENDING DATA\n");
80
                encoders[LS] = tempCount;
81
                encoders[LD] = 1;
82
                encoders[RS] = tempCount;
83
                encoders[RD] = 0;
84
                //tempCount += 50;
85
                int temp = 0;
86
                int count = 0;
87
                do
88
                {
89
//                        tcflush(serialFile, TCIOFLUSH);
90
                        temp = write(serialFileOut, encoders + count, 1);        
91
                        if(temp < 0)
92
                                perror("Write Error");
93
                        count += temp;
94
                        printf("sent: %d\n", count);
95
                        usleep(200);
96
                }while(count < 4);
97
                count = 0;
98
                printf("READING DATA\n");
99
                do
100
                {
101
                        count += read(serialFileIn, encoders, 4);
102
                }while(count < 4);
103
                
104
                short leftEncoder = (encoders[0] << 8) | encoders[1];
105
                short rightEncoder = (encoders[2] << 8) | encoders[3];
106
                printf("%d %d\n", leftEncoder, rightEncoder);
107

    
108
                updatePosition(leftEncoder, rightEncoder, .1);
109
                printf("Location is x: %g \t y: %g \t o: %g\n", position[0], position[1], position[2]);
110

    
111
        }
112

    
113
}
114

    
115
void updatePosition(int vl, int vr, double dt)
116
{
117
           double wdt = (vr - vl)*dt/L;
118
           double R;           
119

    
120
           if(vr - vl > 0)
121
                   R = (L/2)*(vr + vl)/(vr - vl);
122
    
123
        if(wdt != 0)
124
        {
125
                double ICC[2] = {position[0] - R * sin(position[2]), position[1] + R*cos(position[2])};
126
                position[0] = cos(wdt)*(position[0]-ICC[0]) - sin(wdt)*(position[1]-ICC[1]) + ICC[0];
127
                position[1] = sin(wdt)*(position[0]-ICC[0]) + cos(wdt)*(position[1]-ICC[1]) + ICC[1];
128
                position[2] = position[2] + wdt;  
129
        }
130
        else
131
        {
132
                   position[0] += cos(position[2])*vr;
133
                position[1] += sin(position[2])*vr;
134
        }
135
}