【问题标题】:Get ardupilot output channels获取ardupilot输出通道
【发布时间】:2018-11-14 08:57:31
【问题描述】:

我们如何从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,但事实并非如此。

【问题讨论】:

    标签: dronekit-python


    【解决方案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.channelsvehicle.mode

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

    【讨论】:

    • 每次更改时都会打印是什么意思,我不必调用它吗?我尝试使用sitl 模拟它并随机更改channel.overrides['3'],这将改变输出通道,对吧?该函数是否应该自动打印?
    • 它将在每次ch1out 值更改时运行print '%s attribute is: %s' % (name, msg)。是的,如果您随机更改通道 3(油门)值,因为 ch1out - ch4out 是电机 pwm 输出。 (对于四轴飞行器)。
    • 我将代码放在问题中。你能查一下吗?
    【解决方案2】:

    这样做的方法是创建一个新的车辆类。您可以按照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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多