使用陀螺仪和加速度计检测冲击时遇到的问题。

huangapple go评论59阅读模式
英文:

issue with using gyroscope and accelerometer to detect impact

问题

我目前正在进行一个小项目,需要使用树莓派、陀螺仪和加速度计来检测冲击。我的想法是通过一个“while true”循环不断获取陀螺仪和加速度计的值,然后保存其中一个陀螺仪轴的值,等待0.2秒后再将陀螺仪轴的值保存到另一个地方。然后我会将这两个值相减,如果结果大于某个特定值,就表示超过了我的阈值,触发“break”来停止“while true”循环并打印一些信息。我面临的问题是,前后两个值始终相同。我使用了“sleep()”函数。请建议,我是编程新手,感谢帮助。

从sense_hat导入SenseHat
    
sense = SenseHat()

while True:
    acceleration = sense.get_accelerometer_raw()
    a = acceleration['x']
    b = acceleration['y']
    c = acceleration['z']

    a = round(a, 0)
    b = round(b, 0)
    c = round(c, 0)
    sense.set_imu_config(False, True, False)
    gyro_only = sense.get_gyroscope()
    print("a={0},b={1},c={2}".format(a, b, c))
    print("p:{pitch},r:{roll},y:{yaw}".format(**gyro_only))

    checkoneP = a
    time.sleep(0.1)
    checktwoP = a
    if checkoneP - checktwoP != 0:
        break
print("IMPACT")
英文:

i am currently working on a small project where i need to use a raspberry pi, gyroscope and an accelerometer to detect an impact. my idea was to constantly take in values of the gyro and acc using a "while true" loop, subsequently i would save the value of one of the gyro axis value for example, have the program wait 0.2 seconds then save the gyro axis value again into a separate place. i would then subtract the two values and if the result was larger than a certain value i know that it passed my threshold, this would trigger a "break" to stop the "while true" loop and print out some information. the problem i face is that the before and after values of my values are always the same. i use the "sleep()" function. please advise, i am new to coding and would appreciate the help

from sense_hat import SenseHat

sense=SenseHat()

while True:
    acceleration=sense.get_accelerometer_raw()
    a=acceleration[‘x’]
    b=acceleration[‘y’]
    c=acceleration[‘z’]

    a=round(a,0)
    b=round(b,0)
    c=round(c,0)
    sense.set_imu_config(False,True,False)
    gyro_only=sense.get_gyroscope()
    print(“a={0},b={1},c={2}”.format(a,b,c))
    print(“p:{pitch},r:{roll},y:{yaw}”.format(**gyro_only))


    checkoneP=a
    time.sleep(0.1)
    checktwoP=a
    if checkoneP-checktwoP!=0:
        break
print(“IMPACT”)

i tried to test whether it was my other if statement or perhaps "a" was not outputting a constantly updating value but i narrowed the issue down to the fact that my "checkoneP" and "checktwoP" is always the same after the "time.sleep(0.1))" despite shaking and rotating the gyro and acc rapidly, the display also confirmed that i the "a" value was changing fast enough.

答案1

得分: 0

你只在顶部读取传感器一次,所以数值始终相同,实际上你在检查 a 是否等于它自身。a 是一个变量,在被赋值之后不会改变,它不是一个读取传感器的函数。

你需要在休眠后重复以下步骤:

a = acceleration['x']
b = acceleration['y']
c = acceleration['z']

a = round(a, 0)
b = round(b, 0)
c = round(c, 0)

以再次读取传感器。同时,缩短休眠时间,以防冲击时间太短。

英文:

You are only reading the sensor once at the top, so the values are always necessarily the same, you are checking if a is equal to itself basically. a is a variable, after it has been assigned it does not change, it is not a function that reads the sensor.

You need to repeat

a=acceleration[‘x’]
b=acceleration[‘y’]
c=acceleration[‘z’]

a=round(a,0)
b=round(b,0)
c=round(c,0)

after the sleep to read the sensor again.

Also reduce the sleeping time to something smaller in case the impact is too short.

huangapple
  • 本文由 发表于 2023年5月14日 01:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定