1 |
1 |
#include <stdio.h>
|
2 |
2 |
#include <stdlib.h>
|
3 |
|
#include <time.h>
|
|
3 |
#include <sys/time.h>
|
4 |
4 |
#include "data_requests.h"
|
5 |
5 |
#include "queue.h"
|
6 |
6 |
#include "math.h"
|
7 |
7 |
|
8 |
|
#define DEFAULT_WIDTH 50
|
9 |
|
#define DEFAULT_HEIGHT 50
|
|
8 |
#define DEFAULT_WIDTH 512
|
|
9 |
#define DEFAULT_HEIGHT 512
|
10 |
10 |
#define MAX_MAPS 30
|
11 |
11 |
|
12 |
12 |
#define MAX_ID 20
|
... | ... | |
22 |
22 |
short robot_id;
|
23 |
23 |
} Data;
|
24 |
24 |
|
25 |
|
typedef struct robotScreenShot{
|
26 |
|
char robot_current_map;
|
27 |
|
double robot_orientation; //-PI to PI
|
|
25 |
/**
|
|
26 |
* Map oriented data.
|
|
27 |
*/
|
|
28 |
|
|
29 |
typedef struct Map{
|
|
30 |
int data[DEFAULT_WIDTH][DEFAULT_HEIGHT];
|
28 |
31 |
int robot_x;
|
29 |
32 |
int robot_y;
|
30 |
|
} RobotScreenShot;
|
|
33 |
double robot_orientation;
|
|
34 |
short robot_id;
|
|
35 |
char correlation_group;
|
|
36 |
} Map;
|
31 |
37 |
|
32 |
|
/*
|
33 |
|
This is the game plan if anyone is interested.
|
34 |
|
Each robot will go for an unspecified amount of time,
|
35 |
|
accruing encoder, BOM, and IR data. The IR and encoders
|
36 |
|
will be used to generate heat maps for each robot, starting
|
37 |
|
at the center, decreasing in certainty (i.e. the amount of
|
38 |
|
'color' put on during a time unit will be spread over a greater
|
39 |
|
area) over time according to the physical limitations of
|
40 |
|
the measuring devices.
|
|
38 |
Map** map_array;
|
|
39 |
int map_count;
|
41 |
40 |
|
42 |
|
When the uncertainty gets too high, a new map will be allocated for
|
43 |
|
the robot.
|
44 |
|
|
45 |
|
The BOM data will be used to orient individual maps to one another.
|
46 |
|
Along with physical limitations on the size of the environment, and
|
47 |
|
hopefully some brainstorming to do with triangularization and what not,
|
48 |
|
the heat maps will become increasingly well oriented.
|
49 |
|
|
50 |
|
Finally, after a currently unspecified amount of time, the maps which have
|
51 |
|
been properly oriented (this could probably just be all of them due to the nature
|
52 |
|
of the heat maps,) will be summed up into one meta map! Metamap will undergo some
|
53 |
|
signal processing, probably in the form of a second order gradient, to get a map
|
54 |
|
of magnitudes, which we will try to further reduce to lines. These lines are the
|
55 |
|
walls of the environment.
|
56 |
|
|
57 |
|
In time, it is possible that this "unspecfied amount of time" could become a continuous
|
58 |
|
computation, but I haven't put much thought into how this will scale. It will almost
|
59 |
|
definitely depend upon the chosen resolution of the heat map.
|
60 |
|
|
61 |
|
I do want people working with me, this should be a lot of fun,
|
62 |
|
I'm coming to accept the fact that I'm not a very good programmer,
|
63 |
|
but I think this is a solid method of doing SLAM.
|
64 |
|
*/
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
/**
|
69 |
|
* Map oriented data.
|
70 |
|
*/
|
71 |
|
int* map_array[MAX_MAPS][DEFAULT_WIDTH][DEFAULT_HEIGHT];
|
72 |
|
short map_ids[MAX_MAPS];
|
73 |
|
char map_correlated[MAX_MAPS];
|
74 |
|
|
75 |
41 |
/**Robot oriented data.**/
|
76 |
42 |
Queue* dataQueue;
|
|
43 |
int robot_map[MAX_ID];
|
77 |
44 |
|
78 |
45 |
/*These functions simply add the data to the queue.*/
|
79 |
46 |
void bom_handler(short id, BomNode** head);
|
80 |
47 |
void IR_handler(short id, short** data);
|
81 |
48 |
void encoder_handler(short id, unsigned char** data);
|
82 |
49 |
|
|
50 |
void slam_analyze(void);
|
83 |
51 |
void bom_analyze(BomNode* head,short robot_id);
|
84 |
52 |
void IR_analyze(short* data, short robot_id);
|
85 |
53 |
void encoder_analyze(unsigned char* data, short robot_id);
|
86 |
54 |
|
|
55 |
void initialize(void);
|
87 |
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 |
|
88 |
74 |
void slam_analyze(void){
|
89 |
75 |
Data* current_data;
|
90 |
76 |
while(1){
|
91 |
77 |
if(!queue_is_empty(dataQueue)){
|
|
78 |
if(queue_size(dataQueue) < 5){
|
|
79 |
//request_all(/*The robot that most needs it*/);
|
|
80 |
}
|
92 |
81 |
current_data = (Data*)queue_remove(dataQueue);
|
93 |
82 |
switch(current_data->type){
|
94 |
83 |
|
... | ... | |
108 |
97 |
}
|
109 |
98 |
}
|
110 |
99 |
|
111 |
|
|
112 |
|
int main(void){
|
113 |
|
data_requests_init(bom_handler,IR_handler,encoder_handler);
|
114 |
|
dataQueue = queue_create();
|
115 |
|
slam_analyze();
|
116 |
|
return -1;
|
|
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++;
|
117 |
114 |
}
|
118 |
115 |
|
119 |
|
|
120 |
116 |
void bom_analyze(BomNode* head, short robot_id){
|
121 |
117 |
BomNode* current_node = head;
|
|
118 |
if(robot_map[robot_id]==-1) initialize_map(robot_id);
|
|
119 |
|
122 |
120 |
while(current_node!=NULL){
|
123 |
|
|
|
121 |
|
124 |
122 |
current_node = current_node->next;
|
125 |
123 |
}
|
126 |
124 |
}
|
127 |
125 |
|
128 |
126 |
void IR_analyze(short* data, short robot_id){
|
|
127 |
if(robot_map[robot_id]==-1) initialize_map(robot_id);
|
129 |
128 |
|
130 |
129 |
}
|
131 |
130 |
|
132 |
131 |
void encoder_analyze(unsigned char* data, short robot_id){
|
|
132 |
if(robot_map[robot_id]==-1) initialize_map(robot_id);
|
133 |
133 |
|
134 |
134 |
}
|
135 |
135 |
|
136 |
|
/*The packet handling functions for data requests.*/
|
|
136 |
/*The packet handling functions for nata requests.*/
|
137 |
137 |
|
138 |
138 |
void bom_handler(short id, BomNode** head){
|
139 |
139 |
Data* newData = malloc(sizeof(Data));
|
140 |
140 |
newData->type = BOM_TYPE;
|
141 |
141 |
newData->data.u_b = (*head);
|
142 |
142 |
newData->robot_id = id;
|
|
143 |
newData->timeStamp = malloc(sizeof(struct timeval));
|
143 |
144 |
|
144 |
145 |
gettimeofday(&newData->timeStamp,NULL);
|
145 |
146 |
|
... | ... | |
178 |
179 |
*/
|
179 |
180 |
void rotate_map(int*** map, double radians){
|
180 |
181 |
int i,j;
|
181 |
|
double iprime, jprime, xoffset, yoffset;
|
|
182 |
double iprime, jprime, xoffset, yoffset, R1, R2, R3, R4, A;
|
182 |
183 |
int i2,j2;
|
183 |
|
int new_map[DEFAULT_WIDTH][DEFAULT_HEIGHT];
|
184 |
|
|
|
184 |
int** new_map;
|
|
185 |
new_map = malloc(DEFAULT_WIDTH*sizeof(int*));
|
185 |
186 |
for(i=0;i<DEFAULT_WIDTH;i++){
|
|
187 |
*(new_map+i) = malloc(DEFAULT_HEIGHT * sizeof(int));
|
186 |
188 |
for(j=0;j<DEFAULT_WIDTH;i++){
|
187 |
189 |
new_map[i][j] = 0;
|
188 |
190 |
}
|
... | ... | |
199 |
201 |
xoffset = iprime - i2;
|
200 |
202 |
yoffset = jprime - j2;
|
201 |
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 |
|
202 |
210 |
//If the new coordinates are legal...
|
203 |
211 |
if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
|
204 |
|
new_map[i2][j2] += (int)(sqrt(xoffset*xoffset + yoffset*yoffset)*(double)((*map)[i][j]));
|
205 |
|
|
|
212 |
new_map[i2][j2] += (int)((R1*A)*(double)((*map)[i][j]));
|
206 |
213 |
if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 < DEFAULT_HEIGHT && j2 >= 0)
|
207 |
|
new_map[i2+1][j2] += (int)(sqrt((1-xoffset)*(1-xoffset) + yoffset*yoffset)*(double)((*map)[i][j]));
|
208 |
|
|
|
214 |
new_map[i2+1][j2] += (int)((R2*A)*(double)((*map)[i][j]));
|
209 |
215 |
if(i2 < DEFAULT_WIDTH && i2 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
|
210 |
|
new_map[i2][j2+1] += (int)(sqrt(xoffset*xoffset + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]));
|
211 |
|
|
212 |
|
if(i2 + 1 < DEFAULT_WIDTH && i2 + 1 >= 0 && j2 + 1 < DEFAULT_HEIGHT && j2 + 1 >= 0)
|
213 |
|
new_map[i2+1][j2+1] += (int)(sqrt((1-xoffset)*(1-xoffset) + (1-yoffset)*(1-yoffset))*(double)((*map)[i][j]));
|
|
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]));
|
214 |
219 |
}
|
215 |
220 |
}
|
216 |
221 |
|
217 |
|
//Incompatible types? FIXME
|
218 |
222 |
(*map) = new_map;
|
219 |
223 |
return;
|
220 |
224 |
}
|