-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsg_parser.py
More file actions
149 lines (111 loc) · 3.87 KB
/
msg_parser.py
File metadata and controls
149 lines (111 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
from functools import wraps
from tf_transformations import euler_from_quaternion
from typing import Callable, Dict, Any
def with_fixed_topic(func: Callable) -> Callable:
"""Decorator to format topic names
Strips leading '/' and replaces '/' with '.' and adds a trailing '.'
This ensures consistent topic naming across all parser functions.
Args:
func: Parser function to be decorated
Returns:
Wrapped function that handles topic name formatting
"""
@wraps(func)
def wrapper(msg: Any, topic_name: str) -> Dict:
fixed_topic = topic_name.lstrip("/").replace("/", ".") + "."
return func(msg, fixed_topic)
return wrapper
@with_fixed_topic
def parse_header(msg, topic: str) -> dict:
"""Parse ROS header message into dictionary format
Args:
msg: ROS header message
topic: Topic name prefix for the output dictionary keys
Returns:
Dictionary containing timestamp and frame_id
"""
return {
f"{topic}stamp": msg.stamp,
f"{topic}frame_id": msg.frame_id,
}
@with_fixed_topic
def parse_bool(msg, topic: str) -> dict:
"""Parse boolean message into dictionary format"""
return {f"{topic}data": msg.data}
@with_fixed_topic
def parse_float32(msg, topic: str) -> dict:
"""Parse float32 message into dictionary format"""
return {f"{topic}data": msg.data}
@with_fixed_topic
def parse_float32_multiarray(msg, topic: str) -> dict:
"""Parse float32 multi array message into dictionary format
Assumes 1-dimensional array layout
Args:
msg: ROS message containing float32 multi array
topic: Topic name prefix for the output dictionary keys
Returns:
Dictionary with indexed data entries
Raises:
AssertionError: If array dimension is not 1
"""
assert len(msg.layout.dim) == 1, "data length must be 1"
return {f"{topic}data{i}": data for (i, data) in enumerate(msg.data)}
@with_fixed_topic
def parse_vector3(msg, topic: str) -> dict:
"""Parse Vector3 message into dictionary format"""
return {
f"{topic}x": msg.x,
f"{topic}y": msg.y,
f"{topic}z": msg.z,
}
@with_fixed_topic
def parse_point(msg, topic: str) -> dict:
"""Parse Point message into dictionary format"""
return {
f"{topic}x": msg.x,
f"{topic}y": msg.y,
f"{topic}z": msg.z,
}
@with_fixed_topic
def parse_quaternion(msg, topic: str) -> dict:
"""Parse Quaternion message into dictionary format"""
return {
f"{topic}x": msg.x,
f"{topic}y": msg.y,
f"{topic}z": msg.z,
f"{topic}w": msg.w,
}
@with_fixed_topic
def parse_quaternion_as_euler(msg, topic: str) -> dict:
"""Parse Quaternion message into Euler angles (roll, pitch, yaw)
Converts quaternion to Euler angles using tf_transformations
"""
roll, pitch, yaw = euler_from_quaternion([msg.x, msg.y, msg.z, msg.w])
return {
f"{topic}roll": roll,
f"{topic}pitch": pitch,
f"{topic}yaw": yaw,
}
def parse_twist(msg, topic: str) -> dict:
"""Parse Twist message into dictionary format"""
return (
parse_vector3(msg.linear, f"{topic}/linear")
| parse_vector3(msg.angular, f"{topic}/angular")
)
def parse_pose(msg, topic: str) -> dict:
"""Parse Pose message into dictionary format
Combines position, orientation (both quaternion and euler) data
"""
return (
parse_point(msg.position, f"{topic}/position")
| parse_quaternion(msg.orientation, f"{topic}/orientation")
| parse_quaternion_as_euler(msg.orientation, f"{topic}/euler")
)
def parse_pose_stamped(msg, topic: str) -> dict:
"""Parse PoseStamped message into dictionary format
Combines header and pose data
"""
return parse_header(msg.header, f"{topic}/header") | parse_pose(
msg.pose, f"{topic}/pose"
)