兴趣之余,利用晚上的时间,做一些个人兴趣方面的开发. 之前没接触过 arduino, 无意之中买了个开发板做一些小开发, 这里利用python 读取 mpu9250 数据实时绘图.
下位机代码 C++
void Serial_SendDataPython( int16_t *sendData, uint8_t lens )
{
uint8_t tmpData[] = {}; // tmpData lens >= 2 * lens + 4
uint8_t *ptrData = tmpData;
uint8_t dataBytes = lens << ;
uint8_t dataLens = dataBytes + ;
uint8_t count = ;
uint16_t tmpSum = ; tmpData[] = 'S';
while(count < dataBytes) {
// tmpData[count+1] = Byte8H(sendData[count >> 1]);
// tmpData[count+2] = Byte8L(sendData[count >> 1]); tmpData[count+] = (sendData[count >> ])>>;
tmpData[count+] = (sendData[count >> ])&0x00ff; ;
count = count + ;
}
for(uint8_t i = ; i < dataBytes; i++)
tmpSum += tmpData[i+];
tmpData[dataLens - ] = (uint8_t)(tmpSum & 0x00FF);
tmpData[dataLens - ] = '\r';
tmpData[dataLens - ] = '\n'; do {
//Serial_SendByte(*ptrData++);
Serial.write(*ptrData++);
} while(--dataLens);
} .................................... IMU_Buf[] = testLostRate++;
MU_Buf[] = ax/;
IMU_Buf[] = ay/;
IMU_Buf[] = az/;
IMU_Buf[] = gx/;
IMU_Buf[] = gy/;
IMU_Buf[] = gz/;
IMU_Buf[] = mx/;
IMU_Buf[] = my/;
IMU_Buf[] = mz/;
Serial_SendDataMATLAB(IMU_Buf, ); .................................................
这里简要说明一下, 发送数据以'S'开头,传感器数据分低8位和高8位数据分别发送,最后以换行符结尾.
"""
ldr.py
http://electronut.in/plotting-real-time-data-from-arduino-using-python/
Display analog data from Arduino using Python (matplotlib) Author: Mahesh Venkitachalam
Website: electronut.in
"""
import ctypes
import sys, serial, argparse
import numpy as np
from time import sleep
from collections import deque import matplotlib.pyplot as plt
import matplotlib.animation as animation # plot class
class AnalogPlot:
# constr
def __init__(self, strPort, maxLen):
# open serial port
self.ser = serial.Serial(strPort, ) self.ax = deque([0.0]*maxLen)
self.ay = deque([0.0]*maxLen)
self.az = deque([0.0]*maxLen)
self.gx = deque([0.0]*maxLen)
self.gy = deque([0.0]*maxLen)
self.gz = deque([0.0]*maxLen)
self.mx = deque([0.0]*maxLen)
self.my = deque([0.0]*maxLen)
self.mz = deque([0.0]*maxLen)
self.maxLen = maxLen # add to buffer
def addToBuf(self, buf, val):
if len(buf) < self.maxLen:
buf.append(val)
else:
buf.pop()
buf.appendleft(val) # add data
def add(self, data):
assert(len(data) == )
self.addToBuf(self.ax, data[])
self.addToBuf(self.ay, data[])
self.addToBuf(self.az, data[])
self.addToBuf(self.gx, data[])
self.addToBuf(self.gy, data[])
self.addToBuf(self.gz, data[])
self.addToBuf(self.mx, data[])
self.addToBuf(self.my, data[])
self.addToBuf(self.mz, data[])
# update plot
def update(self,frameNum,a0,a1,a2,a3,a4,a5,a6,a7,a8):
try:
data = self.ser.readline()
length = len(data)
if length == and ord(data[])== :
count = (ord(data[])<<)+ord(data[])
value = (ord(data[])<<)+ord(data[])
ax = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
ay = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
az = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
gx = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
gy = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
gz = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
mx = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
my = ctypes.c_int16(value).value
value = (ord(data[])<<)+ord(data[])
mz = ctypes.c_int16(value).value array = [ax,ay,az,gx,gy,gz,mx,my,mz]
print array
self.add(array)
a0.set_data(range(self.maxLen), self.ax)
a1.set_data(range(self.maxLen), self.ay)
a2.set_data(range(self.maxLen), self.az)
a3.set_data(range(self.maxLen), self.gx)
a4.set_data(range(self.maxLen), self.gy)
a5.set_data(range(self.maxLen), self.gz)
a6.set_data(range(self.maxLen), self.mx)
a7.set_data(range(self.maxLen), self.my)
a8.set_data(range(self.maxLen), self.mz)
except KeyboardInterrupt:
print('exiting') return a0, # clean up
def close(self):
# close serial
self.ser.flush()
self.ser.close() # main() function
def main():
# create parser
#parser = argparse.ArgumentParser(description="LDR serial")
# add expected arguments
#parser.add_argument('--port', dest='port', required=True) # parse args
#args = parser.parse_args() strPort = 'COM3'
#strPort = args.port print('reading from serial port %s...' % strPort) # plot parameters
analogPlot = AnalogPlot(strPort, ) print('plotting data...') # set up animation
fig = plt.figure()
ax = plt.axes(xlim=(, ), ylim=(-, ))
a0, = ax.plot([], [])
a1, = ax.plot([], [])
a2, = ax.plot([], [])
a3, = ax.plot([], [])
a4, = ax.plot([], [])
a5, = ax.plot([], [])
a6, = ax.plot([], [])
a7, = ax.plot([], [])
a8, = ax.plot([], [])
anim = animation.FuncAnimation(fig, analogPlot.update,
fargs=(a0,a1,a2,a3,a4,a5,a6,a7,a8),
interval=) # show plot
plt.show() # clean up
analogPlot.close() print('exiting.') # call main
if __name__ == '__main__':
main()
运行结果如下图:
从互联网搜索了一下,可以串口绘图的工具很多,试了一下 SerialChart工具,感觉还不错,界面如下:
下位机数据格式较简单:
interval(两次数据获取时间间隔,可设置为0),ax,ay,az 为int 类型,
Serial.print(interval); //microseconds since last sample, please note that printing more data will increase interval
Serial.print(",");
Serial.print(ax); //Inclination X axis (as measured by accelerometer)
Serial.print(",");
Serial.print(ay); //Inclination X axis (estimated / filtered)
Serial.print(",");
Serial.print(az); //Inclination X axis (estimated / filtered)
Serial.println("");
具体使用方法请参见官方网站: https://en.wikiversity.org/wiki/SerialChart_Tutorial ,这里有详细说明和配置方法.
总结: 利用 python 读取seria 数据似乎效率不怎么高, 和之前matlab 测试遇到的情况情况类似,容易出现卡顿的情况. 下篇博客我将会介绍数据可视化工具 Processing在这方便的用途和代码.
参考,引用:
http://electronut.in/plotting-real-time-data-from-arduino-using-python/
https://en.wikiversity.org/wiki/SerialChart_Tutorial