Revision 346
Undid Jason's style changes. I like tabs.
sensor_matrix.c | ||
---|---|---|
48 | 48 |
* |
49 | 49 |
* @return the newly created sensor matrix |
50 | 50 |
**/ |
51 |
SensorMatrix* sensor_matrix_create() { |
|
52 |
SensorMatrix* m; |
|
53 |
int i; |
|
51 |
SensorMatrix* sensor_matrix_create() |
|
52 |
{ |
|
53 |
SensorMatrix* m; |
|
54 |
int i; |
|
54 | 55 |
|
55 |
m = (SensorMatrix*)malloc(sizeof(SensorMatrix)); |
|
56 |
if (!m) { |
|
57 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix.\r\n"); |
|
58 |
return NULL; |
|
59 |
} |
|
60 |
m->size = DEFAULT_SENSOR_MATRIX_SIZE; |
|
61 |
m->matrix = (int**)malloc(m->size * sizeof(int*)); |
|
62 |
if (!(m->matrix)) { |
|
63 |
WL_DEBUG_PRINT("Out of memory - allocating memory for matrix.\r\n"); |
|
64 |
free(m); |
|
65 |
return NULL; |
|
66 |
} |
|
67 |
m->joined = (int*)malloc(m->size * sizeof(int)); |
|
68 |
if (!(m->joined)) { |
|
69 |
WL_DEBUG_PRINT("Out of memory - allocating memory for joined.\r\n"); |
|
70 |
free(m->matrix); |
|
71 |
free(m); |
|
72 |
return NULL; |
|
73 |
} |
|
74 |
m->numJoined = 0; |
|
75 |
if (!(m->matrix) || !(m->joined)) { |
|
76 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n"); |
|
77 |
return NULL; |
|
78 |
} |
|
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 |
} |
|
79 | 82 |
|
80 |
for (i = 0; i < m->size; i++) { |
|
81 |
m->matrix[i] = NULL; |
|
82 |
m->joined[i] = 0; |
|
83 |
} |
|
84 |
return m; |
|
83 |
for (i = 0; i < m->size; i++) |
|
84 |
{ |
|
85 |
m->matrix[i] = NULL; |
|
86 |
m->joined[i] = 0; |
|
87 |
} |
|
88 |
return m; |
|
85 | 89 |
} |
86 | 90 |
|
87 | 91 |
/** |
... | ... | |
89 | 93 |
* |
90 | 94 |
* @param m the sensor matrix to delete |
91 | 95 |
**/ |
92 |
void sensor_matrix_destroy(SensorMatrix* m) { |
|
93 |
int i; |
|
94 |
for (i = 0; i < m->size; i++) |
|
95 |
if (m->matrix[i] != NULL) |
|
96 |
free(m->matrix[i]); |
|
97 |
free(m->matrix); |
|
98 |
free(m->joined); |
|
99 |
free(m); |
|
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); |
|
100 | 105 |
} |
101 | 106 |
|
102 | 107 |
/** |
... | ... | |
105 | 110 |
* @param m the sensor matrix |
106 | 111 |
* @param id the XBee ID of the robot to add |
107 | 112 |
**/ |
108 |
void sensor_matrix_add_robot(SensorMatrix* m, int id) { |
|
109 |
int i; |
|
110 |
if (id >= m->size) |
|
111 |
sensor_matrix_expand(m, id + 1); |
|
112 |
if (m->matrix[id] != NULL) |
|
113 |
return; |
|
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 |
if (m->matrix[id] != NULL) |
|
119 |
return; |
|
114 | 120 |
|
115 |
m->matrix[id] = (int*)malloc(m->size * sizeof(int)); |
|
116 |
if (!(m->matrix[id])) { |
|
117 |
WL_DEBUG_PRINT("Out of memory - add robot.\r\n"); |
|
118 |
return; |
|
119 |
} |
|
121 |
m->matrix[id] = (int*)malloc(m->size * sizeof(int)); |
|
122 |
if (!(m->matrix[id])) |
|
123 |
{ |
|
124 |
WL_DEBUG_PRINT("Out of memory - add robot.\r\n"); |
|
125 |
return; |
|
126 |
} |
|
120 | 127 |
|
121 |
for (i = 0; i < m->size; i++)
|
|
122 |
if (m->matrix[i] != NULL)
|
|
123 |
m->matrix[i][id] = -1;
|
|
128 |
for (i = 0; i < m->size; i++)
|
|
129 |
if (m->matrix[i] != NULL)
|
|
130 |
m->matrix[i][id] = -1;
|
|
124 | 131 |
} |
125 | 132 |
|
126 | 133 |
/** |
... | ... | |
130 | 137 |
* @param m the sensor matrix |
131 | 138 |
* @param id the XBee ID of the robot to remove |
132 | 139 |
**/ |
133 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id) { |
|
134 |
int i; |
|
140 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id) |
|
141 |
{ |
|
142 |
int i; |
|
135 | 143 |
|
136 |
if (id >= m->size || m->matrix[id] == NULL) { |
|
137 |
WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n"); |
|
138 |
return; |
|
139 |
} |
|
144 |
if (id >= m->size || m->matrix[id] == NULL) |
|
145 |
{ |
|
146 |
WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n"); |
|
147 |
return; |
|
148 |
} |
|
140 | 149 |
|
141 |
free(m->matrix[id]);
|
|
142 |
m->matrix[id] = NULL;
|
|
150 |
free(m->matrix[id]);
|
|
151 |
m->matrix[id] = NULL;
|
|
143 | 152 |
|
144 |
for (i = 0 ; i < m->size; i++)
|
|
145 |
if (m->matrix[i] != NULL)
|
|
146 |
m->matrix[i][id] = -1;
|
|
153 |
for (i = 0 ; i < m->size; i++)
|
|
154 |
if (m->matrix[i] != NULL)
|
|
155 |
m->matrix[i][id] = -1;
|
|
147 | 156 |
|
148 |
m->joined[id] = 0;
|
|
157 |
m->joined[id] = 0;
|
|
149 | 158 |
} |
150 | 159 |
|
151 | 160 |
/** |
... | ... | |
156 | 165 |
* @param size the new size of the sensor matrix |
157 | 166 |
**/ |
158 | 167 |
//Note: this has probably not been tested, hopefully it works |
159 |
void sensor_matrix_expand(SensorMatrix* m, int nextSize) { |
|
160 |
int i, j; |
|
161 |
WL_DEBUG_PRINT("Expanding sensor matrix.\r\n"); |
|
168 |
void sensor_matrix_expand(SensorMatrix* m, int nextSize) |
|
169 |
{ |
|
170 |
int i, j; |
|
171 |
WL_DEBUG_PRINT("Expanding sensor matrix.\r\n"); |
|
162 | 172 |
|
163 |
int** tempMatrix = (int**)malloc(nextSize * sizeof(int*)); |
|
164 |
if (!tempMatrix) { |
|
165 |
WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n"); |
|
166 |
return; |
|
167 |
} |
|
173 |
int** tempMatrix = (int**)malloc(nextSize * sizeof(int*)); |
|
174 |
if (!tempMatrix) |
|
175 |
{ |
|
176 |
WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n"); |
|
177 |
return; |
|
178 |
} |
|
168 | 179 |
|
169 |
for (i = 0; i < nextSize; i++)
|
|
170 |
tempMatrix[i] = NULL;
|
|
180 |
for (i = 0; i < nextSize; i++)
|
|
181 |
tempMatrix[i] = NULL;
|
|
171 | 182 |
|
172 |
//copy over old sensor data |
|
173 |
for (i = 0; i < m->size; i++) |
|
174 |
if (m->matrix[i] != NULL) { |
|
175 |
tempMatrix[i] = (int *)malloc(nextSize * sizeof(int)); |
|
176 |
if (!tempMatrix[i]) { |
|
177 |
WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n"); |
|
178 |
return; |
|
179 |
} |
|
180 |
for (j = 0; j < m->size; j++) |
|
181 |
tempMatrix[i][j] = m->matrix[i][j]; |
|
182 |
for (j = m->size; j < nextSize; j++) |
|
183 |
tempMatrix[i][j] = -1; |
|
183 |
//copy over old sensor data |
|
184 |
for (i = 0; i < m->size; i++) |
|
185 |
if (m->matrix[i] != NULL) |
|
186 |
{ |
|
187 |
tempMatrix[i] = (int *)malloc(nextSize * sizeof(int)); |
|
188 |
if (!tempMatrix[i]) |
|
189 |
{ |
|
190 |
WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n"); |
|
191 |
return; |
|
192 |
} |
|
193 |
for (j = 0; j < m->size; j++) |
|
194 |
tempMatrix[i][j] = m->matrix[i][j]; |
|
195 |
for (j = m->size; j < nextSize; j++) |
|
196 |
tempMatrix[i][j] = -1; |
|
184 | 197 |
|
185 |
free(m->matrix[i]);
|
|
186 |
}
|
|
198 |
free(m->matrix[i]);
|
|
199 |
}
|
|
187 | 200 |
|
188 |
free(m->matrix);
|
|
189 |
m->matrix = tempMatrix;
|
|
190 |
m->size = nextSize;
|
|
201 |
free(m->matrix);
|
|
202 |
m->matrix = tempMatrix;
|
|
203 |
m->size = nextSize;
|
|
191 | 204 |
|
192 |
//expand the size of joined |
|
193 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int)); |
|
194 |
if (!tempJoined) { |
|
195 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
|
196 |
return; |
|
197 |
} |
|
205 |
//expand the size of joined |
|
206 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int)); |
|
207 |
if (!tempJoined) |
|
208 |
{ |
|
209 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
|
210 |
return; |
|
211 |
} |
|
198 | 212 |
|
199 |
for (i = 0; i < m->size; i++)
|
|
200 |
tempJoined[i] = m->joined[i];
|
|
201 |
for (i = m->size; i < nextSize; i++)
|
|
202 |
tempJoined[i] = 0;
|
|
213 |
for (i = 0; i < m->size; i++)
|
|
214 |
tempJoined[i] = m->joined[i];
|
|
215 |
for (i = m->size; i < nextSize; i++)
|
|
216 |
tempJoined[i] = 0;
|
|
203 | 217 |
|
204 |
free(m->joined);
|
|
205 |
m->joined = tempJoined;
|
|
218 |
free(m->joined);
|
|
219 |
m->joined = tempJoined;
|
|
206 | 220 |
} |
207 | 221 |
|
208 | 222 |
/** |
... | ... | |
213 | 227 |
* @param robot the id of the robot who the reading is for |
214 | 228 |
* @param reading the BOM reading from observer to robot |
215 | 229 |
*/ |
216 |
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading) { |
|
217 |
if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) |
|
218 |
sensor_matrix_add_robot(m, observer); |
|
219 |
m->matrix[observer][robot] = reading; |
|
230 |
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading) |
|
231 |
{ |
|
232 |
if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) |
|
233 |
sensor_matrix_add_robot(m, observer); |
|
234 |
m->matrix[observer][robot] = reading; |
|
220 | 235 |
} |
221 | 236 |
|
222 | 237 |
/** |
... | ... | |
228 | 243 |
* |
229 | 244 |
* @return the observer's BOM reading for robot |
230 | 245 |
**/ |
231 |
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot) { |
|
232 |
if (observer >= m->size || robot >= m->size) |
|
233 |
return -1; |
|
234 |
return m->matrix[observer][robot]; |
|
246 |
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot) |
|
247 |
{ |
|
248 |
if (observer >= m->size || robot >= m->size) |
|
249 |
return -1; |
|
250 |
return m->matrix[observer][robot]; |
|
235 | 251 |
} |
236 | 252 |
|
237 | 253 |
/** |
... | ... | |
241 | 257 |
* @param robot the robot to set as a member / nonmember of the token ring |
242 | 258 |
* @param in 1 if the robot is in the token ring, 0 otherwise |
243 | 259 |
**/ |
244 |
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in) { |
|
245 |
if (robot >= m->size) |
|
246 |
sensor_matrix_expand(m, robot + 1); |
|
247 |
if (in == 1) |
|
248 |
sensor_matrix_add_robot(m, robot); |
|
249 |
if (in == 1 && m->joined[robot] == 0) |
|
250 |
m->numJoined++; |
|
251 |
if (in == 0 && m->joined[robot] == 1) |
|
252 |
m->numJoined--; |
|
253 |
m->joined[robot] = in; |
|
260 |
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in) |
|
261 |
{ |
|
262 |
if (robot >= m->size) |
|
263 |
sensor_matrix_expand(m, robot + 1); |
|
264 |
if (in == 1) |
|
265 |
sensor_matrix_add_robot(m, robot); |
|
266 |
if (in == 1 && m->joined[robot] == 0) |
|
267 |
m->numJoined++; |
|
268 |
if (in == 0 && m->joined[robot] == 1) |
|
269 |
m->numJoined--; |
|
270 |
m->joined[robot] = in; |
|
254 | 271 |
} |
255 | 272 |
|
256 | 273 |
/** |
... | ... | |
261 | 278 |
* |
262 | 279 |
* @return 1 if the robot is in the token ring, 0 otherwise |
263 | 280 |
**/ |
264 |
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot) { |
|
265 |
if (robot >= m->size) |
|
266 |
return -1; |
|
267 |
return m->joined[robot]; |
|
281 |
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot) |
|
282 |
{ |
|
283 |
if (robot >= m->size) |
|
284 |
return -1; |
|
285 |
return m->joined[robot]; |
|
268 | 286 |
} |
269 | 287 |
|
270 | 288 |
/** |
... | ... | |
274 | 292 |
* |
275 | 293 |
* @return the size of the sensor matrix |
276 | 294 |
**/ |
277 |
int sensor_matrix_get_size(SensorMatrix* m) { |
|
278 |
return m->size; |
|
295 |
int sensor_matrix_get_size(SensorMatrix* m) |
|
296 |
{ |
|
297 |
return m->size; |
|
279 | 298 |
} |
280 | 299 |
|
281 | 300 |
/** |
... | ... | |
286 | 305 |
* |
287 | 306 |
* @return the number of robots in the token ring |
288 | 307 |
**/ |
289 |
int sensor_matrix_get_joined(SensorMatrix* m) { |
|
290 |
return m->numJoined; |
|
308 |
int sensor_matrix_get_joined(SensorMatrix* m) |
|
309 |
{ |
|
310 |
return m->numJoined; |
|
291 | 311 |
} |
292 | 312 |
|
Also available in: Unified diff