Revision 732
New stuff works, fixed bugs, removed malloc.
sensor_matrix.c | ||
---|---|---|
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 |
static 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 |
if (!(m->matrix)) { |
|
65 |
WL_DEBUG_PRINT("Out of memory - allocating memory for matrix.\r\n"); |
|
66 |
free(m); |
|
67 |
return NULL; |
|
68 |
} |
|
69 |
m->joined = (int*)malloc(m->size * sizeof(int)); |
|
70 |
if (!(m->joined)) { |
|
71 |
WL_DEBUG_PRINT("Out of memory - allocating memory for joined.\r\n"); |
|
72 |
free(m->matrix); |
|
73 |
free(m); |
|
74 |
return NULL; |
|
75 |
} |
|
76 |
m->numJoined = 0; |
|
77 |
if (!(m->matrix) || !(m->joined)) |
|
78 |
{ |
|
79 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n"); |
|
80 |
return NULL; |
|
81 |
} |
|
82 |
|
|
83 |
for (i = 0; i < m->size; i++) |
|
84 |
{ |
|
85 |
m->matrix[i] = NULL; |
|
86 |
m->joined[i] = 0; |
|
87 |
} |
|
88 |
return m; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* Deletes and frees memory from the sensor matrix. |
|
93 |
* |
|
94 |
* @param m the sensor matrix to delete |
|
95 |
**/ |
|
96 |
void sensor_matrix_destroy(SensorMatrix* m) |
|
97 |
{ |
|
98 |
int i; |
|
99 |
for (i = 0; i < m->size; i++) |
|
100 |
if (m->matrix[i] != NULL) |
|
101 |
free(m->matrix[i]); |
|
102 |
free(m->matrix); |
|
103 |
free(m->joined); |
|
104 |
free(m); |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Adds robot with XBee id id to the sensor matrix. |
|
109 |
* |
|
110 |
* @param m the sensor matrix |
|
111 |
* @param id the XBee ID of the robot to add |
|
112 |
**/ |
|
113 |
void sensor_matrix_add_robot(SensorMatrix* m, int id) |
|
114 |
{ |
|
115 |
int i; |
|
116 |
if (id >= m->size) { |
|
117 |
sensor_matrix_expand(m, id + 1); |
|
118 |
} |
|
119 |
|
|
120 |
if (m->matrix[id] != NULL) { |
|
121 |
return; |
|
122 |
} |
|
123 |
|
|
124 |
m->matrix[id] = (int*)malloc(m->size * sizeof(int)); |
|
125 |
if (!(m->matrix[id])) |
|
126 |
{ |
|
127 |
WL_DEBUG_PRINT("Out of memory - add robot.\r\n"); |
|
128 |
return; |
|
129 |
} |
|
130 |
|
|
131 |
for (i = 0; i < m->size; i++) { |
|
132 |
if (m->matrix[i] != NULL) { |
|
133 |
m->matrix[i][id] = -1; |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Removes robot with id from the sensor matrix, and removes |
|
140 |
* all sensor information regarding the robot. |
|
141 |
* |
|
142 |
* @param m the sensor matrix |
|
143 |
* @param id the XBee ID of the robot to remove |
|
144 |
**/ |
|
145 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id) |
|
146 |
{ |
|
147 |
int i; |
|
148 |
|
|
149 |
if (id >= m->size || m->matrix[id] == NULL) |
|
150 |
{ |
|
151 |
WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n"); |
|
152 |
return; |
|
153 |
} |
|
154 |
|
|
155 |
free(m->matrix[id]); |
|
156 |
m->matrix[id] = NULL; |
|
157 |
|
|
158 |
for (i = 0 ; i < m->size; i++) |
|
159 |
if (m->matrix[i] != NULL) |
|
160 |
m->matrix[i][id] = -1; |
|
161 |
|
|
162 |
m->joined[id] = 0; |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* Expands the size of the sensor matrix if an id number we attempt |
|
167 |
* to add is too large. |
|
168 |
* |
|
169 |
* @param m the sensor matrix to expand |
|
170 |
* @param size the new size of the sensor matrix |
|
171 |
**/ |
|
172 |
//Note: this has probably not been tested, hopefully it works |
|
173 |
static void sensor_matrix_expand(SensorMatrix* m, int nextSize) |
|
174 |
{ |
|
175 | 51 |
int i, j; |
176 |
WL_DEBUG_PRINT("Expanding sensor matrix.\r\n"); |
|
177 | 52 |
|
178 |
int** tempMatrix = (int**)malloc(nextSize * sizeof(int*)); |
|
179 |
if (!tempMatrix) |
|
180 |
{ |
|
181 |
WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n"); |
|
182 |
return; |
|
183 |
} |
|
53 |
m.numJoined = 0; |
|
184 | 54 |
|
185 |
for (i = 0; i < nextSize; i++) |
|
186 |
tempMatrix[i] = NULL; |
|
187 |
|
|
188 |
//copy over old sensor data |
|
189 |
for (i = 0; i < m->size; i++) |
|
190 |
if (m->matrix[i] != NULL) |
|
191 |
{ |
|
192 |
tempMatrix[i] = (int *)malloc(nextSize * sizeof(int)); |
|
193 |
if (!tempMatrix[i]) |
|
194 |
{ |
|
195 |
WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n"); |
|
196 |
return; |
|
197 |
} |
|
198 |
for (j = 0; j < m->size; j++) { |
|
199 |
tempMatrix[i][j] = m->matrix[i][j]; |
|
200 |
} |
|
201 |
|
|
202 |
for (j = m->size; j < nextSize; j++) { |
|
203 |
tempMatrix[i][j] = -1; |
|
204 |
} |
|
205 |
|
|
206 |
free(m->matrix[i]); |
|
207 |
} |
|
208 |
|
|
209 |
free(m->matrix); |
|
210 |
m->matrix = tempMatrix; |
|
211 |
m->size = nextSize; |
|
212 |
|
|
213 |
//expand the size of joined |
|
214 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int)); |
|
215 |
if (!tempJoined) |
|
55 |
for (i = 0; i < MAXIMUM_XBEE_ID; i++) |
|
216 | 56 |
{ |
217 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
|
218 |
return; |
|
57 |
m.joined[i] = 0; |
|
58 |
for (j = 0; j < MAXIMUM_XBEE_ID; j++) |
|
59 |
m.matrix[i][j] = READING_UNKNOWN; |
|
219 | 60 |
} |
220 |
|
|
221 |
for (i = 0; i < m->size; i++) { |
|
222 |
tempJoined[i] = m->joined[i]; |
|
223 |
} |
|
224 |
|
|
225 |
for (i = m->size; i < nextSize; i++) { |
|
226 |
tempJoined[i] = 0; |
|
227 |
} |
|
228 |
|
|
229 |
free(m->joined); |
|
230 |
m->joined = tempJoined; |
|
231 | 61 |
} |
232 | 62 |
|
233 | 63 |
/** |
234 | 64 |
* Sets the sensor reading for robot robot to reading. |
235 | 65 |
* |
236 |
* @param m the sensor matrix to set the reading for |
|
237 | 66 |
* @param observer the id of the robot who made the reading |
238 | 67 |
* @param robot the id of the robot who the reading is for |
239 | 68 |
* @param reading the BOM reading from observer to robot |
240 | 69 |
*/ |
241 |
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading)
|
|
70 |
void sensor_matrix_set_reading(int observer, int robot, int reading) |
|
242 | 71 |
{ |
243 |
if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) { |
|
244 |
sensor_matrix_add_robot(m, observer); |
|
72 |
if (robot >= MAXIMUM_XBEE_ID || observer >= MAXIMUM_XBEE_ID) |
|
73 |
{ |
|
74 |
WL_DEBUG_PRINT("ID too large."); |
|
75 |
return; |
|
245 | 76 |
} |
246 | 77 |
|
247 |
m->matrix[observer][robot] = reading;
|
|
78 |
m.matrix[observer][robot] = (unsigned char)reading;
|
|
248 | 79 |
} |
249 | 80 |
|
250 | 81 |
/** |
251 | 82 |
* Gets the sensor reading for a robot to another robot. |
252 | 83 |
* |
253 |
* @param m the sensor matrix |
|
254 | 84 |
* @param observer the robot whose reading we check |
255 | 85 |
* @param robot the robot who we are checking the reading to |
256 | 86 |
* |
257 | 87 |
* @return the observer's BOM reading for robot |
258 | 88 |
**/ |
259 |
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
|
|
89 |
int sensor_matrix_get_reading(int observer, int robot) |
|
260 | 90 |
{ |
261 |
if (observer >= m->size || robot >= m->size) {
|
|
91 |
if (observer >= MAXIMUM_XBEE_ID || robot >= MAXIMUM_XBEE_ID)
|
|
262 | 92 |
return -1; |
263 |
} |
|
264 | 93 |
|
265 |
return m->matrix[observer][robot];
|
|
94 |
return (int)m.matrix[observer][robot];
|
|
266 | 95 |
} |
267 | 96 |
|
268 | 97 |
/** |
269 | 98 |
* Sets whether or not the given robot is part of the token ring. |
270 | 99 |
* |
271 |
* @param m the sensor matrix |
|
272 | 100 |
* @param robot the robot to set as a member / nonmember of the token ring |
273 | 101 |
* @param in 1 if the robot is in the token ring, 0 otherwise |
274 | 102 |
**/ |
275 |
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
|
|
103 |
void sensor_matrix_set_in_ring(int robot, int in) |
|
276 | 104 |
{ |
277 |
if (robot >= m->size) { |
|
278 |
sensor_matrix_expand(m, robot + 1); |
|
105 |
if (robot >= MAXIMUM_XBEE_ID) |
|
106 |
{ |
|
107 |
WL_DEBUG_PRINT("ID too large."); |
|
108 |
return; |
|
279 | 109 |
} |
280 | 110 |
|
281 |
if (in == 1) { |
|
282 |
sensor_matrix_add_robot(m, robot); |
|
283 |
} |
|
111 |
if (in == 1 && m.joined[robot] == 0) |
|
112 |
m.numJoined++; |
|
284 | 113 |
|
285 |
if (in == 1 && m->joined[robot] == 0) { |
|
286 |
m->numJoined++; |
|
287 |
} |
|
114 |
if (in == 0 && m.joined[robot]) |
|
115 |
m.numJoined--; |
|
288 | 116 |
|
289 |
if (in == 0 && m->joined[robot] == 1) { |
|
290 |
m->numJoined--; |
|
291 |
} |
|
292 |
|
|
293 |
m->joined[robot] = in; |
|
117 |
m.joined[robot] = in; |
|
294 | 118 |
} |
295 | 119 |
|
296 | 120 |
/** |
297 | 121 |
* Checks if the given robot is in the token ring. |
298 | 122 |
* |
299 |
* @param m the sensor matrix |
|
300 | 123 |
* @param robot the ID of the robot to check |
301 | 124 |
* |
302 | 125 |
* @return 1 if the robot is in the token ring, 0 otherwise |
303 | 126 |
**/ |
304 |
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
|
|
127 |
int sensor_matrix_get_in_ring(int robot) |
|
305 | 128 |
{ |
306 |
if (robot >= m->size) {
|
|
129 |
if (robot >= MAXIMUM_XBEE_ID)
|
|
307 | 130 |
return -1; |
308 |
} |
|
309 | 131 |
|
310 |
return m->joined[robot];
|
|
132 |
return m.joined[robot];
|
|
311 | 133 |
} |
312 | 134 |
|
313 | 135 |
/** |
314 |
* Returns the size of the sensor matrix. |
|
136 |
* Returns the number of robots which have joined the |
|
137 |
* token ring. |
|
315 | 138 |
* |
316 |
* @param m the sensor matrix |
|
317 |
* |
|
318 |
* @return the size of the sensor matrix |
|
139 |
* @return the number of robots in the token ring |
|
319 | 140 |
**/ |
320 |
int sensor_matrix_get_size(SensorMatrix* m)
|
|
141 |
int sensor_matrix_get_joined(void)
|
|
321 | 142 |
{ |
322 |
return m->size;
|
|
143 |
return m.numJoined;
|
|
323 | 144 |
} |
324 | 145 |
|
325 | 146 |
/** |
326 |
* Returns the number of robots which have joined the
|
|
327 |
* token ring.
|
|
147 |
* Returns the maximum XBee id of a robot which can be stored
|
|
148 |
* in the sensor matrix.
|
|
328 | 149 |
* |
329 |
* @param m the sensor matrix |
|
330 |
* |
|
331 |
* @return the number of robots in the token ring |
|
150 |
* @return the maximum number of robots which can be stored |
|
332 | 151 |
**/ |
333 |
int sensor_matrix_get_joined(SensorMatrix* m)
|
|
152 |
inline int sensor_matrix_get_size(void)
|
|
334 | 153 |
{ |
335 |
return m->numJoined;
|
|
154 |
return MAXIMUM_XBEE_ID;
|
|
336 | 155 |
} |
156 |
|
Also available in: Unified diff