Project

General

Profile

Revision 1224

Added by Rich Hong almost 15 years ago

Parallelized Master and Server

View differences:

branches/encoders/code/behaviors/spline/server/server.c
11 11
#define RD  3
12 12
#define FORWARD 1
13 13
#define BACKWARD 0
14
#define SENDER 1
15
#define RECEIVER 0
14 16

  
15
int main()
17
void setAttrib(int file)
16 18
{
17
	int serialFile = open("/dev/ttyUSB0", O_RDWR);
18
	if(serialFile < 1)
19
	{
20
		printf("Error opening serial\n");
21
		return -1;
22
	}
23
    	
24 19
	struct termios attributes;
25
    	tcgetattr(serialFile, &attributes);
20
    	tcgetattr(file, &attributes);
26 21

  
27 22
	cfsetispeed(&attributes, B115200);
28 23
	cfsetospeed(&attributes, B115200);
......
39 34
//	attributes.c_cc[VTIME] = 50;
40 35

  
41 36

  
42
	if (tcsetattr(serialFile, TCSANOW, &attributes) < 0){
37
	if (tcsetattr(file, TCSANOW, &attributes) < 0){
43 38
		perror("tcsetattr failed");
44 39
		exit(-1);
45 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);
46 55
        
47 56
	unsigned char encoders[4] = {1,1,1,1};
48
///	while(1)
49
//		write(serialFile, encoders, 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
	
50 65
	//Sending velocity as LS LD RS RD
51 66
	int tempCount = 0;
52 67
	
......
62 77
		int count = 0;
63 78
		do
64 79
		{
65
			tcflush(serialFile, TCIOFLUSH);
66
			temp = write(serialFile, encoders + count, 1);	
80
//			tcflush(serialFile, TCIOFLUSH);
81
			temp = write(serialFileOut, encoders + count, 1);	
67 82
			if(temp < 0)
68 83
				perror("Write Error");
69 84
			count += temp;
70 85
			printf("sent: %d\n", count);
71
			usleep(1000);
86
			usleep(100);
72 87
		}while(count < 4);
73 88
		count = 0;
74 89
		printf("READING DATA\n");
75 90
		do
76 91
		{
77
			count += read(serialFile, encoders, 4);
92
			count += read(serialFileIn, encoders, 4);
78 93
		}while(count < 4);
79 94
		
80 95
		int leftEncoder = (encoders[0] << 8) | encoders[1];
branches/encoders/code/behaviors/spline/master/master.c
5 5
//#include <unistd.h>
6 6

  
7 7
#define GROUP 1
8
#define TYPE 0
9
#define ACK 1
8
#define MOTOR 0
9
#define ENCODER 1
10 10

  
11
#define SENDER 1
12
#define RECEIVER 0
13

  
11 14
unsigned char data[4];
12 15

  
13 16
sentCount = 0;
14
char ack = 0;
17
char position = 0;
15 18

  
16 19
void packet_receive (char type, int source, unsigned char* packet, int length);
17 20

  
18 21
PacketGroupHandler packetHandler = {GROUP, 0, 0, &packet_receive, 0};
19 22

  
20 23
void packet_receive (char type, int source, unsigned char* packet, int length) {
21
	//orb_set_color(YELLOW);
22
		
23
	//send to server
24
	//lcd_clear_screen();
25
	//lcd_puts("Writing to server");
26
	usb_putc(packet[0]);
27
	//lcd_puti(1);
28
	usb_putc(packet[1]);
29
	//lcd_puti(2);
30
	usb_putc(packet[2]);
31
	//lcd_puti(3);
32
	usb_putc(packet[3]);
33
	//lcd_puti(4);
34
	//lcd_clear_screen();
35
	//lcd_puts("Reading from server");
36 24

  
37
	//Read in from server
38
	data[0] = usb_getc(); 
39
	//lcd_puti(1);
40
	data[1] = usb_getc();
41
	//lcd_puti(2);
42
	data[2] = usb_getc();
43
	//lcd_puti(3);
44
	data[3] = usb_getc();
45
	//lcd_puti(4);
46
	  
25
	if(type == ENCODER)
26
	{
27
		usb_putc(packet[0]);
28
		usb_putc(packet[1]);
29
		usb_putc(packet[2]);
30
		usb_putc(packet[3]);
31
	}
47 32
	
48
	wl_send_global_packet(GROUP, TYPE, data, 4, 0);
49
		
50
	//lcd_clear_screen();
51
	//lcd_puts("Sent ");
52
	//lcd_puti(sentCount);
53
	//sentCount++;
54
	//orb_set_color(GREEN);
55 33
}
56 34

  
57 35
int main (void) {
58
  	//dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG | MOTORS | BUZZER);
36

  
59 37
	dragonfly_init(ALL_ON);
60 38
	wl_init();
61 39
	lcd_init();
62
	lcd_puts("S");
63 40

  
64
//	orb_set_color(RED);
41
	lcd_puts("STARTING");
42
	position = usb_getc();
43
	lcd_puts("position is ");
44
	lcd_puti(position);
65 45

  
66
//  	wl_register_packet_group(&packetHandler);
67
	
68
  //      orb_set_color(YELLOW);	
69
	//Read in from server
70
//	delay_ms(1000);
71
	lcd_puts("starting init");
72
	data[0] = usb_getc(); 
73
	//motor1_set(0,200);
74
	data[1] = usb_getc();
75
	//motor2_set(0,200);
76
	data[2] = usb_getc();
77
	//motor1_set(1,200);
78
	data[3] = usb_getc();
79
	
80
	lcd_puts("Done with init\n");
81
	wl_register_packet_group(&packetHandler);
82
	//motor2_set(1,200);
83
	
84
//	orb_set_color(PURPLE);
85
	//packet_receive(TYPE, 0, data, 4);
46
	if(position == SENDER)
47
	{
48
		//Read in from server
49
		lcd_puts("starting init");
50
		data[0] = usb_getc(); 
51
		data[1] = usb_getc();
52
		data[2] = usb_getc();
53
		data[3] = usb_getc();
54
		
55
		lcd_puts("Done with init\n");
86 56

  
87
	wl_send_global_packet(GROUP, TYPE, data, 4, 0);
88
	
89
	lcd_clear_screen();
90
	lcd_puts("Sent first wireless Packet");
91
	sentCount++;
92
//	orb_set_color(GREEN);
57
		wl_send_global_packet(GROUP, MOTOR, data, 4, 0);
58
	}
59
	else
60
	{
61
		wl_register_packet_group(&packetHandler);
62
	}
63

  
64
//	lcd_clear_screen();
93 65
  while (1) {
94
	 // char vel[4] = {170, 0, 170, 1};
95
	 // wl_send_global_packet(GROUP, TYPE, vel, 4, 0);	  
66
  if(position == SENDER)
67
  {
68
		data[0] = usb_getc(); 
69
		data[1] = usb_getc();
70
		data[2] = usb_getc();
71
		data[3] = usb_getc();
72
		wl_send_global_packet(GROUP, MOTOR, data, 4, 0);
73
}	
74

  
96 75
	  wl_do();
97 76
  }
98 77
  

Also available in: Unified diff