Revision 618
added some comments
colonet_wireless.cpp | ||
---|---|---|
50 | 50 |
static int log_packet(unsigned char* packet, int len); |
51 | 51 |
|
52 | 52 |
/**************************** Public functions *******************************/ |
53 |
/** |
|
54 |
* @brief Initializes the wireless library |
|
55 |
* |
|
56 |
* @param wl_port_ The port to listen for wireless messages on |
|
57 |
* @param log_filename_ The name of the log file |
|
58 |
* |
|
59 |
* @return 0 on success, negative error code on failure |
|
60 |
*/ |
|
53 | 61 |
int colonet_wl_init(char* wl_port_, char* log_filename_) { |
54 | 62 |
if (log_filename_ != NULL) { |
55 | 63 |
logging_enabled = true; |
... | ... | |
78 | 86 |
return 0; |
79 | 87 |
} |
80 | 88 |
|
89 |
/** |
|
90 |
* @brief This function will kill the thread that is listening for wireless messages |
|
91 |
*/ |
|
81 | 92 |
void colonet_wl_kill_listener_thread() { |
82 | 93 |
pthread_kill(listener_thread, 9); |
83 | 94 |
} |
84 | 95 |
|
96 |
/** |
|
97 |
* @brief This function spawns a thread to listen for wireless messages |
|
98 |
* |
|
99 |
* @return 0 on success, negative error code on failure |
|
100 |
*/ |
|
85 | 101 |
int colonet_wl_run_listener_thread() { |
86 | 102 |
dbg_printf("Spawning listener thread...\n"); |
87 | 103 |
|
... | ... | |
93 | 109 |
return 0; |
94 | 110 |
} |
95 | 111 |
|
112 |
/** |
|
113 |
* @brief This function sends a message out on the wireless |
|
114 |
* |
|
115 |
* @param client_source The index in the connection pool of the client that is sending the message |
|
116 |
* @param dest The robot that is meant to receive this message |
|
117 |
* @param msg_type The type of the message |
|
118 |
* @param msg_code The message code |
|
119 |
* @param args The arguments to send with the message |
|
120 |
* |
|
121 |
* @return 0 on success, a negative error code on failure |
|
122 |
*/ |
|
96 | 123 |
int colonet_wl_send(short client_source, short dest, ColonetRobotMessageType msg_type, unsigned char msg_code, |
97 | 124 |
unsigned char* args) { |
98 | 125 |
//printf("colonet_wl_send: client_source:%d, dest:%d, msg_code:%d\n", client_source, dest, msg_code); |
99 | 126 |
|
127 |
//create a new packet to be sent |
|
100 | 128 |
ColonetRobotServerPacket pkt; |
129 |
|
|
130 |
//set up the packet |
|
131 |
|
|
101 | 132 |
pkt.client_id = client_source; |
102 | 133 |
pkt.msg_code = msg_code; |
103 | 134 |
|
135 |
//copy over the args into the data portion of the packet |
|
104 | 136 |
for (int i = 0; i < PACKET_DATA_LEN; i++) { |
105 | 137 |
//printf("data %i: %u\n", i, args[i]); |
106 | 138 |
pkt.data[i] = args[i]; |
... | ... | |
123 | 155 |
return 0; |
124 | 156 |
} |
125 | 157 |
|
158 |
/** |
|
159 |
* @brief Used to get the number of robots in the token ring |
|
160 |
* |
|
161 |
* @return The result of wl_token_get_num_robots() |
|
162 |
*/ |
|
126 | 163 |
int colonet_get_num_robots(void) { |
127 | 164 |
return wl_token_get_num_robots(); |
128 | 165 |
} |
129 | 166 |
|
167 |
/** |
|
168 |
* @brief Gets a list of the xbee ids of the ones in the token ring |
|
169 |
* |
|
170 |
* @param numrobots A pointer to the variable where you want the number of robots to be stored |
|
171 |
* |
|
172 |
* @return A dynamically allocated piece of memory that contains the list of xbee ids. |
|
173 |
* The function calling this function must free this memory when done with it. |
|
174 |
*/ |
|
130 | 175 |
int* colonet_get_xbee_ids(int* numrobots) { |
131 | 176 |
int num_robots = wl_token_get_num_robots(); |
132 | 177 |
int* ids = (int*)malloc(num_robots * sizeof(int)); |
... | ... | |
143 | 188 |
return ids; |
144 | 189 |
} |
145 | 190 |
|
146 |
// Returns int**; should be freed |
|
191 |
/** |
|
192 |
* @brief Gets the sensor matrix from the wireless library |
|
193 |
* |
|
194 |
* @param numrobots A pointer to the variable where you want the number of robots to be stored |
|
195 |
* @param ids_ A pointer to a pointer that you want to point to the list of xbee ids. This memory is dynamically allocated and should be freed by the calling process when it is done with it |
|
196 |
* |
|
197 |
* @return The 2d matrix reprsenting the sensor matrix. This memory is dynamically allocated and should be freed by the calling process when it is done with it |
|
198 |
*/ |
|
147 | 199 |
int** colonet_get_sensor_matrix(int* numrobots, int** ids_) { |
148 | 200 |
int num_robots; |
149 | 201 |
int* ids = colonet_get_xbee_ids(&num_robots); |
... | ... | |
189 | 241 |
log_packet(data, len); |
190 | 242 |
} |
191 | 243 |
|
244 |
//see if a robot wants the server to tell it where it is |
|
192 | 245 |
if (pkt->msg_code == ROBOT_REQUEST_POSITION_FROM_SERVER) { |
193 | 246 |
/* Robot has requested its position. */ |
194 | 247 |
int robot_x, robot_y; |
195 | 248 |
PositionMonitor* pm = colonet_server.getPositionMonitor(); |
196 | 249 |
int num_robots = pm->getNumVisibleRobots(); |
250 |
//ask the position monitor for where that robot is. |
|
197 | 251 |
int ret = pm->getRobotPosition(source, &robot_x, &robot_y); |
198 | 252 |
if (ret != 0 && num_robots != 1) { |
199 | 253 |
fprintf(stderr, "Robot %d requested position, but its position is not known.\n", source); |
Also available in: Unified diff