Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / mapping / matlab / receive.c @ 959

History | View | Annotate | Download (6.05 KB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <mex.h>
4
#include <pthread.h>
5
#include <unistd.h>
6
#include "../../libwireless/lib/wireless.h"
7
#include "../../libwireless/lib/wl_token_ring.h"
8

    
9
#define WL_COM_PORT "/dev/ttyUSB1" 
10
#define WL_CHANNEL 0xE
11

    
12
#define MAP_RECEIVE_GROUP 1
13
#define MAP_REQUEST_GROUP 2
14

    
15
#define POINT_RAW 1 /* packet type for map data points w/ raw encoder data*/
16
#define POINT_ODO 2 /* packet type for map data points w/ odometry data*/
17

    
18
#define BUFFER_SIZE 1000
19
#define BUFFER_EMPTY -1
20

    
21
pthread_rwlock_t buffer_lock;
22
pthread_t receive_thread;
23

    
24
int packets_received; /*Used to block overlapping packet requests.*/
25

    
26
void packet_receive(char type, int source, unsigned char* packet, int length);
27

    
28
PacketGroupHandler receiveHandler = {MAP_REQUEST_GROUP, NULL, NULL, &packet_receive, NULL};
29
PacketGroupHandler requestHandler = {MAP_RECEIVE_GROUP, NULL, NULL, NULL, NULL};
30

    
31
/*Buffers*/
32
short robot_buffer[BUFFER_SIZE];                        /*by #*/
33
short range_buffer[BUFFER_SIZE][5];                 /*IR1, IR2 ... */
34
short coordinate_buffer[BUFFER_SIZE][2];        /*X Y*/
35
float orientation_buffer[BUFFER_SIZE];
36

    
37
int tail_index; /*Points to the newest data in the buffers.*/
38
int head_index; /*Points to the oldest data in the buffers.*/
39

    
40
void* buffer_data(void* arg);
41

    
42
void mexFunction(int nlhs, mxArray* ret[], int nrhs, const mxArray* args[]){
43
        
44
        if(nrhs == 1){        
45
                
46
                /*Initialization*/
47
                head_index = BUFFER_EMPTY;
48
                tail_index = BUFFER_EMPTY;        
49
                 
50
                pthread_rwlock_init(&buffer_lock, NULL);
51

    
52
                pthread_create(&receive_thread, NULL, buffer_data, NULL);
53
                return;
54
        }
55
        
56
        /*Return stuff to matlab*/
57
        /*deliberately ignore nrhs.        */
58

    
59
        if(nlhs != 4){
60
                mexErrMsgTxt("Error: Expect output arguments [P IR T R]\n  \
61
                                          where P = [X Y], IR = [IR1, IR2 ...],\n \
62
                                          T = angle, and R = Robot number."); 
63
        }
64
        
65
        int index,i,j;
66
        
67
        pthread_rwlock_rdlock(&buffer_lock);
68
        {
69

    
70
                if(tail_index == BUFFER_EMPTY || head_index == BUFFER_EMPTY){
71
                        double* point, *ranges, *theta, *robot;
72
                        /*Appropriately return with nothing.*/
73
                        ret[0] = mxCreateDoubleMatrix(1, 2, mxREAL);
74
                        ret[1] = mxCreateDoubleMatrix(1, 5, mxREAL);
75
                        ret[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
76
                        ret[3] = mxCreateDoubleMatrix(1, 1, mxREAL);
77
                        
78
                        point  = mxGetPr(ret[0]);
79
                        ranges = mxGetPr(ret[1]);
80
                        theta = mxGetPr(ret[2]);
81
                        robot = mxGetPr(ret[3]);
82

    
83
                        point[0] = point[1] = 0.0;
84
                        
85
                        ranges[0] = ranges[1] = ranges[2] = 0.0;
86
                        ranges[3] = ranges[4] = 0.0;
87

    
88
                        theta[0] = 0.0;
89
                        robot[0] = 0.0;
90

    
91
                        return;
92
                }
93
                else{
94
                        double** point, **ranges, *theta, *robot;
95
                        /*TODO Check for the 'already empty' case.*/
96

    
97
                        int num_buffer_elements = tail_index - head_index;
98
                        if(num_buffer_elements < 0) num_buffer_elements += BUFFER_SIZE;        
99

    
100
                        ret[0] = mxCreateDoubleMatrix(num_buffer_elements, 2, mxREAL);
101
                        ret[1] = mxCreateDoubleMatrix(num_buffer_elements, 5, mxREAL);
102
                        ret[2] = mxCreateDoubleMatrix(num_buffer_elements, 1, mxREAL);
103
                        ret[3] = mxCreateDoubleMatrix(num_buffer_elements, 1, mxREAL);
104
                
105
                        /*This may not work on some computers.*/
106
                        point  = (double**)mxGetPr(ret[0]);
107
                        ranges = (double**)mxGetPr(ret[1]);
108
                        theta = mxGetPr(ret[2]);
109
                        robot = mxGetPr(ret[3]);
110

    
111
                        for(index = head_index; index <= tail_index; index = (index + 1)%1024){
112
                                point[i][0] = (double)coordinate_buffer[index][0];        
113
                                point[i][1] = (double)coordinate_buffer[index][1];        
114
                                
115
                                for(j=0;j<5;j++)
116
                                        ranges[i][j] = (double)range_buffer[index][j];
117

    
118
                                theta[i] = (double)(orientation_buffer[index]);        
119
                                robot[i] = (double)(robot_buffer[index]);
120
                        }
121
                        head_index = BUFFER_EMPTY;
122
                        tail_index = BUFFER_EMPTY;
123
                }
124
        }        
125
        pthread_rwlock_unlock(&buffer_lock);
126
}
127

    
128
/*
129
 * buffer_data continuously runs through the robots and
130
 * waits for them to respond.
131
 */
132
void* buffer_data(void* arg){
133
        int robot_num, num_robots;        
134

    
135
        pthread_detach(pthread_self());
136
        
137
        /*General communication initialization.        */
138
        wl_set_com_port(WL_COM_PORT);        
139
        wl_init();
140
        wl_set_channel(WL_CHANNEL);
141
        wl_register_packet_group(&receiveHandler);
142

    
143
        /*Token ring / sensor network initialization.*/
144
        wl_token_ring_register();        
145
        
146
        /*Continuously iterate over all the robots - buffering data.*/
147
        while(1){
148
                
149
                wl_token_iterator_begin();
150
                
151
                num_robots = wl_token_get_num_robots();
152
                
153
                /*Initialize the 'packets received' number. */
154
                packets_received = 0;
155
        
156
                /*Send a data request to all robots.*/
157
                while(wl_token_iterator_has_next()){
158
                        robot_num = wl_token_iterator_next();
159
                    
160
                        wl_send_robot_to_robot_global_packet(MAP_REQUEST_GROUP, 
161
                                POINT_ODO, NULL, 0, robot_num, 0);
162
                    
163

    
164
                }
165
                
166
                /*Wait for all packets to be received before sending more requests.*/
167
                while(packets_received < num_robots 
168
                   && packets_received < wl_token_get_num_robots()){
169
                        
170
                        usleep(10000); /*10ms*/
171
                }
172
                
173
                usleep(10000); 
174
        }
175
}
176

    
177
void packet_receive (char type, int source, unsigned char* packet, int length) {
178
        printf("A packet came in!\n");        
179
        if(type==POINT_ODO){        
180

    
181
                if(length!=18) printf("Packet came without the standard length!\n");
182

    
183
                /* expected input: x y theta(double) IR1 IR2 IR3 IR4 IR5*/
184
                /* Pull in data:*/
185
                
186
                short x,y,i;
187
                short ranges[5];        
188
                
189
                x = ((short)packet[1] << 8)  | (short)packet[0];
190
                y = ((short)packet[3] << 8)  | (short)packet[2];
191

    
192
                for(i=0; i<5; i++){
193
                        ranges[i] 
194
                           = ((short)packet[8+2*i+1] << 8) | (short)packet[8 + 2*i];
195
                }
196

    
197
                float theta = *((float*)(packet + 4));
198
        
199
                /*Write new data to buffers. (Lock down!)*/
200
                pthread_rwlock_wrlock(&buffer_lock);
201
                {        
202
                        /*Note that you write no matter what.        */
203
                        tail_index = (tail_index + 1) % BUFFER_SIZE; 
204
                        
205
                        /*Write the robots number.*/
206
                        robot_buffer[tail_index] = source;
207
                        
208
                        /*Write in range data.*/
209
                        for(i=0; i<5; i++) range_buffer[tail_index][i] = ranges[i];
210
                        
211
                        coordinate_buffer[tail_index][0] = x;
212
                        coordinate_buffer[tail_index][1] = y;
213
                        
214
                        orientation_buffer[tail_index] = theta;
215
                        
216
                        /*If the oldest member didn't exist, buffer was empty.*/
217
                        if(head_index == BUFFER_EMPTY) head_index = 0;
218
                
219
                        /*Otherwise check for overwritten data.        */
220
                        else if(tail_index == head_index) 
221
                                head_index = (head_index +1) % BUFFER_SIZE;  
222
                        
223
                }
224
                pthread_rwlock_unlock(&buffer_lock);
225
                
226
                /*Increment for the transmit thread.*/
227
                packets_received++;
228
        }
229
}
230