i dont really know how to solve this Problem because i think i need the filter command but i get this as output : Attribute Error "filter" has no Attribute A

asked 2021-06-16 03:56:31 -0500

Fxaxo gravatar image

i try to build an Kalman Filter for my UM7 Sensor. First to read out a accurate Value from the pitch when i move it and later i want to build it on a Robot. I found an Open Source File and Tried to moderate it for my case but i get a few errors. My first Error to solve is :AttributeError "filter" has no Attribute A... hope anyone can help The Commands i commented out are some Commands i used for my IMU but now for the Kalman Filter i cant use IMU anymore i think

!/usr/bin/env python3

import rospy # Library to use Python import roslib # Library to use rostools from tf.msg import tfMessage # Transformer Library Algebra Bib from std_msgs.msg import Float32, Header # from um7py import UM7Serial # from dataclasses import dataclass # from nav_msgs.msg import Odometry # from geometry_msgs.msg import Point, Pose, Quaternion, Vector3 from sensor_msgs.msg import Imu import numpy as np from numpy.linalg import inv import matplotlib.pyplot as plt

######################################## Funktion to read

def angles_read_pitch(): um7_serial = UM7Serial(port_name='/dev/ttyUSB0')

print ( "  ")
print ( "Port Name: ttyUSB0")
print ( "  ")

a = [0,1,2,3,4,5,6,7,8,9,10]
i = 1

while i < 2:
    for packet in um7_serial.recv_broadcast(num_packets = 10):

        a[i-1] = packet.pitch
        print( "at the Pos = ",i , " is pitch = ", a[i-1] )
        i = i+1
        rospy.sleep(0.1)

print ( "  ")
Diff = a[10] - a[1] 

print ( "The Diff between prev Pos pitch and current Pos pitch is : ", Diff )
##################################### Kalman Filter
##################################### https://thekalmanfilter.com/kalman-fi....
#

def Kalman_Filter_2(z, updateNumber): # Z = position measurment # updateNumber = dt = 0.1 # delta Time 0,1 sec

    # Initialize State

if updateNumber == 1:
    filter.x = np.array[[0],    # x = two element state vect. pos & veloc.
                        [20]]
    filter.P = np.array[[5, 0], # P = 2x2 state Covariance matrix, unc. in x 
                             [0, 5]]
    filter.A = np.array[[1, dt],    # A = state trans. matrix transposed -> .. 
                         [0, 1]]        # .. -> matrix constant linear motion
    filter.H = np.array[1, 0]   # state to measurement transition matrix
    filter.HT = np.array[[1],   # is the H matrix transposed
                          [0]]
    filter.R = 10           # R = position variance
    filter.Q = np.array[[1, 0], # Q = 2x2 szstem noise covariance matrix
                         [0, 3]]        # accounts for inaccuracy in system model

# Predict State Forward x_p = filter.A.dot(filter.x)
# Predict Covariance Forward P_p = filter.A.dot(filter.P).dot(filter.A.T) + filter.Q # Compute Kalman Gain S = filter.H.dot(P_p).dot(filter.HT) + filter.R
print("S:",S) K = P_p.dot(filter.HT)(1/S) print("K:",K) # Estimate State residual = z - filter.H.dot(x_p) filter.x = x_p + Kresidual print("x:",x) # Estimate Covariance filter.P = P_p - K.dot(filter.H).dot(P_p) print("P:",P) return [filter.x[0], filter.x[1], filter.P];

def getMeasurement_2(updateNumber): if updateNumber == 1:

    #Fabio_Imu.currentPosition = 0      ## truth Data
    #Fabio_Imu.currentVelocity = 60 # m/s   ## truth Data

    currentPosition = 0 
    currentVelocity = 60

dt = 0.1
w = 8 * np.random.randn(1)          ## added to velocity for smll acceler
v = 8 ...
(more)
edit retag flag offensive close merge delete

Comments

The problem is with filter.A and has nothing to do with ROS. Where is filter being created? I'd recommend going through the original code you've copied this from and finding out whether whatever class filter is an object of has an attribute A.

abhishek47 gravatar image abhishek47  ( 2021-06-16 06:46:38 -0500 )edit