激光TOF传感器串口采集数据同matlab 显示

news/2024/11/8 23:58:00 标签: matlab, 开发语言

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

http://www.niftyadmin.cn/n/5744590.html

相关文章

【数据结构-合法括号字符串】【hard】【拼多多面试题】力扣32. 最长有效括号

给你一个只包含 ‘(’ 和 ‘)’ 的字符串&#xff0c;找出最长有效&#xff08;格式正确且连续&#xff09;括号子串的长度。 示例 1&#xff1a; 输入&#xff1a;s “(()” 输出&#xff1a;2 解释&#xff1a;最长有效括号子串是 “()” 示例 2&#xff1a; 输入&#xf…

测试自动化如何和业务流程结合?

测试自动化框架固然重要&#xff0c;但是最终自动化的目的都是为了业务服务的。 那测试自动化如何对业务流程产生积极影响&#xff1f; 业务流程的重要性 测试自动化项目并非孤立存在&#xff0c;其生命周期与被测试的应用程序紧密相关。项目的价值在于被整个开发团队所使用&a…

Prometheus 不是 OPEN TSDB

误解——这无疑是阐释我早期 Prometheus 体验的恰当词汇。彼时&#xff0c;我怀揣着丰富的 Graphite 使用经验以及中等程度的 InfluxDB 经验&#xff0c;开始接触 Prometheus。 在我眼中&#xff0c;Graphite 是一个性能卓越却有着相当局限性的系统。在 Graphite 里&#xff0c…

新系统如何进行模型环境配置

在机器学习和深度学习中&#xff0c;一个良好的开发环境能够显著提高工作效率。本篇博客将详细介绍如何在新的Linux系统&#xff08;以Ubuntu为例&#xff09;上进行模型环境的配置&#xff0c;包括基础系统设置、Python虚拟环境搭建、常用库的安装以及GPU驱动和CUDA的安装等。…

C++【string类,模拟实现string类】

&#x1f31f;个人主页&#xff1a;落叶 &#x1f31f;当前专栏: C专栏 目录 为什么学习string类 C语言中的字符串 标准库中的string类 auto和范围for auto关键字 迭代器 范围for string类的常用接口说明和使用 1. string类对象的常见构造 2.string类对象的容量操作 3…

laravel chunkById 分块查询 使用时的问题

laravel chunkById 分块查询 使用时容易出现的问题 1. SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘id’ in where clause is ambiguous 使用chunkById时&#xff0c;单表进行分块查询&#xff0c;是不会出现id重复的&#xff0c;当用两个表进行 join 查…

netstat中sendq/recvq用于排查发送端发送数据的问题

web同事开发了一个用于接收syslog数据的服务器&#xff0c;不清楚web的开发方式&#xff0c;用来联调的发送端是我们的C模块 反馈syslog udp形式接收正常&#xff0c;速度正常&#xff0c;数量也正常&#xff0c;syslog tcp形式接收开始比较快后面越来越慢&#xff0c;并且知道…

内部知识库:优化企业培训流程的关键驱动力

在当今快速变化的商业环境中&#xff0c;企业培训的重要性日益凸显。内部知识库作为整合、管理和分享企业内部学习资源的关键工具&#xff0c;正逐步成为优化企业培训流程的核心。以下将探讨内部知识库如何通过多种功能&#xff0c;助力企业提升培训效率、质量和员工满意度。 …