我有一个 mpu6050,我想用它来测量和显示设备平面的角度。我希望能够按下一个按钮来保存它当前所处的角度,然后在设备移动并且角度不匹配时使 LED 显示屏闪烁/闪烁。
这是我现在所拥有的:
MPU_Init()
import csv
mylcd = I2C_LCD_driver.lcd()
#displays values onto led display
print (" Reading Data of Gyroscope and Accelerometer")
mylcd.lcd_display_string("X", 1,4)
mylcd.lcd_display_string("Y", 1,8)
mylcd.lcd_display_string("Z", 1,12)
从 gpiozero 导入按钮
button = Button(23)
if button.wait_for_press():
with open("test.csv" , "w", newline="") as f:
for z in range (1):
f.write("%d" %x_rotation)
with open("test.csv" , "r") as g:
data = int(g.read())
而真:
#Read Accelerometer raw value
acc_x = read_raw_data(ACCEL_XOUT_H)
acc_y = read_raw_data(ACCEL_YOUT_H)
acc_z = read_raw_data(ACCEL_ZOUT_H)
#Read Gyroscope raw value
gyro_x = read_raw_data(GYRO_XOUT_H)
gyro_y = read_raw_data(GYRO_YOUT_H)
gyro_z = read_raw_data(GYRO_ZOUT_H)
#Full scale range +/- 250 degree/C as per sensitivity scale factor
Ax = acc_x/16384.0
Ay = acc_y/16384.0
Az = acc_z/16384.0
Gx = gyro_x/131.0
Gy = gyro_y/131.0
Gz = gyro_z/131.0
x_rotation = get_x_rotation(Ax, Ay, Az)
y_rotation = get_y_rotation(Ax, Ay, Az)
z_rotation = get_z_rotation(Ax, Ay, Az)
xt = x_rotation
fontdata1 = [
[ 0b01000,
0b10100,
0b01000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000 ],
]
mylcd.lcd_load_custom_chars(fontdata1)
mylcd.lcd_write(0xC5)
mylcd.lcd_write_char(0)
mylcd.lcd_write(0xC9)
mylcd.lcd_write_char(0)
mylcd.lcd_write(0xC0 + 13)
mylcd.lcd_write_char(0)
mylcd.lcd_display_string("%d" %x_rotation, 2,3)
mylcd.lcd_display_string("%d" %y_rotation, 2,7)
mylcd.lcd_display_string("%d" %z_rotation, 2,11)
print ("x axis=%d" %x_rotation, u'\u00b0', "y axis=%d" %y_rotation, u'\u00b0', "z axis=%d" %z_rotation, u'\u00b0')
xg = int(x_rotation)
sleep (0.5)
#if x_rotation > data+5:
# while True:
# mylcd.blink()
#elif x_rotation < data-5:
# while True:
# mylcd.blink()
sleep(1)