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