kuavo-humanoid-sdk 1.1.2a924__py3-none-any.whl → 1.1.3a1239__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kuavo-humanoid-sdk might be problematic. Click here for more details.
- kuavo_humanoid_sdk/common/global_config.py +15 -0
- kuavo_humanoid_sdk/common/websocket_kuavo_sdk.py +23 -0
- kuavo_humanoid_sdk/interfaces/data_types.py +46 -0
- kuavo_humanoid_sdk/kuavo/__init__.py +3 -0
- kuavo_humanoid_sdk/kuavo/core/audio.py +32 -0
- kuavo_humanoid_sdk/kuavo/core/core.py +17 -6
- kuavo_humanoid_sdk/kuavo/core/ros/audio.py +93 -0
- kuavo_humanoid_sdk/kuavo/core/ros/control.py +645 -19
- kuavo_humanoid_sdk/kuavo/core/ros/param.py +142 -4
- kuavo_humanoid_sdk/kuavo/core/ros/state.py +329 -15
- kuavo_humanoid_sdk/kuavo/core/ros/tools.py +158 -0
- kuavo_humanoid_sdk/kuavo/core/ros/vision.py +276 -0
- kuavo_humanoid_sdk/kuavo/core/ros_env.py +229 -1
- kuavo_humanoid_sdk/kuavo/dexterous_hand.py +6 -2
- kuavo_humanoid_sdk/kuavo/leju_claw.py +6 -2
- kuavo_humanoid_sdk/kuavo/robot.py +43 -22
- kuavo_humanoid_sdk/kuavo/robot_audio.py +39 -0
- kuavo_humanoid_sdk/kuavo/robot_info.py +7 -2
- kuavo_humanoid_sdk/kuavo/robot_state.py +6 -2
- kuavo_humanoid_sdk/kuavo/robot_tool.py +62 -0
- kuavo_humanoid_sdk/kuavo/robot_vision.py +90 -0
- kuavo_humanoid_sdk/msg/kuavo_msgs/msg/_AprilTagDetection.py +306 -0
- kuavo_humanoid_sdk/msg/kuavo_msgs/msg/_AprilTagDetectionArray.py +437 -0
- kuavo_humanoid_sdk/msg/kuavo_msgs/msg/__init__.py +9 -0
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_armHandPose.py +2 -2
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_handPose.py +2 -2
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_ikSolveError.py +13 -13
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_ikSolveParam.py +2 -2
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_robotArmQVVD.py +2 -2
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_twoArmHandPose.py +13 -13
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/msg/_twoArmHandPoseCmd.py +15 -15
- kuavo_humanoid_sdk/msg/kuavo_msgs/srv/_SpeechSynthesis.py +270 -0
- kuavo_humanoid_sdk/msg/kuavo_msgs/srv/__init__.py +5 -0
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/srv/_changeArmCtrlModeKuavo.py +5 -5
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/srv/_fkSrv.py +13 -13
- kuavo_humanoid_sdk/msg/{motion_capture_ik/srv/_changeArmCtrlMode.py → kuavo_msgs/srv/_playmusic.py} +97 -98
- kuavo_humanoid_sdk/msg/{motion_capture_ik → kuavo_msgs}/srv/_twoArmHandPoseCmdSrv.py +23 -23
- {kuavo_humanoid_sdk-1.1.2a924.dist-info → kuavo_humanoid_sdk-1.1.3a1239.dist-info}/METADATA +2 -1
- {kuavo_humanoid_sdk-1.1.2a924.dist-info → kuavo_humanoid_sdk-1.1.3a1239.dist-info}/RECORD +41 -33
- kuavo_humanoid_sdk/msg/motion_capture_ik/msg/__init__.py +0 -9
- kuavo_humanoid_sdk/msg/motion_capture_ik/msg/_headBodyPose.py +0 -145
- kuavo_humanoid_sdk/msg/motion_capture_ik/msg/_robotHandPosition.py +0 -225
- kuavo_humanoid_sdk/msg/motion_capture_ik/srv/__init__.py +0 -4
- {kuavo_humanoid_sdk-1.1.2a924.dist-info → kuavo_humanoid_sdk-1.1.3a1239.dist-info}/WHEEL +0 -0
- {kuavo_humanoid_sdk-1.1.2a924.dist-info → kuavo_humanoid_sdk-1.1.3a1239.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
#! /usr/bin/env python3
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
import copy
|
|
5
|
+
import time
|
|
6
|
+
import numpy as np
|
|
7
|
+
from typing import Tuple, Union
|
|
8
|
+
from kuavo_humanoid_sdk.common.logger import SDKLogger
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
import rospy
|
|
12
|
+
import tf2_ros
|
|
13
|
+
import tf.transformations as tf_trans
|
|
14
|
+
from std_msgs.msg import Float64
|
|
15
|
+
from nav_msgs.msg import Odometry
|
|
16
|
+
from sensor_msgs.msg import JointState
|
|
17
|
+
from kuavo_humanoid_sdk.msg.kuavo_msgs.msg import sensorsData, lejuClawState, gaitTimeName, dexhandTouchState
|
|
18
|
+
from geometry_msgs.msg import TransformStamped
|
|
19
|
+
except ImportError:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
from kuavo_humanoid_sdk.interfaces.data_types import (PoseQuaternion, HomogeneousMatrix)
|
|
23
|
+
from kuavo_humanoid_sdk.kuavo.core.ros.param import make_robot_param, EndEffectorType
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class KuavoRobotToolsCore:
|
|
28
|
+
"""Provides core ROS tools for Kuavo humanoid robot transformations.
|
|
29
|
+
|
|
30
|
+
Attributes:
|
|
31
|
+
tf_buffer (tf2_ros.Buffer): TF2 transform buffer for storing transforms
|
|
32
|
+
tf_listener (tf2_ros.TransformListener): TF2 transform listener
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self):
|
|
36
|
+
"""Initializes TF2 buffer and listener for coordinate transformations."""
|
|
37
|
+
if not hasattr(self, '_initialized'):
|
|
38
|
+
# 初始化TF2相关组件
|
|
39
|
+
self.tf_buffer = tf2_ros.Buffer(cache_time=rospy.Duration(10))
|
|
40
|
+
self.tf_listener = tf2_ros.TransformListener(self.tf_buffer)
|
|
41
|
+
|
|
42
|
+
def _get_tf_tree_transform(self, target_frame: str, source_frame: str,
|
|
43
|
+
time=0.0, timeout=1.0,
|
|
44
|
+
return_type: str = "pose_quaternion") -> Union[PoseQuaternion, HomogeneousMatrix, None]:
|
|
45
|
+
"""Gets transform between coordinate frames.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
target_frame (str): Target coordinate frame name
|
|
49
|
+
source_frame (str): Source coordinate frame name
|
|
50
|
+
time (rospy.Time, optional): Time of transform. Defaults to latest.
|
|
51
|
+
timeout (float, optional): Wait timeout in seconds. Defaults to 1.0.
|
|
52
|
+
return_type (str, optional): Return data format. Options:
|
|
53
|
+
"pose_quaternion", "homogeneous". Defaults to "pose_quaternion".
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
Union[PoseQuaternion, HomogeneousMatrix, None]: Requested transform data
|
|
57
|
+
or None if failed
|
|
58
|
+
|
|
59
|
+
Raises:
|
|
60
|
+
tf2_ros.LookupException: If transform is not available
|
|
61
|
+
tf2_ros.ConnectivityException: If transform connectivity issue
|
|
62
|
+
tf2_ros.ExtrapolationException: If transform time is out of range
|
|
63
|
+
"""
|
|
64
|
+
time = rospy.Time(time)
|
|
65
|
+
try:
|
|
66
|
+
transform = self.tf_buffer.lookup_transform(
|
|
67
|
+
target_frame,
|
|
68
|
+
source_frame,
|
|
69
|
+
time,
|
|
70
|
+
rospy.Duration(timeout)
|
|
71
|
+
)
|
|
72
|
+
return self._parse_transform(transform.transform, return_type)
|
|
73
|
+
except Exception as e:
|
|
74
|
+
SDKLogger.error(f"Transform error: {str(e)}")
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
def _parse_transform(self, transform, return_type: str) -> Union[PoseQuaternion, HomogeneousMatrix, None]:
|
|
78
|
+
"""Parses transform data to specified format.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
transform (geometry_msgs/Transform): Input transform data
|
|
82
|
+
return_type (str): Output format type. Valid values:
|
|
83
|
+
"pose_quaternion", "homogeneous"
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
Union[PoseQuaternion, HomogeneousMatrix, None]: Parsed transform data
|
|
87
|
+
in requested format, or None if invalid input
|
|
88
|
+
|
|
89
|
+
Note:
|
|
90
|
+
Falls back to pose_quaternion format if invalid return_type specified
|
|
91
|
+
"""
|
|
92
|
+
if return_type == "pose_quaternion":
|
|
93
|
+
return PoseQuaternion(
|
|
94
|
+
position=(transform.translation.x,
|
|
95
|
+
transform.translation.y,
|
|
96
|
+
transform.translation.z),
|
|
97
|
+
orientation=(transform.rotation.x,
|
|
98
|
+
transform.rotation.y,
|
|
99
|
+
transform.rotation.z,
|
|
100
|
+
transform.rotation.w)
|
|
101
|
+
)
|
|
102
|
+
elif return_type == "homogeneous":
|
|
103
|
+
return HomogeneousMatrix(
|
|
104
|
+
matrix=self._transform_to_homogeneous(transform)
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
SDKLogger.warn(f"Invalid return_type: {return_type}, using default(pose_quaternion)")
|
|
108
|
+
return self._parse_transform(transform, "pose_quaternion")
|
|
109
|
+
|
|
110
|
+
def _transform_to_homogeneous(self, transform) -> np.ndarray:
|
|
111
|
+
"""Converts geometry_msgs/Transform to homogeneous matrix.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
transform (geometry_msgs/Transform): Input transform message
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
np.ndarray: 4x4 homogeneous transformation matrix (numpy.float32)
|
|
118
|
+
|
|
119
|
+
Example:
|
|
120
|
+
>>> matrix = _transform_to_homogeneous(transform_msg)
|
|
121
|
+
>>> print(matrix.shape)
|
|
122
|
+
(4, 4)
|
|
123
|
+
"""
|
|
124
|
+
# 四元数转旋转矩阵
|
|
125
|
+
rotation = [
|
|
126
|
+
transform.rotation.x,
|
|
127
|
+
transform.rotation.y,
|
|
128
|
+
transform.rotation.z,
|
|
129
|
+
transform.rotation.w
|
|
130
|
+
]
|
|
131
|
+
rot_matrix = tf_trans.quaternion_matrix(rotation)
|
|
132
|
+
|
|
133
|
+
# 设置平移分量
|
|
134
|
+
translation = [
|
|
135
|
+
transform.translation.x,
|
|
136
|
+
transform.translation.y,
|
|
137
|
+
transform.translation.z
|
|
138
|
+
]
|
|
139
|
+
rot_matrix[:3, 3] = translation
|
|
140
|
+
|
|
141
|
+
return rot_matrix.astype(np.float32) # 确保矩阵数据类型一致
|
|
142
|
+
|
|
143
|
+
# if __name__ == "__main__":
|
|
144
|
+
# robot_tools = KuavoRobotToolsCore()
|
|
145
|
+
# time.sleep(0.1)
|
|
146
|
+
# # 获取位姿信息
|
|
147
|
+
# pose = robot_tools._get_tf_tree_transform("odom", "base_link", return_type="pose_quaternion")
|
|
148
|
+
# print(f"Position: {pose.position}")
|
|
149
|
+
# print(f"Orientation: {pose.orientation}")
|
|
150
|
+
|
|
151
|
+
# # 获取齐次矩阵
|
|
152
|
+
# homogeneous = robot_tools._get_tf_tree_transform("odom", "base_link", return_type="homogeneous")
|
|
153
|
+
# print(f"Transformation matrix:\n{homogeneous.matrix}")
|
|
154
|
+
|
|
155
|
+
# # 矩阵运算示例
|
|
156
|
+
# transform_matrix = homogeneous.matrix
|
|
157
|
+
# inverse_matrix = np.linalg.inv(transform_matrix) # 求逆变换
|
|
158
|
+
# print(f"Inverse matrix:\n{inverse_matrix}")
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
#! /usr/bin/env python3
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
import copy
|
|
5
|
+
import time
|
|
6
|
+
from collections import deque
|
|
7
|
+
from typing import Tuple, Optional
|
|
8
|
+
|
|
9
|
+
from kuavo_humanoid_sdk.kuavo.core.ros.param import make_robot_param, EndEffectorType
|
|
10
|
+
from kuavo_humanoid_sdk.common.logger import SDKLogger
|
|
11
|
+
from kuavo_humanoid_sdk.interfaces.data_types import (AprilTagData)
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
import rospy
|
|
15
|
+
from std_msgs.msg import Float64
|
|
16
|
+
from nav_msgs.msg import Odometry
|
|
17
|
+
from sensor_msgs.msg import JointState
|
|
18
|
+
from apriltag_ros.msg import AprilTagDetectionArray
|
|
19
|
+
from geometry_msgs.msg import TransformStamped, PoseStamped
|
|
20
|
+
import tf2_ros
|
|
21
|
+
import tf2_geometry_msgs
|
|
22
|
+
except ImportError:
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
class KuavoRobotVisionCore:
|
|
26
|
+
"""Handles vision-related data processing for Kuavo humanoid robot.
|
|
27
|
+
|
|
28
|
+
Attributes:
|
|
29
|
+
tf_buffer (tf2_ros.Buffer): TF2 transform buffer
|
|
30
|
+
tf_listener (tf2_ros.TransformListener): TF2 transform listener
|
|
31
|
+
tf_broadcaster (tf2_ros.TransformBroadcaster): TF2 transform broadcaster
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self):
|
|
35
|
+
"""Initializes vision system components including TF and AprilTag subscribers."""
|
|
36
|
+
if not hasattr(self, '_initialized'):
|
|
37
|
+
self.tf_buffer = tf2_ros.Buffer()
|
|
38
|
+
self.tf_listener = tf2_ros.TransformListener(self.tf_buffer)
|
|
39
|
+
self.tf_broadcaster = tf2_ros.TransformBroadcaster()
|
|
40
|
+
self._apriltag_data_camera_sub = rospy.Subscriber('/tag_detections', AprilTagDetectionArray, self._apriltag_data_callback_camera)
|
|
41
|
+
self._apriltag_data_base_sub = rospy.Subscriber('/robot_tag_info', AprilTagDetectionArray, self._apriltag_data_callback_base)
|
|
42
|
+
|
|
43
|
+
# 添加TF2监听器
|
|
44
|
+
self._tf_buffer = tf2_ros.Buffer()
|
|
45
|
+
self._tf_listener = tf2_ros.TransformListener(self._tf_buffer)
|
|
46
|
+
|
|
47
|
+
""" data """
|
|
48
|
+
self._apriltag_data_from_camera = AprilTagData(
|
|
49
|
+
id = [],
|
|
50
|
+
size = [],
|
|
51
|
+
pose = []
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
self._apriltag_data_from_base = AprilTagData(
|
|
55
|
+
id = [],
|
|
56
|
+
size = [],
|
|
57
|
+
pose = []
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
self._apriltag_data_from_odom = AprilTagData(
|
|
61
|
+
id = [],
|
|
62
|
+
size = [],
|
|
63
|
+
pose = []
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def _apriltag_data_callback_camera(self, data):
|
|
67
|
+
"""Callback for processing AprilTag detections from camera.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
data (AprilTagDetectionArray): Raw detection data from camera
|
|
71
|
+
"""
|
|
72
|
+
# 清空之前的数据
|
|
73
|
+
self._apriltag_data_from_camera.id = []
|
|
74
|
+
self._apriltag_data_from_camera.size = []
|
|
75
|
+
self._apriltag_data_from_camera.pose = []
|
|
76
|
+
|
|
77
|
+
# 处理每个标签检测
|
|
78
|
+
for detection in data.detections:
|
|
79
|
+
# 添加ID
|
|
80
|
+
for id in detection.id:
|
|
81
|
+
self._apriltag_data_from_camera.id.append(id)
|
|
82
|
+
|
|
83
|
+
# 添加尺寸 (从size数组中获取)
|
|
84
|
+
if detection.size and len(detection.size) > 0:
|
|
85
|
+
self._apriltag_data_from_camera.size.append(detection.size[0])
|
|
86
|
+
else:
|
|
87
|
+
self._apriltag_data_from_camera.size.append(0.0)
|
|
88
|
+
|
|
89
|
+
# 添加姿态
|
|
90
|
+
self._apriltag_data_from_camera.pose.append(detection.pose.pose.pose)
|
|
91
|
+
# # debug
|
|
92
|
+
# rospy.loginfo("Apriltag data from camera: %s", self._apriltag_data_from_camera)
|
|
93
|
+
|
|
94
|
+
def _apriltag_data_callback_base(self, data):
|
|
95
|
+
"""Callback for processing AprilTag detections from base link.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
data (AprilTagDetectionArray): Raw detection data from base frame
|
|
99
|
+
"""
|
|
100
|
+
# 清空之前的数据
|
|
101
|
+
self._apriltag_data_from_base.id = []
|
|
102
|
+
self._apriltag_data_from_base.size = []
|
|
103
|
+
self._apriltag_data_from_base.pose = []
|
|
104
|
+
|
|
105
|
+
# 处理每个标签检测
|
|
106
|
+
for detection in data.detections:
|
|
107
|
+
# 添加ID
|
|
108
|
+
for id in detection.id:
|
|
109
|
+
self._apriltag_data_from_base.id.append(id)
|
|
110
|
+
|
|
111
|
+
# 添加尺寸 (从size数组中获取)
|
|
112
|
+
if detection.size and len(detection.size) > 0:
|
|
113
|
+
self._apriltag_data_from_base.size.append(detection.size[0])
|
|
114
|
+
else:
|
|
115
|
+
self._apriltag_data_from_base.size.append(0.0)
|
|
116
|
+
|
|
117
|
+
# 添加姿态
|
|
118
|
+
self._apriltag_data_from_base.pose.append(detection.pose.pose.pose)
|
|
119
|
+
|
|
120
|
+
# # debug
|
|
121
|
+
# rospy.loginfo("Apriltag data from base: %s", self._apriltag_data_from_base)
|
|
122
|
+
|
|
123
|
+
# 在基础数据处理完后,尝试进行odom坐标系的变换
|
|
124
|
+
self._transform_base_to_odom()
|
|
125
|
+
|
|
126
|
+
def _transform_base_to_odom(self):
|
|
127
|
+
"""Transforms AprilTag poses from base_link to odom coordinate frame.
|
|
128
|
+
|
|
129
|
+
Performs:
|
|
130
|
+
- Coordinate transformation using TF2
|
|
131
|
+
- TF broadcasting for transformed poses
|
|
132
|
+
- Data validation and error handling
|
|
133
|
+
|
|
134
|
+
Raises:
|
|
135
|
+
tf2_ros.LookupException: If transform is not available
|
|
136
|
+
tf2_ros.ConnectivityException: If transform chain is broken
|
|
137
|
+
tf2_ros.ExtrapolationException: If transform time is out of range
|
|
138
|
+
"""
|
|
139
|
+
# 添加节点状态检查
|
|
140
|
+
if rospy.is_shutdown():
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
# 清空之前的数据
|
|
144
|
+
self._apriltag_data_from_odom.id = []
|
|
145
|
+
self._apriltag_data_from_odom.size = []
|
|
146
|
+
self._apriltag_data_from_odom.pose = []
|
|
147
|
+
|
|
148
|
+
# 如果base数据为空,则不处理
|
|
149
|
+
if not self._apriltag_data_from_base.id:
|
|
150
|
+
SDKLogger.warn("No base tag data, skip transform")
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
try:
|
|
154
|
+
# 获取从base_link到odom的变换
|
|
155
|
+
transform = self._tf_buffer.lookup_transform(
|
|
156
|
+
"odom",
|
|
157
|
+
"base_link",
|
|
158
|
+
rospy.Time(0),
|
|
159
|
+
rospy.Duration(1.0)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
# 复制ID和尺寸信息
|
|
163
|
+
self._apriltag_data_from_odom.id = self._apriltag_data_from_base.id.copy()
|
|
164
|
+
self._apriltag_data_from_odom.size = self._apriltag_data_from_base.size.copy()
|
|
165
|
+
|
|
166
|
+
# 对每个姿态进行变换
|
|
167
|
+
for idx, (tag_id, pose) in enumerate(zip(self._apriltag_data_from_base.id, self._apriltag_data_from_base.pose)):
|
|
168
|
+
# 创建PoseStamped消息
|
|
169
|
+
pose_stamped = PoseStamped()
|
|
170
|
+
pose_stamped.header.frame_id = "base_link"
|
|
171
|
+
pose_stamped.header.stamp = rospy.Time.now()
|
|
172
|
+
pose_stamped.pose = pose
|
|
173
|
+
|
|
174
|
+
# 执行变换
|
|
175
|
+
transformed_pose = tf2_geometry_msgs.do_transform_pose(pose_stamped, transform)
|
|
176
|
+
|
|
177
|
+
# 将变换后的姿态添加到odom数据中
|
|
178
|
+
self._apriltag_data_from_odom.pose.append(transformed_pose.pose)
|
|
179
|
+
|
|
180
|
+
# 创建并广播TF
|
|
181
|
+
transform_stamped = TransformStamped()
|
|
182
|
+
transform_stamped.header.stamp = rospy.Time.now()
|
|
183
|
+
transform_stamped.header.frame_id = "odom"
|
|
184
|
+
transform_stamped.child_frame_id = f"tag_odom_{tag_id}"
|
|
185
|
+
transform_stamped.transform.translation.x = transformed_pose.pose.position.x
|
|
186
|
+
transform_stamped.transform.translation.y = transformed_pose.pose.position.y
|
|
187
|
+
transform_stamped.transform.translation.z = transformed_pose.pose.position.z
|
|
188
|
+
transform_stamped.transform.rotation = transformed_pose.pose.orientation
|
|
189
|
+
|
|
190
|
+
# 发送变换前再次检查节点状态
|
|
191
|
+
if not rospy.is_shutdown():
|
|
192
|
+
self.tf_broadcaster.sendTransform(transform_stamped)
|
|
193
|
+
|
|
194
|
+
except (tf2_ros.LookupException,
|
|
195
|
+
tf2_ros.ConnectivityException,
|
|
196
|
+
tf2_ros.ExtrapolationException,
|
|
197
|
+
rospy.ROSException) as e: # 添加ROSException捕获
|
|
198
|
+
SDKLogger.warn(f"TF变换异常: {str(e)}")
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def apriltag_data_from_camera(self) -> AprilTagData:
|
|
202
|
+
"""AprilTag detection data in camera coordinate frame.
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
AprilTagData: Contains lists of tag IDs, sizes and poses
|
|
206
|
+
"""
|
|
207
|
+
return self._apriltag_data_from_camera
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def apriltag_data_from_base(self) -> AprilTagData:
|
|
211
|
+
"""AprilTag detection data in base_link coordinate frame.
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
AprilTagData: Contains lists of tag IDs, sizes and poses
|
|
215
|
+
"""
|
|
216
|
+
return self._apriltag_data_from_base
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def apriltag_data_from_odom(self) -> AprilTagData:
|
|
220
|
+
"""AprilTag detection data in odom coordinate frame.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
AprilTagData: Contains lists of tag IDs, sizes and transformed poses
|
|
224
|
+
"""
|
|
225
|
+
return self._apriltag_data_from_odom
|
|
226
|
+
|
|
227
|
+
def _get_data_by_id(self, target_id: int, data_source: str = "base") -> Optional[dict]:
|
|
228
|
+
"""Retrieves AprilTag data by specific ID from selected source.
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
target_id (int): AprilTag ID to search for
|
|
232
|
+
data_source (str): Data source selector, valid options:
|
|
233
|
+
"camera", "base", "odom"
|
|
234
|
+
|
|
235
|
+
Returns:
|
|
236
|
+
Optional[dict]: Dictionary containing 'sizes' and 'poses' lists if found,
|
|
237
|
+
None if no matching data
|
|
238
|
+
|
|
239
|
+
Raises:
|
|
240
|
+
ValueError: If invalid data_source is specified
|
|
241
|
+
"""
|
|
242
|
+
data_map = {
|
|
243
|
+
"camera": self._apriltag_data_from_camera,
|
|
244
|
+
"base": self._apriltag_data_from_base,
|
|
245
|
+
"odom": self._apriltag_data_from_odom
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if data_source not in data_map:
|
|
249
|
+
SDKLogger.error(f"Invalid data source: {data_source}, must be one of {list(data_map.keys())}")
|
|
250
|
+
return None
|
|
251
|
+
|
|
252
|
+
data = data_map[data_source]
|
|
253
|
+
|
|
254
|
+
# 查找所有匹配的索引
|
|
255
|
+
indices = [i for i, tag_id in enumerate(data.id) if tag_id == target_id]
|
|
256
|
+
|
|
257
|
+
if not indices:
|
|
258
|
+
SDKLogger.debug(f"No data found for tag ID {target_id} in {data_source} source")
|
|
259
|
+
return None
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
"sizes": [data.size[i] for i in indices],
|
|
263
|
+
"poses": [data.pose[i] for i in indices]
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
# if __name__ == "__main__":
|
|
267
|
+
|
|
268
|
+
# kuavo_robot_vision_core = KuavoRobotVisionCore()
|
|
269
|
+
# time.sleep(5)
|
|
270
|
+
# print("apriltag_data_from_camera:")
|
|
271
|
+
# print(kuavo_robot_vision_core.apriltag_data_from_camera)
|
|
272
|
+
# print("apriltag_data_from_base:")
|
|
273
|
+
# print(kuavo_robot_vision_core.apriltag_data_from_base)
|
|
274
|
+
# print("apriltag_data_from_odom:")
|
|
275
|
+
# print(kuavo_robot_vision_core.apriltag_data_from_odom)
|
|
276
|
+
# rospy.spin()
|
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
# coding: utf-8
|
|
3
3
|
|
|
4
4
|
import os
|
|
5
|
-
|
|
5
|
+
try:
|
|
6
|
+
import rospy
|
|
7
|
+
except ImportError:
|
|
8
|
+
pass
|
|
6
9
|
import subprocess
|
|
7
10
|
import atexit
|
|
8
11
|
from kuavo_humanoid_sdk.common.logger import SDKLogger
|
|
12
|
+
from kuavo_humanoid_sdk.common.websocket_kuavo_sdk import WebSocketKuavoSDK
|
|
13
|
+
import roslibpy
|
|
9
14
|
|
|
10
15
|
class KuavoROSEnv:
|
|
11
16
|
_instance = None
|
|
@@ -232,3 +237,226 @@ class KuavoROSEnv:
|
|
|
232
237
|
except Exception as e:
|
|
233
238
|
SDKLogger.error(f"Error checking if node {node_name} exists: {e}")
|
|
234
239
|
return False
|
|
240
|
+
|
|
241
|
+
class KuavoROSEnvWebsocket:
|
|
242
|
+
_instance = None
|
|
243
|
+
_processes = [] # Store all subprocess instances
|
|
244
|
+
|
|
245
|
+
def __new__(cls):
|
|
246
|
+
if cls._instance is None:
|
|
247
|
+
cls._instance = super(KuavoROSEnvWebsocket, cls).__new__(cls)
|
|
248
|
+
cls._instance._initialized = False
|
|
249
|
+
cls._instance._init_success = False # Add flag to track successful Init() call
|
|
250
|
+
# Register cleanup handler
|
|
251
|
+
atexit.register(cls._cleanup_processes)
|
|
252
|
+
return cls._instance
|
|
253
|
+
|
|
254
|
+
def __init__(self):
|
|
255
|
+
if not self._initialized:
|
|
256
|
+
self._initialized = True
|
|
257
|
+
self.websocket = None
|
|
258
|
+
|
|
259
|
+
@classmethod
|
|
260
|
+
def _cleanup_processes(cls):
|
|
261
|
+
"""Cleanup all registered processes on exit"""
|
|
262
|
+
for process in cls._processes:
|
|
263
|
+
if process.poll() is None: # Process is still running
|
|
264
|
+
process.terminate()
|
|
265
|
+
try:
|
|
266
|
+
process.wait(timeout=3) # Wait for process to terminate
|
|
267
|
+
except subprocess.TimeoutExpired:
|
|
268
|
+
process.kill() # Force kill if not terminated
|
|
269
|
+
cls._processes.clear()
|
|
270
|
+
|
|
271
|
+
def _get_kuavo_ws_root(self) -> str:
|
|
272
|
+
# For WebSocket version, we'll use environment variable instead of ROS param
|
|
273
|
+
model_path = os.environ.get('KUAVO_MODEL_PATH')
|
|
274
|
+
if not model_path:
|
|
275
|
+
raise Exception("KUAVO_MODEL_PATH environment variable not found")
|
|
276
|
+
|
|
277
|
+
if not os.path.exists(model_path):
|
|
278
|
+
raise Exception(f"Model path {model_path} not found")
|
|
279
|
+
|
|
280
|
+
# ws
|
|
281
|
+
return model_path.replace('/src/kuavo_assets/models', '')
|
|
282
|
+
|
|
283
|
+
def Init(self) -> bool:
|
|
284
|
+
"""
|
|
285
|
+
Initialize the WebSocket environment.
|
|
286
|
+
Raises:
|
|
287
|
+
Exception: If the KUAVO_MODEL_PATH environment variable is not found or the path is not valid.
|
|
288
|
+
"""
|
|
289
|
+
# if generate docs, skip init.
|
|
290
|
+
if 'GEN_KUAVO_HUMANOID_SDK_DOCS' in os.environ:
|
|
291
|
+
return True
|
|
292
|
+
|
|
293
|
+
# Return directly if already initialized successfully
|
|
294
|
+
if self._init_success:
|
|
295
|
+
return True
|
|
296
|
+
|
|
297
|
+
# Check if WebSocket server is running
|
|
298
|
+
try:
|
|
299
|
+
|
|
300
|
+
self.websocket = WebSocketKuavoSDK()
|
|
301
|
+
if not self.websocket.client.is_connected:
|
|
302
|
+
print(f"\033[31m\nError: Can't connect to WebSocket server. Please ensure the server is running.\033[0m"
|
|
303
|
+
"\nMaybe manually launch the app first?"
|
|
304
|
+
"\n - for example(sim): roslaunch humanoid_controller load_kuavo_mujoco_sim.launch, "
|
|
305
|
+
"\n - for example(real): roslaunch humanoid_controller load_kuavo_real.launch\n")
|
|
306
|
+
exit(1)
|
|
307
|
+
except Exception as e:
|
|
308
|
+
print(f"\033[31m\nError: Failed to connect to WebSocket server: {e}\033[0m")
|
|
309
|
+
exit(1)
|
|
310
|
+
|
|
311
|
+
# Only check nodes exist when Init SDK, if not, tips user manually launch nodes.
|
|
312
|
+
deps_nodes = ['/humanoid_gait_switch_by_name']
|
|
313
|
+
for node in deps_nodes:
|
|
314
|
+
if not self.check_rosnode_exists(node):
|
|
315
|
+
print(f"\033[31m\nError: Node {node} not found. Please launch it manually.\033[0m")
|
|
316
|
+
exit(1)
|
|
317
|
+
|
|
318
|
+
self._init_success = True # Set flag after successful initialization
|
|
319
|
+
|
|
320
|
+
return True
|
|
321
|
+
|
|
322
|
+
def _launch_ros_node(self, node_name, launch_cmd, log_name):
|
|
323
|
+
"""Launch a ROS node with the given command and log the output.
|
|
324
|
+
|
|
325
|
+
Args:
|
|
326
|
+
node_name (str): Name of the node to launch
|
|
327
|
+
launch_cmd (str): Full launch command including source and roslaunch
|
|
328
|
+
log_name (str): Name for the log file
|
|
329
|
+
|
|
330
|
+
Raises:
|
|
331
|
+
Exception: If node launch fails
|
|
332
|
+
"""
|
|
333
|
+
# Launch in background and check if successful
|
|
334
|
+
try:
|
|
335
|
+
os.makedirs('/var/log/kuavo_humanoid_sdk', exist_ok=True)
|
|
336
|
+
log_path = f'/var/log/kuavo_humanoid_sdk/{log_name}.log'
|
|
337
|
+
except PermissionError:
|
|
338
|
+
os.makedirs('log/kuavo_humanoid_sdk', exist_ok=True)
|
|
339
|
+
log_path = f'log/kuavo_humanoid_sdk/{log_name}.log'
|
|
340
|
+
|
|
341
|
+
with open(log_path, 'w') as log_file:
|
|
342
|
+
process = subprocess.Popen(launch_cmd, shell=True, executable='/bin/bash', stdout=log_file, stderr=log_file)
|
|
343
|
+
self._processes.append(process) # Add process to tracking list
|
|
344
|
+
|
|
345
|
+
if process.returncode is not None and process.returncode != 0:
|
|
346
|
+
raise Exception(f"Failed to launch {node_name}, return code: {process.returncode}")
|
|
347
|
+
|
|
348
|
+
SDKLogger.info(f"{node_name} launched successfully")
|
|
349
|
+
|
|
350
|
+
def _get_setup_file(self, ws_root=None):
|
|
351
|
+
"""Get the appropriate ROS setup file path based on shell type.
|
|
352
|
+
|
|
353
|
+
Args:
|
|
354
|
+
ws_root (str, optional): ROS workspace root path. If None, uses ROS_WORKSPACE.
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
str: Path to the setup file
|
|
358
|
+
|
|
359
|
+
Raises:
|
|
360
|
+
Exception: If setup file not found
|
|
361
|
+
"""
|
|
362
|
+
is_zsh = 'zsh' in os.environ.get('SHELL', '')
|
|
363
|
+
|
|
364
|
+
if ws_root is None:
|
|
365
|
+
ws_root = os.environ['ROS_WORKSPACE']
|
|
366
|
+
|
|
367
|
+
setup_files = {
|
|
368
|
+
'zsh': os.path.join(ws_root, 'devel/setup.zsh'),
|
|
369
|
+
'bash': os.path.join(ws_root, 'devel/setup.bash')
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
setup_file = setup_files['zsh'] if is_zsh else setup_files['bash']
|
|
373
|
+
if not os.path.exists(setup_file):
|
|
374
|
+
setup_file = setup_file.replace('devel', 'install')
|
|
375
|
+
if not os.path.exists(setup_file):
|
|
376
|
+
raise Exception(f"Setup file not found in either devel or install: {setup_file}")
|
|
377
|
+
|
|
378
|
+
return setup_file
|
|
379
|
+
|
|
380
|
+
def launch_ik_node(self):
|
|
381
|
+
# nodes: /arms_ik_node
|
|
382
|
+
# services: /ik/two_arm_hand_pose_cmd_srv, /ik/fk_srv
|
|
383
|
+
try:
|
|
384
|
+
if not self.websocket or not self.websocket.client.is_connected:
|
|
385
|
+
raise Exception("WebSocket server is not running")
|
|
386
|
+
except Exception as e:
|
|
387
|
+
raise Exception(f"WebSocket server is not running: {e}")
|
|
388
|
+
|
|
389
|
+
# Check if IK node and services exist
|
|
390
|
+
try:
|
|
391
|
+
# Check if arms_ik_node is running using roslibpy
|
|
392
|
+
service = roslibpy.Service(self.websocket.client, '/rosnode/list', 'rosapi/Nodes')
|
|
393
|
+
response = service.call({})
|
|
394
|
+
nodes = response.get('nodes', [])
|
|
395
|
+
|
|
396
|
+
if '/arms_ik_node' not in nodes:
|
|
397
|
+
# Launch IK node if not running
|
|
398
|
+
kuavo_ws_root = self._get_kuavo_ws_root()
|
|
399
|
+
setup_file = self._get_setup_file(kuavo_ws_root)
|
|
400
|
+
source_cmd = f"source {setup_file}"
|
|
401
|
+
|
|
402
|
+
# Get robot version from environment variable
|
|
403
|
+
robot_version = os.environ.get('ROBOT_VERSION')
|
|
404
|
+
if robot_version is None:
|
|
405
|
+
raise Exception("Failed to get ROBOT_VERSION from environment variables")
|
|
406
|
+
|
|
407
|
+
# Launch IK node
|
|
408
|
+
launch_cmd = f"roslaunch motion_capture_ik ik_node.launch robot_version:={robot_version}"
|
|
409
|
+
full_cmd = f"{source_cmd} && {launch_cmd}"
|
|
410
|
+
|
|
411
|
+
self._launch_ros_node('IK node', full_cmd, 'launch_ik')
|
|
412
|
+
|
|
413
|
+
return True
|
|
414
|
+
|
|
415
|
+
except Exception as e:
|
|
416
|
+
raise Exception(f"Failed to verify IK node and services: {e}")
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def launch_gait_switch_node(self)-> bool:
|
|
420
|
+
"""Verify that the gait switch node is running, launch if not."""
|
|
421
|
+
try:
|
|
422
|
+
# Check if node exists using roslibpy
|
|
423
|
+
service = roslibpy.Service(self.websocket.client, '/rosnode/list', 'rosapi/Nodes')
|
|
424
|
+
response = service.call({})
|
|
425
|
+
nodes = response.get('nodes', [])
|
|
426
|
+
|
|
427
|
+
if '/humanoid_gait_switch_by_name' not in nodes:
|
|
428
|
+
kuavo_ws_root = self._get_kuavo_ws_root()
|
|
429
|
+
setup_file = self._get_setup_file(kuavo_ws_root)
|
|
430
|
+
source_cmd = f"source {setup_file}"
|
|
431
|
+
|
|
432
|
+
# Launch gait switch node
|
|
433
|
+
launch_cmd = "roslaunch humanoid_interface_ros humanoid_switch_gait.launch"
|
|
434
|
+
full_cmd = f"{source_cmd} && {launch_cmd}"
|
|
435
|
+
|
|
436
|
+
self._launch_ros_node('Gait switch node', full_cmd, 'launch_gait_switch')
|
|
437
|
+
|
|
438
|
+
except Exception as e:
|
|
439
|
+
raise Exception(f"Failed to launch gait_switch node: {e}")
|
|
440
|
+
|
|
441
|
+
@staticmethod
|
|
442
|
+
def check_rosnode_exists(node_name):
|
|
443
|
+
"""Check if a ROS node is running using roslibpy.
|
|
444
|
+
|
|
445
|
+
Args:
|
|
446
|
+
node_name (str): Name of the node to check
|
|
447
|
+
|
|
448
|
+
Returns:
|
|
449
|
+
bool: True if node is running, False otherwise
|
|
450
|
+
"""
|
|
451
|
+
websocket = WebSocketKuavoSDK()
|
|
452
|
+
try:
|
|
453
|
+
if not websocket or not websocket.client.is_connected:
|
|
454
|
+
return False
|
|
455
|
+
|
|
456
|
+
service = roslibpy.Service(websocket.client, '/rosapi/nodes', 'rosapi/Nodes')
|
|
457
|
+
response = service.call({})
|
|
458
|
+
nodes = response.get('nodes', [])
|
|
459
|
+
return node_name in nodes
|
|
460
|
+
except Exception as e:
|
|
461
|
+
SDKLogger.error(f"Error checking if node {node_name} exists: {e}")
|
|
462
|
+
return False
|