DroneKit示例分析1---状态的获取与设置

来源:互联网 发布:小提琴音准软件 编辑:程序博客网 时间:2024/06/07 17:50
  • 能获取大部分无人机的状态信息,但只有以下几个可以设置
Vehicle.home_location,Vehicle.gimbal Vehicle.airspeed, Vehicle.groundspeed, Vehicle.mode Vehicle.armed
  • 以下代码涵盖了大部分的无人机状态获取(通过MavLink)
 # vehicle is an instance of the Vehicle classprint "Autopilot Firmware version: %s" % vehicle.versionprint "Autopilot capabilities (supports ftp): %s" % vehicle.capabilities.ftpprint "Global Location: %s" % vehicle.location.global_frameprint "Global Location (relative altitude): %s" % vehicle.location.global_relative_frameprint "Local Location: %s" % vehicle.location.local_frame    #NEDprint "Attitude: %s" % vehicle.attitudeprint "Velocity: %s" % vehicle.velocityprint "GPS: %s" % vehicle.gps_0print "Groundspeed: %s" % vehicle.groundspeedprint "Airspeed: %s" % vehicle.airspeedprint "Gimbal status: %s" % vehicle.gimbalprint "Battery: %s" % vehicle.batteryprint "EKF OK?: %s" % vehicle.ekf_okprint "Last Heartbeat: %s" % vehicle.last_heartbeatprint "Rangefinder: %s" % vehicle.rangefinderprint "Rangefinder distance: %s" % vehicle.rangefinder.distanceprint "Rangefinder voltage: %s" % vehicle.rangefinder.voltageprint "Heading: %s" % vehicle.headingprint "Is Armable?: %s" % vehicle.is_armableprint "System status: %s" % vehicle.system_status.stateprint "Mode: %s" % vehicle.mode.name    # settableprint "Armed: %s" % vehicle.armed    # settable
  • 以下四个属性可以直接设置
Vehicle.modeVehicle.armedVehicle.airspeedVehicle.groundspeed#disarm the vehiclevehicle.armed = False#set the default groundspeed to be used in movement commandsvehicle.groundspeed = 3.2
  • 我们无法确保这些属性值一定能成功修改,所以要加循环
vehicle.mode = VehicleMode("GUIDED")vehicle.armed = Truewhile not vehicle.mode.name=='GUIDED' and not vehicle.armed and not api.exit:    print " Getting ready to take off ..."    time.sleep(1)
  • 云台的设置比较特殊
#Point the gimbal straight downvehicle.gimbal.rotate(-90, 0, 0)time.sleep(10)#Set the camera to track the current home position.vehicle.gimbal.target_location(vehicle.home_location)time.sleep(10)
  • 监听属性值的改变,有两个方法, Vehicle.add_attribute_listener()Vehicle.on_attribute()
#Callback to print the location in global frames. 'value' is the updated value,self指所连接的无人机 def location_callback(self, attr_name, value):     print "Location (Global): ", value # Add a callback `location_callback` for the `global_frame` attribute.有两种方法可以添加 vehicle.add_attribute_listener('location.global_frame', location_callback) #第二种方法 vehicle.location.add_attribute_listener('global_frame', location_callback) # Wait 2s so callback can be notified before the observer is removed time.sleep(2) # Remove observer - specifying the attribute and previously registered callback function vehicle.remove_message_listener('location.global_frame', location_callback)
#如果我们只需要相对明显的状态变化,可以用这个,round(x,1)返回x的四舍五入值---保留一位小数。last_rangefinder_distance=0@vehicle.on_attribute('rangefinder') def rangefinder_callback(self,attr_name):     #attr_name not used here.     global last_rangefinder_distance     if last_rangefinder_distance == round(self.rangefinder.distance, 1):         return     last_rangefinder_distance = round(self.rangefinder.distance, 1)     print " Rangefinder (metres): %s" % last_rangefinder_distance
0 0