Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.63 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/ttyUSB0" 
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_INIT 4 /*Just to give the robot a target.*/
23

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

    
27
pthread_rwlock_t buffer_lock;
28
pthread_t receive_thread;
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
140

    
141
/*Call wl_do and occasionally send a broadcast packet declaring the 
142
 * number of the server.
143
 */
144
void* run_wireless(void* arg){
145
        int robot_num, num_robots,time_out, i;        
146

    
147
        pthread_detach(pthread_self());
148
        
149
        init_wireless();
150
        printf("Wireless initialized\n");
151
        
152
        int a = 0;
153
        while(1){
154
                /*Passively wait for packets to arrive.*/
155
                wl_do();
156
        }
157
}
158

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

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

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

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

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