Revision 743
Fixed compilation errors.
sensor_matrix.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (c) 2007 Colony Project |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Permission is hereby granted, free of charge, to any person |
5 | 5 |
* obtaining a copy of this software and associated documentation |
6 | 6 |
* files (the "Software"), to deal in the Software without |
... | ... | |
9 | 9 |
* copies of the Software, and to permit persons to whom the |
10 | 10 |
* Software is furnished to do so, subject to the following |
11 | 11 |
* conditions: |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* The above copyright notice and this permission notice shall be |
14 | 14 |
* included in all copies or substantial portions of the Software. |
15 |
*
|
|
15 |
* |
|
16 | 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 | 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
... | ... | |
38 | 38 |
|
39 | 39 |
#include "sensor_matrix.h" |
40 | 40 |
|
41 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
|
41 |
// the global sensor matrix |
|
42 |
SensorMatrix m; |
|
42 | 43 |
|
43 |
/*Sensor Matrix Functions*/ |
|
44 |
void sensor_matrix_expand(SensorMatrix* m, int nextSize); |
|
45 |
|
|
46 | 44 |
/** |
47 | 45 |
* Initializes the sensor matrix. |
48 | 46 |
* |
49 | 47 |
* @return the newly created sensor matrix |
50 | 48 |
**/ |
51 |
SensorMatrix* sensor_matrix_create()
|
|
49 |
void sensor_matrix_create()
|
|
52 | 50 |
{ |
53 |
SensorMatrix* m; |
|
54 |
int i; |
|
55 |
|
|
56 |
m = (SensorMatrix*)malloc(sizeof(SensorMatrix)); |
|
57 |
if (!m) |
|
58 |
{ |
|
59 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix.\r\n"); |
|
60 |
return NULL; |
|
61 |
} |
|
62 |
m->size = DEFAULT_SENSOR_MATRIX_SIZE; |
|
63 |
m->matrix = (int**)malloc(m->size * sizeof(int*)); |
|
64 |
m->joined = (int*)malloc(m->size * sizeof(int)); |
|
65 |
m->numJoined = 0; |
|
66 |
if (!(m->matrix) || !(m->joined)) |
|
67 |
{ |
|
68 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n"); |
|
69 |
return NULL; |
|
70 |
} |
|
71 |
|
|
72 |
for (i = 0; i < m->size; i++) |
|
73 |
{ |
|
74 |
m->matrix[i] = NULL; |
|
75 |
m->joined[i] = 0; |
|
76 |
} |
|
77 |
return m; |
|
78 |
} |
|
51 |
int i, j; |
|
79 | 52 |
|
80 |
/** |
|
81 |
* Deletes and frees memory from the sensor matrix. |
|
82 |
* |
|
83 |
* @param m the sensor matrix to delete |
|
84 |
**/ |
|
85 |
void sensor_matrix_destroy(SensorMatrix* m) |
|
86 |
{ |
|
87 |
int i; |
|
88 |
for (i = 0; i < m->size; i++) |
|
89 |
if (m->matrix[i] != NULL) |
|
90 |
free(m->matrix[i]); |
|
91 |
free(m->matrix); |
|
92 |
free(m->joined); |
|
93 |
free(m); |
|
94 |
} |
|
53 |
m.numJoined = 0; |
|
95 | 54 |
|
96 |
/** |
|
97 |
* Adds robot with XBee id id to the sensor matrix. |
|
98 |
* |
|
99 |
* @param m the sensor matrix |
|
100 |
* @param id the XBee ID of the robot to add |
|
101 |
**/ |
|
102 |
void sensor_matrix_add_robot(SensorMatrix* m, int id) |
|
103 |
{ |
|
104 |
int i; |
|
105 |
if (id >= m->size) |
|
106 |
sensor_matrix_expand(m, id + 1); |
|
107 |
if (m->matrix[id] != NULL) |
|
108 |
return; |
|
109 |
|
|
110 |
m->matrix[id] = (int*)malloc(m->size * sizeof(int)); |
|
111 |
if (!(m->matrix[id])) |
|
55 |
for (i = 0; i < MAXIMUM_XBEE_ID; i++) |
|
112 | 56 |
{ |
113 |
WL_DEBUG_PRINT("Out of memory - add robot.\r\n"); |
|
114 |
return; |
|
57 |
m.joined[i] = 0; |
|
58 |
for (j = 0; j < MAXIMUM_XBEE_ID; j++) |
|
59 |
m.matrix[i][j] = READING_UNKNOWN; |
|
115 | 60 |
} |
116 |
|
|
117 |
for (i = 0; i < m->size; i++) |
|
118 |
if (m->matrix[i] != NULL) |
|
119 |
m->matrix[i][id] = -1; |
|
120 | 61 |
} |
121 | 62 |
|
122 | 63 |
/** |
123 |
* Removes robot with id from the sensor matrix, and removes |
|
124 |
* all sensor information regarding the robot. |
|
64 |
* Sets the sensor reading for robot robot to reading. |
|
125 | 65 |
* |
126 |
* @param m the sensor matrix |
|
127 |
* @param id the XBee ID of the robot to remove |
|
128 |
**/ |
|
129 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id) |
|
66 |
* @param observer the id of the robot who made the reading |
|
67 |
* @param robot the id of the robot who the reading is for |
|
68 |
* @param reading the BOM reading from observer to robot |
|
69 |
*/ |
|
70 |
void sensor_matrix_set_reading(int observer, int robot, int reading) |
|
130 | 71 |
{ |
131 |
int i; |
|
132 |
|
|
133 |
if (id >= m->size || m->matrix[id] == NULL) |
|
72 |
if (robot >= MAXIMUM_XBEE_ID || observer >= MAXIMUM_XBEE_ID) |
|
134 | 73 |
{ |
135 |
WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
|
|
74 |
WL_DEBUG_PRINT("ID too large.");
|
|
136 | 75 |
return; |
137 | 76 |
} |
138 |
|
|
139 |
free(m->matrix[id]); |
|
140 |
m->matrix[id] = NULL; |
|
141 |
|
|
142 |
for (i = 0 ; i < m->size; i++) |
|
143 |
if (m->matrix[i] != NULL) |
|
144 |
m->matrix[i][id] = -1; |
|
145 |
|
|
146 |
m->joined[id] = 0; |
|
147 |
} |
|
148 | 77 |
|
149 |
/** |
|
150 |
* Expands the size of the sensor matrix if an id number we attempt |
|
151 |
* to add is too large. |
|
152 |
* |
|
153 |
* @param m the sensor matrix to expand |
|
154 |
* @param size the new size of the sensor matrix |
|
155 |
**/ |
|
156 |
//Note: this has probably not been tested, hopefully it works |
|
157 |
void sensor_matrix_expand(SensorMatrix* m, int nextSize) |
|
158 |
{ |
|
159 |
int i, j; |
|
160 |
WL_DEBUG_PRINT("Expanding sensor matrix.\r\n"); |
|
161 |
|
|
162 |
int** tempMatrix = (int**)malloc(nextSize * sizeof(int*)); |
|
163 |
if (!tempMatrix) |
|
164 |
{ |
|
165 |
WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n"); |
|
166 |
return; |
|
167 |
} |
|
168 |
|
|
169 |
for (i = 0; i < nextSize; i++) |
|
170 |
tempMatrix[i] = NULL; |
|
171 |
|
|
172 |
//copy over old sensor data |
|
173 |
for (i = 0; i < m->size; i++) |
|
174 |
if (m->matrix[i] != NULL) |
|
175 |
{ |
|
176 |
tempMatrix[i] = (int *)malloc(nextSize * sizeof(int)); |
|
177 |
if (!tempMatrix[i]) |
|
178 |
{ |
|
179 |
WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n"); |
|
180 |
return; |
|
181 |
} |
|
182 |
for (j = 0; j < m->size; j++) |
|
183 |
tempMatrix[i][j] = m->matrix[i][j]; |
|
184 |
for (j = m->size; j < nextSize; j++) |
|
185 |
tempMatrix[i][j] = -1; |
|
186 |
|
|
187 |
free(m->matrix[i]); |
|
188 |
} |
|
189 |
|
|
190 |
free(m->matrix); |
|
191 |
m->matrix = tempMatrix; |
|
192 |
m->size = nextSize; |
|
193 |
|
|
194 |
//expand the size of joined |
|
195 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int)); |
|
196 |
if (!tempJoined) |
|
197 |
{ |
|
198 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
|
199 |
return; |
|
200 |
} |
|
201 |
|
|
202 |
for (i = 0; i < m->size; i++) |
|
203 |
tempJoined[i] = m->joined[i]; |
|
204 |
for (i = m->size; i < nextSize; i++) |
|
205 |
tempJoined[i] = 0; |
|
206 |
|
|
207 |
free(m->joined); |
|
208 |
m->joined = tempJoined; |
|
78 |
m.matrix[observer][robot] = (unsigned char)reading; |
|
209 | 79 |
} |
210 | 80 |
|
211 | 81 |
/** |
212 |
* Sets the sensor reading for robot robot to reading. |
|
213 |
* |
|
214 |
* @param m the sensor matrix to set the reading for |
|
215 |
* @param observer the id of the robot who made the reading |
|
216 |
* @param robot the id of the robot who the reading is for |
|
217 |
* @param reading the BOM reading from observer to robot |
|
218 |
*/ |
|
219 |
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading) |
|
220 |
{ |
|
221 |
if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) |
|
222 |
sensor_matrix_add_robot(m, observer); |
|
223 |
m->matrix[observer][robot] = reading; |
|
224 |
} |
|
225 |
|
|
226 |
/** |
|
227 | 82 |
* Gets the sensor reading for a robot to another robot. |
228 | 83 |
* |
229 |
* @param m the sensor matrix |
|
230 | 84 |
* @param observer the robot whose reading we check |
231 | 85 |
* @param robot the robot who we are checking the reading to |
232 | 86 |
* |
233 | 87 |
* @return the observer's BOM reading for robot |
234 | 88 |
**/ |
235 |
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
|
|
89 |
int sensor_matrix_get_reading(int observer, int robot) |
|
236 | 90 |
{ |
237 |
if (observer >= m->size || robot >= m->size)
|
|
91 |
if (observer >= MAXIMUM_XBEE_ID || robot >= MAXIMUM_XBEE_ID)
|
|
238 | 92 |
return -1; |
239 |
return m->matrix[observer][robot]; |
|
93 |
|
|
94 |
return (int)m.matrix[observer][robot]; |
|
240 | 95 |
} |
241 | 96 |
|
242 | 97 |
/** |
243 | 98 |
* Sets whether or not the given robot is part of the token ring. |
244 | 99 |
* |
245 |
* @param m the sensor matrix |
|
246 | 100 |
* @param robot the robot to set as a member / nonmember of the token ring |
247 | 101 |
* @param in 1 if the robot is in the token ring, 0 otherwise |
248 | 102 |
**/ |
249 |
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
|
|
103 |
void sensor_matrix_set_in_ring(int robot, int in) |
|
250 | 104 |
{ |
251 |
if (robot >= m->size) |
|
252 |
sensor_matrix_expand(m, robot + 1); |
|
253 |
if (in == 1) |
|
254 |
sensor_matrix_add_robot(m, robot); |
|
255 |
if (in == 1 && m->joined[robot] == 0) |
|
256 |
m->numJoined++; |
|
257 |
if (in == 0 && m->joined[robot] == 1) |
|
258 |
m->numJoined--; |
|
259 |
m->joined[robot] = in; |
|
105 |
if (robot >= MAXIMUM_XBEE_ID) |
|
106 |
{ |
|
107 |
WL_DEBUG_PRINT("ID too large."); |
|
108 |
return; |
|
109 |
} |
|
110 |
|
|
111 |
if (in == 1 && m.joined[robot] == 0) |
|
112 |
m.numJoined++; |
|
113 |
|
|
114 |
if (in == 0 && m.joined[robot]) |
|
115 |
m.numJoined--; |
|
116 |
m.joined[robot] = in; |
|
260 | 117 |
} |
261 | 118 |
|
262 | 119 |
/** |
263 | 120 |
* Checks if the given robot is in the token ring. |
264 |
* |
|
265 |
* @param m the sensor matrix |
|
121 |
* |
|
266 | 122 |
* @param robot the ID of the robot to check |
267 | 123 |
* |
268 | 124 |
* @return 1 if the robot is in the token ring, 0 otherwise |
269 | 125 |
**/ |
270 |
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
|
|
126 |
int sensor_matrix_get_in_ring(int robot) |
|
271 | 127 |
{ |
272 |
if (robot >= m->size)
|
|
128 |
if (robot >= MAXIMUM_XBEE_ID)
|
|
273 | 129 |
return -1; |
274 |
return m->joined[robot]; |
|
130 |
|
|
131 |
return m.joined[robot]; |
|
275 | 132 |
} |
276 | 133 |
|
277 | 134 |
/** |
278 |
* Returns the size of the sensor matrix. |
|
135 |
* Returns the number of robots which have joined the |
|
136 |
* token ring. |
|
279 | 137 |
* |
280 |
* @param m the sensor matrix |
|
281 |
* |
|
282 |
* @return the size of the sensor matrix |
|
138 |
* @return the number of robots in the token ring |
|
283 | 139 |
**/ |
284 |
int sensor_matrix_get_size(SensorMatrix* m)
|
|
140 |
int sensor_matrix_get_joined(void)
|
|
285 | 141 |
{ |
286 |
return m->size;
|
|
142 |
return m.numJoined;
|
|
287 | 143 |
} |
288 | 144 |
|
289 | 145 |
/** |
290 |
* Returns the number of robots which have joined the
|
|
291 |
* token ring.
|
|
146 |
* Returns the maximum XBee id of a robot which can be stored
|
|
147 |
* in the sensor matrix.
|
|
292 | 148 |
* |
293 |
* @param m the sensor matrix |
|
294 |
* |
|
295 |
* @return the number of robots in the token ring |
|
149 |
* @return the maximum number of robots which can be stored |
|
296 | 150 |
**/ |
297 |
int sensor_matrix_get_joined(SensorMatrix* m)
|
|
151 |
inline int sensor_matrix_get_size(void)
|
|
298 | 152 |
{ |
299 |
return m->numJoined;
|
|
153 |
return MAXIMUM_XBEE_ID;
|
|
300 | 154 |
} |
301 | 155 |
|
Also available in: Unified diff