2021-08-23matlab学习

1、十六进制转成十进制hex2dec.这个函数转换得到的是double类型的十进制;所以要转成uint8类型

cc=hex2dec(‘5’)

cc=5
cc=hex2dec(‘A’)
cc=10
cc=hex2dec(‘a’)
cc=10
cc=hex2dec(‘a1’)
cc=161

161=10*16+1

在这里插入图片描述

2、matlab的数组,从下标1开始;而不是C语言的0开始。

3、测试

a(1,1)=‘a’;
a(1,2)=‘b’;
c=hex2dec(a);
d=uint8©;
上面例子中。c=171,是double类型。因为hex2dec函数,返回值是double;
d=171,是uint8类型,已经转换了一下。

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

4、if----else使用.注意end。

不等于 ~=
大于等于>=,小于等于<=

%from=5,to=8
if size([from:to],2) ~= 4
    disp('error: too few arguments')
elseif size(input,1) <= to
    % float(A,7,10);
    disp('error: message error')
else
    for i = [1:2:8]
        tmp(1,i) = input(to,1);
        tmp(1,i+1) = input(to,2);
        to = to - 1;
    end
    val = typecast(uint32(hex2dec(tmp)),'single');
end

5、

switch case的使用。没有冒号。没有break,用otherwise

 switch msg_id
            case 12
                if int16(crc(A,1,165)) == get_crc(A)
                    pwm(i,1) = int16_t(A,11,12);
                    rpm(i,1) = float(A,7,10);
                    disp(['pwm: ',num2str(pwm(i,1)),', ' ...
                        'rpm: ', num2str(rpm(i,1))])
                    h.XData = pwm;
                    h.YData = rpm;
                    drawnow
                    i = i + 1;
                end
            case 13
                if int16(crc(A,1,196)) == get_crc(A)
                    if uint8_t(A,7) == 1
                        fclose(t)
                        break
                    end
                end
            otherwise
                fclose(t)
                break
        end

6/ uint8_t## 标题## 标题函## 标题数,自己编写。

function [ val ] = uint8_t(input, to)
% float.m
% decode from hex to float
% input - mavlink message from udp
% to    - end byte index
if size(input,1) <= to
    disp('error')
else
    temp(1,1) = input(to,1);
    temp(1,2) = input(to,2);
    val = uint8(hex2dec(temp));
end
end
%example:  msg_id = uint8_t(A,6);

7/matlab开头

clc
clear
close all

8、int16_t(input, from, to)、、 pwm(i,1) = int16_t(A,11,12);

function [ val ] = int16_t(input, from, to)
% float.m
% decode from hex to float
% input - mavlink message from udp
% from  - start byte index
% to    - end byte index
if size([from:to],2) ~= 2
    disp('error: too few arguments')
elseif size(input,1) <= to
    disp('error: message error')
else
    for i = [1:2:4]
        tmp(1,i) = input(to,1);
        tmp(1,i+1) = input(to,2);
        to = to - 1;
    end
    val = typecast(uint16(hex2dec(tmp)),'int16');
end
end

9、 float(input, from, to)、、、 rpm(i,1) = float(A,7,10);

function [ val ] = float(input, from, to)
% float.m
% decode from hex to float
% input - mavlink message from udp
% from  - start byte index
% to    - end byte index

%from=5,to=8
if size([from:to],2) ~= 4
    disp('error: too few arguments')
elseif size(input,1) <= to
    % float(A,7,10);
    disp('error: message error')
else
    for i = [1:2:8]
        tmp(1,i) = input(to,1);
        tmp(1,i+1) = input(to,2);
        to = to - 1;
    end
    val = typecast(uint32(hex2dec(tmp)),'single');
end
end

10、 time(i,1) = uint32_t(A,7,10);

function [ val ] = float(input, from, to)
% float.m
% decode from hex to float
% input - mavlink message from udp
% from  - start byte index
% to    - end byte index
if size([from:to],2) ~= 4
    disp('error: too few arguments')
elseif size(input,1) <= to
    disp('error: message error')
else
    for i = [1:2:8]
        tmp(1,i) = input(to,1);
        tmp(1,i+1) = input(to,2);
        to = to - 1;
    end
    val = typecast(uint32(hex2dec(tmp)),'uint32');
end
end

11、画图。曲线

clc
if exist('pwm')
    clear pwm
end
if exist('rpm')
    clear rpm
end

t = udp('127.0.0.1', 80, 'LocalPort', 14550);
t.InputBufferSize = 89120;

fopen(t)

pwm = 0;
rpm = 0;
h = plot(pwm,rpm,'r.');
i = 1;
while 1
    tic %开始计时间
    A = dec2hex(fread(t));%将读到的数据,从10进制转变成16进制
    if ~isempty(A) %如果A不是空的
        msg_id = uint8_t(A,6);
        switch msg_id
            case 12
                if int16(crc(A,1,165)) == get_crc(A)
                    pwm(i,1) = int16_t(A,11,12);
                    rpm(i,1) = float(A,7,10);
                    disp(['pwm: ',num2str(pwm(i,1)),', ' ...
                        'rpm: ', num2str(rpm(i,1))])
                    h.XData = pwm;
                    h.YData = rpm;
                    drawnow
                    i = i + 1;
                end
            case 13
                if int16(crc(A,1,196)) == get_crc(A)
                    if uint8_t(A,7) == 1
                        fclose(t)
                        break
                    end
                end
            otherwise
                fclose(t)
                break
        end
    end
    freq(i,1) = 1/toc;
end

fclose(t)

绘制换行,三个省略点

     
                        disp(['time: ',num2str(time(i,1)),', ' ...
                            'gx: ',num2str(gx(i,1)),', ' ...
                            'gy: ', num2str(gy(i,1)),', ' ...
                            'gz: ', num2str(gz(i,1)),', ' ...
                            'ax: ', num2str(ax(i,1)),', ' ...
                            'ay: ', num2str(ay(i,1)),', ' ...
                            'az: ', num2str(az(i,1))])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值