1 前言
本文实现一个可串口输出TOF测量距离的传感器,将其测量距离在matlab gui 上面显示。并绘制历史测量数据曲线。
1.1 传感器
nooploop tof 传感器
1.2 协议解析
1.3 串口读取数据
我们用matlab 的串口读取数据
matlab">port =get(handles.ed_com_num,'String');
display(port)
Conm_num_str = port;
display(Conm_num_str)
Scom=serial(Conm_num_str);
Scom=serial(Conm_num_str);
Scom.InputBufferSize=2000; %设置好buf的空间,足够最多一次指令返回数据的存储
Scom.timeout=3;
Scom.BaudRate=115200;
Scom.DataBits=8;
Scom.Parity='none';
Scom.StopBits=1;
Scom.ByteOrder='bigEndian';
fopen(Scom); %打开串口设备对象
g_data = fread(Scom,16);% 向串口读取10个数据,这里的数据根据需求可以进行改动
matlab_29">1.5 matlab协议解析
根据用户手册解析数据,这里我们没有加校验。
matlab">g_data = fread(Scom,16);% 向串口读取10个数据,这里的数据根据需求可以进行改动
%= (int32_t)(byte[0] << 8 | byte[1] << 16 | byte[2] << 24) / 256
dis0 =g_data(9);
dis1 =g_data(10);
dis2 =g_data(11);
distance_or1 = bitor(bitshift(int32(dis0),8),bitshift(int32(dis1),16))
distance_or2 = bitor(distance_or1, bitshift(int32(dis2),24))
real_distance = distance_or2/256/1;
2 GUI
3 所有代码
matlab">function varargout = distance_measure(varargin)
% DISTANCE_MEASURE MATLAB code for distance_measure.fig
% DISTANCE_MEASURE, by itself, creates a new DISTANCE_MEASURE or raises the existing
% singleton*.
%
% H = DISTANCE_MEASURE returns the handle to a new DISTANCE_MEASURE or the handle to
% the existing singleton*.
%
% DISTANCE_MEASURE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DISTANCE_MEASURE.M with the given input arguments.
%
% DISTANCE_MEASURE('Property','Value',...) creates a new DISTANCE_MEASURE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before distance_measure_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to distance_measure_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help distance_measure
% Last Modified by GUIDE v2.5 07-Nov-2024 22:01:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @distance_measure_OpeningFcn, ...
'gui_OutputFcn', @distance_measure_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before distance_measure is made visible.
function distance_measure_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to distance_measure (see VARARGIN)
% Choose default command line output for distance_measure
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes distance_measure wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = distance_measure_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function ed_distance_show_Callback(hObject, eventdata, handles)
% hObject handle to ed_distance_show (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ed_distance_show as text
% str2double(get(hObject,'String')) returns contents of ed_distance_show as a double
% --- Executes during object creation, after setting all properties.
function ed_distance_show_CreateFcn(hObject, eventdata, handles)
% hObject handle to ed_com_num (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ed_com_num_Callback(hObject, eventdata, handles)
% hObject handle to ed_com_num (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ed_com_num as text
% str2double(get(hObject,'String')) returns contents of ed_com_num as a double
% --- Executes during object creation, after setting all properties.
function ed_com_num_CreateFcn(hObject, eventdata, handles)
% hObject handle to ed_com_num (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pb_start_acquisite.
function pb_start_acquisite_Callback(hObject, eventdata, handles)
% hObject handle to pb_start_acquisite (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global port;
global g_data;
global Scom;
port =get(handles.ed_com_num,'String');
display(port)
Conm_num_str = port;
display(Conm_num_str)
Scom=serial(Conm_num_str);
Scom=serial(Conm_num_str);
Scom.InputBufferSize=2000; %设置好buf的空间,足够最多一次指令返回数据的存储
Scom.timeout=3;
Scom.BaudRate=115200;
Scom.DataBits=8;
Scom.Parity='none';
Scom.StopBits=1;
Scom.ByteOrder='bigEndian';
fopen(Scom); %打开串口设备对象
g_data = fread(Scom,16);% 向串口读取10个数据,这里的数据根据需求可以进行改动
disp(g_data)
fprintf("open serial success");
%set(handles.pb_openserial,'enable','off');%打开串口的按钮变成灰色,不再可用
max_num = 80
polt_distance=ones(1,max_num)
while 1
g_data = fread(Scom,16);% 向串口读取10个数据,这里的数据根据需求可以进行改动
%= (int32_t)(byte[0] << 8 | byte[1] << 16 | byte[2] << 24) / 256
dis0 =g_data(9);
dis1 =g_data(10);
dis2 =g_data(11);
distance_or1 = bitor(bitshift(int32(dis0),8),bitshift(int32(dis1),16))
distance_or2 = bitor(distance_or1, bitshift(int32(dis2),24))
real_distance = distance_or2/256/1;
polt_distance(1:max_num-1)=polt_distance(2:max_num);
polt_distance(max_num)=real_distance
ch='Distance',axes(handles.axes1),plot(polt_distance,'g');
xlabel('sample number'),ylabel('distance/mm'),title(ch)
set(handles.ed_distance_show,'string',num2str(real_distance))
disp('distance');
%pause(10)
end
function pb_start_acquisite_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pb_close_port.
function pb_close_port_Callback(hObject, eventdata, handles)
% hObject handle to pb_close_port (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%set(handles.pb_openserial,'enable','on');%打开串口的按钮重新可用
global Scom;%全局变量Scom
display(Scom);
fclose(Scom);
delete(Scom);
delete(instrfind);
function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end