forked from cram2/pycram
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:cram2/pycram into demo-ci
- Loading branch information
Showing
6 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from abc import abstractmethod | ||
|
||
from scipy.signal import butter, lfilter | ||
|
||
|
||
class Filter: | ||
""" | ||
Abstract class to ensure that every supported filter needs to implement the filter method. | ||
:method filter: Abstract method to filter the given data. | ||
""" | ||
|
||
@abstractmethod | ||
def filter(self, data): | ||
pass | ||
|
||
|
||
class Butterworth(Filter): | ||
""" | ||
Implementation for a Butterworth filter. | ||
:param order: The order of the filter (default is 4). | ||
:param cutoff: The cutoff frequency of the filter (default is 10). | ||
:param fs: The sampling frequency of the data (default is 60). | ||
""" | ||
|
||
def __init__(self, order=4, cutoff=10, fs=60): | ||
self.order = order | ||
self.cutoff = cutoff | ||
self.fs = fs | ||
|
||
self.b, self.a = butter(self.order, cutoff / (0.5 * fs), btype='low') | ||
|
||
def filter(self, data: list): | ||
""" | ||
Filters the given data using a Butterworth filter. | ||
:param data: The data to be filtered. | ||
:return: The filtered data. | ||
""" | ||
return lfilter(self.b, self.a, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import unittest | ||
from pycram.ros.filter import Butterworth | ||
|
||
class TestButterworthFilter(unittest.TestCase): | ||
|
||
def test_initialization_with_default_values(self): | ||
filter = Butterworth() | ||
self.assertEqual(filter.order, 4) | ||
self.assertEqual(filter.cutoff, 10) | ||
self.assertEqual(filter.fs, 60) | ||
|
||
def test_initialization_with_custom_values(self): | ||
filter = Butterworth(order=2, cutoff=5, fs=30) | ||
self.assertEqual(filter.order, 2) | ||
self.assertEqual(filter.cutoff, 5) | ||
self.assertEqual(filter.fs, 30) | ||
|
||
def test_filter_data_with_default_values(self): | ||
filter = Butterworth() | ||
data = [1, 2, 3, 4, 5] | ||
filtered_data = filter.filter(data) | ||
self.assertEqual(len(filtered_data), len(data)) | ||
|
||
def test_filter_data_with_custom_values(self): | ||
filter = Butterworth(order=2, cutoff=5, fs=30) | ||
data = [1, 2, 3, 4, 5] | ||
filtered_data = filter.filter(data) | ||
self.assertEqual(len(filtered_data), len(data)) | ||
|
||
def test_filter_empty_data(self): | ||
filter = Butterworth() | ||
data = [] | ||
filtered_data = filter.filter(data) | ||
self.assertEqual(filtered_data.tolist(), data) | ||
|
||
def test_filter_single_value_data(self): | ||
filter = Butterworth() | ||
data = [1] | ||
filtered_data = filter.filter(data) | ||
expected_filtered_data = [0.026077721701092293] # The expected filtered value | ||
self.assertEqual(filtered_data.tolist(), expected_filtered_data) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from pycram.ros_utils.force_torque_sensor import ForceTorqueSensor, ForceTorqueSensorSimulated | ||
from pycram.datastructures.enums import FilterConfig | ||
from geometry_msgs.msg import WrenchStamped | ||
|
||
class ForceTorqueSensorTestCase(unittest.TestCase): | ||
|
||
@patch('pycram.ros_utils.force_torque_sensor.World') | ||
@patch('pycram.ros_utils.force_torque_sensor.create_publisher') | ||
def test_initialization_simulated_sensor(self, mock_create_publisher, mock_world): | ||
mock_world.current_world.robot.joint_name_to_id = {'joint1': 1} | ||
sensor = ForceTorqueSensorSimulated('joint1') | ||
self.assertEqual('joint1', 'joint1') | ||
self.assertIsNotNone(sensor.fts_pub) | ||
sensor._stop_publishing() | ||
|
||
@patch('pycram.ros_utils.force_torque_sensor.World') | ||
@patch('pycram.ros_utils.force_torque_sensor.create_publisher') | ||
def test_initialization_simulated_sensor_invalid_joint(self, mock_create_publisher, mock_world): | ||
mock_world.current_world.robot.joint_name_to_id = {} | ||
with self.assertRaises(RuntimeError): | ||
ForceTorqueSensorSimulated('invalid_joint') | ||
|
||
@patch('pycram.ros_utils.force_torque_sensor.rospy') | ||
def test_initialization_force_torque_sensor(self, mock_rospy): | ||
sensor = ForceTorqueSensor('hsrb') | ||
self.assertEqual(sensor.robot_name, 'hsrb') | ||
self.assertEqual(sensor.wrench_topic_name, '/hsrb/wrist_wrench/compensated') | ||
|
||
@patch('pycram.ros_utils.force_torque_sensor.rospy') | ||
def test_get_last_value(self, mock_rospy): | ||
sensor = ForceTorqueSensor('hsrb') | ||
mock_data = MagicMock(spec=WrenchStamped) | ||
sensor.whole_data = {sensor.filtered: [mock_data], sensor.unfiltered: [mock_data]} | ||
self.assertEqual(sensor.get_last_value(), mock_data) | ||
|
||
@patch('pycram.ros_utils.force_torque_sensor.rospy') | ||
def test_get_derivative(self, mock_rospy): | ||
sensor = ForceTorqueSensor('hsrb') | ||
mock_data1 = MagicMock(spec=WrenchStamped) | ||
mock_data2 = MagicMock(spec=WrenchStamped) | ||
sensor.whole_data = {sensor.filtered: [mock_data1, mock_data2], sensor.unfiltered: [mock_data1, mock_data2]} | ||
derivative = sensor.get_derivative() | ||
self.assertIsInstance(derivative, WrenchStamped) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |