Project

General

Profile

Revision 668

Updated port of wireless library to bay boards.

View differences:

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
......
41 41
#define DEFAULT_SENSOR_MATRIX_SIZE 20
42 42

  
43 43
/*Sensor Matrix Functions*/
44
void sensor_matrix_expand(SensorMatrix* m, int nextSize);
44
static void sensor_matrix_expand(SensorMatrix* m, int nextSize);
45 45

  
46 46
/**
47 47
 * Initializes the sensor matrix.
......
52 52
{
53 53
	SensorMatrix* m;
54 54
	int i;
55
	
55

  
56 56
	m = (SensorMatrix*)malloc(sizeof(SensorMatrix));
57 57
	if (!m)
58 58
	{
......
61 61
	}
62 62
	m->size = DEFAULT_SENSOR_MATRIX_SIZE;
63 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
	}
64 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
	}
65 76
	m->numJoined = 0;
66 77
	if (!(m->matrix) || !(m->joined))
67 78
	{
68 79
		WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n");
69 80
		return NULL;
70 81
	}
71
	
82

  
72 83
	for (i = 0; i < m->size; i++)
73 84
	{
74 85
		m->matrix[i] = NULL;
......
95 106

  
96 107
/**
97 108
 * Adds robot with XBee id id to the sensor matrix.
98
 * 
109
 *
99 110
 * @param m the sensor matrix
100 111
 * @param id the XBee ID of the robot to add
101 112
 **/
102 113
void sensor_matrix_add_robot(SensorMatrix* m, int id)
103 114
{
104 115
	int i;
105
	if (id >= m->size)
116
	if (id >= m->size) {
106 117
		sensor_matrix_expand(m, id + 1);
107
	if (m->matrix[id] != NULL)
118
	}
119

  
120
	if (m->matrix[id] != NULL) {
108 121
		return;
109
	
122
	}
123

  
110 124
	m->matrix[id] = (int*)malloc(m->size * sizeof(int));
111 125
	if (!(m->matrix[id]))
112 126
	{
......
114 128
		return;
115 129
	}
116 130

  
117
	for (i = 0; i < m->size; i++)
118
		if (m->matrix[i] != NULL)
131
	for (i = 0; i < m->size; i++) {
132
		if (m->matrix[i] != NULL) {
119 133
			m->matrix[i][id] = -1;
134
		}
135
	}
120 136
}
121 137

  
122 138
/**
......
135 151
		WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
136 152
		return;
137 153
	}
138
	
154

  
139 155
	free(m->matrix[id]);
140 156
	m->matrix[id] = NULL;
141
	
157

  
142 158
	for (i = 0 ; i < m->size; i++)
143 159
		if (m->matrix[i] != NULL)
144 160
			m->matrix[i][id] = -1;
145
	
161

  
146 162
	m->joined[id] = 0;
147 163
}
148 164

  
......
154 170
 * @param size the new size of the sensor matrix
155 171
 **/
156 172
//Note: this has probably not been tested, hopefully it works
157
void sensor_matrix_expand(SensorMatrix* m, int nextSize)
173
static void sensor_matrix_expand(SensorMatrix* m, int nextSize)
158 174
{
159 175
	int i, j;
160 176
	WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
161
	
177

  
162 178
	int** tempMatrix = (int**)malloc(nextSize * sizeof(int*));
163 179
	if (!tempMatrix)
164 180
	{
165 181
		WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n");
166 182
		return;
167 183
	}
168
	
184

  
169 185
	for (i = 0; i < nextSize; i++)
170 186
		tempMatrix[i] = NULL;
171
	
187

  
172 188
	//copy over old sensor data
173 189
	for (i = 0; i < m->size; i++)
174 190
		if (m->matrix[i] != NULL)
......
179 195
				WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n");
180 196
				return;
181 197
			}
182
			for (j = 0; j < m->size; j++)
198
			for (j = 0; j < m->size; j++) {
183 199
				tempMatrix[i][j] = m->matrix[i][j];
184
			for (j = m->size; j < nextSize; j++)
200
			}
201

  
202
			for (j = m->size; j < nextSize; j++) {
185 203
				tempMatrix[i][j] = -1;
186
			
204
			}
205

  
187 206
			free(m->matrix[i]);
188 207
		}
189
	
208

  
190 209
	free(m->matrix);
191 210
	m->matrix = tempMatrix;
192 211
	m->size = nextSize;
......
198 217
		WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n");
199 218
		return;
200 219
	}
201
	
202
	for (i = 0; i < m->size; i++)
220

  
221
	for (i = 0; i < m->size; i++) {
203 222
		tempJoined[i] = m->joined[i];
204
	for (i = m->size; i < nextSize; i++)
223
	}
224

  
225
	for (i = m->size; i < nextSize; i++) {
205 226
		tempJoined[i] = 0;
206
	
227
	}
228

  
207 229
	free(m->joined);
208 230
	m->joined = tempJoined;
209 231
}
210 232

  
211 233
/**
212 234
 * Sets the sensor reading for robot robot to reading.
213
 * 
235
 *
214 236
 * @param m the sensor matrix to set the reading for
215 237
 * @param observer the id of the robot who made the reading
216 238
 * @param robot the id of the robot who the reading is for
......
218 240
 */
219 241
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading)
220 242
{
221
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL)
243
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) {
222 244
		sensor_matrix_add_robot(m, observer);
245
	}
246

  
223 247
	m->matrix[observer][robot] = reading;
224 248
}
225 249

  
......
234 258
 **/
235 259
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
236 260
{
237
	if (observer >= m->size || robot >= m->size)
261
	if (observer >= m->size || robot >= m->size) {
238 262
		return -1;
263
	}
264

  
239 265
	return m->matrix[observer][robot];
240 266
}
241 267

  
......
248 274
 **/
249 275
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
250 276
{
251
	if (robot >= m->size)
277
	if (robot >= m->size) {
252 278
		sensor_matrix_expand(m, robot + 1);
253
	if (in == 1)
279
	}
280

  
281
	if (in == 1) {
254 282
		sensor_matrix_add_robot(m, robot);
255
	if (in == 1 && m->joined[robot] == 0)
283
	}
284

  
285
	if (in == 1 && m->joined[robot] == 0) {
256 286
		m->numJoined++;
257
	if (in == 0 && m->joined[robot] == 1)
287
	}
288

  
289
	if (in == 0 && m->joined[robot] == 1) {
258 290
		m->numJoined--;
291
	}
292

  
259 293
	m->joined[robot] = in;
260 294
}
261 295

  
262 296
/**
263 297
 * Checks if the given robot is in the token ring.
264
 * 
298
 *
265 299
 * @param m the sensor matrix
266 300
 * @param robot the ID of the robot to check
267 301
 *
......
269 303
 **/
270 304
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
271 305
{
272
	if (robot >= m->size)
306
	if (robot >= m->size) {
273 307
		return -1;
308
	}
309

  
274 310
	return m->joined[robot];
275 311
}
276 312

  
......
278 314
 * Returns the size of the sensor matrix.
279 315
 *
280 316
 * @param m the sensor matrix
281
 * 
317
 *
282 318
 * @return the size of the sensor matrix
283 319
 **/
284 320
int sensor_matrix_get_size(SensorMatrix* m)
......
298 334
{
299 335
	return m->numJoined;
300 336
}
301

  

Also available in: Unified diff