Skip to content

Commit 0a47c5a

Browse files
committed
cras_bag_tools: Added StampTwist filter.
1 parent 0c5ecd0 commit 0a47c5a

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

cras_bag_tools/src/cras_bag_tools/bag_filter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def filter_bag(bags, out, bag_filter=Passthrough(), params=None, start_time=None
6565
# apply topic filters
6666
topics = [t for t in topics if bag_filter.topic_filter(t)]
6767
# apply connection filters
68-
topics = [c.topic for c in bags._get_connections(topics, bag_filter.connection_filter)] # noqa
68+
if len(topics) > 0:
69+
topics = [c.topic for c in bags._get_connections(topics, bag_filter.connection_filter)] # noqa
6970

7071
bag_filter.on_filtering_start()
7172

cras_bag_tools/src/cras_bag_tools/bag_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def __del__(self):
4747
def __getattr__(self, item):
4848
return getattr(self.bag, item)
4949

50+
def read_messages(self, topics, start_time, end_time, topic_filter, raw, return_connection_header=False):
51+
if topics is not None and len(topics) == 0:
52+
return
53+
for msg in self.bag.read_messages(topics, start_time, end_time, topic_filter, raw, return_connection_header):
54+
yield msg
55+
5056
def _get_entries(self, connections=None, start_time=None, end_time=None):
5157
all_ranges = None
5258
if start_time is not None and isinstance(start_time, TimeRanges):
@@ -251,6 +257,8 @@ def _read_messages(self,
251257
return_connection_header=False, # type: bool
252258
):
253259
# type: (...) -> Iterator[Union[rosbag.bag.BagMessage, rosbag.bag.BagMessageWithConnectionHeader]]
260+
if topics is not None and len(topics) == 0:
261+
return
254262
connections = dict(self._get_connections(topics, connection_filter, True))
255263
for bag, entry, _ in self._get_entries(connections, start_time, end_time):
256264
msg = bag._reader.seek_and_read_message_data_record( # noqa

cras_bag_tools/src/cras_bag_tools/message_filters.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from cv_bridge import CvBridge, CvBridgeError
3131
from dynamic_reconfigure.encoding import decode_config
3232
from dynamic_reconfigure.msg import Config
33-
from geometry_msgs.msg import Quaternion, Transform, TransformStamped, Vector3
33+
from geometry_msgs.msg import Quaternion, Transform, TransformStamped, Twist, TwistStamped, Vector3
3434
from image_transport_codecs import decode, encode
3535
from image_transport_codecs.compressed_depth_codec import has_rvl
3636
from image_transport_codecs.parse_compressed_format import guess_any_compressed_image_transport_format
@@ -61,6 +61,19 @@ def dict_to_str(d, sep='='):
6161
k, sep, str(v) if not isinstance(v, dict) else dict_to_str(v, sep)) for k, v in d.items()) + "}"
6262

6363

64+
def create_connection_header(topic, msg_type, latch=False):
65+
header = {
66+
"callerid": "/bag_filter",
67+
"topic": topic,
68+
"message_definition": msg_type._full_text, # noqa
69+
"type": msg_type._type, # noqa
70+
"md5sum": msg_type._md5sum, # noqa
71+
}
72+
if latch:
73+
header["latching"] = "1"
74+
return header
75+
76+
6477
class SetFields(DeserializedMessageFilter):
6578
"""Change values of some fields of a message (pass the fields to change as kwargs)."""
6679

@@ -1128,6 +1141,46 @@ def _str_params(self):
11281141
return ", ".join(parts)
11291142

11301143

1144+
class StampTwist(DeserializedMessageFilter):
1145+
"""Adjust some static transforms."""
1146+
1147+
def __init__(self, source_topic, stamped_topic=None, frame_id="base_link", *args, **kwargs):
1148+
# type: (STRING_TYPE, Optional[STRING_TYPE], STRING_TYPE, Any, Any) -> None
1149+
"""
1150+
:param source_topic: The Twist topic to stamp.
1151+
:param stamped_topic: The stamped Twist topic to create.
1152+
:param frame_id: The frame_id to use in the stamped messages.
1153+
:param args: Standard include/exclude topics/types and min/max stamp args.
1154+
:param kwargs: Standard include/exclude topics/types and min/max stamp kwargs.
1155+
"""
1156+
super(StampTwist, self).__init__(include_topics=[source_topic], *args, **kwargs)
1157+
1158+
self._source_topic = source_topic
1159+
self._stamped_topic = stamped_topic if stamped_topic is not None else (source_topic + "_stamped")
1160+
self._frame_id = frame_id
1161+
1162+
self._connection_header = create_connection_header(self._stamped_topic, TwistStamped)
1163+
1164+
def filter(self, topic, msg, stamp, header):
1165+
stamped_msg = TwistStamped()
1166+
stamped_msg.header.frame_id = self._frame_id
1167+
stamped_msg.header.stamp = stamp
1168+
stamped_msg.twist = msg
1169+
1170+
return [
1171+
(topic, msg, stamp, header),
1172+
(self._stamped_topic, stamped_msg, stamp, self._connection_header)
1173+
]
1174+
1175+
def _str_params(self):
1176+
parts = []
1177+
parts.append('%s=>%s (frame %s)' % (self._source_topic, self._stamped_topic, self._frame_id))
1178+
parent_params = self._default_str_params(include_types=False)
1179+
if len(parent_params) > 0:
1180+
parts.append(parent_params)
1181+
return ", ".join(parts)
1182+
1183+
11311184
class FixExtrinsicsFromIKalibr(DeserializedMessageFilterWithTF):
11321185
"""Apply extrinsic calibrations from IKalibr."""
11331186

@@ -1325,16 +1378,7 @@ def parse_time_str(time_str):
13251378
return subtitles
13261379

13271380
def extra_initial_messages(self):
1328-
connection_header = {
1329-
"callerid": "/bag_filter",
1330-
"topic": self._topic,
1331-
"message_definition": String._full_text,
1332-
"type": String._type,
1333-
"md5sum": String._md5sum,
1334-
}
1335-
if self._latch:
1336-
connection_header["latching"] = "1"
1337-
1381+
connection_header = create_connection_header(self._topic, String, self._latch)
13381382
for start_time, end_time, subtitle in self._subtitles:
13391383
msg = String(data=subtitle)
13401384
abs_time = self._start_time + start_time

0 commit comments

Comments
 (0)