Revision 581
wireless library now times out instead of infinitely looping in waiting for ok
trunk/code/projects/colonet/robot/colonet_dragonfly/colonet_dragonfly.c | ||
---|---|---|
172 | 172 |
//usb_puts("after cur_robot_x/y = robot_x/y.\n"); |
173 | 173 |
|
174 | 174 |
|
175 |
int r_x = target_x - cur_robot_x;
|
|
176 |
int r_y = target_y - cur_robot_y;
|
|
175 |
int e_x = target_x - cur_robot_x;
|
|
176 |
int e_y = target_y - cur_robot_y;
|
|
177 | 177 |
|
178 | 178 |
int v_x = cur_robot_x - last_x; |
179 | 179 |
int v_y = cur_robot_y - last_y; |
180 | 180 |
|
181 |
int e_x = r_x - v_x; |
|
182 |
int e_y = r_y - v_y; |
|
183 | 181 |
|
184 |
sprintf(buf, "vx:%d vy:%d rx:%d ry:%d ex:%d ey:%d\n", v_x, v_y, r_x, r_y, e_x, e_y); |
|
182 |
|
|
183 |
sprintf(buf, "vx:%d vy:%d rx:%d ry:%d ex:%d ey:%d\n", v_x, v_y, e_x, e_y, e_x, e_y); |
|
185 | 184 |
usb_puts(buf); |
186 | 185 |
|
187 | 186 |
int e_mag = e_x*e_x + e_y*e_y; |
188 | 187 |
|
189 |
int motor_differential = e_mag;// / 100;
|
|
188 |
int motor_differential = e_mag >> 7;// / 128;
|
|
190 | 189 |
|
191 | 190 |
/* |
192 | 191 |
sprintf(buf, "Current position: %d %d\n", cur_robot_x, cur_robot_y); |
... | ... | |
222 | 221 |
int p_x = v_y; |
223 | 222 |
int p_y = -v_x; |
224 | 223 |
|
225 |
int mat[2][2]; |
|
226 |
mat[0][0] = v_y; |
|
227 |
mat[0][1] = -v_x; |
|
224 |
int e_trans_x = p_x * e_x + p_y * e_y; |
|
228 | 225 |
|
229 |
int e_trans_x = mat[0][0] * e_x + mat[0][1] * e_y; |
|
230 |
|
|
231 | 226 |
/* |
232 | 227 |
// Determine left or right by transforming error vector to robot axes |
233 | 228 |
// Perpendicular |
trunk/code/projects/colonet/server/Main.cpp | ||
---|---|---|
1 | 1 |
#include <stdio.h> |
2 | 2 |
#include <signal.h> |
3 | 3 |
#include <ColonetServer.h> |
4 |
#include <wireless.h> |
|
4 | 5 |
|
5 | 6 |
ColonetServer colonet_server; |
6 | 7 |
|
7 | 8 |
void cleanup(int a) { |
8 |
printf("TODO deinit wireless here\n"); |
|
9 |
//wl_terminate(); |
|
10 |
printf("todo - close wl port\n"); |
|
11 |
|
|
9 | 12 |
exit(0); |
10 | 13 |
} |
11 | 14 |
|
trunk/code/projects/colonet/server/PositionMonitor.cpp | ||
---|---|---|
43 | 43 |
|
44 | 44 |
//TODO: check for error returned |
45 | 45 |
int numPositions = vision_get_robot_positions(&positions); |
46 |
printf("numPositions is %d\n", numPositions); |
|
46 |
/* |
|
47 |
printf("numPositions is %d\n", numPositions); |
|
47 | 48 |
for (int i = 0; i < numPositions; i++) { |
48 | 49 |
printf("{%d,%d} ", positions[i].x, positions[i].y); |
49 | 50 |
} |
50 | 51 |
printf("\n"); |
52 |
*/ |
|
51 | 53 |
|
52 | 54 |
map<int, VisionPosition> newPositionMap; |
53 | 55 |
|
... | ... | |
101 | 103 |
|
102 | 104 |
positionMap = newPositionMap; |
103 | 105 |
|
104 |
printf("\npositionMap size is %d and deleteBufferMap size is %d\n", positionMap.size(), deleteBufferMap.size()); |
|
106 |
// printf("\npositionMap size is %d and deleteBufferMap size is %d\n", positionMap.size(), deleteBufferMap.size());
|
|
105 | 107 |
|
108 |
/* |
|
106 | 109 |
//TODO: remove this debug information |
107 | 110 |
map<int, VisionPosition>::iterator iter; |
108 | 111 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) { |
109 | 112 |
printf("%d has position (%d, %d) with delete buffer %d\n", iter->first, iter->second.x, iter->second.y, deleteBufferMap[iter->first]); |
110 | 113 |
} |
114 |
*/ |
|
111 | 115 |
|
116 |
|
|
112 | 117 |
pthread_mutex_unlock(&position_map_lock); |
113 | 118 |
|
114 | 119 |
if (positions) { |
trunk/code/projects/colonet/Makefile | ||
---|---|---|
11 | 11 |
clean: clean_server clean_client clean_robot |
12 | 12 |
|
13 | 13 |
$(SERVER): $(SERVER_FILES) |
14 |
cd server/vision; make |
|
14 | 15 |
cd server; make |
15 | 16 |
|
16 | 17 |
$(CLIENT): $(CLIENT_FILES) |
trunk/code/projects/libwireless/lib/xbee.c | ||
---|---|---|
55 | 55 |
#include <string.h> |
56 | 56 |
|
57 | 57 |
#define XBEE_FRAME_START 0x7E |
58 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
58 | 59 |
|
59 | 60 |
/*Frame Types*/ |
60 | 61 |
#define XBEE_FRAME_STATUS 0x8A |
... | ... | |
83 | 84 |
static int xbee_exit_command_mode(void); |
84 | 85 |
static int xbee_enter_api_mode(void); |
85 | 86 |
static int xbee_exit_api_mode(void); |
86 |
static void xbee_wait_for_string(char* s, int len);
|
|
87 |
static void xbee_wait_for_ok(void);
|
|
87 |
static int xbee_wait_for_string(char* s, int len);
|
|
88 |
static int xbee_wait_for_ok(void);
|
|
88 | 89 |
|
89 | 90 |
/*API Mode Functions*/ |
90 | 91 |
|
... | ... | |
124 | 125 |
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT; |
125 | 126 |
static int xbee_channel = XBEE_CHANNEL_DEFAULT; |
126 | 127 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT; |
127 |
static unsigned int xbee_address = 0; |
|
128 |
static volatile unsigned int xbee_address = 0;
|
|
128 | 129 |
|
129 | 130 |
/*Function Implementations*/ |
130 | 131 |
|
... | ... | |
207 | 208 |
arrival_buf[0] = 'A'; |
208 | 209 |
arrival_buf[1] = 'A'; |
209 | 210 |
arrival_buf[2] = 'A'; |
210 |
#ifdef ROBOT
|
|
211 |
#ifdef ROBOT |
|
211 | 212 |
|
212 | 213 |
//enable the receiving interrupt |
213 | 214 |
#ifdef FIREFLY |
... | ... | |
223 | 224 |
{ |
224 | 225 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port); |
225 | 226 |
return -1; |
227 |
} else { |
|
228 |
printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port); |
|
226 | 229 |
} |
227 | 230 |
|
228 | 231 |
// set baud rate, etc. correctly |
... | ... | |
246 | 249 |
{ |
247 | 250 |
fprintf(stderr, "Error setting attributes.\n"); |
248 | 251 |
return -1; |
252 |
} else { |
|
253 |
//printf("Successfully set termios attributes.\n"); |
|
249 | 254 |
} |
250 | 255 |
|
251 | 256 |
//lockf(xbee_stream, F_LOCK, 0); |
... | ... | |
262 | 267 |
{ |
263 | 268 |
fprintf(stderr, "Failed to create listener thread.\r\n"); |
264 | 269 |
return -1; |
270 |
} else { |
|
271 |
//printf("Successfully created listener thread.\n"); |
|
265 | 272 |
} |
266 | 273 |
#endif |
267 | 274 |
|
268 | 275 |
if (xbee_enter_command_mode() != 0) { |
276 |
#ifndef ROBOT |
|
277 |
printf("Error returned from xbee_enter_command_mode\n"); |
|
278 |
#endif |
|
269 | 279 |
return -1; |
270 | 280 |
} |
271 | 281 |
|
272 | 282 |
if (xbee_enter_api_mode() != 0) { |
283 |
#ifndef ROBOT |
|
284 |
printf("Error returned from xbee_enter_api_mode\n"); |
|
285 |
#endif |
|
273 | 286 |
return -1; |
274 | 287 |
} |
275 | 288 |
|
276 | 289 |
if (xbee_exit_command_mode() != 0) { |
290 |
#ifndef ROBOT |
|
291 |
printf("Error returned from xbee_exit_command_mode\n"); |
|
292 |
#endif |
|
277 | 293 |
return -1; |
278 | 294 |
} |
279 | 295 |
|
280 | 296 |
if (xbee_send_read_at_command("MY")) { |
297 |
#ifndef ROBOT |
|
298 |
printf("Error returned from xbee_send_read_at_command\n"); |
|
299 |
#endif |
|
281 | 300 |
return -1; |
282 | 301 |
} |
283 | 302 |
|
303 |
#ifndef ROBOT |
|
304 |
//printf("About to enter while loop to get xbee_address.\n"); |
|
305 |
int i; |
|
306 |
for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) { |
|
307 |
ret = xbee_get_packet(NULL); |
|
308 |
|
|
309 |
usleep(1000); |
|
310 |
|
|
311 |
if (ret == -1) { |
|
312 |
printf("xbee_get_packet(NULL) failed.\n"); |
|
313 |
return -1; |
|
314 |
} |
|
315 |
} |
|
316 |
|
|
317 |
// printf("After exiting while loop to get xbee_address.\n"); |
|
318 |
#else |
|
284 | 319 |
//wait to return until the address is set |
285 |
while (xbee_address == 0) xbee_get_packet(NULL); |
|
320 |
while (xbee_address == 0) { |
|
321 |
xbee_get_packet(NULL); |
|
322 |
} |
|
323 |
#endif |
|
286 | 324 |
|
325 |
#ifndef ROBOT |
|
326 |
if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out. |
|
327 |
|
|
328 |
printf("xbee_get_packet timed out.\n"); |
|
329 |
return -1; |
|
330 |
} else { |
|
331 |
return 0; |
|
332 |
} |
|
333 |
#else |
|
287 | 334 |
return 0; |
335 |
#endif |
|
288 | 336 |
} |
289 | 337 |
|
290 | 338 |
/** |
... | ... | |
371 | 419 |
return -1; |
372 | 420 |
} |
373 | 421 |
|
374 |
xbee_wait_for_ok(); |
|
422 |
#ifndef ROBOT |
|
423 |
// printf("In xbee_enter_command_mode about to call xbee_wait_for_ok()\n"); |
|
424 |
#endif |
|
375 | 425 |
|
376 |
return 0; |
|
426 |
if (xbee_wait_for_ok() != 0) { |
|
427 |
#ifndef ROBOT |
|
428 |
printf("xbee_wait_for_ok failed.\n"); |
|
429 |
#endif |
|
430 |
return -1; |
|
431 |
} else { |
|
432 |
return 0; |
|
433 |
} |
|
377 | 434 |
} |
378 | 435 |
|
379 | 436 |
/** |
... | ... | |
414 | 471 |
/** |
415 | 472 |
* Wait until the string "OK\r" is received from the XBee. |
416 | 473 |
**/ |
417 |
static void xbee_wait_for_ok()
|
|
474 |
static int xbee_wait_for_ok()
|
|
418 | 475 |
{ |
419 |
xbee_wait_for_string("OK\r", 3); |
|
476 |
return xbee_wait_for_string("OK\r", 3);
|
|
420 | 477 |
} |
421 | 478 |
|
422 | 479 |
/** |
... | ... | |
426 | 483 |
* @param s the string to receive |
427 | 484 |
* @param len the length of the string |
428 | 485 |
**/ |
429 |
static void xbee_wait_for_string(char* s, int len)
|
|
486 |
static int xbee_wait_for_string(char* s, int len)
|
|
430 | 487 |
{ |
488 |
#ifndef ROBOT |
|
489 |
//printf("In xbee_wait_for_string.\n"); |
|
490 |
#endif |
|
491 |
|
|
431 | 492 |
char* curr = s; |
432 | 493 |
while (curr - s < len) { |
433 | 494 |
// check if buffer is empty |
... | ... | |
440 | 501 |
if (c == *curr) { |
441 | 502 |
curr++; |
442 | 503 |
} else { |
443 |
curr = s; |
|
504 |
#ifndef ROBOT |
|
505 |
return -1; // Computer is less forgiving. |
|
506 |
#else |
|
507 |
curr = s; |
|
508 |
#endif |
|
444 | 509 |
} |
445 |
} |
|
510 |
} // else buffer is empty.
|
|
446 | 511 |
|
447 | 512 |
#ifndef ROBOT |
448 |
usleep(1000);
|
|
513 |
usleep(100); |
|
449 | 514 |
#endif |
450 | 515 |
} |
516 |
|
|
517 |
return 0; |
|
451 | 518 |
} |
452 | 519 |
|
453 | 520 |
/** |
... | ... | |
510 | 577 |
if (xbee_send(prefix, 3) != 0) { |
511 | 578 |
return -1; |
512 | 579 |
} |
513 |
|
|
580 |
|
|
514 | 581 |
if (xbee_send(buf, len) != 0) { |
515 | 582 |
return -1; |
516 | 583 |
} |
... | ... | |
681 | 748 |
if (buffer_first == XBEE_BUFFER_SIZE) |
682 | 749 |
buffer_first = 0; |
683 | 750 |
// check if buffer is empty |
684 |
if (buffer_first == buffer_last) |
|
685 |
return -1; |
|
751 |
if (buffer_first == buffer_last) { |
|
752 |
return 0; |
|
753 |
} |
|
686 | 754 |
} while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
687 | 755 |
|
688 | 756 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
... | ... | |
713 | 781 |
|
714 | 782 |
// check if buffer is empty |
715 | 783 |
if (buffer_first == buffer_last) { |
716 |
return -1;
|
|
784 |
return 0;
|
|
717 | 785 |
} |
718 | 786 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
719 | 787 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
... | ... | |
730 | 798 |
} |
731 | 799 |
|
732 | 800 |
//we will take care of the packet |
733 |
if (xbee_handle_packet(xbee_buf + 3, len)) { |
|
734 |
return -1;
|
|
801 |
if (xbee_handle_packet(xbee_buf + 3, len) != 0) {
|
|
802 |
return 0;
|
|
735 | 803 |
} |
736 | 804 |
|
737 | 805 |
if (dest == NULL) { |
738 |
return -1;
|
|
806 |
return 0;
|
|
739 | 807 |
} |
740 | 808 |
|
741 | 809 |
int i; |
... | ... | |
819 | 887 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
820 | 888 |
{ |
821 | 889 |
// printf("reading xbee_address\n"); |
822 |
|
|
890 |
|
|
823 | 891 |
xbee_address = 0; |
824 | 892 |
int i; |
825 | 893 |
for (i = 0; i < extraLen; i++) { |
... | ... | |
955 | 1023 |
xbee_com_port = port; |
956 | 1024 |
} |
957 | 1025 |
#endif |
958 |
|
trunk/code/projects/libwireless/lib/Makefile | ||
---|---|---|
49 | 49 |
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. |
50 | 50 |
# AVR Studio 4.10 requires dwarf-2. |
51 | 51 |
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. |
52 |
DEBUG =
|
|
52 |
DEBUG = |
|
53 | 53 |
# dwarf-2 |
54 | 54 |
|
55 | 55 |
# List any extra directories to look for include files here. |
trunk/code/projects/libwireless/lib/wireless.c | ||
---|---|---|
51 | 51 |
|
52 | 52 |
/*Function Prototypes*/ |
53 | 53 |
|
54 |
void wl_do_timeout(void); |
|
54 |
static void wl_do_timeout(void);
|
|
55 | 55 |
|
56 | 56 |
//Note: the actual frame sent has group as the first four bits and |
57 | 57 |
//frame as the last four. |
58 |
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame); |
|
58 |
static int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame);
|
|
59 | 59 |
|
60 | 60 |
/*Data Members*/ |
61 | 61 |
|
62 | 62 |
//used to store incoming and outgoing packets |
63 |
unsigned char wl_buf[128]; |
|
63 |
static unsigned char wl_buf[128];
|
|
64 | 64 |
//1 if we have timed out since we last checked, 0 otherwise. |
65 |
int wl_timeout = 0; |
|
65 |
static int wl_timeout = 0;
|
|
66 | 66 |
|
67 |
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS]; |
|
67 |
static PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
|
|
68 | 68 |
|
69 | 69 |
#ifndef ROBOT |
70 | 70 |
|
71 | 71 |
//called when we time out, or receive interrupt |
72 |
void sig_handler(int signo) |
|
72 |
static void sig_handler(int signo)
|
|
73 | 73 |
{ |
74 | 74 |
switch (signo) |
75 | 75 |
{ |
... | ... | |
86 | 86 |
#else |
87 | 87 |
|
88 | 88 |
//called when the timer ticks |
89 |
void timer_handler(void) |
|
89 |
static void timer_handler(void)
|
|
90 | 90 |
{ |
91 | 91 |
wl_timeout = 1; |
92 | 92 |
} |
... | ... | |
106 | 106 |
wl_packet_groups[i] = NULL; |
107 | 107 |
|
108 | 108 |
if (xbee_lib_init() == -1) { |
109 |
return -1;
|
|
109 |
return -1;
|
|
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
//begin timeout timer |
Also available in: Unified diff