Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / slam.bak2 / robot / data_response.c @ 721

History | View | Annotate | Download (3.84 KB)

1
#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
/**
33
* Sorry for the lack of modularity, I didn't want to deal with memory issues for too long, plus it'll be faster!
34
*/
35
void handle_receive(char type, int source, unsigned char* packet, int length){
36
  
37
  char buf[MAX_PACKET_LENGTH]; 
38
  int size;
39
  int i=0;
40
  short int robot_id, ir_value; 
41
  char ranges[] = {IR1,IR2,IR3,IR4,IR5}; 
42
  
43
  switch(type){
44
  case BOM_TYPE:
45
    
46
    wl_token_iterator_begin();
47
    while(wl_token_iterator_has_next() && i<=96){
48
      robot_id = wl_token_iterator_next();
49
      buf[i++] = (char)((robot_id>>8) & 255);
50
      buf[i++] = (char)(robot_id & 255);
51
      buf[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
52
    }
53
    
54
    //i is one greater than the index of the last byte... the size.
55
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,i,source,0);
56
    break;
57
  
58
  case IR_TYPE:
59

    
60
    //usb_puts("Generating an IR packet.\n");
61
    for(i=0;i<5;i++){
62
      ir_value = (short)range_read_distance(ranges[i]);
63
      buf[2*i] = (char)((ir_value>>8)&255);   
64
      buf[2*i+1] = (ir_value&255);   
65
      //usb_puts("Distance :\t");
66
      //usb_puti(ir_value);
67
      //usb_puts("\n");
68
    }
69
    size = 10;
70

    
71
    //usb_puts("Packet Contents: ");
72
    /*for(i = 0; i<size;i++){
73
        //usb_puti(buf[i]);
74
        usb_putc(' ');
75
    }*/
76
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
77
  
78
    break; 
79
  
80
  case ENCODER_TYPE:
81
    break; 
82
    //Not yet supported.
83
  }
84
  case ALL_TYPE:
85
    for(i=0;i<5;i++){
86
      ir_value = (short)range_read_distance(ranges[i]);
87
      buf[2*i] = (char)((ir_value>>8)&255);   
88
      buf[2*i+1] = (ir_value&255);   
89
    } //Buf 0...9 filled.
90
    
91
    //Tack on the Encoder data... 
92
    //Then BOM: 
93
    wl_token_iterator_begin();
94
    while(wl_token_iterator_has_next() && i<=96){
95
      robot_id = wl_token_iterator_next();
96
      buf[i++] = (char)((robot_id>>8) & 255);
97
      buf[i++] = (char)(robot_id & 255);
98
      buf[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
99
    }
100
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ALL_TYPE,buf,size,source,0);
101
    break;
102
}
103

    
104
/** 
105
* @brief  Generates a BOM Data packet. **NO LONGER USED** Delete once this code is used often.
106
*/
107
int generate_bom_packet(char** buf){
108
  short int i=0, robot_id; 
109
  wl_token_iterator_begin();
110
  while(wl_token_iterator_has_next() && i<=96){
111
    robot_id = wl_token_iterator_next();
112
    (*buf)[i++] = (char)((robot_id>>8) & 0xFF);
113
    (*buf)[i++] = (char)(robot_id & 0xFF);
114
    (*buf)[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
115
  }
116
  //i is one greater than the index of the last byte... the size.
117
  return i;
118
}
119

    
120
/** 
121
* @brief Generates an IR Packet **NO LONGER USED** Delete once this code is used often.
122
* 
123
*/
124
int generate_IR_packet(char** buf){
125
  //usb_puts("Generating an IR packet.\n");
126
  char ranges[] = {IR1,IR2,IR3,IR4,IR5}; 
127
  int i;
128
  short ir_value; 
129
  for(i=0;i<5;i++){
130
    ir_value = (short)range_read_distance(ranges[i]);
131
    (*buf)[2*i] = (char)((ir_value>>8)&255);   
132
    (*buf)[2*i+1] = (ir_value&255);   
133
    //usb_puts("Distance :\t");
134
    //usb_puti(ir_value);
135
    //usb_puts("\n");
136
  }
137
  return 10;
138
}