Project

General

Profile

Statistics
| Revision:

root / branches / slam / code / projects / slam / computer / server_main.c @ 525

History | View | Annotate | Download (5.87 KB)

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

    
8
typedef struct dataNode{
9
  struct timeval* timeStamp;
10
  char type;
11
  union{
12
      BomNode* u_b;
13
      short* u_s;
14
      unsigned char* u_c;
15
  } data;
16
  short robot_id;
17
} Data;
18

    
19
/*
20
This is the game plan if anyone is interested.
21
Each robot will go for an unspecified amount of time,
22
accruing encoder, BOM, and IR data.  The IR and encoders
23
will be used to generate heat maps for each robot, starting
24
at the center, decreasing in certainty (i.e. the amount of
25
'color' put on during a time unit will be spread over a greater
26
area) over time according to the physical limitations of 
27
the measuring devices.
28

29
When the uncertainty gets too high, a new map will be allocated for
30
the robot.
31

32
The BOM data will be used to orient individual maps to one another.
33
Along with physical limitations on the size of the environment, and
34
hopefully some brainstorming to do with triangularization and what not,
35
the heat maps will become increasingly well oriented.
36

37
Finally, after a currently unspecified amount of time, the maps which have
38
been properly oriented (this could probably just be all of them due to the nature
39
of the heat maps,) will be summed up into one meta map! Metamap will undergo some
40
signal processing, probably in the form of a second order gradient, to get a map
41
of magnitudes, which we will try to further reduce to lines.  These lines are the
42
walls of the environment.
43

44
In time, it is possible that this "unspecfied amount of time" could become a continuous
45
computation, but I haven't put much thought into how this will scale.  It will almost 
46
definitely depend upon the chosen resolution of the heat map.
47

48
I do want people working with me, this should be a lot of fun,
49
I'm coming to accept the fact that I'm not a very good programmer,
50
but I think this is a solid method of doing SLAM.
51
*/
52

    
53
/** 
54
* @brief An array of maps.
55
*/
56
#define DEFAULT_WIDTH 50
57
#define DEFAULT_HEIGHT 50
58
//Indexed x,y
59
int* map_array[DEFAULT_WIDTH][DEFAULT_HEIGHT];
60

    
61
/** 
62
* @brief An array of short's indicating which robot each map belongs to.
63
*/
64
short* map_ids;
65

    
66

    
67
/** 
68
* @brief Describes whether a map has been correlated to previous maps with a BOM reading.
69
*/
70
char* map_correlated;
71

    
72
Queue* dataQueue;
73

    
74
/*These functions simply add the data to the queue.*/
75
void bom_handler(short id, BomNode** head);
76
void IR_handler(short id, short** data);
77
void encoder_handler(short id, unsigned char** data);
78

    
79
void bom_analyze(BomNode* head);
80
void IR_analyze(short* data);
81
void encoder_analyze(unsigned char* data);
82

    
83

    
84
void slam_analyze(void){
85
  Data* current_data;
86
  while(1){
87
    if(!queue_is_empty(dataQueue)){
88
      current_data = (Data*)queue_remove(dataQueue);
89
      switch(current_data->type){
90
      
91
      case(BOM_TYPE):
92
        bom_analyze(current_data->data.u_b);
93
        break;
94
      
95
      case(IR_TYPE):
96
        IR_analyze(current_data->data.u_s);
97
        break;
98

    
99
      case(ENCODER_TYPE):
100
        encoder_analyze(current_data->data.u_c);
101
        break;
102
      }
103
    }
104
  }
105
}
106

    
107

    
108
int main(void){
109
  data_requests_init(bom_handler,IR_handler,encoder_handler);
110
  dataQueue = queue_create();
111
  slam_analyze(); 
112
  return -1;
113
}
114

    
115

    
116
void bom_analyze(BomNode* head){
117

    
118
}
119

    
120
void IR_analyze(short* data){
121

    
122
}
123

    
124
void encoder_analyze(unsigned char* data){
125

    
126
}
127

    
128
/*The packet handling functions for data requests.*/
129

    
130
void bom_handler(short id, BomNode** head){
131
  Data* newData = malloc(sizeof(Data));
132
  newData->type = BOM_TYPE;
133
  newData->data.u_b = (*head);
134
  newData->robot_id = id;
135
 
136
  gettimeofday(&newData->timeStamp,NULL);
137

    
138
  queue_add(dataQueue,(void*)(newData));
139
}
140

    
141
void IR_handler(short id, short** data){
142
  Data* newData = malloc(sizeof(Data));
143
  newData->type = IR_TYPE;
144
  newData->data.u_s = (*data);
145
  newData->robot_id = id;
146
  
147
  gettimeofday(&newData->timeStamp,NULL);
148
  
149
  queue_add(dataQueue,(void*)(newData));
150

    
151
}
152

    
153
void encoder_handler(short id, unsigned char** data){
154
  Data* newData = malloc(sizeof(Data));
155
  newData->type = ENCODER_TYPE;
156
  newData->data.u_c = (*data);
157
  newData->robot_id = id;
158
  
159
  gettimeofday(&newData->timeStamp,NULL);
160
  
161
  queue_add(dataQueue,(void*)(newData));
162
}
163

    
164

    
165
/** 
166
* @brief Rotates a map by radians radians.
167
* Weak version...  Uses doubles and a secondary map is probably extra slow.
168
* Can this be done in place?
169
* @param map The map to be rotated.
170
*/
171
void rotate_map(int*** map, double radians){
172
  int i,j;
173
  double iprime, jprime, xoffset, yoffset;
174
  int i2,j2;
175
  int new_map[DEFAULT_WIDTH][DEFAULT_HEIGHT];
176
  
177
  for(i=0;i<DEFAULT_WIDTH;i++){
178
      for(j=0;j<DEFAULT_WIDTH;i++){
179
          new_map[i][j] = 0;
180
      }
181
  } 
182

    
183
  for(i=0;i<DEFAULT_WIDTH;i++){
184
      for(j=0;j<DEFAULT_WIDTH;i++){
185
         iprime = i*cos(radians)-j*sin(radians);
186
         jprime = i*sin(radians)+j*cos(radians);
187
        
188
         //Smear the value over a range near i2,j2 The new values may need to be normalized TODO Figure that out.;
189
         i2 = (int)iprime;
190
         j2 = (int)jprime;
191
         xoffset = iprime - i2;
192
         yoffset = jprime - j2;
193
         
194
         //If the new coordinates are legal...
195
         if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
196
             new_map[i2][j2] += (int)(sqrt(xoffset*xoffset + yoffset*yoffset)*(double)((*map)[i][j]));
197
         
198
         if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
199
             new_map[i2+1][j2] += (int)(sqrt((1-xoffset)*(1-xoffset) + yoffset*yoffset)*(double)((*map)[i][j]));
200
         
201
         if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
202
             new_map[i2][j2+1] += (int)(sqrt(xoffset*xoffset + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]));
203
        
204
        if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
205
             new_map[i2+1][j2+1] += (int)(sqrt((1-xoffset)*(1-xoffset) + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]));
206
      }
207
  }
208
  
209
  //Incompatible types? FIXME
210
  (*map) = new_map;
211
  return;
212
}
213

    
214