Project

General

Profile

Statistics
| Revision:

root / branches / slam / code / projects / colonet / DataRequests / robot / data_response.c @ 393

History | View | Annotate | Download (3.29 KB)

1 393 justin
#include "dragonfly_lib.h"
2 124 jscheine
#include "data_response.h"
3
#include "wl_token_ring.h"
4
#include "wireless.h"
5 294 justin
#include "serial.h"
6 122 jscheine
7 124 jscheine
PacketGroupHandler pgh;
8
9 157 jscheine
void handle_receive(char type, int source, unsigned char* packet, int length);
10 124 jscheine
int generate_bom_packet(char** buf);
11
int generate_IR_packet(char** buf);
12
13 393 justin
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 294 justin
void data_response_init(void){
19
  usb_init();
20
  usb_puts("USB initialized!\n\r");
21 124 jscheine
  range_init();
22 294 justin
  pgh.groupCode=DATA_REQUEST_GROUP;
23 124 jscheine
  pgh.timeout_handler = NULL;
24
  pgh.handle_response = NULL;
25 157 jscheine
  pgh.handle_receive = handle_receive;
26 124 jscheine
  pgh.unregister = NULL;
27 157 jscheine
  wl_register_packet_group(&pgh);
28 294 justin
  usb_puts("Packet Group Registered!\n\r");
29 122 jscheine
}
30 124 jscheine
31 393 justin
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 124 jscheine
void handle_receive(char type, int source, unsigned char* packet, int length){
36 393 justin
37 124 jscheine
  char buf[MAX_PACKET_LENGTH];
38 366 justin
  int size, i;
39 294 justin
  usb_puts("Got a packet!\n\r");
40 124 jscheine
  switch(type){
41
  case BOM_TYPE:
42 393 justin
43
    short int i=0, robot_id;
44
    wl_token_iterator_begin();
45
    while(wl_token_iterator_has_next() && i<=96){
46
      robot_id = wl_token_iterator_next();
47
      (*buf)[i++] = (char)((robot_id>>8) & 255);
48
      (*buf)[i++] = (char)(robot_id & 255);
49
      //TODO Ensure that wl_token_get.... returns the expected char.
50
      (*buf)[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
51
    }
52
53
    //i is one greater than the index of the last byte... the size.
54
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,i,source,0);
55 366 justin
56 124 jscheine
  case IR_TYPE:
57 366 justin
58
    usb_puts("Generating an IR packet.\n");
59
    char ranges[] = {IR1,IR2,IR3,IR4,IR5};
60
    int i;
61
    short ir_value;
62
    for(i=0;i<5;i++){
63
      ir_value = (short)range_read_distance(ranges[i]);
64
      buf[2*i] = (char)((ir_value>>8)&255);
65
      buf[2*i+1] = (ir_value&255);
66
      usb_puts("Distance :\t");
67
      usb_puti(ir_value);
68
      usb_puts("\n");
69
    }
70
    size = 10;
71
72
    usb_puts("Packet Contents: ");
73
    for(i = 0; i<size;i++){
74
        usb_puti(buf[i]);
75
        usb_putc(' ');
76
    }
77 294 justin
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
78 366 justin
79
80
81 124 jscheine
  case ENCODER_TYPE:
82 157 jscheine
    break;
83 124 jscheine
    //Not yet supported.
84
  }
85
}
86
87 393 justin
/**
88
* @brief  Generates a BOM Data packet. **NO LONGER USED** Delete once this code is used often.
89
*/
90 124 jscheine
int generate_bom_packet(char** buf){
91
  short int i=0, robot_id;
92
  wl_token_iterator_begin();
93 294 justin
  while(wl_token_iterator_has_next() && i<=96){
94 124 jscheine
    robot_id = wl_token_iterator_next();
95 157 jscheine
    (*buf)[i++] = (char)((robot_id>>8) & 0xFF);
96
    (*buf)[i++] = (char)(robot_id & 0xFF);
97
    (*buf)[i++] = (char)(wl_token_get_my_sensor_reading(robot_id));
98 124 jscheine
  }
99
  //i is one greater than the index of the last byte... the size.
100
  return i;
101
}
102
103 393 justin
/**
104
* @brief Generates an IR Packet **NO LONGER USED** Delete once this code is used often.
105
*
106
*/
107 124 jscheine
int generate_IR_packet(char** buf){
108 366 justin
  usb_puts("Generating an IR packet.\n");
109
  char ranges[] = {IR1,IR2,IR3,IR4,IR5};
110
  int i;
111
  short ir_value;
112
  for(i=0;i<5;i++){
113
    ir_value = (short)range_read_distance(ranges[i]);
114
    (*buf)[2*i] = (char)((ir_value>>8)&255);
115
    (*buf)[2*i+1] = (ir_value&255);
116
    usb_puts("Distance :\t");
117
    usb_puti(ir_value);
118
    usb_puts("\n");
119
  }
120
  return 10;
121 124 jscheine
}