Demo entry 6662339

MAVLINK

   

Submitted by M-M on Nov 25, 2017 at 17:16
Language: Python. Code size: 9.1 kB.

# -*- coding: utf-8 -*-
import serial
import time
from Tkinter import *
from struct import pack, unpack

#ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=65)
ser = serial.Serial('COM11', 57600, timeout=65)




class mavAnalyse(Frame):
    msec = 50
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._running = False
        #27        
        #加速度
        self.accXstr = StringVar()
        self.accYstr = StringVar()
        self.accZstr = StringVar()
        #角速度
        self.angspdXstr = StringVar()
        self.angspdYstr = StringVar()
        self.angspdZstr = StringVar()
        #地磁强度
        self.mgnfdXstr = StringVar()
        self.mgnfdYstr = StringVar()
        self.mgnfdZstr = StringVar()
        
        #29
        #绝对气压 相对气压 温度
        self.abpressStr = StringVar()
        self.dfpressStr = StringVar()
        self.tmprtStr = StringVar()

        #30
        #滚转角 俯仰角 偏航角
        self.rollangStr = StringVar()
        self.pitchangStr = StringVar()
        self.yawangStr = StringVar()
        #滚转角速度 俯仰角速度 偏航角速度
        self.rollspdStr = StringVar()
        self.pitchspdStr = StringVar()
        self.yawspdStr = StringVar()
        
        #33
        #纬度 精度 海拔高度 地面相对高度
        self.latStr = StringVar()
        self.lonStr = StringVar()
        self.altStr = StringVar()
        self.rltvaltStr = StringVar()
        #地速 罗盘
        self.vxStr = StringVar()
        self.vyStr = StringVar()
        self.vzStr = StringVar()
        self.hdgStr = StringVar()
        
        #74
        #空速 地速 航向 油门 高度 爬升率
        self.airspdStr = StringVar()
        self.gndspdStr = StringVar()
        self.headingStr = StringVar()
        self.throttleStr = StringVar()
        #self.altStr = StringVar()
        self.climbStr = StringVar()
        
        self.makeWidgets()
        self.flag  = True
    def makeWidgets(self):
        fm0 = Frame(self)
        fm1 = Frame(self)
        fm2 = Frame(self)
        fm3 = Frame(self)
        fm4 = Frame(self)
        fm5 = Frame(self)
        fm6 = Frame(self)
        fm7 = Frame(self)
        fm8 = Frame(self)
        fm9 = Frame(self)
        #27
        Label(fm0, text='加速度X ').pack()
        Label(fm0, text='加速度Y ').pack()
        Label(fm0, text='加速度Z ').pack()
        Label(fm0, text='角速度X ').pack()
        Label(fm0, text='角速度Y ').pack()
        Label(fm0, text='角速度Z ').pack()
        Label(fm0, text='地磁强度X ').pack()
        Label(fm0, text='地磁强度Y ').pack()
        Label(fm0, text='地磁强度Z ').pack()
        Label(fm1, textvariable = self.accXstr).pack()
        Label(fm1, textvariable = self.accYstr).pack()
        Label(fm1, textvariable = self.accZstr).pack()
        Label(fm1, textvariable = self.angspdXstr).pack()
        Label(fm1, textvariable = self.angspdYstr).pack()
        Label(fm1, textvariable = self.angspdZstr).pack()
        Label(fm1, textvariable = self.mgnfdXstr).pack()
        Label(fm1, textvariable = self.mgnfdYstr).pack()
        Label(fm1, textvariable = self.mgnfdZstr).pack()
        #29
        Label(fm2, text='绝对气压 ').pack()
        Label(fm2, text='相对气压 ').pack()
        Label(fm2, text='温度 ').pack()
        Label(fm3, textvariable = self.abpressStr).pack()
        Label(fm3, textvariable = self.dfpressStr).pack()
        Label(fm3, textvariable = self.tmprtStr).pack()
        #30
        Label(fm4, text='滚转角 ').pack()
        Label(fm4, text='俯仰角 ').pack()
        Label(fm4, text='偏航角 ').pack()
        Label(fm4, text='滚转角速度 ').pack()
        Label(fm4, text='俯仰角速度 ').pack()
        Label(fm4, text='偏航角速度 ').pack()
        Label(fm5, textvariable = self.rollangStr).pack()
        Label(fm5, textvariable = self.pitchangStr).pack()
        Label(fm5, textvariable = self.yawangStr).pack()
        Label(fm5, textvariable = self.rollspdStr).pack()
        Label(fm5, textvariable = self.pitchspdStr).pack()
        Label(fm5, textvariable = self.yawspdStr).pack()
        #33
        Label(fm6, text='纬度 ').pack()
        Label(fm6, text='精度 ').pack()
        Label(fm6, text='海拔高度 ').pack()
        Label(fm6, text='地面相对高度 ').pack()
        Label(fm6, text='地速X ').pack()
        Label(fm6, text='地速Y ').pack()
        Label(fm6, text='地速Z ').pack()
        Label(fm6, text='罗盘 ').pack()
        Label(fm7, textvariable = self.latStr).pack()
        Label(fm7, textvariable = self.lonStr).pack()
        Label(fm7, textvariable = self.altStr).pack()
        Label(fm7, textvariable = self.rltvaltStr).pack()
        Label(fm7, textvariable = self.vxStr).pack()
        Label(fm7, textvariable = self.vyStr).pack()
        Label(fm7, textvariable = self.vzStr).pack()
        Label(fm7, textvariable = self.hdgStr).pack()
        #74
        Label(fm8, text='空速 ').pack()
        Label(fm8, text='地速 ').pack()
        Label(fm8, text='航向 ').pack()
        Label(fm8, text='油门 ').pack()
        #Label(fm8, text='高度 ').pack()
        Label(fm8, text='爬升率 ').pack()
        Label(fm9, textvariable = self.airspdStr).pack()
        Label(fm9, textvariable = self.gndspdStr).pack()
        Label(fm9, textvariable = self.headingStr).pack()
        Label(fm9, textvariable = self.throttleStr).pack()
        #Label(fm9, textvariable = self.altStr).pack()
        Label(fm9, textvariable = self.climbStr).pack()
        
        fm0.pack(side=LEFT)
        fm1.pack(side=LEFT)
        fm2.pack(side=LEFT)
        fm3.pack(side=LEFT)
        fm4.pack(side=LEFT)
        fm5.pack(side=LEFT)
        fm6.pack(side=LEFT)
        fm7.pack(side=LEFT)
        fm8.pack(side=LEFT)
        fm9.pack(side=LEFT)
    def _update(self):
        self._recvmav()
        self.timer = self.after(self.msec, self._update)
    def _recvmav(self):
            while ser.read(1) != '\xFE':
                pass
            plen = ord(ser.read(1))
            print 'Len: ',plen,
            print '\tSec: ',ord(ser.read(1)),
            print '\tSysID: ',ord(ser.read(1)),
            print '\tCompID: ',ord(ser.read(1)),
            msgID = ord(ser.read(1))
            print '\tMsgID: ',msgID,
            data = ser.read(plen)
            print map(ord,data),
            print map(ord,ser.read(2))

            if msgID == 27:
                
                self.accXstr.set(unpack('h',data[8:10]))                
                self.accYstr.set(unpack('h',data[10:12]))                
                self.accZstr.set(unpack('h',data[12:14]))                
                self.angspdXstr.set(unpack('h',data[14:16]))                
                self.angspdYstr.set(unpack('h',data[16:18]))                
                self.angspdZstr.set(unpack('h',data[18:20]))                
                self.mgnfdXstr.set(unpack('h',data[20:22]))                
                self.mgnfdYstr.set(unpack('h',data[22:24]))                
                self.mgnfdZstr.set(unpack('h',data[24:26]))
                
            elif msgID == 29:
                self.abpressStr.set(unpack('f',data[4:8]))
                self.dfpressStr.set(unpack('f',data[8:12]))
                self.tmprtStr.set(unpack('H',data[12:14]))
                
            elif msgID == 30:
                self.rollangStr.set(unpack('f',data[4:8]))
                self.pitchangStr.set(unpack('f',data[8:12]))
                self.yawangStr.set(unpack('f',data[12:16]))
                self.rollspdStr.set(unpack('f',data[16:20]))
                self.pitchspdStr.set(unpack('f',data[20:24]))
                self.yawspdStr.set(unpack('f',data[24:28]))
            elif msgID == 33:
                self.latStr.set(unpack('i',data[4:8]))
                self.lonStr.set(unpack('i',data[8:12]))
                self.altStr.set(unpack('i',data[12:16]))
                self.rltvaltStr.set(unpack('i',data[16:20]))
                self.vxStr.set(unpack('h',data[20:22]))
                self.vyStr.set(unpack('h',data[22:24]))
                self.vzStr.set(unpack('h',data[24:26]))
                self.hdgStr.set(unpack('H',data[26:28]))
            elif msgID == 74:
                self.airspdStr.set(unpack('f',data[0:4]))
                self.gndspdStr.set(unpack('f',data[4:8]))
                self.headingStr.set(unpack('h',data[8:10]))
                self.throttleStr.set(unpack('H',data[10:12]))
                #self.altStr.set(unpack('h',data[20:22]))
                self.climbStr.set(unpack('f',data[16:20]))
                
    def start(self):
        self._update()
        self.pack(side = TOP)
        
if __name__ == '__main__':
    def main():
        root = Tk()
        root.geometry('1000x300')
        root.title("MavLink analyse")
        frame1 = Frame(root)
        frame1.pack(side = BOTTOM)
        mw = mavAnalyse(root)
        mywatch = Button(frame1, text = '开始', command = mw.start)
        mywatch.pack(side = LEFT)
        root.mainloop()
    main()


    

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).