版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xinyu3307/article/details/66473323
- 能获取大部分无人机的状态信息,但只有以下几个可以设置
Vehicle.home_location,
Vehicle.gimbal
Vehicle.airspeed,
Vehicle.groundspeed,
Vehicle.mode
Vehicle.armed
- 以下代码涵盖了大部分的无人机状态获取(通过MavLink)
# vehicle is an instance of the Vehicle class
print "Autopilot Firmware version: %s" % vehicle.version
print "Autopilot capabilities (supports ftp): %s" % vehicle.capabilities.ftp
print "Global Location: %s" % vehicle.location.global_frame
print "Global Location (relative altitude): %s" % vehicle.location.global_relative_frame
print "Local Location: %s" % vehicle.location.local_frame #NED
print "Attitude: %s" % vehicle.attitude
print "Velocity: %s" % vehicle.velocity
print "GPS: %s" % vehicle.gps_0
print "Groundspeed: %s" % vehicle.groundspeed
print "Airspeed: %s" % vehicle.airspeed
print "Gimbal status: %s" % vehicle.gimbal
print "Battery: %s" % vehicle.battery
print "EKF OK?: %s" % vehicle.ekf_ok
print "Last Heartbeat: %s" % vehicle.last_heartbeat
print "Rangefinder: %s" % vehicle.rangefinder
print "Rangefinder distance: %s" % vehicle.rangefinder.distance
print "Rangefinder voltage: %s" % vehicle.rangefinder.voltage
print "Heading: %s" % vehicle.heading
print "Is Armable?: %s" % vehicle.is_armable
print "System status: %s" % vehicle.system_status.state
print "Mode: %s" % vehicle.mode.name # settable
print "Armed: %s" % vehicle.armed # settable
- 以下四个属性可以直接设置
Vehicle.mode
Vehicle.armed
Vehicle.airspeed
Vehicle.groundspeed
#disarm the vehicle
vehicle.armed = False
#set the default groundspeed to be used in movement commands
vehicle.groundspeed = 3.2
- 我们无法确保这些属性值一定能成功修改,所以要加循环
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
while 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 down
vehicle.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