root / branches / analog / code / projects / mapping / drive / data_response.c @ 1390
History | View | Annotate | Download (2.58 KB)
1 | 722 | justin | #include "dragonfly_lib.h" |
---|---|---|---|
2 | #include "data_response.h" |
||
3 | #include "wl_token_ring.h" |
||
4 | #include "wireless.h" |
||
5 | #include "serial.h" |
||
6 | |||
7 | PacketGroupHandler pgh; |
||
8 | |||
9 | void handle_receive(char type, int source, unsigned char* packet, int length); |
||
10 | int generate_bom_packet(char** buf); |
||
11 | int generate_IR_packet(char** buf); |
||
12 | |||
13 | |||
14 | /**
|
||
15 | * @brief The only function that should be called by user code.
|
||
16 | Prepares the robot to receive and respond to data requests.
|
||
17 | */
|
||
18 | void data_response_init(void){ |
||
19 | //usb_init();
|
||
20 | //usb_puts("USB initialized!\n\r");
|
||
21 | range_init(); |
||
22 | pgh.groupCode=DATA_REQUEST_GROUP; |
||
23 | pgh.timeout_handler = NULL;
|
||
24 | pgh.handle_response = NULL;
|
||
25 | pgh.handle_receive = handle_receive; |
||
26 | pgh.unregister = NULL;
|
||
27 | wl_register_packet_group(&pgh); |
||
28 | //usb_puts("Packet Group Registered!\n\r");
|
||
29 | } |
||
30 | |||
31 | |||
32 | void handle_receive(char type, int source, unsigned char* packet, int length){ |
||
33 | |||
34 | char buf[MAX_PACKET_LENGTH];
|
||
35 | int size;
|
||
36 | int i=0; |
||
37 | short int robot_id, ir_value; |
||
38 | char ranges[] = {IR1,IR2,IR3,IR4,IR5};
|
||
39 | |||
40 | switch(type){
|
||
41 | case BOM_TYPE:
|
||
42 | |||
43 | wl_token_iterator_begin(); |
||
44 | while(wl_token_iterator_has_next() && i<=96){ |
||
45 | robot_id = wl_token_iterator_next(); |
||
46 | buf[i++] = (char)((robot_id>>8) & 255); |
||
47 | buf[i++] = (char)(robot_id & 255); |
||
48 | buf[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
|
||
49 | } |
||
50 | |||
51 | //i is one greater than the index of the last byte... the size.
|
||
52 | wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,i,source,0);
|
||
53 | break;
|
||
54 | |||
55 | case IR_TYPE:
|
||
56 | |||
57 | for(i=0;i<5;i++){ |
||
58 | ir_value = (short)range_read_distance(ranges[i]);
|
||
59 | buf[2*i] = (char)((ir_value>>8)&255); |
||
60 | buf[2*i+1] = (ir_value&255); |
||
61 | } |
||
62 | size = 10;
|
||
63 | |||
64 | //usb_puts("Packet Contents: ");
|
||
65 | /*for(i = 0; i<size;i++){
|
||
66 | //usb_puti(buf[i]);
|
||
67 | usb_putc(' ');
|
||
68 | }*/
|
||
69 | wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
|
||
70 | |||
71 | break;
|
||
72 | |||
73 | case ENCODER_TYPE:
|
||
74 | break;
|
||
75 | //Not yet supported.
|
||
76 | 904 | justin | |
77 | 722 | justin | case ALL_TYPE:
|
78 | for(i=0;i<5;i++){ |
||
79 | ir_value = (short)range_read_distance(ranges[i]);
|
||
80 | buf[2*i] = (char)((ir_value>>8)&255); |
||
81 | buf[2*i+1] = (ir_value&255); |
||
82 | } //Buf 0...9 filled.
|
||
83 | 904 | justin | |
84 | 722 | justin | //Tack on the Encoder data...
|
85 | //Then BOM:
|
||
86 | wl_token_iterator_begin(); |
||
87 | while(wl_token_iterator_has_next() && i<=96){ |
||
88 | robot_id = wl_token_iterator_next(); |
||
89 | buf[i++] = (char)((robot_id>>8) & 255); |
||
90 | buf[i++] = (char)(robot_id & 255); |
||
91 | buf[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
|
||
92 | } |
||
93 | wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ALL_TYPE,buf,size,source,0);
|
||
94 | break;
|
||
95 | } |
||
96 | } |
||
97 |