2021-08-23matlab学习

news/2024/7/2 16:29:32 标签: matlab

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

在这里插入图片描述

matlab1C0_16">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);

matlab_100">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))])

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

相关文章

iOS-关于真机调试那些事

2019独角兽企业重金招聘Python工程师标准>>> 引言 关于开发证书配置&#xff08;Certificates & Identifiers & Provisioning Profiles&#xff09;&#xff0c;相信做iOS开发的同学没少被折腾。对于一个iOS开发小白、半吊子&#xff08;比如像我自己&#…

asp.net 状态的传递和保存

http无状态 Http协议是无状态的&#xff0c;不会记得上次和网页“发生了什么”&#xff08;故事《初恋50次》&#xff09;。试验&#xff1a;private 字段。server不记的上次给了浏览器什么。否则server的压力会太大&#xff0c;浏览器须要记住这些值&#xff0c;下次再提交ser…

Java基础之8个基本类型及基本类型间转换

8种基本数据类型 Java语言有8种基本数据类型&#xff0c; 分别用于存储整数、浮点数、字符数据和布尔类型数据。需要注意的是&#xff1a; 现在所介绍的仅仅是基本数据类型&#xff0c;后续还会介绍很多非基本数据类型。基本数据类型如图 – 1所示&#xff1a; 图- 1 从图- 1中…

fastjson缺陷--map转换json时出现$ref的情况

DisableCircularReferenceDetect来禁止循环引用检测&#xff1a; JSON.toJSONString(..., SerializerFeature.DisableCircularReferenceDetect)转载于:https://www.cnblogs.com/yw-ah/p/7238883.html

python学习之路之案例1(修改haproxy的配置文件,并实现下线、上线的功能)

整个案例运用的知识点&#xff1a;1.python基本数据结构的使用&#xff1a;列表、字典等2.python两个模块的使用&#xff1a;os和json1.os.rename(文件1&#xff0c;文件2)2.json完成自动识别字典、列表&#xff0c;并识别后进行自动转换3.python函数的定义和调用4.标志位的灵活…

echo 和 echo 的区别

echo > 会将目标文件清空后再写入 echo >> 在目标文件后追加写入转载于:https://www.cnblogs.com/EnzoDin/p/7655665.html

spring security4.2.2的maven配置+spring-security配置详解+java源码+数据库设计

一、引子 最近项目需要添加权限拦截&#xff0c;经讨论决定采用spring security4.2.2&#xff01;废话少说直接上干货&#xff01; spring security 4.2.2文档&#xff1a;http://docs.spring.io/spring-security/site/docs/4.2.2.RELEASE/reference/htmlsingle/#el-access-web…

扩展Linux虚拟机硬盘、分区一例

在给客户部署Linux虚拟机时&#xff0c;我一般都会单独做出一个分区来存储客户的所有数据&#xff0c;并取名为data。/data分区的默认大小为35G&#xff0c;随着时间的发展&#xff0c;有的客户提出扩大/data分区的需求。下面是我在ESX4.0上的实施步骤&#xff1a;注&#xff1…