Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / slam.bak2 / computer / data_requests.c @ 722

History | View | Annotate | Download (2.99 KB)

1
#include "data_requests.h"
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include "../../libwireless/lib/wireless.h"
5
#include <string.h>
6

    
7
PacketGroupHandler pgh;
8

    
9
void receive_handle(char type, int source, unsigned char* packet, int length);
10

    
11
void (*bom_handle)(short id, BomNode** head);
12
void (*IR_handle)(short id, short** data);
13
void (*encoder_handle)(short id, unsigned char** data);
14

    
15
void data_requests_init(void(*bom_data_handler)(short id,  BomNode** head),
16
                        void(*IR_data_handler)(short id, short** data),
17
                        void(*encoder_data_handler)(short id, unsigned char** data)){
18
  bom_handle = bom_data_handler;
19
  IR_handle = IR_data_handler;
20
  encoder_handle = encoder_data_handler;
21
  
22
  pgh.groupCode = DATA_REQUEST_GROUP;
23
  pgh.timeout_handler = NULL;
24
  pgh.handle_response = NULL;
25
  pgh.handle_receive = receive_handle;
26
  pgh.unregister = NULL;
27
  
28
  wl_register_packet_group(&pgh);
29
}
30

    
31

    
32
void request_bom_data(int robot_id){
33
  wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,NULL,0,robot_id,0);
34
}
35

    
36
void request_IR_data(int robot_id){
37
  wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,NULL,0,robot_id,0);
38
}
39

    
40
void request_encoder_data(int robot_id){
41
  wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ENCODER_TYPE,NULL,0,robot_id,0);
42
}
43

    
44
void request_all(int robot_id){
45
  wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ALL_TYPE,NULL,0,robot_id,0);
46
  
47
}
48

    
49
void receive_handle(char type, int source, unsigned char* packet, int length){
50
  int i=0;
51
  BomNode* head;
52
  switch(type){
53
    
54
    case  BOM_TYPE:
55
      head = malloc(sizeof(BomNode));
56
      BomNode* iter = head;
57

    
58
      while(i<length){
59
        iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); 
60
        i+=2;
61
        iter->value = packet[i++];
62
        if(i<length){
63
            iter->next = malloc(sizeof(BomNode));
64
            iter = iter->next;
65
        }
66
      }
67
      bom_handle(source,&head);
68
      break;
69
    
70
    case IR_TYPE:
71
      /*An IR packet consists of just the 10 bytes representing the IR
72
      values as shorts, in order.*/
73
      if(length != 10) break;
74
      
75
      short* data = malloc(5*sizeof(short));
76
      for(i=0;i<5;i++){
77
        data[i]=  ((((short)packet[2*i])<<8)&0xff00 ) | ( (short)(packet[2*i+1])&255);
78
      }
79

    
80
      IR_handle(source, &data);
81
      break;
82
    
83
    case ENCODER_TYPE:
84
      /*I have no idea yet, unsupported.*/
85
      break;
86

    
87
    case ALL_TYPE:
88
      short* data = malloc(5*sizeof(short));
89
      for(i=0;i<5;i++)
90
        data[i]=  ((((short)packet[2*i])<<8)&0xff00 ) | ( (short)(packet[2*i+1])&255);
91
      
92
      head = malloc(sizeof(BomNode));
93
      BomNode* iter = head;
94

    
95
      while(i<length){
96
        iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); 
97
        i+=2;
98
        iter->value = packet[i++];
99
        if(i<length){
100
            iter->next = malloc(sizeof(BomNode));
101
            iter = iter->next;
102
        }
103
      }
104
      
105
      bom_handle(source,&head);
106
      IR_handle(source, &data);
107
      encoder_handle(source,NULL);
108
      break;
109
  }
110
}
111

    
112