ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How do I implement a Low Pass filter to reduce the noise coming from a topic that is publishing a WrenchStamped msg type using a Python script?

asked 2019-01-15 17:02:18 -0500

lucasg gravatar image

updated 2019-01-16 03:28:09 -0500

gvdhoorn gravatar image

I was able to record the WrenchStamped data from the topic to a .bag file, export that data to a .csv file, and finally wrote a python script that uses scipy.signal.butter() from here along with scipy.signal.lfilter() from here to "post-process" the data and it worked fine (see image below).

image description

The problem is that when I go to actually implement it in my source code (subscriber node) and run it with some hardware, I get the following error message:

Traceback (most recent call last):
  File "extract_pandas1.py", line 36, in <module>
    output_fx = signal.lfilter(b, a, fx,axis=1) #Forward filter
  File "/usr/lib/python2.7/dist-packages/scipy/signal/signaltools.py", line 974, in lfilter
    return sigtools._linear_filter(b, a, x, axis)
ValueError: selected axis is out of range

Does anyone know what I may be doing wrong? Do you have any suggestions for another option for filtering?

Thanks in advance!! -Cheers

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-02-21 16:26:05 -0500

lucasg gravatar image

I figured out the problem. The linear filter that I used lfilter accepts "Array-Like" objects therefore I appended the incoming signals to empty arrays within the callback function as follows:

import rospy
from scipy import signal
from geometry_msgs.msg import WrenchStamped

def callback(data):

time = data.header.stamp.secs  +  data.header.stamp.nsecs * 1e-9

fx = data.wrench.force.x
fy = data.wrench.force.y
fz = data.wrench.force.z
tx = data.wrench.torque.x
ty = data.wrench.torque.y
tz = data.wrench.torque.z

FX = []
FY = []
FZ = []

TX = []
TY = []
TZ = []

FX.append(fx)
FY.append(fy)
FZ.append(fz)

TX.append(tx)
TY.append(ty)
TZ.append(tz)

fs = 124.956672444 #sampling frequency

fc = 55 # Cut-off frequency of the filter
w = fc / (fs / 2) # Normalize the frequency

b, a = signal.butter(5, w, 'low')


fx_low = signal.lfilter(b, a, FX) #Forward filter
fy_low = signal.lfilter(b, a, FY)
fz_low = signal.lfilter(b, a, FZ)

tx_low = signal.lfilter(b, a, TX)
ty_low = signal.lfilter(b, a, TY)
tz_low = signal.lfilter(b, a, TZ)

It may not be best practice but I hope it helps someone else!

edit flag offensive delete link more

Comments

Do you have a better method to collect the WrenchStamped data? I plan to extract the data force.z to optimise the control in Python. Thank you.

waltergun gravatar image waltergun  ( 2022-08-02 09:00:29 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-01-15 17:02:18 -0500

Seen: 4,685 times

Last updated: Feb 21 '19