【定位问题】基于matlab GUI RSSI无线定位【含Matlab源码 1054期】

233 阅读6分钟

一、简介

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源代码

function varargout = test(varargin)
% TEST MATLAB code for test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST.M with the given input arguments.
%
%      TEST('Property','Value',...) creates a new TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_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 test

% Last Modified by GUIDE v2.5 26-Jun-2021 09:51:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_OpeningFcn, ...
                   'gui_OutputFcn',  @test_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 test is made visible.
function test_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 test (see VARARGIN)

% Choose default command line output for test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_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;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
for t = 1:5
    A = [0,0];
    B = [5*t,5*t*sqrt(3)];
    C = [10*t,0];
    nums = [A(1),A(2),B(1),B(2),C(1),C(2)];
    p = min(nums);
    q = max(nums);
    L = sqrt((A(1)-C(1))^2+(A(2)-C(2))^2);
    m = 10;
    syms b c;
    %生成在[p,q]上满足均匀分布的随机数矩阵
    %即生成一组m行2列的有可能落在等边三角形区域内的坐标
    numbox = p+(q-p)*rand(m,2);%rand函数产生由在(0, 1)之间均匀分布的随机数组成的数组
    
    %计数初值,最终根据计算将随机生成的点中落在等边三角形区域内的坐标存放于新的矩阵
    n = 1;
    for i = 1:m
        dA(i) = sqrt((numbox(i,1)-A(1))^2+(numbox(i,2)-A(2))^2);
        dB(i) = sqrt((numbox(i,1)-B(1))^2+(numbox(i,2)-B(2))^2);
        dC(i) = sqrt((numbox(i,1)-C(1))^2+(numbox(i,2)-C(2))^2);
        %将确实在等边三角形区域内的坐标存入P_position矩阵
        if (dA(i)<=L) & (dB(i)<=L) & (dC(i)<=L)
            P_position(n,1) = numbox(i,1);
            P_position(n,2) = numbox(i,2);
     
             b=P_position(n,1);
            c=P_position(n,2);
            n = n+1;
        end
    end
    %N为随机生成的点中落在等边三角形区域内的点(测试点)的个数
    N = n-1
    if N == 0
        disp('所取的随机坐标无一落在等边三角形内,请增大m值重新运行程序.')
        return
    end

    %计算测试点离三个顶点的实际距离
    %dis为N行3列的矩阵,用于存放N个测试点分别到等边三角形三个顶点A,B,C的实际距离
    for i = 1:N
        dis(i,1) = sqrt((P_position(i,1)-A(1))^2+(P_position(i,2)-A(2))^2);
        dis(i,2) = sqrt((P_position(i,1)-B(1))^2+(P_position(i,2)-B(2))^2);
        dis(i,3) = sqrt((P_position(i,1)-C(1))^2+(P_position(i,2)-C(2))^2);
    end
     a = 7; %由RSSI计算T-R距离时使用的参数
    for i = 1:N
        dis_test(i,1) = Distance(dis(i,1),a);
        dis_test(i,2) = Distance(dis(i,2),a);
        dis_test(i,3) = Distance(dis(i,3),a);
    end
    
    %根据函数Triangle及求得的测试距离进行定位
    %P_calculate为N行2列的矩阵,用于存放定位后的N个坐标
    for i = 1:N
        P_temp = Triangle(A,B,C,dis_test(i,1),dis_test(i,2),dis_test(i,3));
        P_calculate(i,1) = P_temp(1);
        P_calculate(i,2) = P_temp(2);
    end

    %由于测试距离相比真实距离有误差,三角计算中的两圆有可能无交点,导致方程无实根.
    %于是P_calculate中会出现虚数.在测试中虚数无实际意义,因此取其实部存放于另一矩阵
    for i = 1:N
        P_calculate_real(i,1) = real(P_calculate(i,1));
        P_calculate_real(i,2) = real(P_calculate(i,2));
    end

    %对比测试点的定位坐标与实际坐标之间的误差
    P_position;
    P_calculate;
    P_calculate_real;
    %计算定位结果与真实坐标之间的距离误差平均值e_average(测试点等概率)
    e_sum = 0;
    for i = 1:N
        e = sqrt((P_calculate_real(i,1)-P_position(i,1))^2+(P_calculate_real(i,2)-P_position(i,2))^2);
        e_sum = e_sum+e;
    end
    e_average = e_sum/N;
    e_average_percent = e_average/L;
    
    e_average_box(t) = e_average
    e_average_percent_box(t) = e_average_percent
    e_average_box(t) = e_average;
    
   x=e_average_percent_box(t);
   y = e_average_box(t);
end
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(edit1,'string',2);
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(edit2,'string',y);
% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)

for t = 1:5
    A = [0,0];
    B = [5*t,5*t*sqrt(3)];
    C = [10*t,0];
    nums = [A(1),A(2),B(1),B(2),C(1),C(2)];
    p = min(nums);
    q = max(nums);
    L = sqrt((A(1)-C(1))^2+(A(2)-C(2))^2);
    m = 10;
    syms b c;
    %生成在[p,q]上满足均匀分布的随机数矩阵
    %即生成一组m行2列的有可能落在等边三角形区域内的坐标
    numbox = p+(q-p)*rand(m,2);%rand函数产生由在(0, 1)之间均匀分布的随机数组成的数组
    
    %计数初值,最终根据计算将随机生成的点中落在等边三角形区域内的坐标存放于新的矩阵
    n = 1;
    for i = 1:m
        dA(i) = sqrt((numbox(i,1)-A(1))^2+(numbox(i,2)-A(2))^2);
        dB(i) = sqrt((numbox(i,1)-B(1))^2+(numbox(i,2)-B(2))^2);
        dC(i) = sqrt((numbox(i,1)-C(1))^2+(numbox(i,2)-C(2))^2);
        %将确实在等边三角形区域内的坐标存入P_position矩阵
        if (dA(i)<=L) & (dB(i)<=L) & (dC(i)<=L)
            P_position(n,1) = numbox(i,1);
            P_position(n,2) = numbox(i,2);
             b=P_position(n,1);
            c=P_position(n,2);
            n = n+1;
        end
    end
    %N为随机生成的点中落在等边三角形区域内的点(测试点)的个数
    N = n-1
    if N == 0
        disp('所取的随机坐标无一落在等边三角形内,请增大m值重新运行程序.')
        return
    end

三、运行结果

在这里插入图片描述

四、备注

版本:2014a