Revision 743
Fixed compilation errors.
branches/autonomous_recharging/code/lib/include/libwireless/sensor_matrix.h | ||
---|---|---|
1 | 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 |
/** |
|
2 | 27 |
* @file sensor_matrix.h |
3 | 28 |
* @brief Definitions for sensor matrices |
4 | 29 |
* |
... | ... | |
7 | 32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
8 | 33 |
**/ |
9 | 34 |
|
35 |
#ifndef SENSOR_MATRIX_H |
|
36 |
#define SENSOR_MATRIX_H |
|
37 |
|
|
38 |
|
|
10 | 39 |
/** |
11 | 40 |
* @defgroup sensormatrix Sensor Matrix |
12 | 41 |
* @brief the robot sensor matrix |
... | ... | |
27 | 56 |
/** |
28 | 57 |
* The size of the sensor matrix. |
29 | 58 |
**/ |
30 |
unsigned int size;
|
|
59 |
int size; |
|
31 | 60 |
/** |
32 | 61 |
* The matrix. Each row represents the readings of one |
33 | 62 |
* robot. |
... | ... | |
49 | 78 |
/**@brief Destroy a sensor matrix **/ |
50 | 79 |
void sensor_matrix_destroy(SensorMatrix* m); |
51 | 80 |
/**@brief Add a robot to a sensor matrix **/ |
52 |
void sensor_matrix_add_robot(SensorMatrix* m, unsigned int id);
|
|
81 |
void sensor_matrix_add_robot(SensorMatrix* m, int id); |
|
53 | 82 |
/**@brief Remove a robot from a sensor matrix **/ |
54 |
void sensor_matrix_remove_robot(SensorMatrix* m, unsigned int id);
|
|
83 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id); |
|
55 | 84 |
/**@brief Set a reading in a sensor matrix **/ |
56 | 85 |
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading); |
57 | 86 |
/**@brief Get a reading in a sensor matrix **/ |
... | ... | |
67 | 96 |
|
68 | 97 |
/** @} **/ //end defgroup |
69 | 98 |
|
99 |
|
|
100 |
#endif |
branches/autonomous_recharging/code/lib/include/libwireless/wireless.h | ||
---|---|---|
1 | 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 |
/** |
|
2 | 27 |
* @file wireless.h |
3 | 28 |
* @brief Contains definitions for the wireless library. |
4 | 29 |
* |
... | ... | |
6 | 31 |
* |
7 | 32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
8 | 33 |
**/ |
34 |
|
|
35 |
#ifndef WIRELESS_H |
|
36 |
#define WIRELESS_H |
|
9 | 37 |
|
10 | 38 |
//Note: If this is raised above 16, we will need to do |
11 | 39 |
//something about frame numbers for TX Status packets. |
... | ... | |
90 | 118 |
} PacketGroupHandler; |
91 | 119 |
|
92 | 120 |
/**@brief Initialize the wireless library **/ |
93 |
void wl_init(void);
|
|
121 |
int wl_init(void);
|
|
94 | 122 |
/**@brief Uninitialize the wireless library **/ |
95 | 123 |
void wl_terminate(void); |
96 | 124 |
/**@brief Perform wireless library functionality **/ |
... | ... | |
122 | 150 |
/**@brief Get the channel we are using **/ |
123 | 151 |
int wl_get_channel(void); |
124 | 152 |
/**@brief Get the 16-bit address of the XBee module **/ |
125 |
unsigned int wl_get_xbee_id(void); |
|
153 |
int wl_get_xbee_id(void); |
|
154 |
/**@brief Set the com port on a computer, undefined on the robot.**/ |
|
155 |
void wl_set_com_port(char* port); |
|
126 | 156 |
|
127 | 157 |
/** @} **/ // end defgroup |
128 | 158 |
|
159 |
#endif |
|
160 |
|
branches/autonomous_recharging/code/lib/include/libwireless/wl_token_ring.h | ||
---|---|---|
1 | 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 |
/** |
|
2 | 27 |
* @file wl_token_ring.h |
3 | 28 |
* @brief Declarations for the token ring packet group |
4 | 29 |
* |
... | ... | |
7 | 32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
8 | 33 |
**/ |
9 | 34 |
|
35 |
#ifndef WL_TOKEN_RING_H |
|
36 |
#define WL_TOKEN_RING_H |
|
37 |
|
|
10 | 38 |
/** |
11 | 39 |
* @defgroup tokenring Token Ring |
12 | 40 |
* @brief Wireless library token ring implementation |
... | ... | |
30 | 58 |
void wl_token_ring_join(void); |
31 | 59 |
/**@brief Leave the token ring **/ |
32 | 60 |
void wl_token_ring_leave(void); |
33 |
/**@brief Request a BOM flash from the specified robot **/ |
|
34 |
void wl_token_request(int robot); |
|
35 | 61 |
|
36 | 62 |
/**@brief Return the number of robots in the token ring **/ |
37 | 63 |
int wl_token_get_robots_in_ring(void); |
... | ... | |
56 | 82 |
|
57 | 83 |
/** @} **/ //end token ring group |
58 | 84 |
|
85 |
#endif |
branches/autonomous_recharging/code/lib/include/libwireless/wl_defs.h | ||
---|---|---|
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 wl_defs.h |
|
28 |
* @brief Definitions for Wireless |
|
29 |
* |
|
30 |
* Contains definitions for wireless packet groups, packet types, |
|
31 |
* debugging information, etc. |
|
32 |
* |
|
33 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
|
|
36 |
#ifndef WL_DEFS_H |
|
37 |
#define WL_DEFS_H |
|
38 |
|
|
1 | 39 |
//comment out this line if using a computer hooked up to an xbee |
2 | 40 |
//#define ROBOT |
3 | 41 |
|
... | ... | |
16 | 54 |
|
17 | 55 |
#define WL_TOKEN_PASS 1 |
18 | 56 |
#define WL_TOKEN_BOM_ON 2 |
19 |
#define WL_TOKEN_INTERRUPT_REQUEST 3 |
|
20 |
#define WL_TOKEN_INTERRUPT_PASS 4 |
|
21 |
#define WL_TOKEN_JOIN 5 |
|
22 |
#define WL_TOKEN_JOIN_ACCEPT 6 |
|
57 |
#define WL_TOKEN_JOIN 3 |
|
58 |
#define WL_TOKEN_JOIN_ACCEPT 4 |
|
23 | 59 |
|
24 | 60 |
// timing constants |
61 |
#ifndef FIREFLY |
|
25 | 62 |
#define BOM_DELAY 100 |
26 |
#define DEATH_DELAY 3 |
|
63 |
#else |
|
64 |
#define BOM_DELAY 200 |
|
65 |
#endif |
|
66 |
|
|
67 |
#define DEATH_DELAY 4 |
|
27 | 68 |
#define JOIN_DELAY 8 |
28 | 69 |
|
29 | 70 |
// Recharging group |
... | ... | |
63 | 104 |
|
64 | 105 |
#endif |
65 | 106 |
|
107 |
#endif |
|
108 |
|
branches/autonomous_recharging/code/lib/include/libwireless/xbee.h | ||
---|---|---|
1 | 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 |
/** |
|
2 | 27 |
* @file xbee.h |
3 | 28 |
* @brief Contains definitions for using the XBee |
4 | 29 |
* |
... | ... | |
10 | 35 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
11 | 36 |
**/ |
12 | 37 |
|
38 |
#ifndef XBEE_H |
|
39 |
#define XBEE_H |
|
40 |
|
|
13 | 41 |
/** |
14 | 42 |
* The port to use the XBee from on the computer. |
15 | 43 |
* Also, a backup port if the other is used. |
16 | 44 |
**/ |
17 | 45 |
#ifndef ROBOT |
18 |
#ifndef XBEE_PORT |
|
19 |
#define XBEE_PORT "/dev/ttyUSB0" |
|
46 |
#define XBEE_PORT_DEFAULT "/dev/ttyUSB1" |
|
20 | 47 |
#endif |
21 |
#define XBEE_PORT2 "/dev/tty/USB1" |
|
22 |
#endif |
|
23 | 48 |
|
24 | 49 |
/** |
25 | 50 |
* @defgroup xbee XBee |
... | ... | |
49 | 74 |
#define XBEE_RX 0x81 |
50 | 75 |
|
51 | 76 |
/**@brief Initialize the XBee library **/ |
52 |
void xbee_lib_init(void);
|
|
77 |
int xbee_lib_init(void);
|
|
53 | 78 |
/**@brief Uninitialize the XBee library **/ |
54 | 79 |
void xbee_terminate(void); |
55 | 80 |
/**@brief Get a packet from the XBee **/ |
... | ... | |
67 | 92 |
int xbee_get_channel(void); |
68 | 93 |
/**@brief Get the XBee's 16-bit address **/ |
69 | 94 |
unsigned int xbee_get_address(void); |
95 |
/**@brief Set the com port on a computer, undefined on the robot**/ |
|
96 |
void xbee_set_com_port(char* port); |
|
70 | 97 |
|
71 | 98 |
/**@}**/ //end defgroup |
72 | 99 |
|
100 |
#endif |
branches/autonomous_recharging/code/lib/include/libwireless/wl_error_group.h | ||
---|---|---|
1 | 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 |
/** |
|
2 | 27 |
* @file wl_error_group.h |
28 |
* @brief Error Packets |
|
29 |
* |
|
30 |
* A packet group for sending error packets. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
35 |
#ifndef WL_ERROR_GROUP_H |
|
36 |
#define WL_ERROR_GROUP_H |
|
37 |
|
|
38 |
/** |
|
39 |
* @file wl_error_group.h |
|
3 | 40 |
* @brief A packet group for error messages. |
4 | 41 |
* |
5 | 42 |
* A packet group for sending and receiving error |
... | ... | |
26 | 63 |
|
27 | 64 |
/** @} **/ // end defgroup |
28 | 65 |
|
66 |
#endif |
branches/autonomous_recharging/code/lib/src/libwireless/wl_token_ring.c | ||
---|---|---|
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 wl_token_ring.c |
|
28 |
* @brief Token Ring Implementation |
|
29 |
* |
|
30 |
* Implementation of the token ring packet group. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
1 | 35 |
#include <wl_token_ring.h> |
2 | 36 |
|
3 | 37 |
#include <stdlib.h> |
... | ... | |
6 | 40 |
#include <wl_defs.h> |
7 | 41 |
#include <wireless.h> |
8 | 42 |
#include <sensor_matrix.h> |
9 |
#include <queue.h> |
|
10 | 43 |
|
11 | 44 |
#ifdef ROBOT |
12 | 45 |
#ifndef FIREFLY |
... | ... | |
44 | 77 |
|
45 | 78 |
/*Packet Handling Routines*/ |
46 | 79 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
47 |
void wl_token_interrupt_request_receive(int source, int robot); |
|
48 |
void wl_token_interrupt_pass_receive(int source, int robot); |
|
49 | 80 |
void wl_token_bom_on_receive(int source); |
50 | 81 |
void wl_token_join_receive(int source); |
51 | 82 |
void wl_token_join_accept_receive(int source); |
... | ... | |
69 | 100 |
int deathDelay = -1; |
70 | 101 |
//the counter for joining, before we form our own token ring |
71 | 102 |
int joinDelay = -1; |
72 |
//queue containing ids of interruption requests |
|
73 |
Queue* interrupting = NULL; |
|
74 | 103 |
|
75 | 104 |
//current robot to check in the iterator |
76 | 105 |
int iteratorCount = 0; |
77 | 106 |
|
107 |
// the amount of time a robot has had its BOM on for |
|
108 |
int bom_on_count = 0; |
|
109 |
|
|
78 | 110 |
void do_nothing(void) {} |
79 | 111 |
int get_nothing(void) {return -1;} |
80 | 112 |
|
... | ... | |
116 | 148 |
} |
117 | 149 |
|
118 | 150 |
sensorMatrix = sensor_matrix_create(); |
119 |
interrupting = queue_create(); |
|
120 | 151 |
//add ourselves to the sensor matrix |
121 | 152 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
122 | 153 |
|
... | ... | |
157 | 188 |
void wl_token_ring_cleanup() |
158 | 189 |
{ |
159 | 190 |
sensor_matrix_destroy(sensorMatrix); |
160 |
queue_destroy(interrupting); |
|
161 | 191 |
} |
162 | 192 |
|
163 | 193 |
/** |
... | ... | |
176 | 206 |
WL_DEBUG_PRINT("Robot "); |
177 | 207 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
178 | 208 |
WL_DEBUG_PRINT(" has died.\r\n"); |
209 |
wl_token_next_robot = -1; |
|
210 |
deathDelay = DEATH_DELAY; |
|
179 | 211 |
} |
180 | 212 |
|
181 | 213 |
// we may have been dropped from the ring when this is received |
... | ... | |
209 | 241 |
deathDelay--; |
210 | 242 |
if (joinDelay >= 0) |
211 | 243 |
joinDelay--; |
244 |
if (bom_on_count >= 0) |
|
245 |
bom_on_count++; |
|
212 | 246 |
} |
213 | 247 |
|
214 | 248 |
/** |
... | ... | |
249 | 283 |
//add the robot to the sensor matrix if it is not already there |
250 | 284 |
wl_token_bom_on_receive(source); |
251 | 285 |
break; |
252 |
case WL_TOKEN_INTERRUPT_REQUEST: |
|
253 |
wl_token_interrupt_request_receive(source, packet[0]); |
|
254 |
break; |
|
255 |
case WL_TOKEN_INTERRUPT_PASS: |
|
256 |
wl_token_interrupt_pass_receive(source, packet[0]); |
|
257 |
break; |
|
258 | 286 |
case WL_TOKEN_JOIN: |
259 | 287 |
wl_token_join_receive(source); |
260 | 288 |
break; |
... | ... | |
276 | 304 |
{ |
277 | 305 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
278 | 306 |
ringState = JOINING; |
279 |
joinDelay = JOIN_DELAY;
|
|
307 |
joinDelay = DEATH_DELAY * 2;
|
|
280 | 308 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, |
281 | 309 |
NULL, 0, 0); |
282 | 310 |
} |
... | ... | |
292 | 320 |
} |
293 | 321 |
|
294 | 322 |
/** |
295 |
* Requests that the specified robot be given the token and |
|
296 |
* allowed to flash its BOM. After its BOM is flashed, the |
|
297 |
* token will return to the robot who sent it. |
|
298 |
* |
|
299 |
* @param robot the ID of the robot which should flash its BOM |
|
300 |
**/ |
|
301 |
void wl_token_request(int robot) |
|
302 |
{ |
|
303 |
char buf[1]; |
|
304 |
buf[0] = robot; |
|
305 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_REQUEST, |
|
306 |
buf, 1, 0); |
|
307 |
} |
|
308 |
|
|
309 |
/** |
|
310 | 323 |
* Returns the BOM reading robot source has for robot dest. |
311 | 324 |
* |
312 | 325 |
* @param source the robot that made the BOM reading |
... | ... | |
317 | 330 |
**/ |
318 | 331 |
int wl_token_get_sensor_reading(int source, int dest) |
319 | 332 |
{ |
320 |
return sensor_matrix_get_reading(sensorMatrix, source, dest); |
|
333 |
if (wl_token_is_robot_in_ring(dest) && |
|
334 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) |
|
335 |
return sensor_matrix_get_reading(sensorMatrix, source, dest); |
|
336 |
return -1; |
|
321 | 337 |
} |
322 | 338 |
|
323 | 339 |
/** |
... | ... | |
344 | 360 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
345 | 361 |
{ |
346 | 362 |
int i, j; |
363 |
|
|
364 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining) |
|
365 |
if (source != wl_token_next_robot && bom_on_count <= DEATH_DELAY / 2 && |
|
366 |
ringState != ACCEPTED) |
|
367 |
{ |
|
368 |
WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n"); |
|
369 |
WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n"); |
|
370 |
return; |
|
371 |
} |
|
372 |
|
|
373 |
bom_on_count = -1; |
|
347 | 374 |
deathDelay = -1; |
348 |
|
|
349 |
WL_DEBUG_PRINT("Received the token, next robot is "); |
|
375 |
WL_DEBUG_PRINT("Received the token from robot"); |
|
376 |
WL_DEBUG_PRINT_INT(source); |
|
377 |
WL_DEBUG_PRINT(", next robot is "); |
|
350 | 378 |
WL_DEBUG_PRINT_INT((int)nextRobot); |
351 | 379 |
WL_DEBUG_PRINT(" \r\n"); |
352 | 380 |
sensor_matrix_set_in_ring(sensorMatrix, source, 1); |
... | ... | |
496 | 524 |
WL_DEBUG_PRINT("Passing the token to robot "); |
497 | 525 |
WL_DEBUG_PRINT_INT(buf[0]); |
498 | 526 |
WL_DEBUG_PRINT(".\r\n"); |
499 |
usb_puts("token passed\n"); |
|
500 | 527 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
501 | 528 |
buf, packetSize, 3); |
502 | 529 |
|
... | ... | |
517 | 544 |
WL_DEBUG_PRINT("Robot "); |
518 | 545 |
WL_DEBUG_PRINT_INT(source); |
519 | 546 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n"); |
547 |
|
|
548 |
bom_on_count = 0; |
|
549 |
|
|
520 | 550 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
521 | 551 |
source, get_max_bom_function()); |
522 | 552 |
} |
... | ... | |
537 | 567 |
wl_get_xbee_id(), 1); |
538 | 568 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n"); |
539 | 569 |
ringState = MEMBER; |
570 |
joinDelay = -1; |
|
540 | 571 |
} |
541 | 572 |
|
542 | 573 |
if (ringState == LEAVING || ringState == NONMEMBER) |
... | ... | |
549 | 580 |
return; |
550 | 581 |
} |
551 | 582 |
|
552 |
//check for interruption requests |
|
553 |
if (queue_size(interrupting) > 0) |
|
554 |
{ |
|
555 |
char buf[1]; |
|
556 |
buf[0] = (char)(int)queue_remove(interrupting); |
|
557 |
|
|
558 |
//in case this robot has requested multiple times |
|
559 |
queue_remove_all(interrupting, (void*)(int)buf[0]); |
|
560 |
|
|
561 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_INTERRUPT_PASS, |
|
562 |
buf, 1, 0); |
|
563 |
|
|
564 |
deathDelay = DEATH_DELAY; |
|
565 |
wl_token_next_robot = buf[0]; |
|
566 |
return; |
|
567 |
} |
|
568 |
|
|
569 | 583 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n"); |
570 |
usb_puts("bom on sent\n"); |
|
571 | 584 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
572 | 585 |
NULL, 0, 0); |
573 | 586 |
|
... | ... | |
580 | 593 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
581 | 594 |
{ |
582 | 595 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n"); |
596 |
return; |
|
583 | 597 |
} |
584 | 598 |
|
585 | 599 |
wl_token_pass_token(); |
... | ... | |
633 | 647 |
WL_DEBUG_PRINT_INT(source); |
634 | 648 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
635 | 649 |
|
636 |
joinDelay = -1; |
|
637 |
|
|
638 | 650 |
// the token ring has not started yet |
639 | 651 |
if (sensor_matrix_get_joined(sensorMatrix) == 1) |
640 | 652 |
wl_token_pass_token(); |
... | ... | |
652 | 664 |
WL_DEBUG_PRINT("Accepted into the token ring by robot "); |
653 | 665 |
WL_DEBUG_PRINT_INT(source); |
654 | 666 |
WL_DEBUG_PRINT(".\r\n"); |
655 |
joinDelay = -1;
|
|
667 |
joinDelay = JOIN_DELAY;
|
|
656 | 668 |
ringState = ACCEPTED; |
657 | 669 |
acceptor = source; |
658 | 670 |
|
... | ... | |
661 | 673 |
} |
662 | 674 |
|
663 | 675 |
/** |
664 |
* Called when we receive a packet passing the token and interrupting |
|
665 |
* the token ring. |
|
666 |
* If the token has been passed to us, we flash our BOM |
|
667 |
* and pass it back. |
|
668 |
* |
|
669 |
* @param source the robot who sent the interrupt packet |
|
670 |
* @param robot the robot the token has been passed to |
|
671 |
**/ |
|
672 |
void wl_token_interrupt_pass_receive(int source, int robot) |
|
673 |
{ |
|
674 |
if (wl_get_xbee_id() != robot) |
|
675 |
{ |
|
676 |
queue_remove_all(interrupting, (void*)robot); |
|
677 |
wl_token_next_robot = robot; |
|
678 |
deathDelay = DEATH_DELAY + rand() / (RAND_MAX / (2 * DEATH_DELAY)); |
|
679 |
return; |
|
680 |
} |
|
681 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
|
682 |
NULL, 0, 0); |
|
683 |
|
|
684 |
bom_on_function(); |
|
685 |
#ifdef ROBOT |
|
686 |
delay_ms(BOM_DELAY); |
|
687 |
#endif |
|
688 |
bom_off_function(); |
|
689 |
|
|
690 |
//we don't include ourself, only if we are in the ring |
|
691 |
int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1); |
|
692 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
|
693 |
packetSize += 2; |
|
694 |
char* buf = (char*)malloc(packetSize * sizeof(char)); |
|
695 |
if (!buf) |
|
696 |
{ |
|
697 |
WL_DEBUG_PRINT("Out of memory - pass_receive.\r\n"); |
|
698 |
return; |
|
699 |
} |
|
700 |
|
|
701 |
//return the token to where we got it from |
|
702 |
buf[0] = source; |
|
703 |
|
|
704 |
int i = 0, j = 0; |
|
705 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
706 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
|
707 |
{ |
|
708 |
buf[2*j + 1] = i; |
|
709 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
|
710 |
j++; |
|
711 |
} |
|
712 |
|
|
713 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
|
714 |
buf, packetSize, 0); |
|
715 |
|
|
716 |
wl_token_next_robot = source; |
|
717 |
deathDelay = DEATH_DELAY; |
|
718 |
free(buf); |
|
719 |
} |
|
720 |
|
|
721 |
/** |
|
722 | 676 |
* Returns the number of robots in the token ring. |
723 | 677 |
* |
724 | 678 |
* @return the number of robots in the token ring |
... | ... | |
794 | 748 |
} |
795 | 749 |
|
796 | 750 |
/** |
797 |
* Called when we receive a request to interrupt the token ring. |
|
798 |
* We add the robot to our list of interrupt requests, |
|
799 |
* and will send the token to this robot when we next receive the |
|
800 |
* token, unless someone else does so first. |
|
751 |
* Returns the number of robots currently in the token ring. |
|
801 | 752 |
* |
802 |
* @param source the robot requesting interruption |
|
803 |
* @param robt the robot requested to interrupt the token ring |
|
753 |
* @return the number of robots in the token ring |
|
804 | 754 |
**/ |
805 |
void wl_token_interrupt_request_receive(int source, int robot)
|
|
755 |
int wl_token_get_num_robots(void)
|
|
806 | 756 |
{ |
807 |
queue_add(interrupting, (void*)robot);
|
|
757 |
return sensor_matrix_get_joined(sensorMatrix);
|
|
808 | 758 |
} |
809 | 759 |
|
810 |
int wl_token_get_num_robots(void){ |
|
811 |
return sensor_matrix_get_joined(sensorMatrix); |
|
760 |
/** |
|
761 |
* Returns the number of robots in the sensor matrix. |
|
762 |
* |
|
763 |
* @return the number of robots in the sensor matrix |
|
764 |
**/ |
|
765 |
int wl_token_get_matrix_size(void) |
|
766 |
{ |
|
767 |
return sensor_matrix_get_size(sensorMatrix); |
|
812 | 768 |
} |
813 | 769 |
|
814 |
int wl_token_get_matrix_size(void){ |
|
815 |
return sensor_matrix_get_size(sensorMatrix); |
|
816 |
} |
branches/autonomous_recharging/code/lib/src/libwireless/xbee.c | ||
---|---|---|
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 xbee.c |
|
28 |
* @brief XBee Interface |
|
29 |
* |
|
30 |
* Implementation of low level communication with the XBee in API mode. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
1 | 35 |
#include "xbee.h" |
2 | 36 |
#include "wl_defs.h" |
3 | 37 |
|
... | ... | |
7 | 41 |
#include <unistd.h> |
8 | 42 |
#include <pthread.h> |
9 | 43 |
#include <errno.h> |
44 |
#include <termios.h> |
|
10 | 45 |
|
11 | 46 |
#else |
12 | 47 |
|
... | ... | |
19 | 54 |
#include <stdlib.h> |
20 | 55 |
#include <string.h> |
21 | 56 |
|
22 |
#include <queue.h> |
|
23 |
|
|
24 | 57 |
#define XBEE_FRAME_START 0x7E |
25 | 58 |
|
26 | 59 |
/*Frame Types*/ |
... | ... | |
49 | 82 |
void xbee_enter_command_mode(void); |
50 | 83 |
void xbee_exit_command_mode(void); |
51 | 84 |
void xbee_enter_api_mode(void); |
85 |
void xbee_exit_api_mode(void); |
|
52 | 86 |
void xbee_wait_for_string(char* s, int len); |
53 | 87 |
void xbee_wait_for_ok(void); |
54 | 88 |
|
... | ... | |
67 | 101 |
/*Global Variables*/ |
68 | 102 |
|
69 | 103 |
#ifndef ROBOT |
104 |
char* xbee_com_port = XBEE_PORT_DEFAULT; |
|
70 | 105 |
int xbee_stream; |
71 | 106 |
pthread_t* xbee_listen_thread; |
72 | 107 |
#endif |
73 | 108 |
|
74 |
Queue* xbee_queue; |
|
109 |
// TODO: is this a good size? |
|
110 |
#define XBEE_BUFFER_SIZE 256 |
|
111 |
// a buffer for data received from the XBee |
|
112 |
char arrival_buf[XBEE_BUFFER_SIZE]; |
|
113 |
// location of last unread byte in buffer |
|
114 |
volatile int buffer_last = 0; |
|
115 |
// first unread byte in buffer |
|
116 |
volatile int buffer_first = 0; |
|
75 | 117 |
|
118 |
|
|
76 | 119 |
//used to store packets as they are read |
77 | 120 |
char xbee_buf[128]; |
78 | 121 |
int currentBufPos = 0; |
... | ... | |
90 | 133 |
|
91 | 134 |
/** |
92 | 135 |
* Interrupt for the robot. Adds bytes received from the xbee |
93 |
* to the queue.
|
|
136 |
* to the buffer.
|
|
94 | 137 |
**/ |
95 | 138 |
#ifndef FIREFLY |
96 | 139 |
ISR(USART1_RX_vect) |
97 | 140 |
{ |
98 | 141 |
char c = UDR1; |
99 |
queue_add(xbee_queue, (void*)(int)c); |
|
142 |
arrival_buf[buffer_last] = c; |
|
143 |
int t = buffer_last + 1; |
|
144 |
if (t == XBEE_BUFFER_SIZE) |
|
145 |
t = 0; |
|
146 |
if (t == buffer_first) |
|
147 |
{ |
|
148 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
149 |
} |
|
150 |
buffer_last = t; |
|
100 | 151 |
} |
101 | 152 |
#else |
102 | 153 |
SIGNAL(SIG_USART0_RECV) |
103 | 154 |
{ |
104 | 155 |
char c = UDR0; |
105 |
queue_add(xbee_queue, (void*)(int)c); |
|
156 |
arrival_buf[buffer_last] = c; |
|
157 |
int t = buffer_last + 1; |
|
158 |
if (t == XBEE_BUFFER_SIZE) |
|
159 |
t = 0; |
|
160 |
if (t == buffer_first) |
|
161 |
{ |
|
162 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
163 |
} |
|
164 |
buffer_last = t; |
|
106 | 165 |
} |
107 | 166 |
#endif |
108 | 167 |
|
... | ... | |
117 | 176 |
while (1) |
118 | 177 |
{ |
119 | 178 |
xbee_read(&c, 1); |
120 |
queue_add(xbee_queue, (void*)(int)c); |
|
179 |
arrival_buf[buffer_last] = c; |
|
180 |
int t = buffer_last + 1; |
|
181 |
if (t == XBEE_BUFFER_SIZE) |
|
182 |
t = 0; |
|
183 |
if (t == buffer_first) |
|
184 |
{ |
|
185 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
186 |
} |
|
187 |
buffer_last = t; |
|
121 | 188 |
} |
122 | 189 |
return 0; |
123 | 190 |
} |
... | ... | |
126 | 193 |
|
127 | 194 |
/** |
128 | 195 |
* Initializes the XBee library so that other functions may be used. |
129 |
* |
|
130 |
* @param pan_id the PAN to join initially. Use XBEE_PAN_DEFAULT |
|
131 |
* to leave the PAN as it is initially. |
|
132 | 196 |
**/ |
133 |
void xbee_lib_init(void)
|
|
197 |
int xbee_lib_init(void)
|
|
134 | 198 |
{ |
135 |
xbee_queue = queue_create(); |
|
136 |
|
|
199 |
arrival_buf[0] = 'A'; |
|
200 |
arrival_buf[1] = 'A'; |
|
201 |
arrival_buf[2] = 'A'; |
|
137 | 202 |
#ifdef ROBOT |
138 | 203 |
|
139 | 204 |
//enable the receiving interrupt |
... | ... | |
144 | 209 |
#endif |
145 | 210 |
sei(); |
146 | 211 |
#else |
147 |
xbee_stream = open(XBEE_PORT, O_RDWR); |
|
148 |
if (xbee_stream == -1 || lockf(xbee_stream, F_TEST, 0) != 0) |
|
149 |
xbee_stream = open(XBEE_PORT2, O_RDWR); |
|
150 |
if (xbee_stream == -1 || lockf(xbee_stream, F_TEST, 0) != 0) |
|
212 |
printf("Connecting to port %s.\n", xbee_com_port); |
|
213 |
xbee_stream = open(xbee_com_port, O_RDWR); |
|
214 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
|
151 | 215 |
{ |
152 |
printf("Failed to open connection to XBee.\r\n");
|
|
153 |
exit(0);
|
|
216 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
|
|
217 |
return -1;
|
|
154 | 218 |
} |
155 |
lockf(xbee_stream, F_LOCK, 0); |
|
156 | 219 |
|
220 |
// set baud rate, etc. correctly |
|
221 |
struct termios options; |
|
222 |
|
|
223 |
tcgetattr(xbee_stream, &options); |
|
224 |
cfsetispeed(&options, B9600); |
|
225 |
cfsetospeed(&options, B9600); |
|
226 |
options.c_iflag &= ~ICRNL; |
|
227 |
options.c_oflag &= ~OCRNL; |
|
228 |
options.c_cflag |= (CLOCAL | CREAD); |
|
229 |
options.c_cflag &= ~PARENB; |
|
230 |
options.c_cflag &= ~CSTOPB; |
|
231 |
options.c_cflag &= ~CSIZE; |
|
232 |
options.c_cflag |= CS8; |
|
233 |
options.c_lflag &= ~ICANON; |
|
234 |
options.c_cc[VMIN] = 1; |
|
235 |
options.c_cc[VTIME] = 50; |
|
236 |
|
|
237 |
if (tcsetattr(xbee_stream, TCSANOW, &options)) |
|
238 |
{ |
|
239 |
fprintf(stderr, "Error setting attributes.\n"); |
|
240 |
return -1; |
|
241 |
} |
|
242 |
|
|
243 |
//lockf(xbee_stream, F_LOCK, 0); |
|
244 |
|
|
157 | 245 |
xbee_listen_thread = |
158 | 246 |
(pthread_t*)malloc(sizeof(pthread_t)); |
247 |
if (xbee_listen_thread == NULL) |
|
248 |
{ |
|
249 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__); |
|
250 |
return -1; |
|
251 |
} |
|
159 | 252 |
|
160 |
int ret = pthread_create(xbee_listen_thread, NULL,
|
|
253 |
int ret = pthread_create(xbee_listen_thread, NULL, |
|
161 | 254 |
listen_to_xbee, NULL); |
162 | 255 |
if (ret) |
163 | 256 |
{ |
164 |
printf("Failed to create listener thread.\r\n");
|
|
165 |
exit(0);
|
|
257 |
fprintf(stderr, "Failed to create listener thread.\r\n");
|
|
258 |
return -1;
|
|
166 | 259 |
} |
260 |
|
|
167 | 261 |
#endif |
168 | 262 |
xbee_enter_command_mode(); |
169 | 263 |
xbee_enter_api_mode(); |
... | ... | |
172 | 266 |
|
173 | 267 |
//wait to return until the address is set |
174 | 268 |
while (xbee_address == 0) xbee_get_packet(NULL); |
269 |
|
|
270 |
|
|
271 |
return 0; |
|
175 | 272 |
} |
176 | 273 |
|
177 | 274 |
/** |
... | ... | |
186 | 283 |
lockf(xbee_stream, F_ULOCK, 0); |
187 | 284 |
close(xbee_stream); |
188 | 285 |
#endif |
189 |
queue_destroy(xbee_queue); |
|
190 | 286 |
} |
191 | 287 |
|
192 | 288 |
/** |
... | ... | |
270 | 366 |
} |
271 | 367 |
|
272 | 368 |
/** |
369 |
* Exit API mode. (warning - does not check for response) |
|
370 |
**/ |
|
371 |
void xbee_exit_api_mode() |
|
372 |
{ |
|
373 |
xbee_send_string("ATAP 0\r"); |
|
374 |
} |
|
375 |
|
|
376 |
/** |
|
273 | 377 |
* Wait until the string "OK\r" is received from the XBee. |
274 | 378 |
**/ |
275 | 379 |
void xbee_wait_for_ok() |
... | ... | |
289 | 393 |
char* curr = s; |
290 | 394 |
while (curr - s < len) |
291 | 395 |
{ |
292 |
if (queue_is_empty(xbee_queue)) |
|
396 |
// check if buffer is empty |
|
397 |
if (buffer_last == buffer_first) |
|
293 | 398 |
continue; |
294 |
char c = (char)(int)queue_remove(xbee_queue); |
|
399 |
char c = arrival_buf[buffer_first++]; |
|
400 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
401 |
buffer_first = 0; |
|
295 | 402 |
if (c == *curr) |
296 | 403 |
curr++; |
297 | 404 |
else |
... | ... | |
499 | 606 |
if (currentBufPos == 0) |
500 | 607 |
{ |
501 | 608 |
do |
502 |
if (queue_is_empty(xbee_queue)) |
|
609 |
{ |
|
610 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
611 |
buffer_first = 0; |
|
612 |
// check if buffer is empty |
|
613 |
if (buffer_first == buffer_last) |
|
503 | 614 |
return -1; |
504 |
while ((char)(int)queue_remove(xbee_queue) != XBEE_FRAME_START); |
|
615 |
} |
|
616 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
617 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
618 |
buffer_first = 0; |
|
505 | 619 |
xbee_buf[0] = XBEE_FRAME_START; |
506 | 620 |
currentBufPos++; |
507 | 621 |
} |
... | ... | |
514 | 628 |
|| currentBufPos < len + 4) |
515 | 629 |
{ |
516 | 630 |
if (currentBufPos == 3) |
631 |
{ |
|
517 | 632 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
518 |
if (queue_is_empty(xbee_queue)) |
|
633 |
if (len > 120) |
|
634 |
{ |
|
635 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n"); |
|
636 |
currentBufPos = 0; |
|
637 |
return -1; |
|
638 |
} |
|
639 |
} |
|
640 |
// check if buffer is empty |
|
641 |
if (buffer_first == buffer_last) |
|
519 | 642 |
return -1; |
520 |
xbee_buf[currentBufPos++] = (char)(int)queue_remove(xbee_queue); |
|
643 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
|
644 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
645 |
buffer_first = 0; |
|
521 | 646 |
} |
522 | 647 |
|
523 | 648 |
currentBufPos = 0; |
524 | 649 |
|
525 | 650 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) |
526 | 651 |
{ |
527 |
usb_puts("XBee checksum failed.\r\n");
|
|
652 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n");
|
|
528 | 653 |
return -1; |
529 | 654 |
} |
530 | 655 |
|
... | ... | |
740 | 865 |
return xbee_address; |
741 | 866 |
} |
742 | 867 |
|
868 |
#ifndef ROBOT |
|
869 |
void xbee_set_com_port(char* port) |
|
870 |
{ |
|
871 |
xbee_com_port = port; |
|
872 |
} |
|
873 |
#endif |
|
874 |
|
branches/autonomous_recharging/code/lib/src/libwireless/queue.c | ||
---|---|---|
24 | 24 |
Queue* queue_create() |
25 | 25 |
{ |
26 | 26 |
Queue* q = (Queue*)malloc(sizeof(Queue)); |
27 |
|
|
28 |
if (q == NULL) |
|
29 |
{ |
|
30 |
WL_DEBUG_PRINT("Out of memory.\r\n"); |
|
31 |
return NULL; |
|
32 |
} |
|
33 |
|
|
34 | 27 |
q->head = NULL; |
35 | 28 |
q->size = 0; |
36 | 29 |
return q; |
branches/autonomous_recharging/code/lib/src/libwireless/wl_error_group.c | ||
---|---|---|
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 wl_error_group.h |
|
28 |
* @brief Error Packets |
|
29 |
* |
|
30 |
* A wireless group for sending error packets. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
1 | 35 |
#include "wl_error_group.h" |
2 | 36 |
|
3 | 37 |
#include <wireless.h> |
branches/autonomous_recharging/code/lib/src/libwireless/sensor_matrix.c | ||
---|---|---|
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 |
|
|
1 | 35 |
#include <stdlib.h> |
2 | 36 |
#include <stdio.h> |
3 | 37 |
#include <wl_defs.h> |
... | ... | |
19 | 53 |
SensorMatrix* m; |
20 | 54 |
int i; |
21 | 55 |
|
22 |
m = malloc(sizeof(SensorMatrix)); |
|
56 |
m = (SensorMatrix*)malloc(sizeof(SensorMatrix));
|
|
23 | 57 |
if (!m) |
24 | 58 |
{ |
25 | 59 |
WL_DEBUG_PRINT("Out of memory - create sensor matrix.\r\n"); |
26 | 60 |
return NULL; |
27 | 61 |
} |
28 | 62 |
m->size = DEFAULT_SENSOR_MATRIX_SIZE; |
29 |
m->matrix = malloc(m->size * sizeof(int*)); |
|
30 |
m->joined = malloc(m->size * sizeof(int)); |
|
63 |
m->matrix = (int**)malloc(m->size * sizeof(int*));
|
|
64 |
m->joined = (int*)malloc(m->size * sizeof(int));
|
|
31 | 65 |
m->numJoined = 0; |
32 | 66 |
if (!(m->matrix) || !(m->joined)) |
33 | 67 |
{ |
... | ... | |
65 | 99 |
* @param m the sensor matrix |
66 | 100 |
* @param id the XBee ID of the robot to add |
67 | 101 |
**/ |
68 |
void sensor_matrix_add_robot(SensorMatrix* m, unsigned int id)
|
|
102 |
void sensor_matrix_add_robot(SensorMatrix* m, int id) |
|
69 | 103 |
{ |
70 | 104 |
int i; |
71 | 105 |
if (id >= m->size) |
... | ... | |
73 | 107 |
if (m->matrix[id] != NULL) |
74 | 108 |
return; |
75 | 109 |
|
76 |
m->matrix[id] = malloc(m->size * sizeof(int)); |
|
110 |
m->matrix[id] = (int*)malloc(m->size * sizeof(int));
|
|
77 | 111 |
if (!(m->matrix[id])) |
78 | 112 |
{ |
79 | 113 |
WL_DEBUG_PRINT("Out of memory - add robot.\r\n"); |
... | ... | |
92 | 126 |
* @param m the sensor matrix |
93 | 127 |
* @param id the XBee ID of the robot to remove |
94 | 128 |
**/ |
95 |
void sensor_matrix_remove_robot(SensorMatrix* m, unsigned int id)
|
|
129 |
void sensor_matrix_remove_robot(SensorMatrix* m, int id) |
|
96 | 130 |
{ |
97 | 131 |
int i; |
98 | 132 |
|
... | ... | |
125 | 159 |
int i, j; |
126 | 160 |
WL_DEBUG_PRINT("Expanding sensor matrix.\r\n"); |
127 | 161 |
|
128 |
int** tempMatrix = malloc(nextSize * sizeof(int*)); |
|
162 |
int** tempMatrix = (int**)malloc(nextSize * sizeof(int*));
|
|
129 | 163 |
if (!tempMatrix) |
130 | 164 |
{ |
131 | 165 |
WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n"); |
... | ... | |
139 | 173 |
for (i = 0; i < m->size; i++) |
140 | 174 |
if (m->matrix[i] != NULL) |
141 | 175 |
{ |
142 |
tempMatrix[i] = malloc(nextSize * sizeof(int));
|
|
176 |
tempMatrix[i] = (int *)malloc(nextSize * sizeof(int));
|
|
143 | 177 |
if (!tempMatrix[i]) |
144 | 178 |
{ |
145 | 179 |
WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n"); |
... | ... | |
158 | 192 |
m->size = nextSize; |
159 | 193 |
|
160 | 194 |
//expand the size of joined |
161 |
int* tempJoined = malloc(nextSize * sizeof(int)); |
|
195 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int));
|
|
162 | 196 |
if (!tempJoined) |
163 | 197 |
{ |
164 | 198 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
branches/autonomous_recharging/code/lib/src/libwireless/wireless.c | ||
---|---|---|
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 wireless.c |
|
28 |
* @brief Wireless Library Implementation |
|
29 |
* |
|
30 |
* Implementation of the wireless library. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
1 | 35 |
#include "wireless.h" |
2 | 36 |
#include "xbee.h" |
3 | 37 |
#include <stdlib.h> |
... | ... | |
50 | 84 |
} |
51 | 85 |
return; |
52 | 86 |
} |
87 |
#else |
|
88 |
|
|
89 |
//called when the timer ticks |
|
90 |
void timer_handler(void) |
|
91 |
{ |
|
92 |
wl_timeout = 1; |
|
93 |
} |
|
94 |
|
|
53 | 95 |
#endif |
54 | 96 |
|
55 | 97 |
/** |
56 | 98 |
* Initializes the wireless library. Must be called before any |
57 | 99 |
* other function. |
58 | 100 |
**/ |
59 |
void wl_init()
|
|
101 |
int wl_init()
|
|
60 | 102 |
{ |
61 | 103 |
int i; |
62 | 104 |
for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) |
63 | 105 |
wl_packet_groups[i] = NULL; |
64 | 106 |
|
65 |
xbee_lib_init(); |
|
66 |
|
|
107 |
if (xbee_lib_init() == -1) { |
|
108 |
return -1; |
|
109 |
} |
|
110 |
|
|
67 | 111 |
//begin timeout timer |
68 | 112 |
#ifdef ROBOT |
69 | 113 |
#ifdef FIREFLY |
70 |
rtc_init(PRESCALE_DIV_128, 32, &wl_do_timeout);
|
|
114 |
rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
|
|
71 | 115 |
#else |
72 |
rtc_init(HALF_SECOND, &wl_do_timeout);
|
|
116 |
rtc_init(HALF_SECOND, &timer_handler);
|
|
73 | 117 |
#endif |
74 | 118 |
#else |
75 |
|
|
119 |
|
|
76 | 120 |
//create our timer |
77 | 121 |
struct itimerval timer_val; |
78 | 122 |
struct timeval interval; |
... | ... | |
85 | 129 |
timer_val.it_value = first_time; |
86 | 130 |
if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1) |
87 | 131 |
{ |
88 |
WL_DEBUG_PRINT("Error creating a timer.\r\n");
|
|
132 |
WL_DEBUG_PRINT("Error creating a timer.\r\n"); |
|
89 | 133 |
perror("Failure's cause"); |
90 |
exit(1);
|
|
134 |
exit(1); |
|
91 | 135 |
} |
92 | 136 |
|
93 | 137 |
//create signal handler |
94 | 138 |
struct sigaction wl_sig_act; |
95 |
wl_sig_act.sa_handler = (void *)sig_handler;
|
|
139 |
wl_sig_act.sa_handler = sig_handler; |
|
96 | 140 |
wl_sig_act.sa_flags = 0; |
97 | 141 |
sigemptyset(&wl_sig_act.sa_mask); |
98 | 142 |
sigaction(SIGALRM, &wl_sig_act, 0); |
99 | 143 |
sigaction(SIGINT, &wl_sig_act, 0); |
100 | 144 |
#endif |
145 |
|
|
146 |
return 0; |
|
101 | 147 |
} |
102 | 148 |
|
103 | 149 |
/** |
... | ... | |
110 | 156 |
if (wl_packet_groups[i] != NULL && |
111 | 157 |
wl_packet_groups[i]->unregister != NULL) |
112 | 158 |
wl_packet_groups[i]->unregister(); |
113 |
|
|
159 |
|
|
114 | 160 |
xbee_terminate(); |
115 | 161 |
} |
116 | 162 |
|
... | ... | |
167 | 213 |
* |
168 | 214 |
* @return the 16-bit address of the XBee module. |
169 | 215 |
**/ |
170 |
unsigned int wl_get_xbee_id()
|
|
216 |
int wl_get_xbee_id() |
|
171 | 217 |
{ |
172 | 218 |
return xbee_get_address(); |
173 | 219 |
} |
... | ... | |
234 | 280 |
void wl_send_pan_packet(char group, char type, |
235 | 281 |
char* data, int len, char frame) |
236 | 282 |
{ |
237 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST,
|
|
283 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST, |
|
238 | 284 |
XBEE_OPTIONS_NONE, frame); |
239 | 285 |
} |
240 | 286 |
|
... | ... | |
287 | 333 |
|
288 | 334 |
/** |
289 | 335 |
* Unregister a packet group from the wireless library. |
290 |
*
|
|
336 |
* |
|
291 | 337 |
* @param h the packet group to remove |
292 | 338 |
**/ |
293 | 339 |
void wl_unregister_packet_group(PacketGroupHandler* h) |
... | ... | |
325 | 371 |
wl_do_timeout(); |
326 | 372 |
wl_timeout = 0; |
327 | 373 |
} |
328 |
|
|
374 |
|
|
329 | 375 |
int len = xbee_get_packet(wl_buf); |
330 | 376 |
if (len < 0)//no packet received |
331 | 377 |
return; |
332 |
|
|
378 |
|
|
333 | 379 |
if (wl_buf[0] == XBEE_TX_STATUS) |
334 | 380 |
{ |
335 | 381 |
if (len != 3) |
... | ... | |
337 | 383 |
WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n"); |
338 | 384 |
return; |
339 | 385 |
} |
340 |
|
|
386 |
|
|
341 | 387 |
//the first four bits are the packet group |
342 | 388 |
//this only works with under 16 groups |
343 | 389 |
int group = (int)(wl_buf[1] >> 4); |
... | ... | |
356 | 402 |
WL_DEBUG_PRINT("Purged\r\n"); |
357 | 403 |
} |
358 | 404 |
} |
359 |
|
|
405 |
|
|
360 | 406 |
if (wl_packet_groups[group] != NULL && |
361 | 407 |
wl_packet_groups[group]->handle_response != NULL) |
362 | 408 |
wl_packet_groups[group]->handle_response( |
363 | 409 |
(int)wl_buf[1] & 0x0F, success); |
364 | 410 |
return; |
365 | 411 |
} |
366 |
|
|
412 |
|
|
367 | 413 |
if (wl_buf[0] == XBEE_RX) |
368 | 414 |
{ |
369 | 415 |
if (len < 7) |
... | ... | |
371 | 417 |
WL_DEBUG_PRINT("Packet is too small.\r\n"); |
372 | 418 |
return; |
373 | 419 |
} |
374 |
|
|
420 |
|
|
375 | 421 |
int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]); |
376 |
|
|
422 |
|
|
377 | 423 |
/* |
378 | 424 |
//unused for now |
379 | 425 |
int signalStrength = wl_buf[3]; |
380 | 426 |
//1 for Address broadcast, 2 for PAN broadcast |
381 | 427 |
int options = wl_buf[4]; |
382 | 428 |
*/ |
383 |
|
|
429 |
|
|
384 | 430 |
int group = wl_buf[5]; |
385 | 431 |
int type = wl_buf[6]; |
386 | 432 |
int packetLen = len - 7; |
387 |
|
|
433 |
|
|
388 | 434 |
if (wl_packet_groups[group] != NULL |
389 | 435 |
&& wl_packet_groups[group]->handle_receive != NULL) |
390 |
wl_packet_groups[group]->handle_receive(type, source, |
|
436 |
wl_packet_groups[group]->handle_receive(type, source, |
Also available in: Unified diff