Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ndt_evaluation #10

Closed
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7f3a3a7
Added convert_rosbag_for_ndt_evaluation.py
SakodaShintaro Feb 26, 2024
f6299a3
style(pre-commit): autofix
pre-commit-ci[bot] Feb 26, 2024
c31d8ae
Fixed comments
SakodaShintaro Feb 26, 2024
8c26f14
Fixed directory name and add README.md
SakodaShintaro Feb 26, 2024
f3591ef
style(pre-commit): autofix
pre-commit-ci[bot] Feb 26, 2024
3f1ed5e
Added plot_box.py
SakodaShintaro Feb 26, 2024
16fb567
style(pre-commit): autofix
pre-commit-ci[bot] Feb 26, 2024
a7495f0
Fixed initialize of list
SakodaShintaro Feb 26, 2024
8983172
Fixed record command
SakodaShintaro Feb 27, 2024
105d624
Added plot scripts
SakodaShintaro Feb 27, 2024
6db3452
style(pre-commit): autofix
pre-commit-ci[bot] Feb 27, 2024
2c30ddf
Fixed to use interpolate_pose
SakodaShintaro Feb 27, 2024
6ca7c4c
style(pre-commit): autofix
pre-commit-ci[bot] Feb 27, 2024
70d403f
Fixed import
SakodaShintaro Feb 27, 2024
dfcefd6
Added fix_timestamp_awsim_rosbag.py
SakodaShintaro Feb 27, 2024
9cfab6c
style(pre-commit): autofix
pre-commit-ci[bot] Feb 27, 2024
90c1c2c
Merge remote-tracking branch 'origin' into feat/add_convert_rosbag_fo…
SakodaShintaro Feb 27, 2024
e68b0fc
Merge remote-tracking branch 'origin' into feat/add_convert_rosbag_fo…
SakodaShintaro Mar 5, 2024
154749b
Updated convert_rosbag_for_ndt_evaluation.py
SakodaShintaro Mar 5, 2024
55f920b
style(pre-commit): autofix
pre-commit-ci[bot] Mar 5, 2024
7cf0ce0
Added initialpose
SakodaShintaro Mar 5, 2024
4d344a6
style(pre-commit): autofix
pre-commit-ci[bot] Mar 5, 2024
90aa6b3
FIxed header
SakodaShintaro Mar 7, 2024
ae38c33
Fixed to use lidar timestamp
SakodaShintaro Mar 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions localization/scripts/convert_rosbag_for_ndt_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""Convert rosbag for NDT evaluation.

(1) Check if the rosbag is suitable for ndt evaluation
(2) Filter topics
(3) Rename the topic name of the reference kinematic state to /localization/reference_kinematic_state
"""

import argparse
import pathlib

import pandas as pd
from rclpy.serialization import deserialize_message
import rosbag2_py
from rosidl_runtime_py.utilities import get_message

REQUIRED_FPS = {
"/localization/kinematic_state": 40,
"/localization/util/downsample/pointcloud": 9,
"/sensing/vehicle_velocity_converter/twist_with_covariance": 20,
"/sensing/imu/imu_data": 20,
"/tf_static": 0,
"/sensing/gnss/pose_with_covariance": -1, # optional topic
"/initialpose": -1, # optional topic
}


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("rosbag_path", type=pathlib.Path)
parser.add_argument(
"--reference_topic_name",
type=str,
default="/localization/kinematic_state",
choices=[
"/localization/kinematic_state",
"/awsim/ground_truth/localization/kinematic_state",
],
)
return parser.parse_args()


def check_rosbag(duration: float, topic_name_to_msg_list: dict) -> None:
"""Check if the rosbag is suitable for ndt evaluation."""
print(f"{duration=:.1f} sec")

df = pd.DataFrame(
{
"topic": list(topic_name_to_msg_list.keys()),
"count": [len(msg_list) for msg_list in topic_name_to_msg_list.values()],
}
)
df["fps"] = df["count"] / duration
df["enough_fps"] = df["fps"] > df["topic"].map(REQUIRED_FPS)
print(df)

# check
# All topics must have enough fps
assert df[
"enough_fps"
].all(), f"NG! FPS is not enough in {df[df['enough_fps'] == False]['topic'].values}"

# Either /sensing/gnss/pose_with_covariance or /initialpose must be included.
assert (
len(topic_name_to_msg_list["/sensing/gnss/pose_with_covariance"]) >= 1
or len(topic_name_to_msg_list["/initialpose"]) >= 1
), "NG! Neither /sensing/gnss/pose_with_covariance nor /initialpose is found."

# [Warning] Vehicle should be stopping still for about the first 10 seconds
for msg in topic_name_to_msg_list["/localization/kinematic_state"]:
if msg.header.stamp.sec > 10:
break
twist = msg.twist.twist
ok = (
twist.linear.x < 0.1
and twist.linear.y < 0.1
and twist.linear.z < 0.1
and twist.angular.x < 0.1
and twist.angular.y < 0.1
and twist.angular.z < 0.1
)
if not ok:
print(f"Warning: Vehicle is not stopping. time = {msg.header.stamp.sec}")
break

print("OK")


if __name__ == "__main__":
args = parse_args()
rosbag_path = args.rosbag_path
reference_topic_name = args.reference_topic_name

# prepare option
serialization_format = "cdr"
converter_options = rosbag2_py.ConverterOptions(
input_serialization_format=serialization_format,
output_serialization_format=serialization_format,
)

# prepare reader
reader = rosbag2_py.SequentialReader()
storage_options = rosbag2_py.StorageOptions(uri=str(rosbag_path), storage_id="sqlite3")
reader.open(storage_options, converter_options)

# filter topics
target_topics = list(REQUIRED_FPS.keys())
if reference_topic_name not in target_topics:
target_topics.append(reference_topic_name)
storage_filter = rosbag2_py.StorageFilter(topics=target_topics)
reader.set_filter(storage_filter)

# get map of topic name and type
type_map = {}
for topic_type in reader.get_all_topics_and_types():
if topic_type.name in target_topics:
type_map[topic_type.name] = topic_type.type

# read rosbag
topic_name_to_msg_list = {topic: [] for topic in target_topics}
tuple_list = []
while reader.has_next():
(topic_name, data, timestamp_rosbag) = reader.read_next()
tuple_list.append((topic_name, data, timestamp_rosbag))

msg_type = get_message(type_map[topic_name])
msg = deserialize_message(data, msg_type)
topic_name_to_msg_list[topic_name].append(msg)
duration = (tuple_list[-1][2] - tuple_list[0][2]) * 1e-9

# check
check_rosbag(duration, topic_name_to_msg_list)

# write rosbag
rosbag_dir = rosbag_path.parent if rosbag_path.is_dir() else rosbag_path.parent.parent
filtered_rosbag_path = rosbag_dir / "input_bag"
writer = rosbag2_py.SequentialWriter()
storage_options = rosbag2_py.StorageOptions(uri=str(filtered_rosbag_path), storage_id="sqlite3")
writer.open(storage_options, converter_options)
for topic_name, topic_type in type_map.items():
if topic_name == reference_topic_name:
topic_name = "/localization/reference_kinematic_state"
topic_info = rosbag2_py.TopicMetadata(
name=topic_name, type=topic_type, serialization_format=serialization_format
)
writer.create_topic(topic_info)
for topic_name, data, timestamp_rosbag in tuple_list:
if topic_name == reference_topic_name:
topic_name = "/localization/reference_kinematic_state"
writer.write(topic_name, data, timestamp_rosbag)

print(f"rosbag is saved at {filtered_rosbag_path}")
Loading