import socket import time import binascii import time import struct msgsbuffer=bytearray (0) WP_Count=0 WP_Upload_Ready=True MAVLINK_MSG_ID_HEARTBEAT = 0 MAVLINK_MSG_ID_GPS_RAW_INT = 24 MAVLINK_MSG_ID_MISSION_REQUEST = 40 MAVLINK_MSG_ID_MISSION_COUNT = 44 MAVLINK_MSG_ID_REQUEST_DATA_STREAM = 66 MAVLINK_MSG_ID_MISSION_ACK=47 MY_SYSID=12 #Airplane Number MY_SUBSYSID=16 #Subsystem Number, does not have any meaning MSG_MAGIC=0 MSG_LEN=1 MSG_MSGID=7 magic=253 length=0 packet_count=0 def crc_sum (data, accum): tmp=data ^ (accum & 0xff) tmp=(tmp ^ (tmp<<4)) & 0xff accum = (accum >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >>4) accum=accum & 0xffff return accum def generate_checksum (msg,extra): ac=0xffff for i in range (1,len(msg)-2,1): # we exclude the first and the last two entries ac=crc_sum(msg[i],ac) ac=crc_sum(extra,ac) return ac # ================ SENDING MESSAGES ROUTINES ============================ def special_routine (a,crc_extra): #Generate checksum and inserts it, and also increases packet_count global packet_count packet_count=packet_count+1 if packet_count>255: packet_count=0 chksum=generate_checksum (a,crc_extra) a[a[MSG_LEN]+10]=chksum-int (chksum/256)*256 a[a[MSG_LEN]+10+1]=int (chksum/256) return a def make_header (a,msgid): global packet_count,MY_SYSID,MY_SUBSYSID hdr=bytearray (struct.pack ('>>>>>>>> HEART BEAT SENT:",binascii.hexlify(a)) return a def GPS_raw_send (time_usec,lat,lon,alt,eph,epv,vel,cog,fix_type,satellites_visible,alt_ellipsoid,h_acc,v_acc,vel_acc,hdg_acc): global packet_count,MY_SYSID,MY_SUBSYSID ordered_fieldnames = ['time_usec', 'lat', 'lon', 'alt', 'eph', 'epv', 'vel', 'cog', 'fix_type', 'satellites_visible'] packstring='>>>>>>>> GPS RAW SENT:",binascii.hexlify(a)) return a def Mission_request_send (seq,target_system,target_component,mission_type): global packet_count,MY_SYSID,MY_SUBSYSID ordered_fieldnames = ['seq', 'target_system', 'target_component','mission_type'] packstring='>>>>>>>> MISSION REQUEST SENT:", seq, MY_SYSID,MY_SUBSYSID,mission_type,binascii.hexlify(a)) return a def Mission_acknowledge_send (target_system,target_component,ack_type): global packet_count,MY_SYSID,MY_SUBSYSID name = 'MISSION_ACK' ordered_fieldnames = ['target_system', 'target_component', 'type'] packstring= '>>>>>>>> MISSION ACK SENT:", ack_type, MY_SYSID,MY_SUBSYSID,binascii.hexlify(a)) return a # ============= RECEIVING MESSAGES ROUTINES ==================================== def receive_HEART_BEAT (toparse): ordered_fieldnames = ['custom_mode', 'type', 'autopilot', 'base_mode', 'system_status', 'mavlink_version'] packstring='12: i=0 while i0 and WP_Upload_Ready: time.sleep (0.2) WP_Count=WP_Count-1 data=Mission_request_send (1,MY_SYSID,MY_SUBSYSID,0) c.send(data) WP_Upload_Ready=False # We set it to false because we need to receive one WP, where it will be reset to true print ("STARTING THE TEST PROGRAM FOR MAVLINK COMMUNICATION") port = 1032 send(port)