0

我们如何从dronekit-python 获取 ardupilot 的输出通道值?

我们可以从 vehicle.channels 获取输入通道,但我找不到任何类似的输出通道。

编辑:

from time import sleep
from dronekit import connect
import dronekit_sitl

sitl = dronekit_sitl.start_default()
connection_string = sitl.connection_string()

vehicle = connect(connection_string,wait_ready=True)

while not vehicle.is_armable:
    sleep(1)
    print"Initializing"

vehicle.armed = True
while not vehicle.armed:
    print "Arming"
    sleep(1)
print "Armed"

@vehicle.on_attribute('ch1out')
def ch1out_listener(self, name, msg):
    print '%s attribute is: %s' % (name, msg)

for i in range(1000,2000,100):
    vehicle.channels.overrides['3']=i
    sleep(1)

vehicle.close()

每次我更新频道 3 时它都应该打印,ch1out但事实并非如此。

4

2 回答 2

1

我假设你的意思是输出通道是这个'ch1out','ch2out'值等等。

任务规划器状态消息

要获得该值,您可以简单地使用像这样的属性侦听器

@vehicle.on_attribute('ch1out')
def ch1out_listener(self, name, msg):
    print '%s attribute is: %s' % (name, msg)

该函数本质上只是在每次更改时打印“ch1out”值,您可以相应地对其进行修改。您可以在此处阅读更多有关此内容的信息Observing attribute changes

但是,如果您想直接从车辆对象(如输入通道或其他属性)访问输出通道值。

(例如:vehicle.channels, vehicle.mode

您可以按照 Dronekit-Python 文档Create Attribute in App提供的示例将输出通道添加到车辆对象。

于 2018-11-15T03:31:16.123 回答
0

这样做的方法是创建一个新的车辆类。您可以按照dronekit 中给出的示例执行此操作。

然后使用您的新课程进行连接:

     vehicle = connect(connection_string, wait_ready=True,
     vehicle_class=MyVehicle) def raw_servo_callback(self, attr_name,
     value):
         print(value)
    vehicle.add_attribute_listener('raw_servo', raw_servo_callback)

这是上面示例的类:

from dronekit import Vehicle


class RawSERVO(object):
    """
    :param ch1out: servo1
    :param ch3out: servo3
    """
    def __init__(self, ch1out=None, ch3out=None):
        """
        RawIMU object constructor.
        """
        self.ch1out = ch1out
        self.ch3out = ch3out

    def __str__(self):
        """
        String representation used to print 
        """
        return "{},{}".format(self.ch1out, self.ch3out)


class MyVehicle(Vehicle):
    def __init__(self, *args):
        super(MyVehicle, self).__init__(*args)

        # Create an Vehicle.raw_servo object with initial values set to None.
        self._raw_servo = RawSERVO()

        # Create a message listener using the decorator.   
        @self.on_message('SERVO_OUTPUT_RAW')
        def listener(self, name, message):
            """
            The listener is called for messages that contain the string specified in the decorator,
            passing the vehicle, message name, and the message.
            
            The listener writes the message to the (newly attached) ``vehicle.raw_servo`` object 
            and notifies observers.
            """
            self._raw_servo.ch1out=message.servo1_raw
            self._raw_servo.ch3out=message.servo3_raw
            
            # Notify all observers of new message (with new value)
            #   Note that argument `cache=False` by default so listeners
            #   are updated with every new message
            self.notify_attribute_listeners('raw_servo', self._raw_servo) 

    @property
    def raw_servo(self):
        return self._raw_servo
于 2022-02-12T17:27:18.393 回答