forked from OpenGHz/Imitate-All
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ros_tools.py
144 lines (126 loc) · 4.23 KB
/
ros_tools.py
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
from geometry_msgs.msg import Pose
from sensor_msgs.msg import JointState
import rospy
class Lister(object):
@staticmethod
def pose_to_list(no_position, pose: Pose) -> list:
lis = []
if not no_position:
lis += [pose.position.x, pose.position.y, pose.position.z]
ori = [
pose.orientation.x,
pose.orientation.y,
pose.orientation.z,
pose.orientation.w,
]
if sum(ori) > 0:
lis += ori
return lis
@staticmethod
def joint_state_to_list(joint_state: JointState) -> list:
"""Convert all fields of JointState to list"""
return (
list(joint_state.position)
+ list(joint_state.velocity)
+ list(joint_state.effort)
)
@staticmethod
def data_field_to_list(data) -> list:
data = data.data
if not isinstance(data, (tuple, list)):
data = [data]
return data
@staticmethod
def list_to_pose(data: list) -> Pose:
"""Auto convert based on the length of the list"""
pose = Pose()
if len(data) >= 3:
pose.position.x = data[0]
pose.position.y = data[1]
pose.position.z = data[2]
if len(data) >= 4:
pose.orientation.x = data[3]
pose.orientation.y = data[4]
pose.orientation.z = data[5]
pose.orientation.w = data[6]
return pose
@staticmethod
def list_to_joint_state(fields: str, data: list) -> JointState:
"""Convert list to JointState
Parameters:
data: list
The list to convert
fields: str
The fields to convert. Default is 'p' which means only position
'p' for position
'v' for velocity
'e' for effort
'pv' for position and velocity
'pe' for position and effort
've' for velocity and effort
'a' or 'pve' for position, velocity and effort
"""
joint_state = JointState()
if fields == "a":
fields = "pve"
dim = len(data) / len(fields)
assert dim % 1 == 0, "All fields must have the same dim"
start = 0
end = int(dim)
if "p" in fields:
joint_state.position = data[start:end]
start = end
end = int(start + dim)
if "v" in fields:
joint_state.velocity = data[start:end]
start = end
end = int(start + dim)
if "e" in fields:
joint_state.effort = data[start:end]
joint_state.header.stamp = rospy.Time.now()
return joint_state
@staticmethod
def list_to_given_data_field(type: str, data: list):
if len(data) == 1:
data = data[0]
return type(data=data)
if __name__ == "__main__":
# all to list
pose = Pose()
pose.position.x = 1
pose.position.y = 2
pose.position.z = 3
pose.orientation.x = 0
pose.orientation.y = 0
pose.orientation.z = 0
pose.orientation.w = 0
pose_list_1 = Lister.pose_to_list(pose)
pose.orientation.w = 1
pose_list_2 = Lister.pose_to_list(pose)
joint_state = JointState()
joint_state.position = [1, 2, 3]
joint_state.velocity = [4, 5, 6]
joint_state.effort = [7, 8, 9]
joint_state_list_1 = Lister.joint_state_to_list(joint_state)
joint_state.velocity = []
joint_state_list_2 = Lister.joint_state_to_list(joint_state)
from std_msgs.msg import (
Float32MultiArray,
Int32,
)
float32_multi_array = Float32MultiArray()
float32_multi_array.data = [1, 2, 3]
float32_multi_array_list_1 = Lister.data_field_to_list(float32_multi_array)
int32 = Int32()
int32.data = 1
int32_list = Lister.data_field_to_list(int32)
# list to all
pose_1 = Lister.list_to_pose(pose_list_1)
pose_2 = Lister.list_to_pose(pose_list_2)
joint_state_1 = Lister.list_to_joint_state(joint_state_list_1, "a")
joint_state_2 = Lister.list_to_joint_state(joint_state_list_2, "pe")
float32_multi_array_1 = Lister.list_to_given_data_field(
float32_multi_array_list_1, Float32MultiArray
)
int32_1 = Lister.list_to_given_data_field(int32_list, Int32)
print("Done")