Example of use of HOKYUO URG-04LX-UG01
The basic procedure of connecting and acquiring data.
Contents
Measurement parameters initialization
The URG-04LX-UG01 has angular resolution of 1024 steps per 360 degrees. Its measurement range is from step 44 to 725 (-120°,120°), on the front of the sensor is step 384.
start_point = 44; end_point = 725;
The measurements can be merged (grouped) into clusters by n steps. For ex. if cluster = 3, the sensor will send a minimum value from every 3 steps.
cluster = 1;
Concatenate the command for measuring. In this example, 2-character encoding is used (maximum distance value is 4096 mm), hence parameter GS. For 3-char. encoding use parameter GD.
scan_cmd=strcat('GS',... num2str(start_point,'%04g'),... num2str(end_point,'%04g'),... num2str(cluster,'%02g'));
Connecting
First, create serial port object and set its parameters. You have to replace COM7 with corresponding port name. To see available ports, use instrhwinfo or Device Manager.
URG=serial('COM7','baudrate',115200); set(URG,'Timeout',1); set(URG,'InputBufferSize',50000); set(URG,'Terminator','LF');
Then open communication. It's a good practice to introduce delay when using serial communication.
fopen(URG); pause(0.1);
Acquiring data
1. Turn the laser on and flush the response from the sensor.
fprintf(URG,'BM'); pause(0.1); while(URG.BytesAvailable~=0) fgetl(URG); end
2. Send the command for measuring, wait for response, then flush echo and status information. If you don't need a time stamp, you can flush it as well. Follows a stream of distances from the sensor ended by two byte terminator LFLF.
fprintf(URG,scan_cmd); while(URG.BytesAvailable==0) pause(0.001); end fgetl(URG); % echo fgetl(URG); % status timestamp=decodeSCIP_timestamp(fgetl(URG)); % 24bit timer, 1ms resolution data=[]; while(URG.BytesAvailable>2) data=[data;range_convert(fgetl(URG))]; end fgetl(URG); % LFLF
3. Turn the laser off and flush the response from the sensor.
pause(0.1); fprintf(URG,'QT'); pause(0.1); while(URG.BytesAvailable~=0) fgetl(URG); end
Disconnecting
Close communication
fclose(URG);
Appendix: Plotting acquired data
The measurement data don't include any information about the steps/angles, it's only an array of distances. Based on cluster, start_step and end_step, the corresponding angles must be computed.
angles=(((start_point-384):cluster:(end_point-384))*2*pi/1024)';
After converting these polar coordinates into Cartesian system in meters, the data can be plotted for ex. as a scatter plot.
data_xy(:,1)=-(data/1000).*sin(angles); data_xy(:,2)=(data/1000).*cos(angles); scatter(data_xy(:,1),data_xy(:,2),'r.'); grid on axis equal; axis([-4.25 4.25 -4.25 4.25]) shg
