Revision 1216
spline server and master
branches/encoders/code/behaviors/spline/server/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 |
|
branches/encoders/code/behaviors/spline/server/Makefile | ||
---|---|---|
1 |
all: server.c |
|
2 |
gcc server.c -o server |
|
3 |
|
|
4 |
clean: |
|
5 |
rm server |
branches/encoders/code/behaviors/spline/master/master.c | ||
---|---|---|
1 | 1 |
#include <dragonfly_lib.h> |
2 | 2 |
#include <wireless.h> |
3 |
#include <termios.h> |
|
3 |
//#include <termios.h>
|
|
4 | 4 |
#include <stdio.h> |
5 |
#include <unistd.h> |
|
5 |
//#include <unistd.h>
|
|
6 | 6 |
|
7 | 7 |
#define GROUP 1 |
8 | 8 |
#define TYPE 0 |
9 | 9 |
|
10 |
unsigned char data[4]; |
|
11 |
|
|
10 | 12 |
void packet_receive (char type, int source, unsigned char* packet, int length); |
11 | 13 |
|
12 | 14 |
PacketGroupHandler packetHandler = {GROUP, 0, 0, &packet_receive, 0}; |
13 | 15 |
|
14 | 16 |
void packet_receive (char type, int source, unsigned char* packet, int length) { |
17 |
orb_set_color(YELLOW); |
|
18 |
//send to server |
|
19 |
usb_putc(packet[0]); |
|
20 |
usb_putc(packet[1]); |
|
21 |
usb_putc(packet[2]); |
|
22 |
usb_putc(packet[3]); |
|
15 | 23 |
|
24 |
//Read in from server |
|
25 |
data[0] = usb_getc(); |
|
26 |
data[1] = usb_getc(); |
|
27 |
data[2] = usb_getc(); |
|
28 |
data[3] = usb_getc(); |
|
29 |
|
|
30 |
wl_send_global_packet(GROUP, TYPE, data, 4, 0); |
|
31 |
//orb_set_color(GREEN); |
|
16 | 32 |
} |
17 | 33 |
|
18 | 34 |
int main (void) { |
19 |
dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG | MOTORS | BUZZER); |
|
35 |
//dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG | MOTORS | BUZZER); |
|
36 |
dragonfly_init(ALL_ON); |
|
20 | 37 |
wl_init(); |
21 |
|
|
38 |
lcd_init(); |
|
39 |
lcd_puts("S"); |
|
40 |
|
|
41 |
// orb_set_color(RED); |
|
42 |
|
|
43 |
// wl_register_packet_group(&packetHandler); |
|
44 |
|
|
45 |
// orb_set_color(YELLOW); |
|
46 |
//Read in from server |
|
47 |
// delay_ms(1000); |
|
48 |
// data[0] = usb_getc(); |
|
49 |
//motor1_set(0,200); |
|
50 |
// data[1] = usb_getc(); |
|
51 |
//motor2_set(0,200); |
|
52 |
// data[2] = usb_getc(); |
|
53 |
//motor1_set(1,200); |
|
54 |
// data[3] = usb_getc(); |
|
55 |
wl_register_packet_group(&packetHandler); |
|
56 |
//motor2_set(1,200); |
|
57 |
|
|
58 |
// orb_set_color(PURPLE); |
|
59 |
//packet_receive(TYPE, 0, data, 4); |
|
60 |
wl_send_global_packet(GROUP, TYPE, data, 4, 0); |
|
61 |
// orb_set_color(GREEN); |
|
22 | 62 |
while (1) { |
23 |
char vel[4] = {170, 0, 170, 1}; |
|
24 |
wl_send_global_packet(GROUP, TYPE, vel, 4, 0); |
|
63 |
// char vel[4] = {170, 0, 170, 1};
|
|
64 |
// wl_send_global_packet(GROUP, TYPE, vel, 4, 0);
|
|
25 | 65 |
wl_do(); |
26 | 66 |
} |
27 | 67 |
|
Also available in: Unified diff