Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / slam / computer_main.c @ 71

History | View | Annotate | Download (2.73 KB)

1
#include <stdio.h>
2
#include <wireless.h>
3
#include <unistd.h>
4

    
5
#define SLAM_PACKET_GROUP 11
6
#define CONTROL_PACKET_TYPE 'C'
7
#define REQUEST_PACKET_TYPE 'R'
8
#define ROBOT_RESPONSE 'I'
9

    
10
#define MAX_ROBOTS 50 
11

    
12
void handle_timeout(void);
13
void handle_response(int frame, int received);
14
void handle_receive(char type, int source, unsigned char* packet, int length);
15
void unregister(void);
16
void RobotData* getRobot(int id);
17

    
18
typedef struct bom_node{
19
  char value;
20
  short int id;
21
  BomNode* next;
22
} BomNode;
23

    
24
//If at some point we have more than 50 robots, this will fail.
25
typedef struct robot_data{
26
  int id; 
27
  unsigned char IR[5]; 
28
  RobotData* next;
29
  BomNode* bom_head;
30
} RobotData;
31

    
32
RobotData* head_bot;
33
RobotData* current_bot;
34

    
35
int main(void)
36
{
37
  PacketGroupHandler* pgh = malloc(sizeof(PacketGroupHandler)); 
38
  pgh->groupCode = SLAM_PACKET_GROUP;
39
  pgh->timeout_handler = handle_timeout;
40
  pgh->handle_response = handle_response;
41
  pgh->handle_receive = handle_receive;
42
  pgh->unregister = unregister;
43

    
44
  
45
  wl_init();
46
  wl_register_packet_group(pgh);
47
  wl_token_ring_register();
48
  wl_token_ring_join();
49
  wl_token_ring_set_bom_functions(NULL,NULL,NULL);
50

    
51
  while(1){
52
    wl_do();
53
    wl_token_iterator_begin() 
54
    
55
    //Iterate through the robots in the token ring. 
56
    while(wl_token_iterator_has_next()){
57
      wl_send_robot_to_robot_packet(SLAM_PACKET_GROUP,REQUEST_PACKET_TYPE,
58
        NULL,0,wl_token_iterator_next(),0);   
59
      usleep(50000); 
60
    }
61
  }
62
        return 0;
63
}
64

    
65
void handle_timeout(void){}
66
void handle_response(int frame, int received){}
67

    
68
void handle_receive(char type, int source, unsigned char* packet, int length){
69
  if(type == ROBOT_RESPONSE){
70
    int i; 
71
    current_bot = getRobot(source);
72
    if(current_bot == NULL){
73
      //Add the new robot to the front of the list.
74
      current_bot = malloc(sizeof(RobotData));
75
      current_bot->next = head_bot;
76
      head_bot = current_bot;
77
    }
78
    for(i=0;i<5;i++)
79
      current_bot->IR[i] = packet[i];
80
    i++; //i=5 
81
   
82
    //Bom Handling.
83
    //Clear the current information.
84
    BomNode* head = current_bot->bom_head;
85
    if(head!=NULL){
86
      BomNode* next = head-next;
87
      while(next!=NULL){
88
        free(head);
89
        head = next;
90
        next = head->next;
91
      }
92
      free(head);
93
    }
94
    //All of the memory allocated for the BomList is free.
95

    
96
    //The remainder of the packet is bom data. 
97
    //TODO Finish this shit.
98
    BomNode* bom_node = malloc(sizeof(BomNode));
99
    current_bot->bom_head = bom_node;
100
    while(i<length){
101
      bom_node->id = (short int)packet[i];
102
      i+=2;
103
      bom_node->value = packet[i];
104
      i++;
105
    }
106
  }
107
}
108

    
109
RobotData* getRobot(int id){
110
  RobotData* next = head_bot; 
111
  while(next->id != id && next->next!=NULL);
112
  
113
  if(next->id != id) return NULL;
114
  
115
  return next;
116
}
117