justd0 发表于 2020-8-24 23:46

【LSM6DSOX有限状态机例程学习 三】--垂直翻转(Flip Down)

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">上周末,</span><a href="https://bbs.eeworld.com.cn/thread-1137203-1-1.html"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">有限状态机及例程简介</span></a><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">和</span><a href="https://bbs.eeworld.com.cn/thread-1137248-1-1.html"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">有限状态机编程使用方法</span></a><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">这两篇帖子已经较为详细的介绍了</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">L</span><span lang="zh-CN" style="font-family:&quot;Times New Roman&quot;">SM6DSOX</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">的有限状态机的特点和基本的使用方法。本周开始,我将对</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">ST</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">官方所提供的</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">FSM</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">例程进行讲解分享,从例程理解到复现,进一步掌握</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">L</span><span lang="zh-CN" style="font-family:&quot;Times New Roman&quot;">SM6DSOX</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">有限状态机的使用方法。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">小伙伴们可以先去github上下载下ST的<a href="https://github.com/STMicroelectronics/STMems_Finite_State_Machine">MEMS有限状态机的例程仓库</a>,为方便起见,我已经将LSM6DSOX的例程打包上传到百度云了,链接和提取码如下:</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><a href="https://pan.baidu.com/s/1PAexCkAoEr5qmKMo5xYMOg">https://pan.baidu.com/s/1PAexCkAoEr5qmKMo5xYMOg</a></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">提取码:ufln</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">下载并解压后,可以得到如下七个文件夹:</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">每个文件中包含了</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">README.</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">md</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">、后缀</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">.h</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">、后缀</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">.ucf</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">文件,其中:</span></span></p>

<p><span style="font-size:14.0pt"><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">README.</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">md </span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">简单的介绍了示例的基本信息</span></span></p>

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">后缀</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">.</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">h</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">文件是根据</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">FSM</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">的具体配置参数,通过</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">Unico GUI</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">生成的</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">C</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">代码头文件,可以很方便的将其独立的运行在自己的工程程序中。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">后缀.ucf文件是Unico GUI的标准配置文件,Unico GUI可以直接将配置信息保存为ucf文件,也可以将其加载并导入到传感器中。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">本帖主要来讲解下</span> <span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">翻转识别</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">F</span><span lang="zh-CN" style="font-family:&quot;Times New Roman&quot;">lip-Down detection</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">示例的实现过程。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">LSM6DSOX这款传感器定位在物联网、低功耗角度,那么一定是考虑各种物联网设备的应用角度来设计的。 </span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">例程中的翻转识别,顾名思义就是识别设备是否被翻转到一定的角度,这个应用在智能手表、手机上可以经常简单到,比如有的手机可以设置翻扣手机设置静音、智能手表抬腕显示时间等等。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">那么翻转识别是怎么是实现的呢?</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">在地球上任意地方,都会存在一个指向地心的重力加速度,而传感器中的加速度计就可以感应到这个加速度存在,加速度计有三个方向的感知能力,分别是X\Y\Z,重力加速度在这三个轴方向上的矢量和便指向了地心,那么通过重力加速度就可以判断出传感器的方向。</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Times New Roman&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">明白了该如何实现此功能之后,我们来看下官方给的</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">F</span><span lang="zh-CN" style="font-family:&quot;Times New Roman&quot;">lip-Down detection</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">例程,通过</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">Unico GUI</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">的</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">load</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">功能,将</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">ucf</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">文件配置到传感器上</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Times New Roman&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">当load按钮左边显示Loaded的时候,参数就加载好了。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">此时,打开左边的FSM控件,我们就可以看到<span lang="zh-CN" style="font-weight:bold">翻转识别</span>的状态机代码了。</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Times New Roman&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">关于这个编程界面的讲解,可以参考上一篇文章:<a href="https://bbs.eeworld.com.cn/thread-1137248-1-1.html">有限状态机编程使用方法</a></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">我们来看下这个这段例程,一共7条语句,结合上篇文章中的末尾的指令集说明文档,</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">逐句的分析下每句的意义:</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-weight:bold"><span style="font-family:&quot;Times New Roman&quot;">S0: </span></span><span style="font-weight:bold"><span style="font-family:&quot;Microsoft YaHei&quot;">RNC&nbsp; NOP GNTH2</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">一条判断语句,GNTH2:当某轴数据大于Thresh2则成立,NOP:否则就重置。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">可以看到左边变量区中Thresh2 = 0,Mask A = 08 ,那么根据掩码的设置规则:</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">这里选择的就是Z轴正方向,那么这条语句的意义就是 当Z轴正向加速度数值大于0的时候就成立,可以跳转到S1条,否则则返回S0.</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><span style="font-weight:bold">S1: RNC LNTH2 TI4</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">又是一条判断语句,这里的前进条件是TI4:当定时计数大于Timer4设置的阈值则成立,重置条件是LNTH2:当某轴的数据小于Thresh2。这里的定时计数器时间基准是根据FSM OCR设置的频率来计数的,Timer = 0x0D,则总时长为1/26*13 = 0.5s。那么这条语句的意思就是如果Z轴正方向加速度值大于0g超过0.5s则跳转到S2,否则跳转到S0.</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><span style="font-weight:bold">S2: SRP</span></span></span></p>

<p><span style="font-size:14.0pt"><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">这是一条命令指令,意义是将复位重置指针设置为下一个地址,也就是之后再遇到判断语句的重置状态则跳转到</span><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">S3,</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">而不跳转到</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">S0.</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><span style="font-weight:bold">S3: GNTH1 TI3</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">这条判断语句的前进条件为TI3:也就是当定时计数大于Timer3设置的阈值则成立,重置条件是GNTH1:当某轴的数据大于Thresh1,则重置。Thresh1 = -0.85,Z轴正方向加速度值大于-0.85,那也就是Z轴正向加速度小于-0.85g超过1/26*10=0.38s以上,则跳转到S4,否则跳转S3。</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-weight:bold">S4:CRP</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">这条命令指令重置SRP的作用,将复位重置指针设置回S0,</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><span style="font-weight:bold">S5:CONTREL</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">这条指令执行循环到重置点S0,并且将掩码输出到OUTSx寄存器中,同时,由于SM1 Status中设置了INT1,也就是将中断路由到了外部输出中断引脚上,那么在执行S5指令时,则可以看到外部中断输出INT1产生脉冲。</span></span></p>

<p lang="en-US"><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;"><span style="font-weight:bold">S6:STOP</span></span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">这条指令时停止执行指令,从上数代码来看一般不会跳转到这个位置。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">ST</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">官方为了方便编程,还做了状态机图显示界面,打开</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">Debug</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">标题栏,就可以看到如下:</span></span></p>

<p></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">那么结合上述指令简介和状态机图,我们不难分析出他的具体工作流程是:</span></span></p>

<p><span style="font-size:14.0pt"><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">1</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">、首先,传感器正向为</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">Z</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">轴正向方向,那么在一般的时候,他应该是朝着向上的方向,</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">S0</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">和</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">S1 </span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">才回成立,也就避免了设备处于面板朝下的状态,再变到反向正朝下的时候产生中断的问题。</span></span></p>

<p><span style="font-size:14.0pt"><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">2</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">、当排除了</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">1</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">的问题后,只要判断设备是否正向超下了一定时间,就可以得到翻转识别的效果了。</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">&nbsp;</span></span></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">下面是测试效果的视频:</span></span></p>

<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=27141" style="background:#eee;margin-bottom:10px;" width="750"></iframe></p>

<p><span style="font-size:14.0pt"><span style="font-family:&quot;Microsoft YaHei&quot;">思考题:</span></span></p>

<p><span style="font-size:14.0pt"><span lang="en-US" style="font-family:&quot;Times New Roman&quot;">1</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">、假如设备安装传感器的方向不是</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">Z</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">轴正向朝向面板方向,而是</span><span lang="en-US" style="font-family:&quot;Microsoft YaHei&quot;">X</span><span lang="zh-CN" style="font-family:&quot;Microsoft YaHei&quot;">轴正向的话,那么我们想要实现识别翻扣设备的动作该怎么修改配置呢?</span></span></p>
页: [1]
查看完整版本: 【LSM6DSOX有限状态机例程学习 三】--垂直翻转(Flip Down)