过去2天,我一直在尝试运行以下Python脚本,该脚本需要matplotlib来绘制来自传感器的实时数据,但没有成功。
#!/usr/bin/python
import smbus
import math
import matplotlib as mpl
mpl.use('tkagg')
import matplotlib.pyplot as plt
# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c
incl = []
plt.ion
def makefig():
plt.plot(incl,'ro-')
def read_byte(adr):
return bus.read_byte_data(address, adr)
def read_word(adr):
high = bus.read_byte_data(address, adr)
low = bus.read_byte_data(address, adr+1)
val = (high << 8) + low
return val
def read_word_2c(adr):
val = read_word(adr)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val
def dist(a,b):
return math.sqrt((a*a)+(b*b))
def get_y_rotation(x,y,z):
radians = math.atan2(x, dist(y,z))
return -math.degrees(radians)
def get_x_rotation(x,y,z):
radians = math.atan2(y, dist(x,z))
return math.degrees(radians)
bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
address = 0x68 # This is the address value read via the i2cdetect command
while True:
# Now wake the 6050 up as it starts in sleep mode
bus.write_byte_data(address, power_mgmt_1, 0)
gyro_xout = read_word_2c(0x43)
gyro_yout = read_word_2c(0x45)
gyro_zout = read_word_2c(0x47)
#print ("gyro_xout: ", gyro_xout, " scaled: ", (gyro_xout / 131))
#print ("gyro_yout: ", gyro_yout, " scaled: ", (gyro_yout / 131))
#print ("gyro_zout: ", gyro_zout, " scaled: ", (gyro_zout / 131))
accel_xout = read_word_2c(0x3b)
accel_yout = read_word_2c(0x3d)
accel_zout = read_word_2c(0x3f)
accel_xout_scaled = accel_xout / 16384.0
accel_yout_scaled = accel_yout / 16384.0
accel_zout_scaled = accel_zout / 16384.0
#print ("accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled)
#print ("accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled)
#print ("accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled)
#my_incl = (get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
#incl.appned(my_incl)
#drawnow(makefig)
#plt.pause(0.00001)
# print ("x rotation: " , '{0:.0f}'.format(my_incl))
#print ("y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
运行以上命令时,我从numpy中收到以下错误:
RuntimeError:针对API版本0xc编译的模块,但此版本的numpy为0xa 追溯(最近一次通话): 在第7行的文件“ /home/pi/Desktop/car/car.py” 导入matplotlib.pyplot作为plt 在第32行的“ /usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py”文件中 导入matplotlib.colorbar 在第28行的“ /usr/local/lib/python3.5/dist-packages/matplotlib/colorbar.py”文件中 将matplotlib.artist作为martist导入 在第11行的“ /usr/local/lib/python3.5/dist-packages/matplotlib/artist.py”文件中 从.path导入路径 在第17行的“ /usr/local/lib/python3.5/dist-packages/matplotlib/path.py”文件中 来自。导入_path,rcParams ImportError:numpy.core.multiarray导入失败
在我看来,我的numpy版本错误。 但是仍然在更新numpy,卸载,重新安装,在此处搜索帮助等之后。(不是所有的尝试都一样,每个都作为单独的尝试)我仍然不知道如何解决此问题...
我们将不胜感激。