[Chennaipy] Python Animation in realtime

Guru Venkat guruvenkat7 at gmail.com
Sat Jan 10 05:20:23 CET 2015


Hi,

I have not used the animation subroutine of matplotlib for animating 3D
plots.

However, I have used Visual Python for such animations. Take a look at the
attached code. Maybe you are looking for something on these lines?

On Fri, Jan 9, 2015 at 3:47 PM, Akhil Mohan <akhilmohanmec at gmail.com> wrote:

> 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
>
> _______________________________________________
> Chennaipy mailing list
> Chennaipy at python.org
> https://mail.python.org/mailman/listinfo/chennaipy
>
>


-- 
Thanks & Regards

Guru Venkat
PhD Scholar,
Department of Electrical Engineering,
Indian Institute of Technology, Madras.
http://www.ee.iitm.ac.in/optics/guru
Mobile: +91-9884314268
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chennaipy/attachments/20150110/a080e013/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SW_anim_V_Python.py
Type: text/x-python
Size: 1092 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/chennaipy/attachments/20150110/a080e013/attachment-0001.py>


More information about the Chennaipy mailing list