3 mavlink python utility functions 5 Copyright Andrew Tridgell 2011 6 Released under GNU GPL version 3 or later 8 from __future__
import print_function
9 from builtins
import object
11 import socket, math, struct, time, os, fnmatch, array, sys, errno
13 from pymavlink
import mavexpression
25 UDP_MAX_PACKET_LEN = 65535
36 default_native =
False 42 if not 'MAVLINK_DIALECT' in os.environ:
43 os.environ[
'MAVLINK_DIALECT'] =
'ardupilotmega' 46 '''return True if using MAVLink 1.0 or later''' 47 return not 'MAVLINK09' in os.environ
50 '''return True if using MAVLink 2.0''' 51 return 'MAVLINK20' in os.environ
54 '''evaluation an expression''' 55 return mavexpression.evaluate_expression(expression, vars)
58 '''evaluation a conditional (boolean) statement''' 67 return ord(c)
if sys.version_info.major < 3
else c
70 '''represent a GPS coordinate''' 71 def __init__(self, lat, lng, alt=0, heading=0):
78 return "lat=%.6f,lon=%.6f,alt=%.1f" % (self.
lat, self.
lng, self.
alt)
81 '''set the MAVLink dialect to work with. 82 For example, set_dialect("ardupilotmega") 84 global mavlink, current_dialect
85 from .generator
import mavparse
86 if 'MAVLINK20' in os.environ:
87 wire_protocol = mavparse.PROTOCOL_2_0
88 modname =
"pymavlink.dialects.v20." + dialect
89 elif mavlink
is None or mavlink.WIRE_PROTOCOL_VERSION ==
"1.0" or not 'MAVLINK09' in os.environ:
90 wire_protocol = mavparse.PROTOCOL_1_0
91 modname =
"pymavlink.dialects.v10." + dialect
93 wire_protocol = mavparse.PROTOCOL_0_9
94 modname =
"pymavlink.dialects.v09." + dialect
97 mod = __import__(modname)
100 from .generator.mavgen
import mavgen_python_dialect
102 mod = __import__(modname)
103 components = modname.split(
'.')
104 for comp
in components[1:]:
105 mod = getattr(mod, comp)
106 current_dialect = dialect
113 '''state for a particular system id''' 122 if float(mavlink.WIRE_PROTOCOL_VERSION) >= 1:
123 self.
messages[
'HOME'] = mavlink.MAVLink_gps_raw_int_message(0,0,0,0,0,0,0,0,0,0)
124 mavlink.MAVLink_waypoint_message = mavlink.MAVLink_mission_item_message
126 self.
messages[
'HOME'] = mavlink.MAVLink_gps_raw_message(0,0,0,0,0,0,0,0,0)
129 '''state for a particular system id/component id pair''' 134 '''a generic mavlink port''' 135 def __init__(self, fd, address, source_system=255, source_component=0, notimestamps=False, input=True, use_native=default_native):
136 global mavfile_global
138 mavfile_global = self
184 @target_system.setter
194 @target_component.setter
204 eff_tuple = (self.
sysid, 1)
206 return getattr(self.
param_state[eff_tuple],
'params')
246 '''auto-switch mavlink protocol version''' 254 if not magic
in [ 85, 254, 253 ]:
262 os.environ[
'MAVLINK09'] =
'1' 266 os.environ[
'MAVLINK20'] =
'1' 271 (callback, callback_args, callback_kwargs) = (self.mav.callback,
272 self.mav.callback_args,
273 self.mav.callback_kwargs)
277 (self.mav.callback, self.mav.callback_args, self.mav.callback_kwargs) = (callback,
282 '''default recv method''' 283 raise RuntimeError(
'no recv() method supplied')
286 '''default close method''' 287 raise RuntimeError(
'no close() method supplied')
290 '''default write method''' 291 raise RuntimeError(
'no write() method supplied')
295 '''wait for up to timeout seconds for more data''' 297 time.sleep(min(timeout,0.5))
300 (rin, win, xin) = select.select([self.
fd], [], [], timeout)
306 '''default pre message call''' 310 '''enable/disable RTS/CTS if applicable''' 314 if msg.get_srcComponent() == mavlink.MAV_COMP_ID_GIMBAL:
316 if msg.type
in (mavlink.MAV_TYPE_GCS,
317 mavlink.MAV_TYPE_GIMBAL,
318 mavlink.MAV_TYPE_ADSB,
319 mavlink.MAV_TYPE_ONBOARD_CONTROLLER):
324 '''default post message call''' 325 if '_posted' in msg.__dict__:
328 msg._timestamp = time.time()
329 type = msg.get_type()
331 if 'usec' in msg.__dict__:
332 self.
uptime = msg.usec * 1.0e-6
333 if 'time_boot_ms' in msg.__dict__:
334 self.
uptime = msg.time_boot_ms * 1.0e-3
338 msg._timestamp = self.
uptime 342 src_system = msg.get_srcSystem()
343 src_component = msg.get_srcComponent()
344 src_tuple = (src_system, src_component)
346 radio_tuple = (ord(
'3'), ord(
'D'))
354 if src_tuple == radio_tuple:
356 for s
in self.sysid_state.keys():
359 if not (src_tuple == radio_tuple
or msg.get_type() ==
'BAD_DATA'):
364 seq = (last_seq+1) % 256
366 if seq != seq2
and last_seq != -1:
367 diff = (seq2 - seq) % 256
377 self.
sysid = src_system
378 if float(mavlink.WIRE_PROTOCOL_VERSION) >= 1:
382 self.
sysid_state[self.
sysid].armed = (msg.base_mode & mavlink.MAV_MODE_FLAG_SAFETY_ARMED)
384 elif type ==
'PARAM_VALUE':
387 self.
param_state[src_tuple].params[msg.param_id] = msg.param_value
388 elif type ==
'SYS_STATUS' and mavlink.WIRE_PROTOCOL_VERSION ==
'0.9':
390 elif type ==
'GPS_RAW':
391 if self.
sysid_state[src_system].messages[
'HOME'].fix_type < 2:
392 self.
sysid_state[src_system].messages[
'HOME'] = msg
393 elif type ==
'GPS_RAW_INT':
394 if self.
sysid_state[src_system].messages[
'HOME'].fix_type < 3:
395 self.
sysid_state[src_system].messages[
'HOME'] = msg
399 if (msg.get_signed()
and 400 self.mav.signing.link_id == 0
and 401 msg.get_link_id() != 0
and 405 self.mav.signing.link_id = msg.get_link_id()
409 '''packet loss as a percentage''' 416 '''message receive routine''' 419 n = self.mav.bytes_needed()
425 self.logfile_raw.write(
str(s))
431 msg = self.mav.parse_char(s)
433 if self.
logfile and msg.get_type() !=
'BAD_DATA' :
434 usec =
int(time.time() * 1.0e6) & ~3
435 self.logfile.write(
str(struct.pack(
'>Q', usec) + msg.get_msgbuf()))
444 def recv_match(self, condition=None, type=None, blocking=False, timeout=None):
445 '''recv the next MAVLink message that matches the given condition 446 type can be a string or a list of strings''' 447 if type
is not None and not isinstance(type, list)
and not isinstance(type, set):
449 start_time = time.time()
451 if timeout
is not None:
455 if start_time + timeout < time.time():
468 if type
is not None and not m.get_type()
in type:
475 '''check if a condition is true''' 479 '''return True if using MAVLink 1.0 or later''' 483 '''return True if using MAVLink 2.0 or later''' 487 '''start logging to the given logfile, with timestamps''' 488 self.
logfile = open(logfile, mode=mode)
491 '''start logging raw bytes to the given logfile, without timestamps''' 495 '''wait for a heartbeat so we know the target system IDs''' 496 return self.
recv_match(type=
'HEARTBEAT', blocking=blocking, timeout=timeout)
499 '''initiate fetch of all parameters''' 507 '''initiate fetch of one parameter''' 512 if sys.version_info.major >= 3
and not isinstance(name, bytes):
513 name = bytes(name,
'ascii')
517 '''return the time since the last message of type mtype was received''' 520 return time.time() - self.
messages[mtype]._timestamp
523 '''wrapper for parameter set''' 525 if parm_type
is None:
526 parm_type = mavlink.MAVLINK_TYPE_FLOAT
528 parm_name.encode(
'utf8'), parm_value, parm_type)
531 parm_name.encode(
'utf8'), parm_value)
534 '''wrapper for waypoint_request_list_send''' 541 '''wrapper for waypoint_clear_all_send''' 548 '''wrapper for waypoint_request_send''' 555 '''wrapper for waypoint_set_current_send''' 562 '''return current waypoint''' 564 m = self.
recv_match(type=
'MISSION_CURRENT', blocking=
True)
566 m = self.
recv_match(type=
'WAYPOINT_CURRENT', blocking=
True)
570 '''wrapper for waypoint_count_send''' 578 Enables/ disables MAV_MODE_FLAG 579 @param flag The mode flag, 580 see MAV_MODE_FLAG enum 581 @param enable Enable the flag, (True/False) 590 mavlink.MAV_CMD_DO_SET_MODE, 0,
594 print(
"Set mode flag not supported")
597 '''enter auto mode''' 600 mavlink.MAV_CMD_MISSION_START, 0, 0, 0, 0, 0, 0, 0, 0)
602 MAV_ACTION_SET_AUTO = 13
606 '''return dictionary mapping mode names to numbers, or None if unknown''' 608 mav_autopilot = self.
field(
'HEARTBEAT',
'autopilot',
None)
609 if mav_autopilot == mavlink.MAV_AUTOPILOT_PX4:
614 if mav_type
in [mavlink.MAV_TYPE_QUADROTOR,
615 mavlink.MAV_TYPE_HELICOPTER,
616 mavlink.MAV_TYPE_HEXAROTOR,
617 mavlink.MAV_TYPE_OCTOROTOR,
618 mavlink.MAV_TYPE_DODECAROTOR,
619 mavlink.MAV_TYPE_COAXIAL,
620 mavlink.MAV_TYPE_TRICOPTER]:
621 map = mode_mapping_acm
622 if mav_type == mavlink.MAV_TYPE_FIXED_WING:
623 map = mode_mapping_apm
624 if mav_type == mavlink.MAV_TYPE_GROUND_ROVER:
625 map = mode_mapping_rover
626 if mav_type == mavlink.MAV_TYPE_SURFACE_BOAT:
627 map = mode_mapping_rover
628 if mav_type == mavlink.MAV_TYPE_ANTENNA_TRACKER:
629 map = mode_mapping_tracker
630 if mav_type == mavlink.MAV_TYPE_SUBMARINE:
631 map = mode_mapping_sub
634 inv_map = dict((a, b)
for (b, a)
in map.items())
638 '''enter arbitrary mode''' 639 if isinstance(mode, str):
641 if mode_map
is None or mode
not in mode_map:
642 print(
"Unknown mode '%s'" % mode)
644 mode = mode_map[mode]
647 mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
651 '''enter arbitrary mode''' 652 if isinstance(mode, str):
654 if mode_map
is None or mode
not in mode_map:
655 print(
"Unknown mode '%s'" % mode)
658 mode, custom_mode, custom_sub_mode = px4_map[mode]
660 mavlink.MAV_CMD_DO_SET_MODE, 0, mode, custom_mode, custom_sub_mode, 0, 0, 0, 0)
662 def set_mode(self, mode, custom_mode = 0, custom_sub_mode = 0):
663 '''set arbitrary flight mode''' 664 mav_autopilot = self.
field(
'HEARTBEAT',
'autopilot',
None)
665 if mav_autopilot == mavlink.MAV_AUTOPILOT_PX4:
674 mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH, 0, 0, 0, 0, 0, 0, 0, 0)
676 MAV_ACTION_RETURN = 3
680 '''enter MANUAL mode''' 683 mavlink.MAV_CMD_DO_SET_MODE, 0,
684 mavlink.MAV_MODE_MANUAL_ARMED,
687 MAV_ACTION_SET_MANUAL = 12
691 '''enter FBWA mode''' 694 mavlink.MAV_CMD_DO_SET_MODE, 0,
695 mavlink.MAV_MODE_STABILIZE_ARMED,
698 print(
"Forcing FBWA not supported")
701 '''enter LOITER mode''' 704 mavlink.MAV_CMD_NAV_LOITER_UNLIM, 0, 0, 0, 0, 0, 0, 0, 0)
706 MAV_ACTION_LOITER = 27
710 '''set a servo value''' 712 mavlink.MAV_CMD_DO_SET_SERVO, 0,
718 '''Set relay_pin to value of state''' 720 self.mav.command_long_send(
723 mavlink.MAV_CMD_DO_SET_RELAY,
733 print(
"Setting relays not supported.")
736 '''calibrate accels (1D version)''' 738 mavlink.MAV_CMD_PREFLIGHT_CALIBRATION, 0,
742 '''calibrate pressure''' 745 mavlink.MAV_CMD_PREFLIGHT_CALIBRATION, 0,
748 MAV_ACTION_CALIBRATE_PRESSURE = 20
752 '''reboot the autopilot''' 754 if hold_in_bootloader:
759 mavlink.MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 0,
760 param1, 0, 0, 0, 0, 0, 0)
764 mavlink.MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 0,
768 self.
recv_match(type=
'VFR_HUD', blocking=
True)
770 self.
recv_match(type=
'GPS_RAW_INT', blocking=
True,
771 condition=
'GPS_RAW_INT.fix_type>=3 and GPS_RAW_INT.lat != 0')
773 self.
recv_match(type=
'GPS_RAW', blocking=
True,
774 condition=
'GPS_RAW.fix_type>=2 and GPS_RAW.lat != 0')
777 '''return current location''' 780 self.
recv_match(type=
'VFR_HUD', blocking=
True)
781 self.
recv_match(type=
'GLOBAL_POSITION_INT', blocking=
True)
783 alt = self.
messages[
'GLOBAL_POSITION_INT'].relative_alt*0.001
787 self.
messages[
'GPS_RAW_INT'].lon*1.0e-7,
792 '''arm motors (arducopter only)''' 794 self.mav.command_long_send(
797 mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
808 '''calibrate pressure''' 810 self.mav.command_long_send(
813 mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
824 '''return true if motors armed''' 828 '''wait for motors to be armed''' 835 '''wait for motors to be disarmed''' 842 def field(self, type, field, default=None):
843 '''convenient function for returning an arbitrary MAVLink 844 field with a default''' 847 return getattr(self.
messages[type], field, default)
849 def param(self, name, default=None):
850 '''convenient function for returning an arbitrary MAVLink 851 parameter with a default''' 852 if not name
in self.
params:
856 def setup_signing(self, secret_key, sign_outgoing=True, allow_unsigned_callback=None, initial_timestamp=None, link_id=None):
857 '''setup for MAVLink2 signing''' 858 self.mav.signing.secret_key = secret_key
859 self.mav.signing.sign_outgoing = sign_outgoing
860 self.mav.signing.allow_unsigned_callback = allow_unsigned_callback
863 global global_link_id
864 link_id = global_link_id
865 global_link_id = min(global_link_id + 1, 255)
866 self.mav.signing.link_id = link_id
867 if initial_timestamp
is None:
869 epoch_offset = 1420070400
870 now = max(time.time(), epoch_offset)
871 initial_timestamp = now - epoch_offset
872 initial_timestamp =
int(initial_timestamp * 100 * 1000)
874 self.mav.signing.timestamp = initial_timestamp
877 '''disable MAVLink2 signing''' 878 self.mav.signing.secret_key =
None 879 self.mav.signing.sign_outgoing =
False 880 self.mav.signing.allow_unsigned_callback =
None 881 self.mav.signing.link_id = 0
882 self.mav.signing.timestamp = 0
885 '''set the clone on exec flag on a file descriptor. Ignore exceptions''' 888 flags = fcntl.fcntl(fd, fcntl.F_GETFD)
889 flags |= fcntl.FD_CLOEXEC
890 fcntl.fcntl(fd, fcntl.F_SETFD, flags)
900 raise Exception(
"write always fails")
906 class mavserial(mavfile):
907 '''a serial mavlink port''' 908 def __init__(self, device, baud=115200, autoreconnect=False, source_system=255, source_component=0, use_native=default_native, force_connected=False):
910 if ',' in device
and not os.path.exists(device):
911 device, baud = device.split(
',')
921 dsrdtr=
False, rtscts=
False, xonxoff=
False)
922 except serial.SerialException
as e:
923 if not force_connected:
928 fd = self.port.fileno()
933 mavfile.__init__(self, fd, device, source_system=source_system, source_component=source_component, use_native=use_native)
937 '''enable/disable RTS/CTS if applicable''' 939 self.port.setRtsCts(enable)
941 self.port.rtscts = enable
947 self.port.setBaudrate(baudrate)
950 self.port.baudrate = baudrate
957 n = self.mav.bytes_needed()
959 waiting = self.port.inWaiting()
962 ret = self.port.read(n)
967 return self.port.write(bytes(buf))
970 print(
"Device %s is dead" % self.
device)
980 newport = serial.Serial(self.
device, self.
baud, timeout=0,
981 dsrdtr=
False, rtscts=
False, xonxoff=
False)
982 except serial.SerialException
as e:
989 print(
"Device %s reopened OK" % self.
device)
992 self.
fd = self.port.fileno()
1004 '''a UDP mavlink socket''' 1005 def __init__(self, device, input=True, broadcast=False, source_system=255, source_component=0, use_native=default_native):
1006 a = device.split(
':')
1008 print(
"UDP ports must be specified as host:port")
1010 self.
port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1014 self.port.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1015 self.port.bind((a[0],
int(a[1])))
1019 self.port.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
1022 self.port.setblocking(0)
1025 mavfile.__init__(self, self.port.fileno(), device, source_system=source_system, source_component=source_component, input=input, use_native=use_native)
1032 data, new_addr = self.port.recvfrom(UDP_MAX_PACKET_LEN)
1033 except socket.error
as e:
1034 if e.errno
in [ errno.EAGAIN, errno.EWOULDBLOCK, errno.ECONNREFUSED ]:
1057 except socket.error:
1061 '''message receive routine for UDP link''' 1068 m = self.mav.parse_char(s)
1075 '''a UDP multicast mavlink socket''' 1076 def __init__(self, device, broadcast=False, source_system=255, source_component=0, use_native=default_native):
1077 a = device.split(
':')
1078 mcast_ip =
"239.255.145.50" 1080 if len(a) == 1
and len(a[0]) > 0:
1081 mcast_port =
int(a[0])
1084 mcast_port =
int(a[1])
1089 self.
port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
1090 self.port.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1091 self.port.bind((mcast_ip, mcast_port))
1092 mreq = struct.pack(
"4sl", socket.inet_aton(mcast_ip), socket.INADDR_ANY)
1093 self.port.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
1094 self.port.setblocking(0)
1098 self.
port_out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1099 self.port_out.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1100 self.port_out.setblocking(0)
1101 self.port_out.connect((mcast_ip, mcast_port))
1105 mavfile.__init__(self, self.port.fileno(), device,
1106 source_system=source_system, source_component=source_component,
1107 input=
False, use_native=use_native)
1111 self.port_out.close()
1115 data, new_addr = self.port.recvfrom(UDP_MAX_PACKET_LEN)
1118 (myaddr,self.
myport) = self.port_out.getsockname()
1121 except socket.error
as e:
1122 if e.errno
in [ errno.EAGAIN, errno.EWOULDBLOCK, errno.ECONNREFUSED ]:
1125 if self.
myport == new_addr[1]:
1132 self.port_out.send(buf)
1133 except socket.error
as e:
1137 '''message receive routine for UDP link''' 1144 m = self.mav.parse_char(s)
1152 '''a TCP mavlink socket''' 1155 autoreconnect=
False,
1159 use_native=default_native):
1160 a = device.split(
':')
1162 print(
"TCP ports must be specified as host:port")
1171 mavfile.__init__(self, self.port.fileno(),
"tcp:" + device, source_system=source_system, source_component=source_component, use_native=use_native)
1174 self.
port = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1184 except Exception
as e:
1187 print(e,
"sleeping")
1189 self.port.setblocking(0)
1191 self.port.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
1198 print(
"EOF on TCP socket")
1200 print(
"Attempting reconnect")
1201 if self.
port is not None:
1207 if self.
port is None:
1210 n = self.mav.bytes_needed()
1212 data = self.port.recv(n)
1213 except socket.error
as e:
1214 if e.errno
in [ errno.EAGAIN, errno.EWOULDBLOCK ]:
1223 if self.
port is None:
1227 except socket.error:
1232 '''a TCP input mavlink socket''' 1233 def __init__(self, device, source_system=255, source_component=0, retries=3, use_native=default_native):
1234 a = device.split(
':')
1236 print(
"TCP ports must be specified as host:port")
1238 self.
listen = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1240 self.listen.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1242 self.listen.listen(1)
1243 self.listen.setblocking(0)
1245 self.listen.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
1246 mavfile.__init__(self, self.listen.fileno(),
"tcpin:" + device, source_system=source_system, source_component=source_component, use_native=use_native)
1255 (self.
port, addr) = self.listen.accept()
1258 self.port.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
1259 self.port.setblocking(0)
1261 self.
fd = self.port.fileno()
1264 n = self.mav.bytes_needed()
1266 data = self.port.recv(n)
1267 except socket.error
as e:
1268 if e.errno
in [ errno.EAGAIN, errno.EWOULDBLOCK ]:
1272 self.
fd = self.listen.fileno()
1277 if self.
port is None:
1281 except socket.error
as e:
1282 if e.errno
in [ errno.EPIPE ]:
1285 self.
fd = self.listen.fileno()
1290 '''a MAVLink logfile reader/writer''' 1291 def __init__(self, filename, planner_format=None,
1292 write=
False, append=
False,
1293 robust_parsing=
True, notimestamps=
False, source_system=255, source_component=0, use_native=default_native):
1305 self.
f = open(filename, mode)
1308 mavfile.__init__(self,
None, filename, source_system=source_system, source_component=source_component, notimestamps=notimestamps, use_native=use_native)
1323 n = self.mav.bytes_needed()
1324 return self.f.read(n)
1330 '''scan forward looking in a tlog for a timestamp in a reasonable range''' 1332 (tusec,) = struct.unpack(
'>Q', tbuf)
1344 '''read timestamp if needed''' 1351 tbuf = self.f.read(21)
1352 if len(tbuf) != 21
or tbuf[0] !=
'-' or tbuf[20] !=
':':
1353 raise RuntimeError(
'bad planner timestamp %s' % tbuf)
1356 t -= 719163 * 24 * 60 * 60
1359 tbuf = self.f.read(8)
1362 (tusec,) = struct.unpack(
'>Q', tbuf)
1365 self._last_message.get_type() ==
"BAD_DATA" and 1368 self.
_link = tusec & 0x3
1372 '''add timestamp to message''' 1379 if msg.get_type() !=
"BAD_DATA":
1381 msg._link = self.
_link 1385 '''a MAVLink log file accessed via mmap. Used for fast read-only 1386 access with low memory overhead where particular message types are wanted''' 1388 import platform, mmap
1389 mavlogfile.__init__(self, filename)
1393 if platform.system() ==
"Windows":
1396 self.
data_map = mmap.mmap(self.f.fileno(), self.
data_len, mmap.MAP_PRIVATE, mmap.PROT_READ)
1402 '''rewind to start of log''' 1409 '''rewind to start of log''' 1413 '''initialise arrays for fast recv_match()''' 1440 if marker == MARKER_V1:
1443 elif marker == MARKER_V2:
1447 if incompat_flags & mavlink.MAVLINK_IFLAG_SIGNED:
1448 mlen += mavlink.MAVLINK_SIGNATURE_BLOCK_LEN
1455 if not mtype
in mavlink.mavlink_map:
1460 msg = mavlink.mavlink_map[mtype]
1467 self.
offsets[mtype].append(ofs)
1471 new_pct = (100 * ofs) // self.
data_len 1472 if progress_callback
is not None and new_pct != pct:
1473 progress_callback(new_pct)
1476 for mtype
in self.
counts:
1482 '''skip fwd to next msg matching given type set''' 1486 type.update(set([
'HEARTBEAT',
'PARAM_VALUE']))
1493 self.indexes.append(0)
1501 if ofs < smallest_offset:
1502 smallest_offset = ofs
1504 if smallest_index >= 0:
1505 self.
indexes[smallest_index] += 1
1506 self.
offset = smallest_offset
1507 self.f.seek(smallest_offset)
1509 def recv_match(self, condition=None, type=None, blocking=False, timeout=None):
1510 '''recv the next message that matches the given condition 1511 type can be a string or a list of strings''' 1512 if type
is not None:
1513 if isinstance(type, str):
1515 elif isinstance(type, list):
1518 if type
is not None:
1531 if type
is not None and not m.get_type()
in type:
1538 '''return an array of tuples for all flightmodes in log. Tuple is (modestring, t0, t1)''' 1544 types = set([
'HEARTBEAT'])
1549 tstamp = m._timestamp
1555 self._flightmodes.append((self.
flightmode, tstamp,
None))
1557 if tstamp
is not None:
1565 '''a MAVLink child processes reader/writer''' 1566 def __init__(self, filename, source_system=255, source_component=0, use_native=default_native):
1567 from subprocess
import Popen, PIPE
1571 self.
child = Popen(filename, shell=
False, stdout=PIPE, stdin=PIPE, bufsize=0)
1572 self.
fd = self.child.stdout.fileno()
1574 fl = fcntl.fcntl(self.
fd, fcntl.F_GETFL)
1575 fcntl.fcntl(self.
fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
1577 fl = fcntl.fcntl(self.child.stdout.fileno(), fcntl.F_GETFL)
1578 fcntl.fcntl(self.child.stdout.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)
1580 mavfile.__init__(self, self.
fd, filename, source_system=source_system, source_component=source_component, use_native=use_native)
1587 x = self.child.stdout.read(1)
1593 self.child.stdin.write(buf)
1597 planner_format=
None, write=
False, append=
False,
1598 robust_parsing=
True, notimestamps=
False, input=
True,
1599 dialect=
None, autoreconnect=
False, zero_time_base=
False,
1600 retries=3, use_native=default_native,
1601 force_connected=
False, progress_callback=
None):
1602 '''open a serial, UDP, TCP or file mavlink connection''' 1603 global mavfile_global
1607 autoreconnect =
True 1609 if dialect
is not None:
1611 if device.startswith(
'tcp:'):
1612 return mavtcp(device[4:],
1613 autoreconnect=autoreconnect,
1614 source_system=source_system,
1615 source_component=source_component,
1617 use_native=use_native)
1618 if device.startswith(
'tcpin:'):
1619 return mavtcpin(device[6:], source_system=source_system, source_component=source_component, retries=retries, use_native=use_native)
1620 if device.startswith(
'udpin:'):
1621 return mavudp(device[6:], input=
True, source_system=source_system, source_component=source_component, use_native=use_native)
1622 if device.startswith(
'udpout:'):
1623 return mavudp(device[7:], input=
False, source_system=source_system, source_component=source_component, use_native=use_native)
1624 if device.startswith(
'udpbcast:'):
1625 return mavudp(device[9:], input=
False, source_system=source_system, source_component=source_component, use_native=use_native, broadcast=
True)
1627 if device.startswith(
'udp:'):
1628 return mavudp(device[4:], input=input, source_system=source_system, source_component=source_component, use_native=use_native)
1629 if device.startswith(
'mcast:'):
1630 return mavmcast(device[6:], source_system=source_system, source_component=source_component, use_native=use_native)
1632 if device.lower().endswith(
'.bin')
or device.lower().endswith(
'.px4log'):
1634 from pymavlink
import DFReader
1639 if device.endswith(
'.log'):
1641 from pymavlink
import DFReader
1642 if DFReader.DFReader_is_text_log(device):
1648 logsuffixes = [
'mavlink',
'log',
'raw',
'tlog' ]
1649 suffix = device.split(
'.')[-1].lower()
1650 if device.find(
':') != -1
and not suffix
in logsuffixes:
1651 return mavudp(device, source_system=source_system, source_component=source_component, input=input, use_native=use_native)
1652 if os.path.isfile(device):
1653 if device.endswith(
".elf")
or device.find(
"/bin/") != -1:
1654 print(
"executing '%s'" % device)
1655 return mavchildexec(device, source_system=source_system, source_component=source_component, use_native=use_native)
1656 elif not write
and not append
and not notimestamps:
1657 return mavmmaplog(device, progress_callback=progress_callback)
1659 return mavlogfile(device, planner_format=planner_format, write=write,
1660 append=append, robust_parsing=robust_parsing, notimestamps=notimestamps,
1661 source_system=source_system, source_component=source_component, use_native=use_native)
1664 source_system=source_system,
1665 source_component=source_component,
1666 autoreconnect=autoreconnect,
1667 use_native=use_native,
1668 force_connected=force_connected)
1671 '''a class for fixed frequency events''' 1677 '''force immediate triggering''' 1681 '''return True if we should trigger now''' 1685 print(
"Warning, time moved backwards. Restarting timer.")
1695 from curses
import ascii
1701 '''see if a character is printable''' 1704 return ascii.isprint(c)
1705 if isinstance(c, int):
1709 return ic >= 32
and ic <= 126
1712 '''see if a string is all printable''' 1714 if not is_printable(c)
and not c
in [
'\r',
'\n',
'\t']
and not c
in [ord(
'\r'), ord(
'\n'), ord(
'\t')]:
1719 '''auto-detected serial port''' 1720 def __init__(self, device, description=None, hwid=None):
1729 if self.
hwid is not None:
1730 ret +=
" : " + self.
hwid 1734 '''try to auto-detect serial ports on win32''' 1736 from serial.tools.list_ports_windows
import comports
1737 list = sorted(comports())
1742 for port, description, hwid
in list:
1744 p =
SerialPort(port, description=description, hwid=hwid)
1745 for preferred
in preferred_list:
1746 if fnmatch.fnmatch(description, preferred)
or fnmatch.fnmatch(hwid, preferred):
1762 '''try to auto-detect serial ports on unix''' 1764 glist = glob.glob(
'/dev/ttyS*') + glob.glob(
'/dev/ttyUSB*') + glob.glob(
'/dev/ttyACM*') + glob.glob(
'/dev/serial/by-id/*')
1770 for preferred
in preferred_list:
1771 if fnmatch.fnmatch(d, preferred):
1783 '''try to auto-detect serial port''' 1790 '''mode string for 0.9 protocol''' 1792 nav_mode = msg.nav_mode
1802 MAV_NAV_GROUNDED = 0
1805 MAV_NAV_WAYPOINT = 3
1807 MAV_NAV_RETURNING = 5
1812 cmode = (mode, nav_mode)
1814 (MAV_MODE_UNINIT, MAV_NAV_GROUNDED) :
"INITIALISING",
1815 (MAV_MODE_MANUAL, MAV_NAV_VECTOR) :
"MANUAL",
1816 (MAV_MODE_TEST3, MAV_NAV_VECTOR) :
"CIRCLE",
1817 (MAV_MODE_GUIDED, MAV_NAV_VECTOR) :
"GUIDED",
1818 (MAV_MODE_TEST1, MAV_NAV_VECTOR) :
"STABILIZE",
1819 (MAV_MODE_TEST2, MAV_NAV_LIFTOFF) :
"FBWA",
1820 (MAV_MODE_AUTO, MAV_NAV_WAYPOINT) :
"AUTO",
1821 (MAV_MODE_AUTO, MAV_NAV_RETURNING) :
"RTL",
1822 (MAV_MODE_AUTO, MAV_NAV_LOITER) :
"LOITER",
1823 (MAV_MODE_AUTO, MAV_NAV_LIFTOFF) :
"TAKEOFF",
1824 (MAV_MODE_AUTO, MAV_NAV_LANDING) :
"LANDING",
1825 (MAV_MODE_AUTO, MAV_NAV_HOLD) :
"LOITER",
1826 (MAV_MODE_GUIDED, MAV_NAV_VECTOR) :
"GUIDED",
1827 (MAV_MODE_GUIDED, MAV_NAV_WAYPOINT) :
"GUIDED",
1828 (100, MAV_NAV_VECTOR) :
"STABILIZE",
1829 (101, MAV_NAV_VECTOR) :
"ACRO",
1830 (102, MAV_NAV_VECTOR) :
"ALT_HOLD",
1831 (107, MAV_NAV_VECTOR) :
"CIRCLE",
1832 (109, MAV_NAV_VECTOR) :
"LAND",
1834 if cmode
in mapping:
1835 return mapping[cmode]
1836 return "Mode(%s,%s)" % cmode
1838 mode_mapping_apm = {
1853 16 :
'INITIALISING',
1861 mode_mapping_acm = {
1881 20 :
'GUIDED_NOGPS',
1886 mode_mapping_rover = {
1900 mode_mapping_tracker = {
1908 mode_mapping_sub = {
1925 mainstate_mapping_px4 = {
1936 10 :
'AUTO_TAKEOFF',
1938 12 :
'AUTO_FOLLOW_TARGET',
1942 return mainstate_mapping_px4.get(MainState,
"Unknown")
1946 PX4_CUSTOM_MAIN_MODE_MANUAL = 1
1947 PX4_CUSTOM_MAIN_MODE_ALTCTL = 2
1948 PX4_CUSTOM_MAIN_MODE_POSCTL = 3
1949 PX4_CUSTOM_MAIN_MODE_AUTO = 4
1950 PX4_CUSTOM_MAIN_MODE_ACRO = 5
1951 PX4_CUSTOM_MAIN_MODE_OFFBOARD = 6
1952 PX4_CUSTOM_MAIN_MODE_STABILIZED = 7
1953 PX4_CUSTOM_MAIN_MODE_RATTITUDE = 8
1955 PX4_CUSTOM_SUB_MODE_OFFBOARD = 0
1956 PX4_CUSTOM_SUB_MODE_AUTO_READY = 1
1957 PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF = 2
1958 PX4_CUSTOM_SUB_MODE_AUTO_LOITER = 3
1959 PX4_CUSTOM_SUB_MODE_AUTO_MISSION = 4
1960 PX4_CUSTOM_SUB_MODE_AUTO_RTL = 5
1961 PX4_CUSTOM_SUB_MODE_AUTO_LAND = 6
1962 PX4_CUSTOM_SUB_MODE_AUTO_RTGS = 7
1963 PX4_CUSTOM_SUB_MODE_AUTO_FOLLOW_TARGET = 8
1965 auto_mode_flags = mavlink.MAV_MODE_FLAG_AUTO_ENABLED \
1966 | mavlink.MAV_MODE_FLAG_STABILIZE_ENABLED \
1967 | mavlink.MAV_MODE_FLAG_GUIDED_ENABLED
1969 px4_map = {
"MANUAL": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_STABILIZE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_MANUAL, 0 ),
1970 "STABILIZED": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_STABILIZE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_STABILIZED, 0 ),
1971 "ACRO": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_ACRO, 0 ),
1972 "RATTITUDE": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_RATTITUDE, 0 ),
1973 "ALTCTL": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_STABILIZE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_ALTCTL, 0 ),
1974 "POSCTL": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | mavlink.MAV_MODE_FLAG_STABILIZE_ENABLED | mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, PX4_CUSTOM_MAIN_MODE_POSCTL, 0 ),
1975 "LOITER": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_LOITER ),
1976 "MISSION": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_MISSION ),
1977 "RTL": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_RTL ),
1978 "LAND": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_LAND ),
1979 "RTGS": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_RTGS ),
1980 "FOLLOWME": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_FOLLOW_TARGET ),
1981 "OFFBOARD": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_OFFBOARD, 0 ),
1982 "TAKEOFF": (mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | auto_mode_flags, PX4_CUSTOM_MAIN_MODE_AUTO, PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF )}
1986 custom_main_mode = (custom_mode & 0xFF0000) >> 16
1987 custom_sub_mode = (custom_mode & 0xFF000000) >> 24
1989 if base_mode & mavlink.MAV_MODE_FLAG_MANUAL_INPUT_ENABLED != 0:
1990 if custom_main_mode == PX4_CUSTOM_MAIN_MODE_MANUAL:
1992 elif custom_main_mode == PX4_CUSTOM_MAIN_MODE_ACRO:
1994 elif custom_main_mode == PX4_CUSTOM_MAIN_MODE_RATTITUDE:
1996 elif custom_main_mode == PX4_CUSTOM_MAIN_MODE_STABILIZED:
1998 elif custom_main_mode == PX4_CUSTOM_MAIN_MODE_ALTCTL:
2000 elif custom_main_mode == PX4_CUSTOM_MAIN_MODE_POSCTL:
2002 elif (base_mode & auto_mode_flags) == auto_mode_flags:
2003 if custom_main_mode & PX4_CUSTOM_MAIN_MODE_AUTO != 0:
2004 if custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_MISSION:
2006 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF:
2008 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LOITER:
2010 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_FOLLOW_TARGET:
2012 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_RTL:
2014 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LAND:
2016 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_RTGS:
2018 elif custom_sub_mode == PX4_CUSTOM_SUB_MODE_OFFBOARD:
2023 '''return dictionary mapping mode names to numbers, or None if unknown''' 2025 if mav_type
in [mavlink.MAV_TYPE_QUADROTOR,
2026 mavlink.MAV_TYPE_HELICOPTER,
2027 mavlink.MAV_TYPE_HEXAROTOR,
2028 mavlink.MAV_TYPE_OCTOROTOR,
2029 mavlink.MAV_TYPE_COAXIAL,
2030 mavlink.MAV_TYPE_TRICOPTER]:
2031 map = mode_mapping_acm
2032 if mav_type == mavlink.MAV_TYPE_FIXED_WING:
2033 map = mode_mapping_apm
2034 if mav_type == mavlink.MAV_TYPE_GROUND_ROVER:
2035 map = mode_mapping_rover
2036 if mav_type == mavlink.MAV_TYPE_SURFACE_BOAT:
2037 map = mode_mapping_rover
2038 if mav_type == mavlink.MAV_TYPE_ANTENNA_TRACKER:
2039 map = mode_mapping_tracker
2040 if mav_type == mavlink.MAV_TYPE_SUBMARINE:
2041 map = mode_mapping_sub
2044 inv_map = dict((a, b)
for (b, a)
in map.items())
2048 '''return dictionary mapping mode numbers to name, or None if unknown''' 2050 if mav_type
in [mavlink.MAV_TYPE_QUADROTOR,
2051 mavlink.MAV_TYPE_HELICOPTER,
2052 mavlink.MAV_TYPE_HEXAROTOR,
2053 mavlink.MAV_TYPE_OCTOROTOR,
2054 mavlink.MAV_TYPE_DODECAROTOR,
2055 mavlink.MAV_TYPE_COAXIAL,
2056 mavlink.MAV_TYPE_TRICOPTER]:
2057 map = mode_mapping_acm
2058 if mav_type == mavlink.MAV_TYPE_FIXED_WING:
2059 map = mode_mapping_apm
2060 if mav_type == mavlink.MAV_TYPE_GROUND_ROVER:
2061 map = mode_mapping_rover
2062 if mav_type == mavlink.MAV_TYPE_SURFACE_BOAT:
2063 map = mode_mapping_rover
2064 if mav_type == mavlink.MAV_TYPE_ANTENNA_TRACKER:
2065 map = mode_mapping_tracker
2066 if mav_type == mavlink.MAV_TYPE_SUBMARINE:
2067 map = mode_mapping_sub
2074 '''mode string for 1.0 protocol, from heartbeat''' 2075 if msg.autopilot == mavlink.MAV_AUTOPILOT_PX4:
2077 if not msg.base_mode & mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED:
2078 return "Mode(0x%08x)" % msg.base_mode
2079 if msg.type
in [ mavlink.MAV_TYPE_QUADROTOR, mavlink.MAV_TYPE_HEXAROTOR,
2080 mavlink.MAV_TYPE_OCTOROTOR, mavlink.MAV_TYPE_TRICOPTER,
2081 mavlink.MAV_TYPE_COAXIAL,
2082 mavlink.MAV_TYPE_HELICOPTER ]:
2083 if msg.custom_mode
in mode_mapping_acm:
2084 return mode_mapping_acm[msg.custom_mode]
2085 if msg.type == mavlink.MAV_TYPE_FIXED_WING:
2086 if msg.custom_mode
in mode_mapping_apm:
2087 return mode_mapping_apm[msg.custom_mode]
2088 if msg.type == mavlink.MAV_TYPE_GROUND_ROVER:
2089 if msg.custom_mode
in mode_mapping_rover:
2090 return mode_mapping_rover[msg.custom_mode]
2091 if msg.type == mavlink.MAV_TYPE_SURFACE_BOAT:
2092 if msg.custom_mode
in mode_mapping_rover:
2093 return mode_mapping_rover[msg.custom_mode]
2094 if msg.type == mavlink.MAV_TYPE_ANTENNA_TRACKER:
2095 if msg.custom_mode
in mode_mapping_tracker:
2096 return mode_mapping_tracker[msg.custom_mode]
2097 if msg.type == mavlink.MAV_TYPE_SUBMARINE:
2098 if msg.custom_mode
in mode_mapping_sub:
2099 return mode_mapping_sub[msg.custom_mode]
2100 return "Mode(%u)" % msg.custom_mode
2103 '''return mode string for APM:Plane''' 2104 if mode_number
in mode_mapping_apm:
2105 return mode_mapping_apm[mode_number]
2106 return "Mode(%u)" % mode_number
2109 '''return mode string for APM:Copter''' 2110 if mode_number
in mode_mapping_acm:
2111 return mode_mapping_acm[mode_number]
2112 return "Mode(%u)" % mode_number
2115 '''x25 CRC - based on checksum.h from mavlink library''' 2121 '''add in some more bytes''' 2122 byte_buf = array.array(
'B')
2123 if isinstance(buf, array.array):
2124 byte_buf.extend(buf)
2126 byte_buf.fromstring(buf)
2129 tmp = b ^ (accum & 0xff)
2130 tmp = (tmp ^ (tmp<<4)) & 0xFF
2131 accum = (accum>>8) ^ (tmp<<8) ^ (tmp<<3) ^ (tmp>>4)
2132 accum = accum & 0xFFFF
2136 '''an object that looks like a serial port, but 2137 transmits using mavlink SERIAL_CONTROL packets''' 2138 def __init__(self, portname, baudrate, devnum=0, devbaud=0, timeout=3, debug=0):
2139 from .
import mavutil
2146 self.
debug(
"Connecting with MAVLink to %s ..." % portname)
2147 self.
mav = mavutil.mavlink_connection(portname, autoreconnect=
True, baud=baudrate)
2148 self.mav.wait_heartbeat()
2149 self.
debug(
"HEARTBEAT OK\n")
2152 self.
debug(
"Locked serial device\n")
2155 '''write some debug text''' 2160 '''write some bytes''' 2161 from .
import mavutil
2162 self.
debug(
"sending '%s' (0x%02x) of len %u\n" % (b, ord(b[0]), len(b)), 2)
2167 buf = [ord(x)
for x
in b[:n]]
2168 buf.extend([0]*(70-len(buf)))
2169 self.mav.mav.serial_control_send(self.
port,
2170 mavutil.mavlink.SERIAL_CONTROL_FLAG_EXCLUSIVE |
2171 mavutil.mavlink.SERIAL_CONTROL_FLAG_RESPOND,
2179 '''read some bytes into self.buf''' 2180 from .
import mavutil
2181 start_time = time.time()
2182 while time.time() < start_time + self.
timeout:
2183 m = self.mav.recv_match(condition=
'SERIAL_CONTROL.count!=0',
2184 type=
'SERIAL_CONTROL', blocking=
False, timeout=0)
2185 if m
is not None and m.count != 0:
2187 self.mav.mav.serial_control_send(self.
port,
2188 mavutil.mavlink.SERIAL_CONTROL_FLAG_EXCLUSIVE |
2189 mavutil.mavlink.SERIAL_CONTROL_FLAG_RESPOND,
2193 m = self.mav.recv_match(condition=
'SERIAL_CONTROL.count!=0',
2194 type=
'SERIAL_CONTROL', blocking=
True, timeout=0.01)
2195 if m
is not None and m.count != 0:
2200 data = m.data[:m.count]
2201 self.
buf +=
''.join(
str(chr(x))
for x
in data)
2204 '''read some bytes''' 2205 if len(self.
buf) == 0:
2207 if len(self.
buf) > 0:
2208 if n > len(self.
buf):
2214 self.
debug(
"read 0x%x" % ord(b), 2)
2219 '''flush any pending input''' 2226 self.
debug(
"flushInput")
2230 from .
import mavutil
2234 self.mav.mav.serial_control_send(self.
port,
2235 mavutil.mavlink.SERIAL_CONTROL_FLAG_EXCLUSIVE,
2242 if __name__ ==
'__main__':
2243 serial_list =
auto_detect_serial(preferred_list=[
'*FTDI*',
"*Arduino_Mega_2560*",
"*3D_Robotics*",
"*USB_to_UART*",
'*PX4*',
'*FMU*'])
2244 for port
in serial_list:
def debug(self, s, level=1)
def auto_detect_serial_unix(preferred_list=['*'])
def __init__(self, filename, progress_callback=None)
def waypoint_count_send(self, seq)
def __init__(self, device, autoreconnect=False, source_system=255, source_component=0, retries=3, use_native=default_native)
def auto_detect_serial_win32(preferred_list=['*'])
def motors_armed_wait(self)
def param_fetch_one(self, name)
def waypoint_current(self)
def auto_mavlink_version(self, buf)
def setup_signing(self, secret_key, sign_outgoing=True, allow_unsigned_callback=None, initial_timestamp=None, link_id=None)
def __init__(self, lat, lng, alt=0, heading=0)
def set_baudrate(self, baudrate)
def post_message(self, msg)
def __init__(self, fd, address, source_system=255, source_component=0, notimestamps=False, input=True, use_native=default_native)
def accumulate(self, buf)
def param_fetch_all(self)
def set_rtscts(self, enable)
def time_since(self, mtype)
def mavgen_python_dialect(dialect, wire_protocol)
def flightmode_list(self)
def auto_detect_serial(preferred_list=['*'])
def mode_mapping_byname(mav_type)
def motors_disarmed_wait(self)
def location(self, relative_alt=False)
def setup_logfile_raw(self, logfile, mode='w')
def setup_logfile(self, logfile, mode='w')
def calibrate_pressure(self)
def __init__(self, device, input=True, broadcast=False, source_system=255, source_component=0, use_native=default_native)
def mode_mapping_bynumber(mav_type)
def set_mode_manual(self)
def check_condition(self, condition)
def waypoint_request_list_send(self)
def post_message(self, msg)
def probably_vehicle_heartbeat(self, msg)
def skip_to_type(self, type)
def interpret_px4_mode(base_mode, custom_mode)
def waypoint_clear_all_send(self)
def mavlink_connection(device, baud=115200, source_system=255, source_component=0, planner_format=None, write=False, append=False, robust_parsing=True, notimestamps=False, input=True, dialect=None, autoreconnect=False, zero_time_base=False, retries=3, use_native=default_native, force_connected=False, progress_callback=None)
def set_servo(self, channel, pwm)
def reboot_autopilot(self, hold_in_bootloader=False)
def mode_string_acm(mode_number)
def waypoint_set_current_send(self, seq)
def evaluate_expression(expression, vars)
def calibrate_level(self)
def __init__(self, portname, baudrate, devnum=0, devbaud=0, timeout=3, debug=0)
def disable_signing(self)
def waypoint_request_send(self, seq)
def __init__(self, filename, source_system=255, source_component=0, use_native=default_native)
def __init__(self, device, baud=115200, autoreconnect=False, source_system=255, source_component=0, use_native=default_native, force_connected=False)
def set_mode(self, mode, custom_mode=0, custom_sub_mode=0)
def __init__(self, filename, planner_format=None, write=False, append=False, robust_parsing=True, notimestamps=False, source_system=255, source_component=0, use_native=default_native)
def __init__(self, device, source_system=255, source_component=0, retries=3, use_native=default_native)
def select(self, timeout)
def recv_match(self, condition=None, type=None, blocking=False, timeout=None)
def field(self, type, field, default=None)
def set_mode_px4(self, mode, custom_mode, custom_sub_mode)
def __init__(self, buf='')
def init_arrays(self, progress_callback=None)
def recv_match(self, condition=None, type=None, blocking=False, timeout=None)
def set_relay(self, relay_pin=0, state=True)
resolved_destination_addr
def scan_timestamp(self, tbuf)
def mode_string_apm(mode_number)
def param_set_send(self, parm_name, parm_value, parm_type=None)
def set_mode_loiter(self)
def set_close_on_exec(fd)
def __init__(self, frequency)
def evaluate_condition(condition, vars)
def set_rtscts(self, enable)
def __init__(self, device, broadcast=False, source_system=255, source_component=0, use_native=default_native)
def set_mode_apm(self, mode, custom_mode=0, custom_sub_mode=0)
def param(self, name, default=None)
def setBaudrate(self, baudrate)
def __init__(self, device, description=None, hwid=None)
def wait_heartbeat(self, blocking=True, timeout=None)
def arducopter_disarm(self)
def set_mode_flag(self, flag, enable)
def mode_string_px4(MainState)