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.
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.