Project

General

Profile

Revision 395

Added the data response code to the slam project folder

View differences:

branches/slam/code/projects/slam/robot/data_response.c
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, i;
39
  usb_puts("Got a packet!\n\r");
40
  switch(type){
41
  case BOM_TYPE:
42
    
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
  
56
  case IR_TYPE:
57

  
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
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
78
  
79
  
80
  
81
  case ENCODER_TYPE:
82
    break; 
83
    //Not yet supported.
84
  }
85
}
86

  
87
/** 
88
* @brief  Generates a BOM Data packet. **NO LONGER USED** Delete once this code is used often.
89
*/
90
int generate_bom_packet(char** buf){
91
  short int i=0, robot_id; 
92
  wl_token_iterator_begin();
93
  while(wl_token_iterator_has_next() && i<=96){
94
    robot_id = wl_token_iterator_next();
95
    (*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
  }
99
  //i is one greater than the index of the last byte... the size.
100
  return i;
101
}
102

  
103
/** 
104
* @brief Generates an IR Packet **NO LONGER USED** Delete once this code is used often.
105
* 
106
*/
107
int generate_IR_packet(char** buf){
108
  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
}
branches/slam/code/projects/slam/robot/data_response.h
1
#ifndef _DATA_RESPONSE_H
2
#define _DATA_REQUESTS_H
3

  
4
#define DATA_REQUEST_GROUP 7
5
#define BOM_TYPE 0
6
#define IR_TYPE 1
7
#define ENCODER_TYPE 2
8

  
9
#define MAX_PACKET_LENGTH 100
10

  
11
/* This requires the wireless library
12
and the token ring to already be initialized.*/
13
void data_response_init(void);
14

  
15
#endif
branches/slam/code/projects/slam/computer/data_requests.c
1
#include "data_requests.h"
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <pthread.h>
5
#include "../../../libwireless/lib/wireless.h"
6
#include <string.h>
7

  
8
PacketGroupHandler pgh;
9
pthread_t* dr_thread;
10

  
11
void receive_handle(char type, int source, unsigned char* packet, int length);
12

  
13
void (*bom_handle)(BomNode** head);
14
void (*IR_handle)(short** data);
15
void (*encoder_handle)(unsigned char** data);
16

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

  
33

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

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

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

  
46
/*void* pthread_handle(void* arg){
47
  RawData* data = (RawData*)arg;
48
  unsigned char* packet = data->packet;
49
  int length = data->length;
50
  char type = data->type;
51
  int i=0;
52
  BomNode* head;
53
  switch(type){
54
    
55
    case  BOM_TYPE:
56
      //BomNode* head;
57
      head = malloc(sizeof(BomNode));
58
      
59
      BomNode* iter = head;
60

  
61
      while(i<length){
62
        iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); 
63
        i+=2;
64
        iter->value = packet[i++];
65
        if(i<length){
66
            iter-> next = malloc(sizeof(BomNode));
67
            iter = iter->next;
68
        }
69
      }
70
      bom_handle(head);
71
      break;
72
    
73
    case IR_TYPE:
74
      An IR packet consists of just the 5 bytes representing the IR
75
      values, in order.
76
      if(length != 10) break;
77
      short data[10];
78
      for(i=0;i<5;i++){
79
        data[i] = ((short)packet[2*i]<<8)+((short)packet[2*i+1]);
80
      }
81
      IR_handle(data);
82
      break;
83
    
84
    case ENCODER_TYPE:
85
      /*I have no idea, unsupported.
86
      break;
87
  }
88
  free(data->packet);
89
  free(data);
90
  return NULL;
91
  
92
}*/
93

  
94

  
95
void receive_handle(char type, int source, unsigned char* packet, int length){
96
  int i=0;
97
  BomNode* head;
98
  printf("Packet contents: [ ");
99
  for(i=0;i<length;i++){
100
    printf("%i ",packet[i]);
101
  }
102
  switch(type){
103
    
104
    case  BOM_TYPE:
105
      printf("I shouldn't be here during testing\n");
106
      head = malloc(sizeof(BomNode));
107
      BomNode* iter = head;
108

  
109
      while(i<length){
110
        iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); 
111
        i+=2;
112
        iter->value = packet[i++];
113
        if(i<length){
114
            iter-> next = malloc(sizeof(BomNode));
115
            iter = iter->next;
116
        }
117
      }
118
      bom_handle(head);
119
      break;
120
    
121
    case IR_TYPE:
122
      /*An IR packet consists of just the 10 bytes representing the IR
123
      values as shorts, in order.*/
124
      if(length != 10) break;
125
      short* data = malloc(5*sizeof(short));
126
      for(i=0;i<5;i++){
127
        data[i]=  ((((short)packet[2*i])<<8)&0xff00 ) | ( (short)(packet[2*i+1])&255);
128
      }
129

  
130
      IR_handle(&data);
131
      break;
132
    
133
    case ENCODER_TYPE:
134
      /*I have no idea, unsupported.*/
135
      break;
136
  }
137
}
138

  
139

  
140
/*void receive_handle(char type, int source, unsigned char* packet, int length){
141
  
142
  RawData* data = malloc(sizeof(RawData));
143
  if(!data){
144
    return;
145
  }
146
  
147
  unsigned char* newPacket = malloc(length);
148
  memcpy(newPacket, packet,length);
149
 
150
  data->packet=newPacket;
151
  data->source=source;
152
  data->length=length;
153
  data->type=type;
154
  
155
  dr_thread = malloc(sizeof(pthread_t));  
156
  pthread_create(dr_thread,NULL,pthread_handle,(void*)data);
157
}*/
branches/slam/code/projects/slam/computer/data_requests.h
1
#define DATA_REQUEST_GROUP 7
2
#define BOM_TYPE 0
3
#define IR_TYPE 1
4
#define ENCODER_TYPE 2
5

  
6
typedef struct {
7
  short int robot_id;
8
  char value;
9
  struct BomNode* next;
10
} BomNode;
11

  
12
typedef struct{
13
    unsigned char* packet;
14
    char type;
15
    int source;
16
    int length;
17
} RawData;
18

  
19
void data_requests_init(
20
                        void(*bom_data_handler)(BomNode** head),
21
                        void(*IR_data_handler)(short** data),
22
                        void(*encoder_data_handler)(unsigned char** data));
23

  
24
void request_bom_data(int robot_id);
25
void request_IR_data(int robot_id);
26
void request_encoder_data(int robot_id);
27

  

Also available in: Unified diff