Project

General

Profile

Revision 343

Added by Jason knichel over 16 years ago

changed the brace style and reformatted the files

View differences:

sensor_matrix.c
48 48
 *
49 49
 * @return the newly created sensor matrix
50 50
 **/
51
SensorMatrix* sensor_matrix_create()
52
{
53
	SensorMatrix* m;
54
	int i;
51
SensorMatrix* sensor_matrix_create() {
52
  SensorMatrix* m;
53
  int i;
55 54
	
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
	}
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
  }
82 79
	
83
	for (i = 0; i < m->size; i++)
84
	{
85
		m->matrix[i] = NULL;
86
		m->joined[i] = 0;
87
	}
88
	return m;
80
  for (i = 0; i < m->size; i++) {
81
    m->matrix[i] = NULL;
82
    m->joined[i] = 0;
83
  }
84
  return m;
89 85
}
90 86

  
91 87
/**
......
93 89
 *
94 90
 * @param m the sensor matrix to delete
95 91
 **/
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);
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);
105 100
}
106 101

  
107 102
/**
......
110 105
 * @param m the sensor matrix
111 106
 * @param id the XBee ID of the robot to add
112 107
 **/
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;
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;
120 114
	
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
	}
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
  }
127 120

  
128
	for (i = 0; i < m->size; i++)
129
		if (m->matrix[i] != NULL)
130
			m->matrix[i][id] = -1;
121
  for (i = 0; i < m->size; i++)
122
    if (m->matrix[i] != NULL)
123
      m->matrix[i][id] = -1;
131 124
}
132 125

  
133 126
/**
......
137 130
 * @param m the sensor matrix
138 131
 * @param id the XBee ID of the robot to remove
139 132
 **/
140
void sensor_matrix_remove_robot(SensorMatrix* m, int id)
141
{
142
	int i;
133
void sensor_matrix_remove_robot(SensorMatrix* m, int id) {
134
  int i;
143 135

  
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
	}
136
  if (id >= m->size || m->matrix[id] == NULL) {
137
    WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
138
    return;
139
  }
149 140
	
150
	free(m->matrix[id]);
151
	m->matrix[id] = NULL;
141
  free(m->matrix[id]);
142
  m->matrix[id] = NULL;
152 143
	
153
	for (i = 0 ; i < m->size; i++)
154
		if (m->matrix[i] != NULL)
155
			m->matrix[i][id] = -1;
144
  for (i = 0 ; i < m->size; i++)
145
    if (m->matrix[i] != NULL)
146
      m->matrix[i][id] = -1;
156 147
	
157
	m->joined[id] = 0;
148
  m->joined[id] = 0;
158 149
}
159 150

  
160 151
/**
......
165 156
 * @param size the new size of the sensor matrix
166 157
 **/
167 158
//Note: this has probably not been tested, hopefully it works
168
void sensor_matrix_expand(SensorMatrix* m, int nextSize)
169
{
170
	int i, j;
171
	WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
159
void sensor_matrix_expand(SensorMatrix* m, int nextSize) {
160
  int i, j;
161
  WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
172 162
	
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
	}
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
  }
179 168
	
180
	for (i = 0; i < nextSize; i++)
181
		tempMatrix[i] = NULL;
169
  for (i = 0; i < nextSize; i++)
170
    tempMatrix[i] = NULL;
182 171
	
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;
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;
197 184
			
198
			free(m->matrix[i]);
199
		}
185
      free(m->matrix[i]);
186
    }
200 187
	
201
	free(m->matrix);
202
	m->matrix = tempMatrix;
203
	m->size = nextSize;
188
  free(m->matrix);
189
  m->matrix = tempMatrix;
190
  m->size = nextSize;
204 191

  
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
	}
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
  }
212 198
	
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;
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;
217 203
	
218
	free(m->joined);
219
	m->joined = tempJoined;
204
  free(m->joined);
205
  m->joined = tempJoined;
220 206
}
221 207

  
222 208
/**
......
227 213
 * @param robot the id of the robot who the reading is for
228 214
 * @param reading the BOM reading from observer to robot
229 215
 */
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;
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;
235 220
}
236 221

  
237 222
/**
......
243 228
 *
244 229
 * @return the observer's BOM reading for robot
245 230
 **/
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];
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];
251 235
}
252 236

  
253 237
/**
......
257 241
 * @param robot the robot to set as a member / nonmember of the token ring
258 242
 * @param in 1 if the robot is in the token ring, 0 otherwise
259 243
 **/
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;
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;
271 254
}
272 255

  
273 256
/**
......
278 261
 *
279 262
 * @return 1 if the robot is in the token ring, 0 otherwise
280 263
 **/
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];
264
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot) {
265
  if (robot >= m->size)
266
    return -1;
267
  return m->joined[robot];
286 268
}
287 269

  
288 270
/**
......
292 274
 * 
293 275
 * @return the size of the sensor matrix
294 276
 **/
295
int sensor_matrix_get_size(SensorMatrix* m)
296
{
297
	return m->size;
277
int sensor_matrix_get_size(SensorMatrix* m) {
278
  return m->size;
298 279
}
299 280

  
300 281
/**
......
305 286
 *
306 287
 * @return the number of robots in the token ring
307 288
 **/
308
int sensor_matrix_get_joined(SensorMatrix* m)
309
{
310
	return m->numJoined;
289
int sensor_matrix_get_joined(SensorMatrix* m) {
290
  return m->numJoined;
311 291
}
312 292

  

Also available in: Unified diff