Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / libwireless / lib / sensor_matrix.c @ 793

History | View | Annotate | Download (3.76 KB)

1 340 bcoltin
/**
2
 * Copyright (c) 2007 Colony Project
3 668 bcoltin
 *
4 340 bcoltin
 * 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 668 bcoltin
 *
13 340 bcoltin
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15 668 bcoltin
 *
16 340 bcoltin
 * 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 17 bcoltin
#include <stdlib.h>
36
#include <stdio.h>
37
#include <wl_defs.h>
38
39
#include "sensor_matrix.h"
40
41 793 bcoltin
// the global sensor matrix
42
SensorMatrix m;
43 17 bcoltin
44
/**
45
 * Initializes the sensor matrix.
46
 *
47
 * @return the newly created sensor matrix
48
 **/
49 793 bcoltin
void sensor_matrix_create()
50 17 bcoltin
{
51
        int i, j;
52 668 bcoltin
53 793 bcoltin
        m.numJoined = 0;
54 668 bcoltin
55 793 bcoltin
        for (i = 0; i < MAXIMUM_XBEE_ID; i++)
56 17 bcoltin
        {
57 793 bcoltin
                m.joined[i] = 0;
58
                for (j = 0; j < MAXIMUM_XBEE_ID; j++)
59
                        m.matrix[i][j] = READING_UNKNOWN;
60 17 bcoltin
        }
61
}
62
63
/**
64
 * Sets the sensor reading for robot robot to reading.
65 668 bcoltin
 *
66 17 bcoltin
 * @param observer the id of the robot who made the reading
67
 * @param robot the id of the robot who the reading is for
68
 * @param reading the BOM reading from observer to robot
69
 */
70 793 bcoltin
void sensor_matrix_set_reading(int observer, int robot, int reading)
71 17 bcoltin
{
72 793 bcoltin
        if (robot >= MAXIMUM_XBEE_ID || observer >= MAXIMUM_XBEE_ID)
73
        {
74
                WL_DEBUG_PRINT("ID too large.");
75
                return;
76 668 bcoltin
        }
77
78 793 bcoltin
        m.matrix[observer][robot] = (unsigned char)reading;
79 17 bcoltin
}
80
81
/**
82
 * Gets the sensor reading for a robot to another robot.
83
 *
84
 * @param observer the robot whose reading we check
85
 * @param robot the robot who we are checking the reading to
86
 *
87
 * @return the observer's BOM reading for robot
88
 **/
89 793 bcoltin
int sensor_matrix_get_reading(int observer, int robot)
90 17 bcoltin
{
91 793 bcoltin
        if (observer >= MAXIMUM_XBEE_ID || robot >= MAXIMUM_XBEE_ID)
92 17 bcoltin
                return -1;
93 668 bcoltin
94 793 bcoltin
        return (int)m.matrix[observer][robot];
95 17 bcoltin
}
96
97
/**
98
 * Sets whether or not the given robot is part of the token ring.
99
 *
100
 * @param robot the robot to set as a member / nonmember of the token ring
101
 * @param in 1 if the robot is in the token ring, 0 otherwise
102
 **/
103 793 bcoltin
void sensor_matrix_set_in_ring(int robot, int in)
104 17 bcoltin
{
105 793 bcoltin
        if (robot >= MAXIMUM_XBEE_ID)
106
        {
107
                WL_DEBUG_PRINT("ID too large.");
108
                return;
109 668 bcoltin
        }
110
111 793 bcoltin
        if (in == 1 && m.joined[robot] == 0)
112
                m.numJoined++;
113 668 bcoltin
114 793 bcoltin
        if (in == 0 && m.joined[robot])
115
                m.numJoined--;
116
        m.joined[robot] = in;
117 17 bcoltin
}
118
119
/**
120
 * Checks if the given robot is in the token ring.
121 668 bcoltin
 *
122 17 bcoltin
 * @param robot the ID of the robot to check
123
 *
124
 * @return 1 if the robot is in the token ring, 0 otherwise
125
 **/
126 793 bcoltin
int sensor_matrix_get_in_ring(int robot)
127 17 bcoltin
{
128 793 bcoltin
        if (robot >= MAXIMUM_XBEE_ID)
129 17 bcoltin
                return -1;
130 668 bcoltin
131 793 bcoltin
        return m.joined[robot];
132 17 bcoltin
}
133
134
/**
135 793 bcoltin
 * Returns the number of robots which have joined the
136
 * token ring.
137 17 bcoltin
 *
138 793 bcoltin
 * @return the number of robots in the token ring
139 17 bcoltin
 **/
140 793 bcoltin
int sensor_matrix_get_joined(void)
141 17 bcoltin
{
142 793 bcoltin
        return m.numJoined;
143 17 bcoltin
}
144
145
/**
146 793 bcoltin
 * Returns the maximum XBee id of a robot which can be stored
147
 * in the sensor matrix.
148 17 bcoltin
 *
149 793 bcoltin
 * @return the maximum number of robots which can be stored
150 17 bcoltin
 **/
151 793 bcoltin
int sensor_matrix_get_size(void)
152 17 bcoltin
{
153 793 bcoltin
        return MAXIMUM_XBEE_ID;
154 17 bcoltin
}