Dronekit Python, Scaled_IMU2 attribute listner

I got the RAW_IMU reading from dronekit python attribute listner.

now i need Scaled IMU2 reading.

I followed the same method and tried to read the scaled_imu call back. but after connecting with the vehicle, nothing is printing.

I’ll attach my MyVehicle and Create_attribut file programs below.

Kindly correct it.

create_attribute.txt (1.1 KB)
my_vehicle.txt (1.8 KB)

my_vechcle.py

from dronekit import Vehicle

class ScaledIMU2(object):

def __init__(self, time_boot_us=None, xacc=None, yacc=None, zacc=None, xygro=None, ygyro=None, zgyro=None, xmag=None, ymag=None, zmag=None):
 
    self.time_boot_us = time_boot_us
    self.xacc = xacc
    self.yacc = yacc
    self.zacc = zacc
    self.xgyro = zgyro
    self.ygyro = ygyro
    self.zgyro = zgyro
    self.xmag = xmag        
    self.ymag = ymag
    self.zmag = zmag      
    
def __str__(self):
 
    return "Scaled_IMU2: time_boot_us={},xacc={},yacc={},zacc={},xgyro={},ygyro={},zgyro={},xmag={},ymag={},zmag={}".format(self.time_boot_us, self.xacc, self.yacc,self.zacc,self.xgyro,self.ygyro,self.zgyro,self.xmag,self.ymag,self.zmag)

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

    # Create an Vehicle.scaled_imu2 object with initial values set to None.
    self._scaled_imu2 = ScaledIMU2()

    # Create a message listener using the decorator.   
    @self.on_message('Scaled_IMU2')
    def listener(self, name, message):

        self._scaled_imu2.time_boot_us=message.time_usec
        self._scaled_imu2.xacc=message.xacc
        self._scaled_imu2.yacc=message.yacc
        self._scaled_imu2.zacc=message.zacc
        self._scaled_imu2.xgyro=message.xgyro
        self._scaled_imu2.ygyro=message.ygyro
        self._scaled_imu2.zgyro=message.zgyro
        self._scaled_imu2.xmag=message.xmag
        self._scaled_imu2.ymag=message.ymag
        self._scaled_imu2.zmag=message.zmag
        
        self.notify_attribute_listeners('scaled_imu2', self._scaled_imu2) 

@property
def scaled_imu2(self):
    return self._scaled_imu2

create_attribute

from future import print_function

from dronekit import connect, Vehicle
from my_vehicle import MyVehicle
import time

Connect to the Vehicle.

print("\nConnecting to vehicle")
vehicle = connect(’/dev/ttyAMA0’, wait_ready=True, baud = 57600)
vehicle.wait_ready(‘autopilot_version’)

Set up option parsing to get connection string

import argparse
parser = argparse.ArgumentParser(description=‘Creates a CherryPy based web application that displays a mapbox map to let you view the current vehicle position and send the vehicle commands to fly to a particular latitude and longitude. Will start and connect to SITL if no connection string specified.’)
parser.add_argument(’–connect’,
help=“vehicle connection target string. If not specified, SITL is automatically started and used.”)
args = parser.parse_args()

connection_string = args.connect

Add observer for the custom attribute

def scaled_imu2_callback(self, attr_name, value):
# attr_name == ‘scaled_imu2’
# value == vehicle.scaled_imu2
print(value)

vehicle.add_attribute_listener(‘scaled_imu2’, scaled_imu2_callback)

time.sleep(5)