Telemetry from APM2 to mission planner via UDP - mission planner says link is connected but link lost immediately

Hi,

I’d like to ask your help because I’m so stucked in with APM I cannot figure out what the mission planner needs to receive from it to being properly connected.

What I do is have a raspberry connected to APM2 via telemetry where a py script reads out the serial and sends it to the mission planner to UDP/14550. Also the packages sent back by mission planner to the port I send the packets from I wrote back to the serial:

def tty2UDP():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while(1):
line = ser.readline()
if (line != ‘’):
sock.sendto(line, ( GSC_address, 14550 ))
print ’ message sent to GSC '
else:
print ’ no output to GSC ’

        data, addr = sock.recvfrom(1024)
        if (data !=''):
            print 'received message: ', data
            ser.write(data)
        else:
            print 'no input'
        ser.close()
        exit -1

#------------------------------------------------------------------------------------------------------------

ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False)
ser.flushInput()
ser.flushOutput()

tty2UDP()

Mission planner sometimes says it is connected but a seconds later it says link is down. I saw the messages from APM to delivered to Mission planner with wireshark - parameters like position, informations about servos, etc can be read out.

I’d appreciate any constructive advise, please! :slight_smile: Frankly I would not like to start using python mavlink bindings to do everything from scratch.

Thank you in advance

Cheers

Peter

In addition in wireshark I see that some of the packets contains human readable labels and some do not. Should have all the packets the same structure from APM to mission planner? Regarding possible mistakes on my side I think it is possible I improperly read the input and that’s why mission planner cannot read it.

I tried harder and understood that MAVLink packets starts with 0xfe so I started syncronizing my buffer with the received messages both from APM and mission planner. At my serial-UDP converting script I had messages like these ones:

sent message: fe1af801019b1141de36000000000600830017fcfdff000003000000000080006acb
sent message: fe1cf981011e0f954f0016aba3bb3309a8bb94a29c3ec868ddb9a4cb9eb9667158bbdbe2
sent message: fe94fa81014a0080000000008080666642410080000011005d803aeb
sent message: fe9cfb8101213595cf00000000008080000098bdfffff62f0000000080000000d906f5d1age: fe15ec010124c4c8db368007d885ac03d8050f06d885d805840300d219
received message: fe09b9fc0100000000000608c004038468
sent message: fe16ed0101a3ec944f008007d885ac03d8050f86d805d805840380809135
sent message: fe1fee010101a3dca0002380200003dc200070018000ffff000000808000000080000000ff40e4
sent message: fe04ef0101983f1a7585eca7
sent message: fe02f001812a0000cd66
sent message: fe9ef101011800008000000000000000008000000000d8bdffff0f27ffff000000800100d063
sent message: fe09f2810100000000000a03c10483e759
sent message: fe1cf301019ea9944f001e71a3fbcae3a73b997b9cbec8e8ddb9c05df0b890e1693b625c
sent message: fe14f401014a80000000008000005c8f42c18000000011805d001345
sent message: fe1cf50101a352e0cb3ba41692ba2c50d1b60000000080000000e8c5bdb9000080bfd3c1
sent message: fe03f60101a51b920136fc
sent message: fe0cf70181820000008000000000d1944f0006da
sent message: fe1af801019b1141de36000000000600830017fcfdff000003000000000080006acb
sent message: fe1cf981011e0f954f0016aba3bb3309a8bb94a29c3ec868ddb9a4cb9eb9667158bbdbe2
sent message: fe94fa81014a0080000000008080666642410080000011005d803aeb
sent message: fe9cfb8101213595cf00000000008080000098bdfffff62f0000000080000000d906f5d1

I can interpret these messages now with information from https://en.wikipedia.org/wiki/MAVLink#Packet_Structure and http://mavlink.org/messages/common so I think now I do it right - but what happens is mission planner indicates connection for a very short period of time.

What else shall I take into account, please? What are the requirements for mission planner to consider APM link proper?

Thank you in advance for any hints

The script I do it with now is:
#!/usr/bin/env python

import socket
import serial
import sys
import fcntl
import struct
import math
import os
import errno
import time
from threading import Thread
GSC_address = ‘192.168.6.129’

def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack(‘256s’, ifname[:15])
)[20:24])

def tty2UDP():
SerialTXbuffer = bytearray()
while(1):
for x in ser.read():
if ( x == ‘\xfe’ ):
# send the buffer, clear it and add this Byte to it
sock.sendto(SerialTXbuffer, (GSC_address, 14550))
#print ‘sent message: ‘, ‘’.join(’{:02x}’.format(z) for z in SerialTXbuffer)
SerialTXbuffer = bytearray()
SerialTXbuffer.append(x)
else:
# just add the Byte to the buffer
SerialTXbuffer.append(x)
ser.close()
exit -1

def UDP2tty():
SerialRXbuffer = bytearray()
while(1):
t, addr = sock.recvfrom(512)
if (t !=’’):
for tx in t:
if ( tx == ‘\xfe’ ):
# send the buffer, clear it and add this Byte to it
ser.write(SerialRXbuffer)
#print ‘received message: ‘, ‘’.join(’{:02x}’.format(z) for z in SerialRXbuffer)
SerialRXbuffer = bytearray()
SerialRXbuffer.append(tx)
else:
# just add the Byte to the buffer
SerialRXbuffer.append(tx)
else:
print 'no input from GCS to be sent to APM '
ser.close()
exit -1

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser = serial.Serial(’/dev/ttyUSB0’, 57600, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False)
ser.flushInput()
ser.flushOutput()

SerialRXThread = Thread(target=tty2UDP)
SerialRXThread.start()
#SerialRXThread.join()

UDP2tty()

SerialTXThread = Thread(target=UDP2tty)
#SerialTXThread.start()
#SerialTXThread.join()

The missing part of puzzle was the broken way of writing the serial port. Python seems unable directly write binary into the serial port - it must be converted into text in advance this way:

			if ( tx == '\xfe' ):

				# send the buffer, clear it and add this Byte to it

				converted=''.join('\\x{:02x}'.format(z) for z in SerialRXbuffer

				ser.write(converted)

				print 'received message: ', converted

Once the connection was went away for a second so this needs further troubleshooting but the basic idea works :slight_smile:

Anyway I still expect your help guys - can someone comment what is the way APM checks the link status?

Thanks
Peter

Mission planner requires 2 way communication to connect. ie expects to request parameters, and get a response, with the parameter list.