create container

This commit is contained in:
Xuwznln
2025-06-15 12:51:37 +08:00
parent 5478ba3237
commit 934276d2f7
7 changed files with 116 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
container:
description: regular organic container
class:
module: unilabos.resources.container:RegularContainer
type: unilabos

View File

@@ -0,0 +1,61 @@
import json
from unilabos_msgs.msg import Resource
from unilabos.ros.msgs.message_converter import convert_from_ros_msg
class RegularContainer(object):
# 第一个参数必须是id传入
# noinspection PyShadowingBuiltins
def __init__(self, id: str, data: dict = None):
self.id = id
self.ulr_resource = Resource()
self.ulr_resource_data = data
@property
def ulr_resource_data(self):
return json.loads(self.ulr_resource.data) if self.ulr_resource.data else {}
@ulr_resource_data.setter
def ulr_resource_data(self, value: dict):
self.ulr_resource.data = json.dumps(value)
@property
def liquid_type(self):
return self.ulr_resource_data.get("liquid_type", None)
@liquid_type.setter
def liquid_type(self, value: str):
if value is not None:
self.ulr_resource_data["liquid_type"] = value
else:
self.ulr_resource_data.pop("liquid_type", None)
@property
def liquid_volume(self):
return self.ulr_resource_data.get("liquid_volume", None)
@liquid_volume.setter
def liquid_volume(self, value: float):
if value is not None:
self.ulr_resource_data["liquid_volume"] = value
else:
self.ulr_resource_data.pop("liquid_volume", None)
def get_ulr_resource(self) -> Resource:
"""
获取UlrResource对象
:return: UlrResource对象
"""
return self.ulr_resource
def get_ulr_resource_as_dict(self) -> Resource:
"""
获取UlrResource对象
:return: UlrResource对象
"""
return convert_from_ros_msg(self.ulr_resource)
def __str__(self):
return f"{self.id}"

View File

@@ -5,6 +5,8 @@ from typing import Union
import numpy as np
import networkx as nx
from unilabos.resources.container import RegularContainer
try:
from pylabrobot.resources.resource import Resource as ResourcePLR
except ImportError:
@@ -466,6 +468,9 @@ def initialize_resource(resource_config: dict) -> list[dict]:
if resource_config.get("position") is not None:
r["position"] = resource_config["position"]
r = tree_to_list([r])
elif resource_class_config["type"] == "unilabos":
res_instance: RegularContainer = RESOURCE(id=resource_config["name"], data=resource_config.get("data", {}))
r = [res_instance.get_ulr_resource_as_dict()]
elif isinstance(RESOURCE, dict):
r = [RESOURCE.copy()]

View File

@@ -343,6 +343,8 @@ class BaseROS2DeviceNode(Node, Generic[T]):
ADD_LIQUID_TYPE = other_calling_param.pop("ADD_LIQUID_TYPE", [])
LIQUID_VOLUME = other_calling_param.pop("LIQUID_VOLUME", [])
LIQUID_INPUT_SLOT = other_calling_param.pop("LIQUID_INPUT_SLOT", [])
if len(LIQUID_INPUT_SLOT) and LIQUID_INPUT_SLOT[0] == -1:
print("create container")
slot = other_calling_param.pop("slot", "-1")
if slot != "-1": # slot为负数的时候采用assign方法
other_calling_param["slot"] = slot

View File

@@ -383,18 +383,24 @@ class HostNode(BaseROS2DeviceNode):
liquid_volume: list[int],
slot_on_deck: str,
):
init_new_res = initialize_resource(
{
"name": res_id,
"class": class_name,
"parent": parent,
"position": {
"x": bind_locations.x,
"y": bind_locations.y,
"z": bind_locations.z,
},
}
) # flatten的格式
res_creation_input = {
"name": res_id,
"class": class_name,
"parent": parent,
"position": {
"x": bind_locations.x,
"y": bind_locations.y,
"z": bind_locations.z,
},
}
if len(liquid_input_slot) and liquid_input_slot[0] == -1: # 目前container只逐个创建
res_creation_input.update({
"data": {
"liquid_type": liquid_type[0] if liquid_type else None,
"liquid_volume": liquid_volume[0] if liquid_volume else None,
}
})
init_new_res = initialize_resource(res_creation_input) # flatten的格式
resources = init_new_res # initialize_resource已经返回list[dict]
device_ids = [device_id]
bind_parent_id = [parent]