Revision 908
remote control of robot and data output for matlab
trunk/code/projects/mapping/server/test.c | ||
---|---|---|
1 |
#include <stdlib.h> |
|
2 |
#include <stdio.h> |
|
3 |
#include <curses.h> |
|
4 |
#include "../../libwireless/lib/wireless.h" |
|
5 |
|
|
6 |
#define WL_CNTL_GROUP 4 |
|
7 |
|
|
8 |
#define CNTL_FORWARD 0 |
|
9 |
#define CNTL_BACK 1 |
|
10 |
#define CNTL_LEFT 2 |
|
11 |
#define CNTL_RIGHT 3 |
|
12 |
#define CNTL_LEFT_CURVE 4 |
|
13 |
#define CNTL_RIGHT_CURVE 5 |
|
14 |
#define CNTL_STOP 6 |
|
15 |
#define CNTL_VEL_UP 7 |
|
16 |
#define CNTL_VEL_DOWN 8 |
|
17 |
|
|
18 |
void send_packet (char color, int dst_robot); |
|
19 |
void packet_receive(char type, int source, unsigned char* packet, int length); |
|
20 |
|
|
21 |
PacketGroupHandler cntlHandler = {WL_CNTL_GROUP, NULL, NULL, NULL, NULL}; |
|
22 |
PacketGroupHandler receiveHandler = {1, NULL, NULL, &packet_receive, NULL}; |
|
23 |
|
|
24 |
FILE *file; |
|
25 |
|
|
26 |
int main(int argc, char *argv[]) { |
|
27 |
if (argc != 2) { |
|
28 |
printf("Usage: ./test <robot #>\n"); |
|
29 |
return 1; |
|
30 |
} |
|
31 |
|
|
32 |
char c; |
|
33 |
int robot = atoi(argv[1]); |
|
34 |
|
|
35 |
file = fopen("data_test.txt", "w"); |
|
36 |
if (file == NULL) { |
|
37 |
printf("fopen error\n"); |
|
38 |
return 1; |
|
39 |
} |
|
40 |
initscr(); |
|
41 |
printf("Beginning: robot=%d\r\n", robot); |
|
42 |
wl_set_com_port("/dev/ttyUSB0"); |
|
43 |
wl_init(); |
|
44 |
wl_set_channel(0xF); |
|
45 |
printf("Wireless initialized.\r\n"); |
|
46 |
wl_register_packet_group(&cntlHandler); |
|
47 |
wl_register_packet_group(&receiveHandler); |
|
48 |
printf("Packet groups initialized.\r\n"); |
|
49 |
fflush(stdout); |
|
50 |
//while (1); |
|
51 |
while (1) { |
|
52 |
wl_do(); |
|
53 |
c = getch(); |
|
54 |
printf("%c\r\n", c); |
|
55 |
fflush(stdout); |
|
56 |
if (c=='0') |
|
57 |
break; |
|
58 |
switch (c) { |
|
59 |
case 'w': |
|
60 |
send_packet(CNTL_FORWARD, robot); |
|
61 |
break; |
|
62 |
case 's': |
|
63 |
send_packet(CNTL_BACK, robot); |
|
64 |
break; |
|
65 |
case 'a': |
|
66 |
send_packet(CNTL_LEFT, robot); |
|
67 |
break; |
|
68 |
case 'd': |
|
69 |
send_packet(CNTL_RIGHT, robot); |
|
70 |
break; |
|
71 |
case 'q': |
|
72 |
send_packet(CNTL_LEFT_CURVE, robot); |
|
73 |
break; |
|
74 |
case 'e': |
|
75 |
send_packet(CNTL_RIGHT_CURVE, robot); |
|
76 |
break; |
|
77 |
case 'x': |
|
78 |
send_packet(CNTL_STOP, robot); |
|
79 |
break; |
|
80 |
case ']': |
|
81 |
send_packet(CNTL_VEL_UP, robot); |
|
82 |
break; |
|
83 |
case '[': |
|
84 |
send_packet(CNTL_VEL_DOWN, robot); |
|
85 |
break; |
|
86 |
default: |
|
87 |
//send_packet(CNTL_STOP, robot); |
|
88 |
break; |
|
89 |
} |
|
90 |
} |
|
91 |
fclose(file); |
|
92 |
return 0; |
|
93 |
} |
|
94 |
|
|
95 |
void send_packet (char type, int dst_robot) { |
|
96 |
wl_send_robot_to_robot_global_packet(WL_CNTL_GROUP, type, NULL, 0, dst_robot, 0); |
|
97 |
//wl_send_global_packet(WL_CNTL_GROUP, type, NULL, 0, 0); |
|
98 |
} |
|
99 |
|
|
100 |
void packet_receive (char type, int source, unsigned char* packet, int length) { |
|
101 |
fprintf(file, "received packet: "); |
|
102 |
int i, temp; |
|
103 |
for (i = 0; i < length; i += 2) { |
|
104 |
temp = packet[i+1] << 8; |
|
105 |
temp &= 0xFF00; |
|
106 |
temp |= packet[i]; |
|
107 |
fprintf(file, "%d, ", temp); |
|
108 |
} |
|
109 |
fprintf(file, "\n"); |
|
110 |
} |
trunk/code/projects/mapping/server/Makefile | ||
---|---|---|
1 |
CC = gcc |
|
2 |
CFLAGS = -Wall -g |
|
3 |
|
|
4 |
all: test |
|
5 |
|
|
6 |
test: *.c ../../libwireless/lib/*.c |
|
7 |
$(CC) $(CFLAGS) *.c -L../../libwireless/lib -o test -lwireless -lpthread -lncurses |
|
8 |
|
|
9 |
clean: |
|
10 |
rm *.o test ../lib/*.o |
|
11 |
|
Also available in: Unified diff