#!/usr/bin/env python # # script to bidirectionally bridge mavlink and the SLIP stream # supported on artoo # # i mainly use this script to communicate with the SITL environment as follows: # * connect an FTDI cable (or similar) from the UART on Artoo to your dev machine # * run SITL in a VM, using `--out 192.168.1.158:14550` to pipe to your machine (adjust IP addr as necessary) # * fire up this script on your dev machine: `python slip-mavlink.py /dev/tty.usbserial-FTFA80EV` # import sys, os, socket, serial, select, struct, datetime, array import slip, button, artoo from pymavlink import mavutil # enable for more verbose output SHOW_THROUGHPUT = False MAVPROXY_DEFAULT_PORT = 14550 ARTOO_DSM_ID = chr(0x1) ARTOO_MAVLINK_ID = chr(0x4) # artoo msg type specifying this is a mavlink packet #ARTOO_SHOT_INFO_ID = chr() ARTOO_MAVLINK_SYSID = 0xff # default sysid pixhawk looks for DEFAULT_BAUD = 115200 FLIGHT_MODES = { 0: "STABILIZE", 1: "ACRO", 2: "ALT HOLD", 3: "AUTO", 4: "GUIDED", 5: "LOITER", 6: "RTL", 7: "CIRCLE", 9: "LAND", 10: "OF_LOITER", 11: "DRIFT", 13: "SPORT", 14: "FLIP", 15: "AUTOTUNE", 16: "POS HOLD", 17: "STOP", } def artooMavlinkPkt(mav, slip_pkt): if slip_pkt[0] == ARTOO_MAVLINK_ID: # extract and return the mavlink portion of this packet return "".join(slip_pkt[1:]) if slip_pkt[0] == ARTOO_DSM_ID: # convert a raw DSM packet into an RC override mavlink packet # it's possible to send raw DSM packets directly into the ArduCopter simulation process, # but if SITL is running within a VM, it's extra work to expose that network connection, # so we just send via mavproxy since we already have a connection to it. pkt_length = len(slip_pkt[1:]) if (pkt_length != 16): print ("[slip-mavlink] Received packet of length: {}".format(pkt_length)) return None ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8 = struct.unpack(" 5: sys.stdout.write(("throughput (bytes/sec) - tx: %d, rx: %d\r") % (tx_bytes/5, rx_bytes/5)) sys.stdout.flush() ts = datetime.datetime.now() rx_bytes = 0 tx_bytes = 0 if len(sys.argv) < 2: print "usage: slip-mavlink.py /dev/ttyMyDev (addr:port)" sys.exit(1) serial_path = sys.argv[1] if len(sys.argv) >= 3: addr, port = sys.argv[2].split(":") if port == "": port = MAVPROXY_DEFAULT_PORT else: addr, port = '', MAVPROXY_DEFAULT_PORT # this assumes we want to listen to something coming from our own address, # ie, a SITL instance running in a VM vai NAT myaddr = socket.gethostbyname(socket.gethostname()) if myaddr is not '127.0.0.1': addr = myaddr mainloop(serial_path, addr, int(port))