Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / sensor_matrix.c @ 309

History | View | Annotate | Download (7.17 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26
/**
27
 * @file sensor_matrix.c
28
 * @brief Sensor Matrix implementation
29
 *
30
 * Implementation of a sensor matrix for storing localization implementation.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34

    
35
#include <stdlib.h>
36
#include <stdio.h>
37
#include <wl_defs.h>
38

    
39
#include "sensor_matrix.h"
40

    
41
#define DEFAULT_SENSOR_MATRIX_SIZE 20
42

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

    
46
/**
47
 * Initializes the sensor matrix.
48
 *
49
 * @return the newly created sensor matrix
50
 **/
51
SensorMatrix* sensor_matrix_create()
52
{
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
}
79

    
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
}
95

    
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]))
112
        {
113
                WL_DEBUG_PRINT("Out of memory - add robot.\r\n");
114
                return;
115
        }
116

    
117
        for (i = 0; i < m->size; i++)
118
                if (m->matrix[i] != NULL)
119
                        m->matrix[i][id] = -1;
120
}
121

    
122
/**
123
 * Removes robot with id from the sensor matrix, and removes
124
 * all sensor information regarding the robot.
125
 *
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)
130
{
131
        int i;
132

    
133
        if (id >= m->size || m->matrix[id] == NULL)
134
        {
135
                WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
136
                return;
137
        }
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

    
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;
209
}
210

    
211
/**
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
 * Gets the sensor reading for a robot to another robot.
228
 *
229
 * @param m the sensor matrix
230
 * @param observer the robot whose reading we check
231
 * @param robot the robot who we are checking the reading to
232
 *
233
 * @return the observer's BOM reading for robot
234
 **/
235
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
236
{
237
        if (observer >= m->size || robot >= m->size)
238
                return -1;
239
        return m->matrix[observer][robot];
240
}
241

    
242
/**
243
 * Sets whether or not the given robot is part of the token ring.
244
 *
245
 * @param m the sensor matrix
246
 * @param robot the robot to set as a member / nonmember of the token ring
247
 * @param in 1 if the robot is in the token ring, 0 otherwise
248
 **/
249
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
250
{
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;
260
}
261

    
262
/**
263
 * Checks if the given robot is in the token ring.
264
 * 
265
 * @param m the sensor matrix
266
 * @param robot the ID of the robot to check
267
 *
268
 * @return 1 if the robot is in the token ring, 0 otherwise
269
 **/
270
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
271
{
272
        if (robot >= m->size)
273
                return -1;
274
        return m->joined[robot];
275
}
276

    
277
/**
278
 * Returns the size of the sensor matrix.
279
 *
280
 * @param m the sensor matrix
281
 * 
282
 * @return the size of the sensor matrix
283
 **/
284
int sensor_matrix_get_size(SensorMatrix* m)
285
{
286
        return m->size;
287
}
288

    
289
/**
290
 * Returns the number of robots which have joined the
291
 * token ring.
292
 *
293
 * @param m the sensor matrix
294
 *
295
 * @return the number of robots in the token ring
296
 **/
297
int sensor_matrix_get_joined(SensorMatrix* m)
298
{
299
        return m->numJoined;
300
}
301