[Chennaipy] Python Animation in realtime

Akhil Mohan akhilmohanmec at gmail.com
Sat Jan 10 18:42:08 CET 2015


Hello Vijay,

I am able to print the data till end, but only not able to execute the Func
Animation in the proper manner. I have updated my modified code and am able
to print the x, y and z in my program. What I wanted is to have temporal
change of x y and z as data streams in.

Thanks and Regards,

---------------------------------------------------
# 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')
line, = ax.plot([], [], [], lw=1)

# Setting the axes properties
ax.set_xlim3d([-180.0, 180.0])
ax.set_xlabel('X')
ax.set_ylim3d([-180.0, 180.0])
ax.set_ylabel('Y')
ax.set_zlim3d([-180.0, 180.0])
ax.set_zlabel('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,

# Decode 2s complement
def complement2s(n, nb):
    if (n >= 2**(nb-1)):
        return -2**nb + n
    else:
        return n

# Conversion of hex to float
def convert(s):
    i = int(s, 16)                   # convert from hex to a Python int
    cp = pointer(c_int(i))           # make this into a c integer
    fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float
pointer
    return fp.contents.value

def main():
global msg
while(s_handle != 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
# print len(f)
if (len(f) == 12):
dn = 4
data = np.array([complement2s(convert(f), 16) for i in range(0, len(f), dn)
])
if (len(full_msg[-1]) < 12):
msg = full_msg[-1]
# print data
return data

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

if __name__ == '__main__':
while(s_handle != 0):
# main()
animate()
# call the animator.  blit=True means only re-draw the parts that have
changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=10000, interval=20, blit=True)
plt.show

-------------------------------------------------------------------------------------

On Sat, Jan 10, 2015 at 9:33 PM, Vijay Kumar <vijaykumar at bravegnu.org>
wrote:

> On Friday 09 January 2015 03:47 PM, Akhil Mohan wrote:
> [clip]
>
>> 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 :)
>>
>
> Hi Akhil,
> When you post code, please ensure that the indentation is
> preserved. As for your question, are you sure the serial port code is
> not blocking your animation?
>
> Regards,
> Vijay
>
> _______________________________________________
> Chennaipy mailing list
> Chennaipy at python.org
> https://mail.python.org/mailman/listinfo/chennaipy
>



-- 
-------------------------------------------------
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/20150110/79bfe540/attachment-0001.html>


More information about the Chennaipy mailing list