Skip to content

Commit 932b980

Browse files
committed
[jsk_topic_tools/filtered_relay] Add test and document
1 parent 6a22406 commit 932b980

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# filtered_relay.py
2+
3+
## Description
4+
5+
`rostopic echo` has a filter function, but it cannot publish a topic.
6+
`topic_tools/transform` can format a topic and publish it, but it does not have the ability to filter.
7+
Transforming while filtering a topic is simple and powerful.
8+
This node provides a filter and publish (i.e. relay) functions.
9+
10+
## Subscribing Topic
11+
12+
* `~input` (`rospy/AnyMsg`)
13+
14+
Input message.
15+
16+
## Publishing Topic
17+
18+
* `~output` (`~output_type`)
19+
20+
Output topic. You can specify `~output_type` param to publish topic.
21+
22+
## Parameters
23+
24+
* `~output_type` (`String`, required)
25+
26+
Message type of output_topic like `std_msgs/Float32`. This is the input for the [get_message_class](http://docs.ros.org/en/diamondback/api/roslib/html/python/roslib.message-module.html#get_message_class) function.
27+
28+
* `~filter` (`String`, default: `None`)
29+
30+
Condition of messages that match a specified Python expression.
31+
32+
The Python expression can access any of the Python builtins plus: ``topic`` (the topic of the message), ``m`` (the message) and ``t`` (time of message).
33+
34+
For example, ``~input`` topic is ``std_msgs/String`` and if you want to check whether a sentence is a ``hello``, you can do the following.
35+
36+
```bash
37+
filter: m.data == 'hello'
38+
```
39+
40+
Note that, use escape sequence when using the following symbols ``<(&lt;)``, ``>(&gt;)``, ``&(&amp;)``, ``'(&apos;)`` and ``"(&quot;)`` in launch file.
41+
42+
* `~transform` (`String`, default: `m`)
43+
44+
Python expression that transform the input messages, which are given in the variable m. The default expression is `m`, which results in forwarding input (which can be a topic field) into output_topic.
45+
46+
* `~import` (`List[String]`, default: `[]`)
47+
48+
List of Python modules to import and use in the expression.
49+
50+
## Usage
51+
52+
```bash
53+
$ rosrun jsk_topic_tools filtered_relay.py ~input:=/right_endeffector/wrench \
54+
~output:=/right_endeffector/force_norm \
55+
_filter:='numpy.linalg.norm([m.wrench.force.x, m.wrench.force.y, m.wrench.force.z]) > 10.0' \
56+
_output_type:=geometry_msgs/WrenchStamped _import:="[geometry_msgs, numpy]"
57+
```
58+
59+
## Example
60+
61+
The following example subscribe to `/right_endeffector/wrench` and only those with a force norm greater than 10 are published as `/right_endeffector/force_norm`.
62+
63+
```bash
64+
roslaunch jsk_topic_tools sample_filtered_relay.launch
65+
```

jsk_topic_tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ if (CATKIN_ENABLE_TESTING)
166166
endmacro()
167167
find_package(rostest REQUIRED)
168168
jsk_topic_tools_add_rostest(test/test_boolean_node.test)
169+
jsk_topic_tools_add_rostest(test/test_filtered_relay.test)
169170
jsk_topic_tools_add_rostest(test/test_topic_buffer.test)
170171
jsk_topic_tools_add_rostest(test/test_topic_buffer_close_wait.test)
171172
# topic_buffer tests are tend to fail in GA because of CPU high load.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<launch>
2+
3+
<arg name="gui" default="false" />
4+
<arg name="launch_robot_model" default="false" />
5+
6+
<include file="$(find jsk_topic_tools)/sample/sample_transform_wrench.launch" >
7+
<arg name="launch_robot_model" value="$(arg launch_robot_model)" />
8+
<arg name="gui" value="$(arg gui)" />
9+
</include>
10+
11+
<node name="filtered_relay"
12+
pkg="jsk_topic_tools" type="filtered_relay.py"
13+
output="screen"
14+
clear_params="true" >
15+
<remap from="~input" to="right_endeffector/wrench" />
16+
<remap from="~output" to="right_endeffector/force_norm" />
17+
<rosparam>
18+
output_type: std_msgs/Float32
19+
import: [std_msgs, geometry_msgs, numpy]
20+
filter: "numpy.linalg.norm([m.wrench.force.x, m.wrench.force.y, m.wrench.force.z]) &gt; 10.0"
21+
transform: "numpy.linalg.norm([m.wrench.force.x, m.wrench.force.y, m.wrench.force.z])"
22+
</rosparam>
23+
</node>
24+
25+
<node name="echo_force_norm"
26+
pkg="rostopic" type="rostopic"
27+
args="echo /right_endeffector/force_norm"
28+
output="screen"
29+
clear_params="true" >
30+
</node>
31+
32+
<node name="filtered_relay_without_transform"
33+
pkg="jsk_topic_tools" type="filtered_relay.py"
34+
output="screen"
35+
clear_params="true" >
36+
<remap from="~input" to="right_endeffector/wrench" />
37+
<remap from="~output" to="right_endeffector/filtered_wrench" />
38+
<rosparam>
39+
output_type: geometry_msgs/WrenchStamped
40+
import: [geometry_msgs, numpy]
41+
filter: "numpy.linalg.norm([m.wrench.force.x, m.wrench.force.y, m.wrench.force.z]) &gt; 10.0"
42+
</rosparam>
43+
</node>
44+
45+
<node name="filtered_relay_without_filter"
46+
pkg="jsk_topic_tools" type="filtered_relay.py"
47+
output="screen"
48+
clear_params="true" >
49+
<remap from="~input" to="right_endeffector/wrench" />
50+
<remap from="~output" to="right_endeffector/relayed_wrench" />
51+
<rosparam>
52+
output_type: geometry_msgs/WrenchStamped
53+
import: [geometry_msgs]
54+
</rosparam>
55+
</node>
56+
57+
</launch>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<launch>
2+
3+
<include file="$(find jsk_topic_tools)/sample/sample_filtered_relay.launch" >
4+
</include>
5+
6+
<test test-name="test_filtered_relay"
7+
name="test_filtered_relay"
8+
pkg="jsk_tools" type="test_topic_published.py">
9+
<rosparam>
10+
topic_0: /right_endeffector/force_norm
11+
timeout_0: 30
12+
topic_1: /right_endeffector/filtered_wrench
13+
timeout_1: 30
14+
topic_2: /right_endeffector/relayed_wrench
15+
timeout_2: 30
16+
</rosparam>
17+
</test>
18+
19+
</launch>

0 commit comments

Comments
 (0)