这一篇对于在做这个事情的人应该是有参考价值的。
前几天,我把树莓派连接IMU传感器连通了。
接着头就要求根据传感器接到的数据判断是否有翻转,若有翻转就向某接口发送请求,记录此事件。
1) 第一次错误的探索
我想先从简单的做起,先判断是否有触碰。
我想这个简单,既然accx,accy,accz三轴的加速度都得到了,
那么我只需要观察一下,传感器静止时,这3值情况如何,被触动时3值如何就可以了。
结果被“证实”了,静止时,3值小,触碰时值稍大。然后就做了一个简单算法,先存下静止的头10秒3值 sqrt(x*x+y*y+z*z) 的最大值maxAccTotal,然后后面判断当传感器 发现当前3值sqrt > (maxAccTotal*1.1) 时,告警并发送接口。
运行了一段时间,感觉是OK了。 (其实,做错了,后面解释。)
相关代码片段:
初始化部分
mpu = mpu9250.MPU9250()
print('Device connected:', mpu.searchDevice())
max_acc_normal = 0;
for i in range(50):
#
if mpu.checkDataReady:
acc_x = mpu.readAccel()["x"];
acc_y = mpu.readAccel()["y"];
acc_z = mpu.readAccel()["z"];
acc_total = math.sqrt(acc_x*acc_x+acc_y*acc_y+acc_z*acc_z)
if acc_total>max_acc_normal:
max_acc_normal = acc_total
time.sleep(0.1)
max_acc_normal = max_acc_normal*1.1
print('max_acc_normal:',max_acc_normal)
判断触碰
while True:
if mpu.checkDataReady:
#
acc_x = mpu.readAccel()["x"];
acc_y = mpu.readAccel()["y"];
acc_z = mpu.readAccel()["z"];
acc_total = math.sqrt(acc_x*acc_x+acc_y*acc_y+acc_z*acc_z)
if acc_total>max_acc_normal :
print(round(acc_total,4))
# Send ...
2)然后我开始考虑分析如何判断 传感器是否被翻转,
我想了一下,我先得判断当前传感器的姿态,然后根据姿态的变化来判断是否翻转;
而如何得到当前姿态呢? 我就想到了,当静止时,当前传感器一直应该有一个指向地球的垂直加速度。当然因为传感器没那么巧正好放得很正,所以不会一直那么巧 accz=9.8; 而恰好应该利用重力作用导致的三轴不同,得到当前传感器姿态。
这时,我才意识到前面的做法是错误的。于是我验证了一下,按照我现在的理解,若我触碰传感器,左右碰传感器,它都会告警,但是,若我把传感器往下时,它不会告警,经实测。果真如此。
3)经过上面的分析,我重新整理思路分析了一下:
下面贴一下今天的设计思路:(哦,我再看一遍,发现自己写的语言纯粹属于自言自语自己才能看懂的思路描述,今天没时间先不整理重写了。)
——————————————————
关于imu序列判断反转或碰触。
1) 用queue统计各值,这是必须的,先实现。
2) 在正常静止模式,不断保存小漂浮,也是需要的,注意时间间隔以及漂浮量,时间可先设置为5秒,漂浮量我们先统计看看一般5秒漂浮多少?
3) 若触碰,漂浮量必大,若反转漂浮量也大。所以,进入待定模式。 在下面两秒內判断? 是触碰还是翻转?? 也可能两者的程度都没到,也结束。
4) 待定模式结束后,五秒内进入校正模式,仅仅不断统计queue最后10个值。(等漂浮稳定5秒后结束校正模式,进入正常静止模式)
(校正模式) -> (静止模式) -> (待定模式) -> (判断出来是 翻转 还是 触碰 还是 None ) -> (校正模式) ->
===============================
今天OK的代码也这里贴一下,有空的话再补充说明。今天还要其他任务要开发,先就不改进了。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 27 10:05:05 2019
@author: jinyun
"""
import MPU9250 as mpu9250
import time
import math
import urllib.request
from urllib import parse
import ssl
import json
import numpy as np
strlogFileName = time.strftime("/home/pi/jinyuSrc/IMULog_%Y%m%d_%H%M%S.txt",time.localtime())
f_log = open(strlogFileName,"w+")
url = "https://10.16.6.101:4000/lifting/saveLiftingData"
headers={"User-Agent":"Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36" }
context = ssl._create_unverified_context()
mpu = mpu9250.MPU9250()
print('Device connected:', mpu.searchDevice())
f_log.write("Device connected:"+str(mpu.searchDevice())+"\n")
max_acc_normal = 0;
import queue
q_accx = queue.Queue(10)
q_accy = queue.Queue(10)
q_accz = queue.Queue(10)
accx_0 = 0
accy_0 = 0
accz_0 = 0
def JiaoZheng():
global accx_0,accy_0,accz_0
for i in range(20):
#
if mpu.checkDataReady:
acc_x = mpu.readAccel()["x"];
acc_y = mpu.readAccel()["y"];
acc_z = mpu.readAccel()["z"];
if q_accx.full():
q_accx.get_nowait()
q_accy.get_nowait()
q_accz.get_nowait()
q_accx.put(acc_x)
q_accy.put(acc_y)
q_accz.put(acc_z)
'''
acc_total = math.sqrt(acc_x*acc_x+acc_y*acc_y+acc_z*acc_z)
if acc_total>max_acc_normal:
max_acc_normal = acc_total
'''
time.sleep(0.1)
accx_0 = np.average( q_accx.queue )
accy_0 = np.average( q_accy.queue )
accz_0 = np.average( q_accz.queue )
def CalDeltxyz(newx,newy,newz):
return math.sqrt( (newx-accx_0)*(newx-accx_0) + (newy-accy_0)*(newy-accy_0) + (newz-accz_0)*(newz-accz_0) )
def NeiJi(newx,newy,newz):
return newx*accx_0+newy*accy_0+newz*accz_0
def SendEvent(strEventType,strMem):
# accTooBig, '\'maxAccelation\':'+str(round(acc_toobig,4))
try:
strTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
raw = '{"BODY": {"deviceNumber":"pi123","strTime":"'+strTime+'", "eventType":"'+strEventType+'" ' + \
' , "characteristicData2":"{}", "characteristicData3":"{}", "characteristicData1":"{' + \
strMem+'}" }}'
print(raw);
f_log.write(str(raw)+"\n")
f_log.flush()
data = bytes(raw,'ascii')
request = urllib.request.Request(url,headers=headers,data=data)
#strRet = " access url '"+url+"' failed ";
#
response = urllib.request.urlopen(request, context=context)
strRet = response.read().decode('utf-8')
print(strRet)
f_log.write(strRet+"\n");
f_log.flush()
except Exception as ex:
print(ex)
pass;
JiaoZheng()
'''
accx_0 = np.average( q_accx.queue )
accy_0 = np.average( q_accy.queue )
accz_0 = np.average( q_accz.queue )
'''
print(accx_0,accy_0, accz_0)
f_log.write('ccx_0,accy_0, accz_0:'+str(accx_0)+","+str(accy_0)+","+str(accz_0)+'\n')
f_log.flush()
nowMode = 0 # 0 free; 1-20 acc too big; 101-150 turn upside;
acc_toobig = 0;
max_acc_thistime = 0
idx = 0
while True:
if mpu.checkDataReady:
'''
print('readGyro', mpu.readGyro())
print('readAccel', mpu.readAccel())
print('readMagnet', mpu.readMagnet())
print('readTemperature', mpu.readTemperature())
'''
idx = (idx+1)%1000
acc_x = mpu.readAccel()["x"];
acc_y = mpu.readAccel()["y"];
acc_z = mpu.readAccel()["z"];
#print(acc_x)
acc_total = math.sqrt(acc_x*acc_x+acc_y*acc_y+acc_z*acc_z)
acc_delt = CalDeltxyz(acc_x,acc_y,acc_z)
#if acc_delt>0.0000001:
#print(acc_delt)
if acc_delt>0.1:
# mode should change
if nowMode==0:
print("==========================something happen ==================")
f_log.write("== something happen ==\n")
f_log.flush()
nowMode = 101
max_acc_thistime = acc_delt
elif nowMode>100:
if max_acc_thistime<acc_delt:
max_acc_thistime = acc_delt
pass
else:
q_accx.get_nowait()
q_accy.get_nowait()
q_accz.get_nowait()
q_accx.put(acc_x)
q_accy.put(acc_y)
q_accz.put(acc_z)
if acc_delt>0.02:
print("---")
elif acc_delt>0.01:
print(".",end="")
if nowMode>120:
#
print("================= Be Knocked ================= maxAcc:"+ str(round(max_acc_thistime,4) ) )
f_log.write("== Be Knocked == maxAcc:"+ str(round(max_acc_thistime,4) ) +"\n")
f_log.flush()
# accTooBig, '\'maxAccelation\':'+str(round(acc_toobig,4))
SendEvent("accTooBig",'\'maxAccelation\':'+str(round(max_acc_thistime,4)))
#
max_acc_thistime = 0
nowMode=0
#time.sleep(2)
JiaoZheng()
elif nowMode>100:
neiji = NeiJi(acc_x,acc_y,acc_z)
if neiji <-0.5:
# is Turn up down
print("== UP DOWN == neiji:"+str(round(neiji,4)) )
f_log.write("== UP DOWN == neiji:"+ str(round(neiji,4) ) +"\n")
f_log.flush()
# updown, '\'maxAccelation\':'+str(round(acc_toobig,4))
SendEvent("updown",'\'neiji\':'+str(round(neiji,4)))
#
nowMode=0
#time.sleep(2)
JiaoZheng()
else:
nowMode += 1
pass
elif(idx%20 == 0):
#
accx_0 = np.average( q_accx.queue )
accy_0 = np.average( q_accy.queue )
accz_0 = np.average( q_accz.queue )
'''
try:
gyro_x = mpu.readGyro()['x'];
gyro_y = mpu.readGyro()['y'];
gyro_z = mpu.readGyro()['z'];
mag_x = mpu.readMagnet()['x'];
mag_y = mpu.readMagnet()['y'];
mag_z = mpu.readMagnet()['z'];
temperature = mpu.readTemperature();
f_log.write(strRet+"\n");
f_log.flush()
except Exception as ex:
print(ex)
time.sleep(1)
pass
'''
time.sleep(0.1)
f_log.close()
今天的文章fasterrcnn代码详解_java获取传感器数据[通俗易懂]分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/87633.html