Get ardupilot output channels

1k views Asked by At

How do we get output channel values of an ardupilot from dronekit-python?

We can get input channels from vehicle.channels but I couldn't find anything similar for output channels.

EDIT:

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()

It should print ch1out everytime I update channel 3 but it is not.

3

There are 3 answers

3
Baskara On

I assume what you mean output channels is this 'ch1out','ch2out' value and so on.

Mission Planner Status Message

To get that value you can simply just using an attribute listener like this

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

This function essentially just print the 'ch1out' value everytime it changes, you can just modify it accordingly. You can read more about this at here Observing attribute changes.

But if you want to access the output channel value directly from the vehicle object like the input channel or other attribute.

(Ex: vehicle.channels, vehicle.mode)

You can add the output channel to the vehicle object by following this example provided by the Dronekit-Python documentation Create Attribute in App.

0
Aron Kisdi On

The way to do this is to create a new vehicle class. You can do this following the example given in dronekit.

Then connect using your new class:

     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)

Here is the class for the above example:

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
0
Melon Streams On

This is the solution if you are using PixHawk and Raspberry Pi connected through UART Issue source. You can configure this one using Mission Planner. Output Channels read to none when both Serial1 and Serial2 Protocols are set to 2. I set both protocols to 1 and I set my Serial 1 baud to 57 and Serial 2 baud to 921. Had the UART connected through Telem 2.

Hope it helps!