<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import monotonic <EOL> import time <EOL> import socket <EOL> import sys <EOL> import os <EOL> import platform <EOL> import re <EOL> from dronekit . util import errprinter <EOL> from pymavlink import mavutil , mavwp <EOL> from Queue import Queue , Empty <EOL> from threading import Thread <EOL> import types <EOL> import threading <EOL> import math <EOL> import copy <EOL> import collections <EOL> from pymavlink . dialects . v10 import ardupilotmega <EOL> class APIException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message ) : <EOL> super ( APIException , self ) . __init__ ( message ) <EOL> class Attitude ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pitch , yaw , roll ) : <EOL> self . pitch = pitch <EOL> self . yaw = yaw <EOL> self . roll = roll <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . pitch , self . yaw , self . roll ) <EOL> class LocationGlobal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lat , lon , alt = None ) : <EOL> self . lat = lat <EOL> self . lon = lon <EOL> self . alt = alt <EOL> self . local_frame = None <EOL> self . global_frame = None <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . lat , self . lon , self . alt ) <EOL> class LocationGlobalRelative ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lat , lon , alt = None ) : <EOL> self . lat = lat <EOL> self . lon = lon <EOL> self . alt = alt <EOL> self . local_frame = None <EOL> self . global_frame = None <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . lat , self . lon , self . alt ) <EOL> class LocationLocal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , north , east , down ) : <EOL> self . north = north <EOL> self . east = east <EOL> self . down = down <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . north , self . east , self . down ) <EOL> class GPSInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , eph , epv , fix_type , satellites_visible ) : <EOL> self . eph = eph <EOL> self . epv = epv <EOL> self . fix_type = fix_type <EOL> self . satellites_visible = satellites_visible <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . fix_type , self . satellites_visible ) <EOL> class Battery ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , voltage , current , level ) : <EOL> self . voltage = voltage / <NUM_LIT> <EOL> if current == - <NUM_LIT:1> : <EOL> self . current = None <EOL> else : <EOL> self . current = current / <NUM_LIT> <EOL> if level == - <NUM_LIT:1> : <EOL> self . level = None <EOL> else : <EOL> self . level = level <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . voltage , self . current , <EOL> self . level ) <EOL> class Rangefinder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , distance , voltage ) : <EOL> self . distance = distance <EOL> self . voltage = voltage <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . distance , self . voltage ) <EOL> class Version ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw_version , autopilot_type , vehicle_type ) : <EOL> self . autopilot_type = autopilot_type <EOL> self . vehicle_type = vehicle_type <EOL> self . raw_version = raw_version <EOL> if ( raw_version == None ) : <EOL> self . major = None <EOL> self . minor = None <EOL> self . patch = None <EOL> self . release = None <EOL> else : <EOL> self . major = raw_version >> <NUM_LIT> & <NUM_LIT> <EOL> self . minor = raw_version >> <NUM_LIT:16> & <NUM_LIT> <EOL> self . patch = raw_version >> <NUM_LIT:8> & <NUM_LIT> <EOL> self . release = raw_version & <NUM_LIT> <EOL> def is_stable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . release == <NUM_LIT:255> <EOL> def release_version ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . release is None : <EOL> return None <EOL> if ( self . release == <NUM_LIT:255> ) : <EOL> return <NUM_LIT:0> <EOL> return self . release % <NUM_LIT:64> <EOL> def release_type ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . release is None : <EOL> return None <EOL> types = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> return types [ self . release / <NUM_LIT:64> ] <EOL> def __str__ ( self ) : <EOL> prefix = "<STR_LIT>" <EOL> if ( self . autopilot_type == mavutil . mavlink . MAV_AUTOPILOT_ARDUPILOTMEGA ) : <EOL> prefix += "<STR_LIT>" <EOL> elif ( self . autopilot_type == mavutil . mavlink . MAV_AUTOPILOT_PX4 ) : <EOL> prefix += "<STR_LIT>" <EOL> else : <EOL> prefix += "<STR_LIT>" <EOL> if ( self . vehicle_type == mavutil . mavlink . MAV_TYPE_QUADROTOR ) : <EOL> prefix += "<STR_LIT>" <EOL> elif ( self . vehicle_type == mavutil . mavlink . MAV_TYPE_FIXED_WING ) : <EOL> prefix += "<STR_LIT>" <EOL> elif ( self . vehicle_type == mavutil . mavlink . MAV_TYPE_GROUND_ROVER ) : <EOL> prefix += "<STR_LIT>" <EOL> else : <EOL> prefix += "<STR_LIT>" % ( self . vehicle_type ) <EOL> if self . release_type ( ) is None : <EOL> release_type = "<STR_LIT>" <EOL> elif self . is_stable ( ) : <EOL> release_type = "<STR_LIT>" <EOL> else : <EOL> release_type = "<STR_LIT:->" + str ( self . release_type ( ) ) + str ( self . release_version ( ) ) <EOL> return prefix + "<STR_LIT>" % ( self . major , self . minor , self . patch ) + release_type <EOL> class Capabilities : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , capabilities ) : <EOL> self . mission_float = ( ( ( capabilities >> <NUM_LIT:0> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . param_float = ( ( ( capabilities >> <NUM_LIT:1> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . mission_int = ( ( ( capabilities >> <NUM_LIT:2> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . command_int = ( ( ( capabilities >> <NUM_LIT:3> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . param_union = ( ( ( capabilities >> <NUM_LIT:4> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . ftp = ( ( ( capabilities >> <NUM_LIT:5> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . set_attitude_target = ( ( ( capabilities >> <NUM_LIT:6> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . set_attitude_target_local_ned = ( ( ( capabilities >> <NUM_LIT:7> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . set_altitude_target_global_int = ( ( ( capabilities >> <NUM_LIT:8> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . terrain = ( ( ( capabilities >> <NUM_LIT:9> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . set_actuator_target = ( ( ( capabilities >> <NUM_LIT:10> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . flight_termination = ( ( ( capabilities >> <NUM_LIT:11> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . compass_calibration = ( ( ( capabilities >> <NUM_LIT:12> ) & <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> class VehicleMode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other <EOL> def __ne__ ( self , other ) : <EOL> return self . name != other <EOL> class SystemStatus ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state ) : <EOL> self . state = state <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . state <EOL> def __eq__ ( self , other ) : <EOL> return self . state == other <EOL> def __ne__ ( self , other ) : <EOL> return self . state != other <EOL> class HasObservers ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _attribute_listeners = { } <EOL> self . _attribute_cache = { } <EOL> def add_attribute_listener ( self , attr_name , observer ) : <EOL> """<STR_LIT>""" <EOL> l = self . _attribute_listeners . get ( attr_name ) <EOL> if l is None : <EOL> l = [ ] <EOL> self . _attribute_listeners [ attr_name ] = l <EOL> if not observer in l : <EOL> l . append ( observer ) <EOL> def remove_attribute_listener ( self , attr_name , observer ) : <EOL> """<STR_LIT>""" <EOL> l = self . _attribute_listeners . get ( attr_name ) <EOL> if l is not None : <EOL> l . remove ( observer ) <EOL> if len ( l ) == <NUM_LIT:0> : <EOL> del self . _attribute_listeners [ attr_name ] <EOL> def notify_attribute_listeners ( self , attr_name , value , cache = False ) : <EOL> """<STR_LIT>""" <EOL> if cache : <EOL> if attr_name in self . _attribute_cache and self . _attribute_cache [ attr_name ] == value : <EOL> return <EOL> self . _attribute_cache [ attr_name ] = value <EOL> for fn in self . _attribute_listeners . get ( attr_name , [ ] ) : <EOL> try : <EOL> fn ( self , attr_name , value ) <EOL> except Exception as e : <EOL> errprinter ( '<STR_LIT>' % <EOL> attr_name ) <EOL> errprinter ( '<STR_LIT>' + str ( e ) ) <EOL> for fn in self . _attribute_listeners . get ( '<STR_LIT:*>' , [ ] ) : <EOL> try : <EOL> fn ( self , attr_name , value ) <EOL> except Exception as e : <EOL> errprinter ( '<STR_LIT>' % <EOL> attr_name ) <EOL> errprinter ( '<STR_LIT>' + str ( e ) ) <EOL> def on_attribute ( self , name ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> if isinstance ( name , list ) : <EOL> for n in name : <EOL> self . add_attribute_listener ( n , fn ) <EOL> else : <EOL> self . add_attribute_listener ( name , fn ) <EOL> return decorator <EOL> class ChannelsOverride ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle ) : <EOL> self . _vehicle = vehicle <EOL> self . _count = <NUM_LIT:8> <EOL> self . _active = True <EOL> def __getitem__ ( self , key ) : <EOL> return dict . __getitem__ ( self , str ( key ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not ( int ( key ) > <NUM_LIT:0> and int ( key ) <= self . _count ) : <EOL> raise Exception ( '<STR_LIT>' % key ) <EOL> if not value : <EOL> try : <EOL> dict . __delitem__ ( self , str ( key ) ) <EOL> except : <EOL> pass <EOL> else : <EOL> dict . __setitem__ ( self , str ( key ) , value ) <EOL> self . _send ( ) <EOL> def __delitem__ ( self , key ) : <EOL> dict . __delitem__ ( self , str ( key ) ) <EOL> self . _send ( ) <EOL> def __len__ ( self ) : <EOL> return self . _count <EOL> def _send ( self ) : <EOL> if self . _active : <EOL> overrides = [ <NUM_LIT:0> ] * <NUM_LIT:8> <EOL> for k , v in self . iteritems ( ) : <EOL> overrides [ int ( k ) - <NUM_LIT:1> ] = v <EOL> self . _vehicle . _master . mav . rc_channels_override_send ( <NUM_LIT:0> , <NUM_LIT:0> , * overrides ) <EOL> class Channels ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle , count ) : <EOL> self . _vehicle = vehicle <EOL> self . _count = count <EOL> self . _overrides = ChannelsOverride ( vehicle ) <EOL> self . _readonly = False <EOL> for k in range ( <NUM_LIT:0> , count ) : <EOL> self [ k + <NUM_LIT:1> ] = None <EOL> self . _readonly = True <EOL> @ property <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _count <EOL> def __getitem__ ( self , key ) : <EOL> return dict . __getitem__ ( self , str ( key ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if self . _readonly : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return dict . __setitem__ ( self , str ( key ) , value ) <EOL> def __len__ ( self ) : <EOL> return self . _count <EOL> def _update_channel ( self , channel , value ) : <EOL> channel = int ( channel ) <EOL> self . _readonly = False <EOL> self [ channel ] = value <EOL> self . _readonly = True <EOL> self . _count = max ( self . _count , channel ) <EOL> @ property <EOL> def overrides ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _overrides <EOL> @ overrides . setter <EOL> def overrides ( self , newch ) : <EOL> self . _overrides . _active = False <EOL> self . _overrides . clear ( ) <EOL> for k , v in newch . iteritems ( ) : <EOL> if v : <EOL> self . _overrides [ str ( k ) ] = v <EOL> else : <EOL> try : <EOL> del self . _overrides [ str ( k ) ] <EOL> except : <EOL> pass <EOL> self . _overrides . _active = True <EOL> self . _overrides . _send ( ) <EOL> class Locations ( HasObservers ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle ) : <EOL> super ( Locations , self ) . __init__ ( ) <EOL> self . _lat = None <EOL> self . _lon = None <EOL> self . _alt = None <EOL> self . _relative_alt = None <EOL> @ vehicle . on_message ( '<STR_LIT>' ) <EOL> def listener ( vehicle , name , m ) : <EOL> ( self . _lat , self . _lon ) = ( m . lat / <NUM_LIT> , m . lon / <NUM_LIT> ) <EOL> self . _relative_alt = m . relative_alt / <NUM_LIT> <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . global_relative_frame ) <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT>' , <EOL> vehicle . location . global_relative_frame ) <EOL> if self . _alt != None or m . alt != <NUM_LIT:0> : <EOL> self . _alt = m . alt / <NUM_LIT> <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . global_frame ) <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT>' , <EOL> vehicle . location . global_frame ) <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT:location>' , vehicle . location ) <EOL> self . _north = None <EOL> self . _east = None <EOL> self . _down = None <EOL> @ vehicle . on_message ( '<STR_LIT>' ) <EOL> def listener ( vehicle , name , m ) : <EOL> self . _north = m . x <EOL> self . _east = m . y <EOL> self . _down = m . z <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . local_frame ) <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT>' , vehicle . location . local_frame ) <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT:location>' , vehicle . location ) <EOL> @ property <EOL> def local_frame ( self ) : <EOL> """<STR_LIT>""" <EOL> return LocationLocal ( self . _north , self . _east , self . _down ) <EOL> @ property <EOL> def global_frame ( self ) : <EOL> """<STR_LIT>""" <EOL> return LocationGlobal ( self . _lat , self . _lon , self . _alt ) <EOL> @ property <EOL> def global_relative_frame ( self ) : <EOL> """<STR_LIT>""" <EOL> return LocationGlobalRelative ( self . _lat , self . _lon , self . _relative_alt ) <EOL> class Vehicle ( HasObservers ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , handler ) : <EOL> super ( Vehicle , self ) . __init__ ( ) <EOL> self . _handler = handler <EOL> self . _master = handler . master <EOL> self . _ready_attrs = set ( [ '<STR_LIT>' ] ) <EOL> self . _default_ready_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ self . on_attribute ( '<STR_LIT:*>' ) <EOL> def listener ( _ , name , value ) : <EOL> self . _ready_attrs . add ( name ) <EOL> self . _message_listeners = dict ( ) <EOL> @ handler . forward_message <EOL> def listener ( _ , msg ) : <EOL> self . notify_message_listeners ( msg . get_type ( ) , msg ) <EOL> self . _location = Locations ( self ) <EOL> self . _vx = None <EOL> self . _vy = None <EOL> self . _vz = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> ( self . _vx , self . _vy , self . _vz ) = ( m . vx / <NUM_LIT> , m . vy / <NUM_LIT> , m . vz / <NUM_LIT> ) <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . velocity ) <EOL> self . _pitch = None <EOL> self . _yaw = None <EOL> self . _roll = None <EOL> self . _pitchspeed = None <EOL> self . _yawspeed = None <EOL> self . _rollspeed = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _pitch = m . pitch <EOL> self . _yaw = m . yaw <EOL> self . _roll = m . roll <EOL> self . _pitchspeed = m . pitchspeed <EOL> self . _yawspeed = m . yawspeed <EOL> self . _rollspeed = m . rollspeed <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . attitude ) <EOL> self . _heading = None <EOL> self . _airspeed = None <EOL> self . _groundspeed = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _heading = m . heading <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . heading ) <EOL> self . _airspeed = m . airspeed <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . airspeed ) <EOL> self . _groundspeed = m . groundspeed <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . groundspeed ) <EOL> self . _rngfnd_distance = None <EOL> self . _rngfnd_voltage = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _rngfnd_distance = m . distance <EOL> self . _rngfnd_voltage = m . voltage <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . rangefinder ) <EOL> self . _mount_pitch = None <EOL> self . _mount_yaw = None <EOL> self . _mount_roll = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _mount_pitch = m . pointing_a / <NUM_LIT> <EOL> self . _mount_roll = m . pointing_b / <NUM_LIT> <EOL> self . _mount_yaw = m . pointing_c / <NUM_LIT> <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . mount_status ) <EOL> self . _capabilities = None <EOL> self . _raw_version = None <EOL> self . _autopilot_version_msg_count = <NUM_LIT:0> <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( vehicle , name , m ) : <EOL> self . _capabilities = m . capabilities <EOL> self . _raw_version = m . flight_sw_version <EOL> self . _autopilot_version_msg_count += <NUM_LIT:1> <EOL> if self . _capabilities != <NUM_LIT:0> or self . _autopilot_version_msg_count > <NUM_LIT:5> : <EOL> vehicle . remove_message_listener ( '<STR_LIT>' , self . send_capabilties_request ) <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . _raw_version ) <EOL> self . _gimbal = Gimbal ( self ) <EOL> self . _channels = Channels ( self , <NUM_LIT:8> ) <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> def set_rc ( chnum , v ) : <EOL> '''<STR_LIT>''' <EOL> self . _channels . _update_channel ( str ( m . port * <NUM_LIT:8> + chnum ) , v ) <EOL> set_rc ( <NUM_LIT:1> , m . chan1_raw ) <EOL> set_rc ( <NUM_LIT:2> , m . chan2_raw ) <EOL> set_rc ( <NUM_LIT:3> , m . chan3_raw ) <EOL> set_rc ( <NUM_LIT:4> , m . chan4_raw ) <EOL> set_rc ( <NUM_LIT:5> , m . chan5_raw ) <EOL> set_rc ( <NUM_LIT:6> , m . chan6_raw ) <EOL> set_rc ( <NUM_LIT:7> , m . chan7_raw ) <EOL> set_rc ( <NUM_LIT:8> , m . chan8_raw ) <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . channels ) <EOL> self . _voltage = None <EOL> self . _current = None <EOL> self . _level = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _voltage = m . voltage_battery <EOL> self . _current = m . current_battery <EOL> self . _level = m . battery_remaining <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . battery ) <EOL> self . _eph = None <EOL> self . _epv = None <EOL> self . _satellites_visible = None <EOL> self . _fix_type = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _eph = m . eph <EOL> self . _epv = m . epv <EOL> self . _satellites_visible = m . satellites_visible <EOL> self . _fix_type = m . fix_type <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . gps_0 ) <EOL> self . _current_waypoint = <NUM_LIT:0> <EOL> @ self . on_message ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def listener ( self , name , m ) : <EOL> self . _current_waypoint = m . seq <EOL> self . _ekf_poshorizabs = False <EOL> self . _ekf_constposmode = False <EOL> self . _ekf_predposhorizabs = False <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _ekf_poshorizabs = ( m . flags & ardupilotmega . EKF_POS_HORIZ_ABS ) > <NUM_LIT:0> <EOL> self . _ekf_constposmode = ( m . flags & ardupilotmega . EKF_CONST_POS_MODE ) > <NUM_LIT:0> <EOL> self . _ekf_predposhorizabs = ( m . flags & ardupilotmega . EKF_PRED_POS_HORIZ_ABS ) > <NUM_LIT:0> <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . ekf_ok , cache = True ) <EOL> self . _flightmode = '<STR_LIT>' <EOL> self . _armed = False <EOL> self . _system_status = None <EOL> self . _autopilot_type = None <EOL> self . _vehicle_type = None <EOL> @ self . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> self . _armed = ( m . base_mode & mavutil . mavlink . MAV_MODE_FLAG_SAFETY_ARMED ) != <NUM_LIT:0> <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . armed , cache = True ) <EOL> self . _autopilot_type = m . autopilot <EOL> self . _vehicle_type = m . type <EOL> if self . _is_mode_available ( m . custom_mode ) == False : <EOL> raise APIException ( "<STR_LIT>" % m . custom_mode ) <EOL> self . _flightmode = self . _mode_mapping_bynumber [ m . custom_mode ] <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . mode , cache = True ) <EOL> self . _system_status = m . system_status <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . system_status , cache = True ) <EOL> self . _home_location = None <EOL> self . _wploader = mavwp . MAVWPLoader ( ) <EOL> self . _wp_loaded = True <EOL> self . _wp_uploaded = None <EOL> self . _wpts_dirty = False <EOL> self . _commands = CommandSequence ( self ) <EOL> @ self . on_message ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def listener ( self , name , msg ) : <EOL> if not self . _wp_loaded : <EOL> self . _wploader . clear ( ) <EOL> self . _wploader . expected_count = msg . count <EOL> self . _master . waypoint_request_send ( <NUM_LIT:0> ) <EOL> @ self . on_message ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def listener ( self , name , msg ) : <EOL> if not self . _wp_loaded : <EOL> if msg . seq == <NUM_LIT:0> : <EOL> if not ( msg . x == <NUM_LIT:0> and msg . y == <NUM_LIT:0> and msg . z == <NUM_LIT:0> ) : <EOL> self . _home_location = LocationGlobal ( msg . x , msg . y , msg . z ) <EOL> if msg . seq > self . _wploader . count ( ) : <EOL> pass <EOL> elif msg . seq < self . _wploader . count ( ) : <EOL> pass <EOL> else : <EOL> self . _wploader . add ( msg ) <EOL> if msg . seq + <NUM_LIT:1> < self . _wploader . expected_count : <EOL> self . _master . waypoint_request_send ( msg . seq + <NUM_LIT:1> ) <EOL> else : <EOL> self . _wp_loaded = True <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . commands ) <EOL> @ self . on_message ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def listener ( self , name , msg ) : <EOL> if self . _wp_uploaded != None : <EOL> wp = self . _wploader . wp ( msg . seq ) <EOL> handler . fix_targets ( wp ) <EOL> self . _master . mav . send ( wp ) <EOL> self . _wp_uploaded [ msg . seq ] = True <EOL> start_duration = <NUM_LIT> <EOL> repeat_duration = <NUM_LIT:1> <EOL> self . _params_count = - <NUM_LIT:1> <EOL> self . _params_set = [ ] <EOL> self . _params_loaded = False <EOL> self . _params_start = False <EOL> self . _params_map = { } <EOL> self . _params_last = monotonic . monotonic ( ) <EOL> self . _params_duration = start_duration <EOL> self . _parameters = Parameters ( self ) <EOL> @ handler . forward_loop <EOL> def listener ( _ ) : <EOL> if not self . _params_start : <EOL> return <EOL> if None not in self . _params_set and not self . _params_loaded : <EOL> self . _params_loaded = True <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . parameters ) <EOL> if not self . _params_loaded and monotonic . monotonic ( ) - self . _params_last > self . _params_duration : <EOL> c = <NUM_LIT:0> <EOL> for i , v in enumerate ( self . _params_set ) : <EOL> if v == None : <EOL> self . _master . mav . param_request_read_send ( <NUM_LIT:0> , <NUM_LIT:0> , '<STR_LIT>' , i ) <EOL> c += <NUM_LIT:1> <EOL> if c > <NUM_LIT:50> : <EOL> break <EOL> self . _params_duration = repeat_duration <EOL> self . _params_last = monotonic . monotonic ( ) <EOL> @ self . on_message ( [ '<STR_LIT>' ] ) <EOL> def listener ( self , name , msg ) : <EOL> if self . _params_count != msg . param_count : <EOL> self . _params_loaded = False <EOL> self . _params_start = True <EOL> self . _params_count = msg . param_count <EOL> self . _params_set = [ None ] * msg . param_count <EOL> try : <EOL> if msg . param_index < msg . param_count and msg : <EOL> if self . _params_set [ msg . param_index ] == None : <EOL> self . _params_last = monotonic . monotonic ( ) <EOL> self . _params_duration = start_duration <EOL> self . _params_set [ msg . param_index ] = msg <EOL> self . _params_map [ msg . param_id ] = msg . param_value <EOL> self . _parameters . notify_attribute_listeners ( msg . param_id , msg . param_value , <EOL> cache = True ) <EOL> except : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> self . _heartbeat_started = False <EOL> self . _heartbeat_lastsent = <NUM_LIT:0> <EOL> self . _heartbeat_lastreceived = <NUM_LIT:0> <EOL> self . _heartbeat_timeout = False <EOL> self . _heartbeat_warning = <NUM_LIT:5> <EOL> self . _heartbeat_error = <NUM_LIT:30> <EOL> self . _heartbeat_system = None <EOL> @ handler . forward_loop <EOL> def listener ( _ ) : <EOL> if monotonic . monotonic ( ) - self . _heartbeat_lastsent > <NUM_LIT:1> : <EOL> self . _master . mav . heartbeat_send ( mavutil . mavlink . MAV_TYPE_GCS , <EOL> mavutil . mavlink . MAV_AUTOPILOT_INVALID , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _heartbeat_lastsent = monotonic . monotonic ( ) <EOL> if self . _heartbeat_started : <EOL> if self . _heartbeat_error and self . _heartbeat_error > <NUM_LIT:0> and monotonic . monotonic ( <EOL> ) - self . _heartbeat_lastreceived > self . _heartbeat_error : <EOL> raise APIException ( '<STR_LIT>' % <EOL> self . _heartbeat_error ) <EOL> elif monotonic . monotonic ( ) - self . _heartbeat_lastreceived > self . _heartbeat_warning : <EOL> if self . _heartbeat_timeout == False : <EOL> errprinter ( '<STR_LIT>' % <EOL> self . _heartbeat_warning ) <EOL> self . _heartbeat_timeout = True <EOL> @ self . on_message ( [ '<STR_LIT>' ] ) <EOL> def listener ( self , name , msg ) : <EOL> self . _heartbeat_system = msg . get_srcSystem ( ) <EOL> self . _heartbeat_lastreceived = monotonic . monotonic ( ) <EOL> if self . _heartbeat_timeout : <EOL> errprinter ( '<STR_LIT>' ) <EOL> self . _heartbeat_timeout = False <EOL> self . _last_heartbeat = None <EOL> @ handler . forward_loop <EOL> def listener ( _ ) : <EOL> if self . _heartbeat_lastreceived : <EOL> self . _last_heartbeat = monotonic . monotonic ( ) - self . _heartbeat_lastreceived <EOL> self . notify_attribute_listeners ( '<STR_LIT>' , self . last_heartbeat ) <EOL> @ property <EOL> def last_heartbeat ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _last_heartbeat <EOL> def on_message ( self , name ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> if isinstance ( name , list ) : <EOL> for n in name : <EOL> self . add_message_listener ( n , fn ) <EOL> else : <EOL> self . add_message_listener ( name , fn ) <EOL> return decorator <EOL> def add_message_listener ( self , name , fn ) : <EOL> """<STR_LIT>""" <EOL> name = str ( name ) <EOL> if name not in self . _message_listeners : <EOL> self . _message_listeners [ name ] = [ ] <EOL> if fn not in self . _message_listeners [ name ] : <EOL> self . _message_listeners [ name ] . append ( fn ) <EOL> def remove_message_listener ( self , name , fn ) : <EOL> """<STR_LIT>""" <EOL> name = str ( name ) <EOL> if name in self . _message_listeners : <EOL> if fn in self . _message_listeners [ name ] : <EOL> self . _message_listeners [ name ] . remove ( fn ) <EOL> if len ( self . _message_listeners [ name ] ) == <NUM_LIT:0> : <EOL> del self . _message_listeners [ name ] <EOL> def notify_message_listeners ( self , name , msg ) : <EOL> for fn in self . _message_listeners . get ( name , [ ] ) : <EOL> try : <EOL> fn ( self , name , msg ) <EOL> except Exception as e : <EOL> errprinter ( '<STR_LIT>' % <EOL> msg . get_type ( ) ) <EOL> errprinter ( '<STR_LIT>' + str ( e ) ) <EOL> for fn in self . _message_listeners . get ( '<STR_LIT:*>' , [ ] ) : <EOL> try : <EOL> fn ( self , name , msg ) <EOL> except Exception as e : <EOL> errprinter ( '<STR_LIT>' % <EOL> msg . get_type ( ) ) <EOL> errprinter ( '<STR_LIT>' + str ( e ) ) <EOL> def close ( self ) : <EOL> return self . _handler . close ( ) <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . commands . upload ( ) <EOL> @ property <EOL> def _mode_mapping ( self ) : <EOL> return self . _master . mode_mapping ( ) <EOL> @ property <EOL> def _mode_mapping_bynumber ( self ) : <EOL> return mavutil . mode_mapping_bynumber ( self . _vehicle_type ) <EOL> def _is_mode_available ( self , mode_code ) : <EOL> try : <EOL> return mode_code in self . _mode_mapping_bynumber <EOL> except : <EOL> return False <EOL> @ property <EOL> def mode ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _flightmode : <EOL> return None <EOL> return VehicleMode ( self . _flightmode ) <EOL> @ mode . setter <EOL> def mode ( self , v ) : <EOL> self . _master . set_mode ( self . _mode_mapping [ v . name ] ) <EOL> @ property <EOL> def location ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _location <EOL> @ property <EOL> def battery ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _voltage == None or self . _current == None or self . _level == None : <EOL> return None <EOL> return Battery ( self . _voltage , self . _current , self . _level ) <EOL> @ property <EOL> def rangefinder ( self ) : <EOL> """<STR_LIT>""" <EOL> return Rangefinder ( self . _rngfnd_distance , self . _rngfnd_voltage ) <EOL> @ property <EOL> def velocity ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _vx , self . _vy , self . _vz ] <EOL> @ property <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> return Version ( self . _raw_version , self . _autopilot_type , self . _vehicle_type ) <EOL> @ property <EOL> def capabilities ( self ) : <EOL> """<STR_LIT>""" <EOL> return Capabilities ( self . _capabilities ) <EOL> @ property <EOL> def attitude ( self ) : <EOL> """<STR_LIT>""" <EOL> return Attitude ( self . _pitch , self . _yaw , self . _roll ) <EOL> @ property <EOL> def gps_0 ( self ) : <EOL> """<STR_LIT>""" <EOL> return GPSInfo ( self . _eph , self . _epv , self . _fix_type , self . _satellites_visible ) <EOL> @ property <EOL> def armed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _armed <EOL> @ armed . setter <EOL> def armed ( self , value ) : <EOL> if bool ( value ) != self . _armed : <EOL> if value : <EOL> self . _master . arducopter_arm ( ) <EOL> else : <EOL> self . _master . arducopter_disarm ( ) <EOL> @ property <EOL> def is_armable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mode != '<STR_LIT>' and self . gps_0 . fix_type > <NUM_LIT:1> and self . _ekf_predposhorizabs <EOL> @ property <EOL> def system_status ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> <NUM_LIT:0> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:1> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:2> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:3> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:4> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:5> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:6> : SystemStatus ( '<STR_LIT>' ) , <EOL> <NUM_LIT:7> : SystemStatus ( '<STR_LIT>' ) , <EOL> } . get ( self . _system_status , None ) <EOL> @ property <EOL> def heading ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _heading <EOL> @ property <EOL> def groundspeed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _groundspeed <EOL> @ groundspeed . setter <EOL> def groundspeed ( self , speed ) : <EOL> speed_type = <NUM_LIT:1> <EOL> msg = self . message_factory . command_long_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> mavutil . mavlink . MAV_CMD_DO_CHANGE_SPEED , <EOL> <NUM_LIT:0> , <EOL> speed_type , <EOL> speed , <EOL> - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> ) <EOL> self . send_mavlink ( msg ) <EOL> @ property <EOL> def airspeed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _airspeed <EOL> @ airspeed . setter <EOL> def airspeed ( self , speed ) : <EOL> speed_type = <NUM_LIT:0> <EOL> msg = self . message_factory . command_long_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> mavutil . mavlink . MAV_CMD_DO_CHANGE_SPEED , <EOL> <NUM_LIT:0> , <EOL> speed_type , <EOL> speed , <EOL> - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> ) <EOL> self . send_mavlink ( msg ) <EOL> @ property <EOL> def gimbal ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _gimbal <EOL> @ property <EOL> def mount_status ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _mount_pitch , self . _mount_yaw , self . _mount_roll ] <EOL> @ property <EOL> def ekf_ok ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . armed : <EOL> return self . _ekf_poshorizabs and not self . _ekf_constposmode <EOL> else : <EOL> return self . _ekf_poshorizabs or self . _ekf_predposhorizabs <EOL> @ property <EOL> def channels ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _channels <EOL> @ property <EOL> def home_location ( self ) : <EOL> """<STR_LIT>""" <EOL> return copy . copy ( self . _home_location ) <EOL> @ home_location . setter <EOL> def home_location ( self , pos ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pos , LocationGlobal ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . _home_location = copy . copy ( pos ) <EOL> self . send_mavlink ( self . message_factory . command_long_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> mavutil . mavlink . MAV_CMD_DO_SET_HOME , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:2> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> pos . lat , pos . lon , pos . alt ) ) <EOL> @ property <EOL> def commands ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _commands <EOL> @ property <EOL> def parameters ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parameters <EOL> def simple_takeoff ( self , alt = None ) : <EOL> """<STR_LIT>""" <EOL> if alt is not None : <EOL> altitude = float ( alt ) <EOL> if math . isnan ( alt ) or math . isinf ( alt ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _master . mav . command_long_send ( <NUM_LIT:0> , <NUM_LIT:0> , mavutil . mavlink . MAV_CMD_NAV_TAKEOFF , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , altitude ) <EOL> def simple_goto ( self , location , airspeed = None , groundspeed = None ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( location , LocationGlobalRelative ) : <EOL> frame = mavutil . mavlink . MAV_FRAME_GLOBAL_RELATIVE_ALT <EOL> alt = location . alt <EOL> elif isinstance ( location , LocationGlobal ) : <EOL> frame = mavutil . mavlink . MAV_FRAME_GLOBAL_RELATIVE_ALT <EOL> if not self . home_location : <EOL> self . commands . download ( ) <EOL> self . commands . wait_ready ( ) <EOL> alt = location . alt - self . home_location . alt <EOL> else : <EOL> raise APIException ( '<STR_LIT>' ) <EOL> self . _master . mav . mission_item_send ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , frame , <EOL> mavutil . mavlink . MAV_CMD_NAV_WAYPOINT , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , location . lat , location . lon , <EOL> alt ) <EOL> if airspeed != None : <EOL> self . airspeed = airspeed <EOL> if groundspeed != None : <EOL> self . groundspeed = groundspeed <EOL> def send_mavlink ( self , message ) : <EOL> """<STR_LIT>""" <EOL> self . _master . mav . send ( message ) <EOL> @ property <EOL> def message_factory ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _master . mav <EOL> def initialize ( self , rate = <NUM_LIT:4> , heartbeat_timeout = <NUM_LIT:30> ) : <EOL> self . _handler . start ( ) <EOL> start = monotonic . monotonic ( ) <EOL> self . _heartbeat_error = heartbeat_timeout or <NUM_LIT:0> <EOL> self . _heartbeat_started = True <EOL> self . _heartbeat_lastreceived = start <EOL> while self . _handler . _alive : <EOL> time . sleep ( <NUM_LIT> ) <EOL> if self . _heartbeat_lastreceived != start : <EOL> break <EOL> if not self . _handler . _alive : <EOL> raise APIException ( '<STR_LIT>' ) <EOL> self . _handler . target_system = self . _heartbeat_system <EOL> while True : <EOL> if self . _flightmode not in [ None , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> break <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if rate != None : <EOL> self . _master . mav . request_data_stream_send ( <NUM_LIT:0> , <NUM_LIT:0> , mavutil . mavlink . MAV_DATA_STREAM_ALL , <EOL> rate , <NUM_LIT:1> ) <EOL> self . add_message_listener ( '<STR_LIT>' , self . send_capabilties_request ) <EOL> while True : <EOL> self . _master . param_fetch_all ( ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if self . _params_count > - <NUM_LIT:1> : <EOL> break <EOL> def send_capabilties_request ( self , vehicle , name , m ) : <EOL> '''<STR_LIT>''' <EOL> capability_msg = vehicle . message_factory . command_long_encode ( <NUM_LIT:0> , <NUM_LIT:0> , mavutil . mavlink . MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> vehicle . send_mavlink ( capability_msg ) <EOL> def wait_ready ( self , * types , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> timeout = kwargs . get ( '<STR_LIT>' , <NUM_LIT:30> ) <EOL> raise_exception = kwargs . get ( '<STR_LIT>' , True ) <EOL> if list ( types ) == [ True ] or list ( types ) == [ ] : <EOL> types = self . _default_ready_attrs <EOL> if not all ( isinstance ( item , basestring ) for item in types ) : <EOL> raise APIException ( '<STR_LIT>' ) <EOL> await = set ( types ) <EOL> start = monotonic . monotonic ( ) <EOL> while not await . issubset ( self . _ready_attrs ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if monotonic . monotonic ( ) - start > timeout : <EOL> if raise_exception : <EOL> raise APIException ( '<STR_LIT>' % <EOL> timeout ) <EOL> else : <EOL> return False <EOL> return True <EOL> class Gimbal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle ) : <EOL> super ( Gimbal , self ) . __init__ ( ) <EOL> self . _pitch = None <EOL> self . _roll = None <EOL> self . _yaw = None <EOL> self . _vehicle = vehicle <EOL> @ vehicle . on_message ( '<STR_LIT>' ) <EOL> def listener ( vehicle , name , m ) : <EOL> self . _pitch = m . pointing_a / <NUM_LIT> <EOL> self . _roll = m . pointing_b / <NUM_LIT> <EOL> self . _yaw = m . pointing_c / <NUM_LIT> <EOL> vehicle . notify_attribute_listeners ( '<STR_LIT>' , vehicle . gimbal ) <EOL> @ property <EOL> def pitch ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pitch <EOL> @ property <EOL> def roll ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _roll <EOL> @ property <EOL> def yaw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _yaw <EOL> def rotate ( self , pitch , roll , yaw ) : <EOL> """<STR_LIT>""" <EOL> msg = self . _vehicle . message_factory . mount_configure_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> mavutil . mavlink . MAV_MOUNT_MODE_MAVLINK_TARGETING , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> ) <EOL> self . _vehicle . send_mavlink ( msg ) <EOL> msg = self . _vehicle . message_factory . mount_control_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> pitch * <NUM_LIT:100> , <EOL> roll * <NUM_LIT:100> , <EOL> yaw * <NUM_LIT:100> , <EOL> <NUM_LIT:0> ) <EOL> self . _vehicle . send_mavlink ( msg ) <EOL> def target_location ( self , roi ) : <EOL> """<STR_LIT>""" <EOL> msg = self . _vehicle . message_factory . mount_configure_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> mavutil . mavlink . MAV_MOUNT_MODE_GPS_POINT , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> ) <EOL> self . _vehicle . send_mavlink ( msg ) <EOL> if isinstance ( roi , LocationGlobalRelative ) : <EOL> alt = roi . alt <EOL> elif isinstance ( roi , LocationGlobal ) : <EOL> if not self . home_location : <EOL> self . commands . download ( ) <EOL> self . commands . wait_ready ( ) <EOL> alt = roi . alt - self . home_location . alt <EOL> else : <EOL> raise APIException ( '<STR_LIT>' ) <EOL> msg = self . _vehicle . message_factory . command_long_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> mavutil . mavlink . MAV_CMD_DO_SET_ROI , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> roi . lat , <EOL> roi . lon , <EOL> alt <EOL> ) <EOL> self . _vehicle . send_mavlink ( msg ) <EOL> def release ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = self . _vehicle . message_factory . mount_configure_encode ( <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> mavutil . mavlink . MAV_MOUNT_MODE_RC_TARGETING , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <EOL> ) <EOL> self . _vehicle . send_mavlink ( msg ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . pitch , self . roll , self . yaw ) <EOL> class Parameters ( collections . MutableMapping , HasObservers ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle ) : <EOL> super ( Parameters , self ) . __init__ ( ) <EOL> self . _vehicle = vehicle <EOL> def __getitem__ ( self , name ) : <EOL> name = name . upper ( ) <EOL> self . wait_ready ( ) <EOL> return self . _vehicle . _params_map [ name ] <EOL> def __setitem__ ( self , name , value ) : <EOL> name = name . upper ( ) <EOL> self . wait_ready ( ) <EOL> self . set ( name , value ) <EOL> def __delitem__ ( self , name ) : <EOL> raise APIException ( '<STR_LIT>' ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _vehicle . _params_map ) <EOL> def __iter__ ( self ) : <EOL> return self . _vehicle . _params_map . __iter__ ( ) <EOL> def get ( self , name , wait_ready = True ) : <EOL> name = name . upper ( ) <EOL> if wait_ready : <EOL> self . wait_ready ( ) <EOL> return self . _vehicle . _params_map . get ( name , None ) <EOL> def set ( self , name , value , retries = <NUM_LIT:3> , wait_ready = False ) : <EOL> if wait_ready : <EOL> self . wait_ready ( ) <EOL> name = name . upper ( ) <EOL> value = float ( value ) <EOL> success = False <EOL> remaining = retries <EOL> while True : <EOL> self . _vehicle . _master . param_set_send ( name , value ) <EOL> tstart = monotonic . monotonic ( ) <EOL> if remaining == <NUM_LIT:0> : <EOL> break <EOL> remaining -= <NUM_LIT:1> <EOL> while monotonic . monotonic ( ) - tstart < <NUM_LIT:1> : <EOL> if name in self . _vehicle . _params_map and self . _vehicle . _params_map [ name ] == value : <EOL> return True <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if retries > <NUM_LIT:0> : <EOL> errprinter ( "<STR_LIT>" % ( name , value ) ) <EOL> return False <EOL> def wait_ready ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _vehicle . wait_ready ( '<STR_LIT>' , ** kwargs ) <EOL> def add_attribute_listener ( self , attr_name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> attr_name = attr_name . upper ( ) <EOL> return super ( Parameters , self ) . add_attribute_listener ( attr_name , * args , ** kwargs ) <EOL> def remove_attribute_listener ( self , attr_name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> attr_name = attr_name . upper ( ) <EOL> return super ( Parameters , self ) . remove_attribute_listener ( attr_name , * args , ** kwargs ) <EOL> def notify_attribute_listeners ( self , attr_name , * args , ** kwargs ) : <EOL> attr_name = attr_name . upper ( ) <EOL> return super ( Parameters , self ) . notify_attribute_listeners ( attr_name , * args , ** kwargs ) <EOL> def on_attribute ( self , attr_name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> attr_name = attr_name . upper ( ) <EOL> return super ( Parameters , self ) . on_attribute ( attr_name , * args , ** kwargs ) <EOL> class Command ( mavutil . mavlink . MAVLink_mission_item_message ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CommandSequence ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vehicle ) : <EOL> self . _vehicle = vehicle <EOL> def download ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . wait_ready ( ) <EOL> self . _vehicle . _ready_attrs . remove ( '<STR_LIT>' ) <EOL> self . _vehicle . _wp_loaded = False <EOL> self . _vehicle . _master . waypoint_request_list_send ( ) <EOL> def wait_ready ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _vehicle . wait_ready ( '<STR_LIT>' , ** kwargs ) <EOL> def clear ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . wait_ready ( ) <EOL> home = None <EOL> try : <EOL> home = self . _vehicle . _wploader . wp ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> self . _vehicle . _wploader . clear ( ) <EOL> if home : <EOL> self . _vehicle . _wploader . add ( home , comment = '<STR_LIT>' ) <EOL> self . _vehicle . _wpts_dirty = True <EOL> def add ( self , cmd ) : <EOL> '''<STR_LIT>''' <EOL> self . wait_ready ( ) <EOL> self . _vehicle . _handler . fix_targets ( cmd ) <EOL> self . _vehicle . _wploader . add ( cmd , comment = '<STR_LIT>' ) <EOL> self . _vehicle . _wpts_dirty = True <EOL> def upload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _vehicle . _wpts_dirty : <EOL> self . _vehicle . _master . waypoint_clear_all_send ( ) <EOL> if self . _vehicle . _wploader . count ( ) > <NUM_LIT:0> : <EOL> self . _vehicle . _wp_uploaded = [ False ] * self . _vehicle . _wploader . count ( ) <EOL> self . _vehicle . _master . waypoint_count_send ( self . _vehicle . _wploader . count ( ) ) <EOL> while False in self . _vehicle . _wp_uploaded : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> self . _vehicle . _wp_uploaded = None <EOL> self . _vehicle . _wpts_dirty = False <EOL> @ property <EOL> def count ( self ) : <EOL> '''<STR_LIT>''' <EOL> return max ( self . _vehicle . _wploader . count ( ) - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> @ property <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vehicle . _current_waypoint <EOL> @ next . setter <EOL> def next ( self , index ) : <EOL> """<STR_LIT>""" <EOL> self . _vehicle . _master . waypoint_set_current_send ( index ) <EOL> def __len__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return max ( self . _vehicle . _wploader . count ( ) - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def __getitem__ ( self , index ) : <EOL> if isinstance ( index , slice ) : <EOL> return [ self [ ii ] for ii in xrange ( * index . indices ( len ( self ) ) ) ] <EOL> elif isinstance ( index , int ) : <EOL> item = self . _vehicle . _wploader . wp ( index + <NUM_LIT:1> ) <EOL> if not item : <EOL> raise IndexError ( '<STR_LIT>' % index ) <EOL> return item <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __setitem__ ( self , index , value ) : <EOL> self . _vehicle . _wploader . set ( value , index + <NUM_LIT:1> ) <EOL> self . _vehicle . _wpts_dirty = True <EOL> from dronekit . mavlink import MAVConnection <EOL> def connect ( ip , <EOL> _initialize = True , <EOL> wait_ready = None , <EOL> status_printer = errprinter , <EOL> vehicle_class = None , <EOL> rate = <NUM_LIT:4> , <EOL> baud = <NUM_LIT> , <EOL> heartbeat_timeout = <NUM_LIT:30> , <EOL> source_system = <NUM_LIT:255> , <EOL> use_native = False ) : <EOL> """<STR_LIT>""" <EOL> if not vehicle_class : <EOL> vehicle_class = Vehicle <EOL> handler = MAVConnection ( ip , baud = baud , source_system = source_system , use_native = use_native ) <EOL> vehicle = vehicle_class ( handler ) <EOL> if status_printer : <EOL> @ vehicle . on_message ( '<STR_LIT>' ) <EOL> def listener ( self , name , m ) : <EOL> status_printer ( re . sub ( r'<STR_LIT>' , '<STR_LIT>' , m . text . rstrip ( ) ) ) <EOL> if _initialize : <EOL> vehicle . initialize ( rate = rate , heartbeat_timeout = heartbeat_timeout ) <EOL> if wait_ready : <EOL> if wait_ready == True : <EOL> vehicle . wait_ready ( True ) <EOL> else : <EOL> vehicle . wait_ready ( * wait_ready ) <EOL> return vehicle </s>
