function [s1, fireString, theta] = hokuyoNavLabInit(COMM_PORT) % Supply com port Number % returns a serial port object, a firing string and set of angles (rad) % zero is forward, positive CCW loooking down on laser % set up serial comms, baud rate must start at 19200 but can be tweaked % later % output buffer must be big enough to take whole scan %% clean up old serial port warning off comm = strcat('COM', num2str(COMM_PORT)); a = instrfind('port',comm); if ~isempty(a) disp('That com port is in use. Closing it.') % name = get(a, 'Tag') % if strcmp(name, 'Hokuyo') % fopen(a) % fprintf(a,'S0192000000000'); % confirmation = char(fread(a,2000)) % end fclose(a) delete(a) end %% open new serial port disp('Creating connection....') s1 = serial(comm,'BaudRate', 19200); set(s1,'Terminator','LF'); set(s1,'InputBufferSize',2000); set(s1, 'Timeout', 1); set(s1, 'Tag', 'Hokuyo') disp('Opening connection...') fopen(s1); disp('Turning laser on....') % turn laser on fprintf(s1,'L1'); confirmation = char(fread(s1,6)); if isempty(confirmation) || confirmation(4)~='0' confirmation disp('Error initializing laser or serial port. Check port number and connection. Try power cylcing laser.'); return end %% Establish 115200 baud rate %fprintf(s1,'S1152000000000'); %confirmation = char(fread(s1,20)); %if confirmation(16)~='0' % disp('Error initializing baud Rate. Try power cycling laser.'); % return %end %set(s1,'BaudRate', 115200); % flushing buffer %confirmation = char(fread(s1,2000)); % Should see command echo and a 0 indicating all is well, Expecting 6 % characters... %% set up angular resolution % this is what manual indicates range of sweep should be % for some reason the data gets sent in reverse order (starting w/ positive % theta) % count = (theta(deg)+135)*384/135 % theta(deg) = count*135/384-135 disp('Creating angles....') theta = [fliplr( pi/180*(0.3515625*[128:14:632] - 135) )]; fireString = strcat('G12863214'); disp('Flush buffer....') while (s1.BytesAvailable()~=0) fread(s1,1) end