feat: add more necessary params

This commit is contained in:
wznln
2025-05-06 20:18:49 +08:00
parent b6a3f17e9b
commit e373220ce3
6 changed files with 66 additions and 17 deletions

View File

@@ -77,9 +77,9 @@ def parse_args():
) )
parser.add_argument( parser.add_argument(
"--visual", "--visual",
choices=["rviz", "web", "disable"], choices=["rviz", "web", "deck", "disable"],
default="disable", default="disable",
help="选择可视化工具: rviz, web", help="选择可视化工具: rviz, web, deck(2D bird view)",
) )
return parser.parse_args() return parser.parse_args()
@@ -179,7 +179,7 @@ def main():
signal.signal(signal.SIGTERM, _exit) signal.signal(signal.SIGTERM, _exit)
mqtt_client.start() mqtt_client.start()
args_dict["resources_mesh_config"] = {} args_dict["resources_mesh_config"] = {}
# web visiualize 2D
if args_dict["visual"] != "disable": if args_dict["visual"] != "disable":
enable_rviz = args_dict["visual"] == "rviz" enable_rviz = args_dict["visual"] == "rviz"
if devices_and_resources is not None: if devices_and_resources is not None:

View File

@@ -37,6 +37,9 @@ class Registry:
"goal": { "goal": {
"resources": "resources", "resources": "resources",
"device_ids": "device_ids", "device_ids": "device_ids",
"bind_parent_ids": "bind_parent_ids",
"bind_locations": "bind_locations",
"other_calling_params": "other_calling_params",
}, },
"feedback": {}, "feedback": {},
"result": { "result": {

View File

@@ -1,5 +1,5 @@
import sys import traceback
from unilabos.utils.log import logger
resource_schema = { resource_schema = {
"workstation": {"type": "object", "properties": {}}, "workstation": {"type": "object", "properties": {}},
@@ -132,7 +132,8 @@ def add_schema(resources_config: list[dict]) -> list[dict]:
try: try:
if type(resource["children"][0]) == dict: if type(resource["children"][0]) == dict:
resource["children"] = add_schema(resource["children"]) resource["children"] = add_schema(resource["children"])
except: except Exception as ex:
sys.exit(0) logger.error("添加物料schema时出错")
traceback.print_exc()
return resources_config return resources_config

View File

@@ -15,8 +15,9 @@ from rclpy.action.server import ServerGoalHandle
from rclpy.client import Client from rclpy.client import Client
from rclpy.callback_groups import ReentrantCallbackGroup from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.service import Service from rclpy.service import Service
from unilabos_msgs.srv._serial_command import SerialCommand_Request
from unilabos.resources.graphio import convert_resources_to_type, convert_resources_from_type from unilabos.resources.graphio import convert_resources_to_type, convert_resources_from_type, resource_ulab_to_plr
from unilabos.ros.msgs.message_converter import ( from unilabos.ros.msgs.message_converter import (
convert_to_ros_msg, convert_to_ros_msg,
convert_from_ros_msg, convert_from_ros_msg,
@@ -304,7 +305,42 @@ class BaseROS2DeviceNode(Node, Generic[T]):
res.response = "" res.response = ""
return res return res
def append_resource(req, res): def append_resource(req: SerialCommand_Request, res):
# 物料传输到对应的node节点
rclient = self.create_client(ResourceAdd, "/resources/add")
rclient.wait_for_service()
request = ResourceAdd.Request()
command_json = json.loads(req.command)
namespace = command_json["namespace"]
bind_parent_id = command_json["bind_parent_id"]
edge_device_id = command_json["edge_device_id"]
location = command_json["location"]
other_calling_param = command_json["other_calling_param"]
resources = command_json["resource"]
# 本地拿到这个物料,可能需要先做初始化?
if isinstance(resources, list):
request.resources = [convert_to_ros_msg(Resource, resource) for resource in resources]
else:
request.resources = [convert_to_ros_msg(Resource, resources)]
response = rclient.call(request)
# 应该本地先add_resource
res.response = "OK"
# 接下来该根据bind_parent_id进行assign了目前只有plr可以进行assign不然没有办法输入到物料系统中
resource = self.resource_tracker.figure_resource({"name": bind_parent_id})
try:
from pylabrobot.resources.resource import Resource as ResourcePLR
from pylabrobot.resources.deck import Deck
from pylabrobot.resources import Coordinate
contain_model = not isinstance(resource, Deck)
if isinstance(resource, ResourcePLR):
# resources.list()
plr_instance = resource_ulab_to_plr(resources, contain_model)
resource.assign_child_resource(plr_instance, Coordinate(location["x"], location["y"], location["z"]), **other_calling_param)
except ImportError:
self.lab_logger().error("Host请求添加物料时本环境并不存在pylabrobot")
except Exception as e:
self.lab_logger().error("Host请求添加物料时出错")
self.lab_logger().error(traceback.format_exc())
pass pass
self._service_server: Dict[str, Service] = { self._service_server: Dict[str, Service] = {

View File

@@ -7,6 +7,7 @@ import uuid
from typing import Optional, Dict, Any, List, ClassVar, Set from typing import Optional, Dict, Any, List, ClassVar, Set
from action_msgs.msg import GoalStatus from action_msgs.msg import GoalStatus
from geometry_msgs.msg import Point
from rclpy.action import ActionClient, get_action_server_names_and_types_by_node from rclpy.action import ActionClient, get_action_server_names_and_types_by_node
from rclpy.callback_groups import ReentrantCallbackGroup from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.service import Service from rclpy.service import Service
@@ -267,21 +268,27 @@ class HostNode(BaseROS2DeviceNode):
except Exception as e: except Exception as e:
self.lab_logger().error(f"[Host Node] Failed to create ActionClient for {action_id}: {str(e)}") self.lab_logger().error(f"[Host Node] Failed to create ActionClient for {action_id}: {str(e)}")
def add_resource_from_outer(self, resources: list["Resource"], device_ids: list[str], bind_parent_ids: list[str]): def add_resource_from_outer(self, resources: list["Resource"], device_ids: list[str], bind_parent_ids: list[str], bind_locations: list[Point], other_calling_params: list[str]):
for resource, device_id, bind_parent_id in zip(resources, device_ids, bind_parent_ids): for resource, device_id, bind_parent_id, bind_location, other_calling_param in zip(resources, device_ids, bind_parent_ids, bind_locations, other_calling_params):
# 这里要求device_id传入必须是edge_device_id # 这里要求device_id传入必须是edge_device_id
namespace = "/devices/" + device_id namespace = "/devices/" + device_id
srv_address = f"/srv{namespace}/append_resource" srv_address = f"/srv{namespace}/append_resource"
sclient = self.create_client(SerialCommand, srv_address) sclient = self.create_client(SerialCommand, srv_address)
sclient.wait_for_service()
request = SerialCommand.Request() request = SerialCommand.Request()
request.command = json.dumps({ request.command = json.dumps({
"machine_name": BasicConfig.machine_name, "resource": resource,
"type": "slave", "namespace": namespace,
"devices_config": devices_config_copy, "edge_device_id": device_id,
"registry_config": lab_registry.obtain_registry_device_info() "bind_parent_id": bind_parent_id,
}, ensure_ascii=False, cls=TypeEncoder) "bind_location": {
"x": bind_location.x,
"y": bind_location.y,
"z": bind_location.z,
},
"other_calling_param": json.loads(other_calling_param),
}, ensure_ascii=False)
response = sclient.call(request) response = sclient.call(request)
print("111")
pass pass
def initialize_device(self, device_id: str, device_config: Dict[str, Any]) -> None: def initialize_device(self, device_id: str, device_config: Dict[str, Any]) -> None:

View File

@@ -1,6 +1,8 @@
Resource[] resources Resource[] resources
string[] device_ids string[] device_ids
string[] bind_parent_ids string[] bind_parent_ids
geometry_msgs/Point[] bind_locations
string[] other_calling_params
--- ---
bool success bool success
--- ---