Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.65 KB)

1
/* This version of receive initializes
2
 * the token ring and then requests and
3
 * receives data in a different packet group
4
 * unsynchronized with the token ring.
5
 */
6

    
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <mex.h>
10
#include <pthread.h>
11
#include <unistd.h>
12
#include "../../libwireless/lib/wireless.h"
13
#include "../../libwireless/lib/wl_token_ring.h"
14

    
15
#define WL_COM_PORT "/dev/ttyUSB1" 
16
#define WL_CHANNEL 0xE
17

    
18
#define MAP_REQUEST_GROUP 23
19

    
20
#define DATA_POINT 2 /* packet type for map data points w/ odometry data*/
21
#define DATA_REQUEST 3
22
#define DATA_SERVER_REPLY 4 /*Just to give the robot a target.*/
23
#define DATA_SERVER_IDENTIFY 5
24

    
25
#define BUFFER_SIZE 1000
26
#define BUFFER_EMPTY -1
27

    
28
pthread_rwlock_t buffer_lock;
29
pthread_t receive_thread;
30

    
31
void packet_receive(char type, int source, unsigned char* packet, int length);
32

    
33
PacketGroupHandler requestHandler = {MAP_REQUEST_GROUP, NULL, NULL, &packet_receive, NULL};
34

    
35
/*Buffers*/
36
short robot_buffer[BUFFER_SIZE];                        /*by #*/
37
short range_buffer[BUFFER_SIZE][5];                 /*IR1, IR2 ... */
38
short coordinate_buffer[BUFFER_SIZE][2];        /*X Y*/
39
float orientation_buffer[BUFFER_SIZE];
40

    
41
int tail_index; /*Points to the newest data in the buffers.*/
42
int head_index; /*Points to the oldest data in the buffers.*/
43

    
44
void* run_wireless(void* arg);
45
void init_wireless(void);
46

    
47
void mexFunction(int nlhs, mxArray* ret[], int nrhs, const mxArray* args[]){
48
        
49
        if(nrhs == 1){        
50
                
51
                /*Initialization*/
52
                head_index = BUFFER_EMPTY;
53
                tail_index = BUFFER_EMPTY;        
54
                 
55
                pthread_rwlock_init(&buffer_lock, NULL);
56

    
57
                pthread_create(&receive_thread, NULL, run_wireless, NULL);
58
                return;
59
        }
60
        
61
        /*Return stuff to matlab*/
62
        /*deliberately ignore nrhs.        */
63

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

    
75
                if(tail_index == BUFFER_EMPTY || head_index == BUFFER_EMPTY){
76
                        double* point, *ranges, *theta, *robot;
77
                        /*Appropriately return with nothing. (til I figure that out, return 0's)*/
78
                        ret[0] = mxCreateDoubleMatrix(1, 2, mxREAL);
79
                        ret[1] = mxCreateDoubleMatrix(1, 5, mxREAL);
80
                        ret[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
81
                        ret[3] = mxCreateDoubleMatrix(1, 1, mxREAL);
82
                        
83
                        point  = mxGetPr(ret[0]);
84
                        ranges = mxGetPr(ret[1]);
85
                        theta = mxGetPr(ret[2]);
86
                        robot = mxGetPr(ret[3]);
87

    
88
                        point[0] = point[1] = 0.0;
89
                        
90
                        ranges[0] = ranges[1] = ranges[2] = 0.0;
91
                        ranges[3] = ranges[4] = 0.0;
92

    
93
                        theta[0] = 0.0;
94
                        robot[0] = 0.0;
95

    
96
                }
97
                else{ /*Copy the buffer data and set the buffer to empty.*/
98
                        double** point, **ranges, *theta, *robot;
99
                        
100
                        int num_buffer_elements = tail_index - head_index;
101
                        if(num_buffer_elements < 0) num_buffer_elements += BUFFER_SIZE;        
102

    
103
                        ret[0] = mxCreateDoubleMatrix(num_buffer_elements, 2, mxREAL);
104
                        ret[1] = mxCreateDoubleMatrix(num_buffer_elements, 5, mxREAL);
105
                        ret[2] = mxCreateDoubleMatrix(num_buffer_elements, 1, mxREAL);
106
                        ret[3] = mxCreateDoubleMatrix(num_buffer_elements, 1, mxREAL);
107
                
108
                        point  = (double**)mxGetPr(ret[0]);
109
                        ranges = (double**)mxGetPr(ret[1]);
110
                        theta = mxGetPr(ret[2]);
111
                        robot = mxGetPr(ret[3]);
112

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

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

    
130
void init_wireless(void){
131
        /*General communication initialization.        */
132
        wl_set_com_port(WL_COM_PORT);        
133
        wl_init();
134
        wl_set_channel(WL_CHANNEL);
135
        wl_register_packet_group(&requestHandler);
136

    
137
        /*Token ring / sensor network initialization.*/
138
        wl_token_ring_register();        
139
}
140

    
141

    
142
/*Call wl_do and occasionally send a broadcast packet declaring the 
143
 * number of the server.
144
 */
145
void* run_wireless(void* arg){
146
        pthread_detach(pthread_self());
147
        
148
        init_wireless();
149
        printf("Wireless initialized\n");
150
        
151
        int a = 0;
152
        while(1){
153
                /*Passively wait for packets to arrive.*/
154
                wl_do();
155
        }
156
}
157

    
158
void packet_receive (char type, int source, unsigned char* packet, int length) {
159
        printf("A packet came in!\n");        
160
        if(type==DATA_POINT){        
161

    
162
                if(length!=18) 
163
                        printf("Packet came without the standard length!\n");
164

    
165
                /* expected input: x y theta(double) IR1 IR2 IR3 IR4 IR5*/
166
                /* Pull in data:*/
167
                
168
                short x,y,i;
169
                short ranges[5];        
170
                
171
                x = ((short)packet[1] << 8)  | (short)packet[0];
172
                y = ((short)packet[3] << 8)  | (short)packet[2];
173

    
174
                for(i=0; i<5; i++){
175
                        ranges[i] 
176
                           = ((short)packet[8+2*i+1] << 8) | (short)packet[8 + 2*i];
177
                }
178

    
179
                float theta = *((float*)(packet + 4));
180
        
181
                /*Write new data to buffers. (Lock down!)*/
182
                pthread_rwlock_wrlock(&buffer_lock);
183
                {        
184
                        /*Note that you write no matter what.        */
185
                        tail_index = (tail_index + 1) % BUFFER_SIZE; 
186
                        
187
                        /*Write the robots number.*/
188
                        robot_buffer[tail_index] = source;
189
                        
190
                        /*Write in range data.*/
191
                        for(i=0; i<5; i++) range_buffer[tail_index][i] = ranges[i];
192
                        
193
                        coordinate_buffer[tail_index][0] = x;
194
                        coordinate_buffer[tail_index][1] = y;
195
                        
196
                        orientation_buffer[tail_index] = theta;
197
                        
198
                        /*If the oldest member didn't exist, buffer was empty.*/
199
                        if(head_index == BUFFER_EMPTY) head_index = 0;
200
                
201
                        /*Otherwise check for overwritten data.        */
202
                        else if(tail_index == head_index) 
203
                                head_index = (head_index +1) % BUFFER_SIZE;  
204
                        
205
                }
206
                pthread_rwlock_unlock(&buffer_lock);
207
        }
208
        else if(type == DATA_SERVER_IDENTIFY){
209
                wl_send_global_packet(MAP_REQUEST_GROUP, DATA_SERVER_REPLY, NULL, 0, 0);
210
                printf("Announcing self as server\n");
211
        }
212
}
213