Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.88 KB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/time.h>
4
#include "data_requests.h"
5
#include "queue.h"
6
#include "math.h"
7

    
8
#define DEFAULT_WIDTH 512
9
#define DEFAULT_HEIGHT 512
10
#define MAX_MAPS 30
11

    
12
#define MAX_ID 20
13

    
14
typedef struct dataNode{
15
  struct timeval* timeStamp;
16
  char type;
17
  union{
18
      BomNode* u_b;
19
      short* u_s;
20
      unsigned char* u_c;
21
  } data;
22
  short robot_id;
23
} Data;
24

    
25
/** 
26
* Map oriented data.
27
*/
28

    
29
typedef struct Map{
30
  int data[DEFAULT_WIDTH][DEFAULT_HEIGHT];
31
  int robot_x;
32
  int robot_y;
33
  double robot_orientation;
34
  short robot_id;
35
  char correlation_group;  
36
} Map;
37

    
38
Map** map_array;
39
int map_count;
40

    
41
/**Robot oriented data.**/
42
Queue* dataQueue;
43
int robot_map[MAX_ID];
44

    
45
/*These functions simply add the data to the queue.*/
46
void bom_handler(short id, BomNode** head);
47
void IR_handler(short id, short** data);
48
void encoder_handler(short id, unsigned char** data);
49

    
50
void slam_analyze(void);
51
void bom_analyze(BomNode* head,short robot_id);
52
void IR_analyze(short* data, short robot_id);
53
void encoder_analyze(unsigned char* data, short robot_id);
54

    
55
void initialize(void);
56

    
57
int main(void){
58
  data_requests_init(bom_handler,IR_handler,encoder_handler);
59
  dataQueue = queue_create();
60
  initialize();
61
  slam_analyze(); 
62
  return -1;
63
}
64

    
65
void initialize(void){
66
  map_array = malloc(MAX_MAPS * sizeof(Map*));
67
  map_count = 0;
68
  int i;
69
  for(i=0; i<MAX_ID;i++){
70
    robot_map[i] = -1;
71
  }
72
}
73

    
74
void slam_analyze(void){
75
  Data* current_data;
76
  while(1){
77
    if(!queue_is_empty(dataQueue)){
78
      if(queue_size(dataQueue) < 5){
79
        //request_all(/*The robot that most needs it*/);
80
      }
81
      current_data = (Data*)queue_remove(dataQueue);
82
      switch(current_data->type){
83
      
84
      case(BOM_TYPE):
85
        bom_analyze(current_data->data.u_b,current_data->robot_id);
86
        break;
87
      
88
      case(IR_TYPE):
89
        IR_analyze(current_data->data.u_s,current_data->robot_id);
90
        break;
91

    
92
      case(ENCODER_TYPE):
93
        encoder_analyze(current_data->data.u_c,current_data->robot_id);
94
        break;
95
      }
96
    }
97
  }
98
}
99

    
100
void initialize_map(int robot_id){
101
    int i,j;
102
    map_array[map_count] = (Map*)malloc(sizeof(Map));
103
    for(i=0;i<DEFAULT_WIDTH;i++){
104
        for(j=0;j<DEFAULT_HEIGHT;j++){
105
          map_array[map_count]->data[i][j] = 0;
106
        }
107
    }
108
    map_array[map_count]->robot_x = DEFAULT_WIDTH / 2;
109
    map_array[map_count]->robot_y = DEFAULT_HEIGHT/ 2;
110
    map_array[map_count]->robot_orientation = 0;
111
    map_array[map_count]->robot_id = robot_id;
112
    map_array[map_count]->correlation_group = -1;
113
    robot_map[robot_id] = map_count++;
114
}
115

    
116
void bom_analyze(BomNode* head, short robot_id){
117
  BomNode* current_node = head;
118
  if(robot_map[robot_id]==-1) initialize_map(robot_id);
119
      
120
  while(current_node!=NULL){
121
    
122
    current_node = current_node->next;
123
  }
124
}
125

    
126
void IR_analyze(short* data, short robot_id){
127
  if(robot_map[robot_id]==-1) initialize_map(robot_id);
128

    
129
}
130

    
131
void encoder_analyze(unsigned char* data, short robot_id){
132
  if(robot_map[robot_id]==-1) initialize_map(robot_id);
133

    
134
}
135

    
136
/*The packet handling functions for nata requests.*/
137

    
138
void bom_handler(short id, BomNode** head){
139
  Data* newData = malloc(sizeof(Data));
140
  newData->type = BOM_TYPE;
141
  newData->data.u_b = (*head);
142
  newData->robot_id = id;
143
  newData->timeStamp = malloc(sizeof(struct timeval));
144
 
145
  gettimeofday(&newData->timeStamp,NULL);
146

    
147
  queue_add(dataQueue,(void*)(newData));
148
}
149

    
150
void IR_handler(short id, short** data){
151
  Data* newData = malloc(sizeof(Data));
152
  newData->type = IR_TYPE;
153
  newData->data.u_s = (*data);
154
  newData->robot_id = id;
155
  
156
  gettimeofday(&newData->timeStamp,NULL);
157
  
158
  queue_add(dataQueue,(void*)(newData));
159

    
160
}
161

    
162
void encoder_handler(short id, unsigned char** data){
163
  Data* newData = malloc(sizeof(Data));
164
  newData->type = ENCODER_TYPE;
165
  newData->data.u_c = (*data);
166
  newData->robot_id = id;
167
  
168
  gettimeofday(&newData->timeStamp,NULL);
169
  
170
  queue_add(dataQueue,(void*)(newData));
171
}
172

    
173

    
174
/** 
175
* @brief Rotates a map by radians radians.
176
* Weak version...  Uses doubles and a secondary map is probably extra slow.
177
* Can this be done in place?
178
* @param map The map to be rotated.
179
*/
180
void rotate_map(int*** map, double radians){
181
  int i,j;
182
  double iprime, jprime, xoffset, yoffset, R1, R2, R3, R4, A;
183
  int i2,j2;
184
  int** new_map;
185
  new_map = malloc(DEFAULT_WIDTH*sizeof(int*)); 
186
  for(i=0;i<DEFAULT_WIDTH;i++){
187
      *(new_map+i) = malloc(DEFAULT_HEIGHT * sizeof(int));
188
      for(j=0;j<DEFAULT_WIDTH;i++){
189
          new_map[i][j] = 0;
190
      }
191
  } 
192

    
193
  for(i=0;i<DEFAULT_WIDTH;i++){
194
      for(j=0;j<DEFAULT_WIDTH;i++){
195
         iprime = i*cos(radians)-j*sin(radians);
196
         jprime = i*sin(radians)+j*cos(radians);
197
        
198
         //Smear the value over a range near i2,j2 The new values may need to be normalized TODO Figure that out.;
199
         i2 = (int)iprime;
200
         j2 = (int)jprime;
201
         xoffset = iprime - i2;
202
         yoffset = jprime - j2;
203
         
204
         R1 = sqrt(xoffset*xoffset + yoffset*yoffset)*(double)((*map)[i][j]);
205
         R2 = sqrt((1-xoffset)*(1-xoffset) + yoffset*yoffset)*(double)((*map)[i][j]);
206
         R3 = sqrt(xoffset*xoffset + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]);
207
         R4 = sqrt((1-xoffset)*(1-xoffset) + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]);
208
         A = 1/(R1+R2+R3+R4); 
209
         
210
         //If the new coordinates are legal...
211
         if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
212
             new_map[i2][j2] += (int)((R1*A)*(double)((*map)[i][j]));
213
         if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
214
             new_map[i2+1][j2] += (int)((R2*A)*(double)((*map)[i][j]));
215
         if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
216
             new_map[i2][j2+1] += (int)((R3*A)*(double)((*map)[i][j]));
217
         if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
218
             new_map[i2+1][j2+1] += (int)((R4*A)*(double)((*map)[i][j]));
219
      }
220
  }
221
  
222
  (*map) = new_map;
223
  return;
224
}
225

    
226