Project

General

Profile

Revision 1361

Made some small changes reading through the code, no mex file compiler at home so I might have broken it.. but probably not.

View differences:

trunk/code/projects/mapping/matlab/testRobot/test.c
1 1
/** This test should just have the robot stay in place,
2 2
 * register the token ring, participate in the token ring,
3
 * and reply to packets when asked by the computer.*/
3
 * and send packets after flashing its BOM*/
4 4

  
5 5
#include <dragonfly_lib.h>
6 6
#include <wireless.h>
......
40 40
    //odometry_init();
41 41
    
42 42
	wl_init(); usb_puts("wireless turned on\r\n");
43
    wl_set_channel(WL_CHANNEL);
44
    wl_register_packet_group(&requestHandler);
43
    	wl_set_channel(WL_CHANNEL);
44
    	wl_register_packet_group(&requestHandler);
45 45
    
46
    wl_token_ring_register();
46
    	wl_token_ring_register();
47 47
	wl_token_ring_set_bom_functions(transmit, bom_off, get_max_bom);
48
    wl_token_ring_join();
48
    	wl_token_ring_join();
49 49
	usb_puts("token ring joined\r\n");
50 50

  
51 51
	char buf[500];
52 52
	
53 53
	int prescalar = 0;
54 54

  
55
    while(1)
56
    {
55
    	while(1)
56
    	{
57 57
		wl_do();
58 58
		
59 59
		if(!(++prescalar%1000)){	
......
76 76
	
77 77
		int store[9];
78 78
		store[0] = 0;
79
    	store[1] = 0;
80
    	*((double*)(store + 2)) = 5.0;
81
    	store[4] = 125;	/* IR1 range*/
79
    		store[1] = 0;
80
    		*((double*)(store + 2)) = 5.0;
81
    		store[4] = 125;	/* IR1 range*/
82 82
		store[5] = 125;	// IR2 range
83 83
		store[6] = 125;	// IR3 range
84 84
		store[7] = 125;	// IR4 range
......
88 88

  
89 89
		wl_send_robot_to_robot_global_packet(MAP_REQUEST_GROUP, 
90 90
			DATA_POINT, data, 18, source, 0);
91
		delay_ms(50);
92 91
	}
93 92
	else if(type==DATA_SERVER_REPLY){
94 93
		server = source;
......
100 99
	if(server == -1){
101 100
		wl_send_global_packet(MAP_REQUEST_GROUP, 
102 101
			DATA_SERVER_IDENTIFY, NULL, 0, 0);
103
		delay_ms(50);
104 102
	}
105 103
	else{
106 104
	
107 105
		int store[9];
108 106
		store[0] = 0; //X
109
    	store[1] = 0; //Y
110
    	*((double*)(store + 2)) = 5.0; //Theta.
111
    	store[4] = 125;	// IR1 range
107
    		store[1] = 0; //Y
108
    		*((double*)(store + 2)) = 5.0; //Theta.
109
    		store[4] = 125;	// IR1 range
112 110
		store[5] = 125;	// IR2 range
113 111
		store[6] = 125;	// IR3 range
114 112
		store[7] = 125;	// IR4 range
......
118 116

  
119 117
		wl_send_robot_to_robot_global_packet(MAP_REQUEST_GROUP, 
120 118
			DATA_POINT, data, 18, server, 0);
121
		delay_ms(50);
122 119
	
123 120
		bom_on();
124 121
	}
trunk/code/projects/mapping/matlab/prob_map.m
47 47

  
48 48
%loop through each line of file
49 49
i = 1; ir = 1;
50
%for i=1:size(Data, 1)
50
for i=1:size(Data, 1)
51 51
    row = Data(i, :);
52 52
    x = row(1);
53 53
    y = row(2);
54 54
    theta = row(3);
55 55
	%look at each IR sensor reading
56
    %for ir=1:5
57
        %if row(ir+3) ~= -1
56
    for ir=1:5
57
        if row(ir+3) ~= -1
58 58
            %Find vector of object relative to center of bot
59 59
            distance = row(ir+3) * 1; %TODO!!!!!!!!! replace 1 with a conversion factor to cm
60 60
            ObjectVector = distance * Angle_IR(ir, :);
......
66 66
            objTheta = objTheta + theta;
67 67
            
68 68
            %add absolute position and shift into image
69
            objX = (objR * cos(objTheta)) + x + 1500;
70
            objY = (objR * sin(objTheta)) + y + 1500;
69
            objX = (objR * cos(objTheta)) + x + size(X1,1);
70
            objY = (objR * sin(objTheta)) + y + size(X2,2);
71 71
            ObjectVector = [objX, objY];
72 72
        	
73
			%Calculate distribution for this point:
73
			%Calculate distribution for this data point:
74 74
			
75 75
			%Range variance in y direction, rotated by the object angle.
76 76
			rotation = [cos(objTheta),sin(objTheta); -sin(objTheta),cos(objTheta)];
......
80 80
			%(TODO introduce error in position estimation perpendicular to the robot)
81 81
			rotation = [cos(theta),sin(theta); -sin(theta),cos(theta)];
82 82
			position_error_cov = inv(rotation)*[0,0;0,pos_error]*rotation;
83

  
83
			
84 84
			covar = position_error_cov + range_error_cov;
85 85
			
86
			dist = reshape(mvnpdf(X,[x,y],covar), size(X1,1),size(X2,2))
87
			surf(X1,X2,dist);
88
			colormap default;
89
			return;
86
			dist = reshape(mvnpdf(X,[x,y],covar), size(X1,1),size(X2,2));
90 87

  
91
			%Using the values of the pdf as an approximation for the prob. density map
88
			%Using the values of the pdf as an approximation for the prob. density map NOT SAFE
92 89
			%(needs to change) update as:
93 90
			prob_map = prob_map.*(1-dist) + dist;	
94
        %end
95
    %end
96
%end
91
        end
92
    end
93
end
97 94

  
98 95
%graph the probability map.
99 96
surf(X1,X2,prob_map);
97
colormap default;
100 98

  
101 99

  
trunk/code/projects/mapping/matlab/receive.c
31 31
PacketGroupHandler requestHandler = {MAP_REQUEST_GROUP, NULL, NULL, &packet_receive, NULL};
32 32

  
33 33
struct robot_datapoint{
34
	time_t time;
34
	time_t time; //TODO write time too.
35 35
	short number;
36 36
	short range[5];
37 37
	short x;
......
98 98
		{
99 99
			double** point, **ranges, *theta, *robot;
100 100
			
101
			int num_buffer_elements = tail_index - head_index;
102
			if(num_buffer_elements < 0) num_buffer_elements += BUFFER_SIZE;	
101
			int num_buffer_elements = tail_index >= head_index ? 
102
				tail_index - head_index : tail_index - head_index + BUFFER_SIZE;
103 103

  
104 104
			ret[0] = mxCreateDoubleMatrix(num_buffer_elements, 2, mxREAL);
105 105
			ret[1] = mxCreateDoubleMatrix(num_buffer_elements, 5, mxREAL);
......
112 112
			robot = mxGetPr(ret[3]);
113 113

  
114 114
			for(index = 0; index < num_buffer_elements; index++){
115
				int target = (head_index + index) % BUFFER_SIZE;	
115
				int target = (head_index + index) % BUFFER_SIZE;
116 116
				point[i][0] = (double)robot_data[target].x;	
117 117
				point[i][1] = (double)robot_data[target].y;	
118 118
				
......
167 167
		
168 168
		short x,y,i;
169 169
		short ranges[5];	
170
		struct robot_datapoint data;
170 171
		
171 172
		/*Bytes 0-3*/
172
		x = ((short)packet[1] << 8)  | (short)packet[0];
173
		y = ((short)packet[3] << 8)  | (short)packet[2];
173
		data.x = ((short)packet[1] << 8)  | (short)packet[0];
174
		data.y = ((short)packet[3] << 8)  | (short)packet[2];
175

  
174 176
		
175 177
		/*Bytes 4-7*/
176
		float theta = *((float*)(packet + 4));
178
		data.orientation = *((float*)(packet + 4));
177 179
	
178 180
		/*Bytes 8-17*/
179 181
		for(i=0; i<5; i++){
180
			ranges[i] 
182
			data.range[i] 
181 183
			   = ((short)packet[8+2*i+1] << 8) | (short)packet[8 + 2*i];
182 184
		}
183

  
185
		
184 186
		/*Write new data to buffers. (Lock down!)*/
185 187
		pthread_rwlock_wrlock(&buffer_lock);
186 188
		{	
187 189
			/*Note that you write no matter what.
188 190
			* Also, if the buffer is empty, tail_index becomes 0*/
189 191
			tail_index = (tail_index + 1) % BUFFER_SIZE; 
190
			
192

  
191 193
			/*Write the robots number.*/
192 194
			robot_data[tail_index].number = source;
193 195
			
......
201 203
			
202 204
			/*If the oldest member didn't exist, buffer was empty.*/
203 205
			if(head_index == -1) head_index = 0;
204
		
205
			/*Otherwise check for overwritten data.	*/
206
	
207
			/*Otherwise check for overwritten data.*/
206 208
			else if(tail_index == head_index) 
207 209
				head_index = (head_index + 1) % BUFFER_SIZE;  
208
			
209 210
		}
210 211
		pthread_rwlock_unlock(&buffer_lock);
211 212
	}
212 213
	else if(type == DATA_SERVER_IDENTIFY){
213 214
		/*Sleep before sending an identity packet.*/
214 215
		printf("Announcing self as server\n");
215
		usleep(5000);
216 216
		wl_send_global_packet(MAP_REQUEST_GROUP, DATA_SERVER_REPLY, NULL, 0, 0);
217
		usleep(5000);
218 217
	}
219 218
}
220 219

  

Also available in: Unified diff