[Chennaipy] Python Animation in realtime

Akhil Mohan akhilmohanmec at gmail.com
Fri Jan 9 11:17:03 CET 2015


Hello,

Let me explain a bit of what I am trying to do. I am using an Inertial
Measurement Unit (IMU) for obtaining orientation and this data is filtered
and sent via Bluetooth to PC. On the PC I am receiving the data using
pyserial. So the data being sent out in quaternion form (like Euler angle)
is used for creating an animation. The figure or object has to periodically
update the data and do the rotation according to the algorithm. These all
things works well.
Now the problem is the object I created is not updating or not moving in
real time and the implementation Funcfunction seems to be the culprit. Can
anyone suggest what is the mistake that I am doing ? Please see the sample
code :)

Cheers!!
Akhil Mohan
------------------------------


# Global variables
msg = ''
full_msg = None

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.gca(projection='3d')
# ax = plt.axes(xlim=(0, 2), ylim=(-2, 2), zlim=(0, 10))
line, = ax.plot([], [], [], lw=1)

# Setting the axes properties
ax.set_xlim3d([-2.0, 2.0])
ax.set_ylabel('X')
ax.set_ylim3d([-2.0, 2.0])
ax.set_ylabel('Y')
ax.set_zlim3d([-2.0, 2.0])
ax.set_ylabel('Z')

# Get serial handle
s_handle = serial.Serial("COM15", 115200, timeout=0)
# initialization function: plot the background of each frame

def init():
line.set_data([], [])
line.set_3d_properties([])
return line,

# object vertices
obj = np.array([[-0.5, 0.5, 0.5, -0.5, -0.5, 0, -0.5, 0.5, 0, 0.5, -0.5],
[-0.5, -0.5, 0.5, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, -0.5],
[ 0, 0, 0, 0, 0, 1.2, 0, 0, 1.2, 0, 0]]).T

def main():
while(s_handle):
global msg
# i=0
if s_handle.inWaiting() != 0:
msg += str(s_handle.read(s_handle.inWaiting()))­
# Look for full message.
full_msg = msg.split('Z')
for f in full_msg:
# print f,len(f)
if (len(f) == 35):
q = np.array([float(m) for m in f.split(',')])
# print q
quat_data = np.array([quaternion_rotate(q, pure_quaternion(p)) for p in
obj])[:, 1:] # quaternion function is working fine :)
return quat_data

def animate(i):
rot_obj = main()
x = list(rot_obj[:, 0])
y = list(rot_obj[:, 1])
z = list(rot_obj[:, 2])
line.set_data(x, y)
line.set_3d_properties(z)
return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=10000,
interval=20, blit=True)
plt.show()

-- 
-------------------------------------------------
Akhil Mohan (+91-7639168728)
Ph.D Scholar
IITM-CMC-SCTIMST
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chennaipy/attachments/20150109/af406da8/attachment.html>


More information about the Chennaipy mailing list