Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The kinematic model fixtures are a pinned copy of rdk's, so GitHub collapses them in a diff. This lives here and not in the
# .gitattributes inside the fixture directory so that the copy stays identical to rdk.
tests/data/kinematic_models/** linguist-generated
50 changes: 48 additions & 2 deletions src/viam/components/arm/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from viam.components import KinematicsReturn
from viam.components.component_base import ComponentBase
from viam.errors import MethodNotImplementedError
from viam.proto.common import KinematicModel
from viam.proto.component.arm import (
GetPropertiesResponse,
JointAccelerations,
Expand Down Expand Up @@ -417,7 +419,6 @@ async def is_moving(self) -> bool:
"""
...

@abc.abstractmethod
async def get_kinematics(
self,
*,
Expand All @@ -430,6 +431,9 @@ async def get_kinematics(
"""
Get the kinematics information associated with the arm.

.. deprecated:: 0.86.0
Use ``get_kinematic_model``, which returns the typed model. A new arm implements that method and leaves this one alone.

::

my_arm = Arm.from_robot(robot=machine, name="my_arm")
Expand Down Expand Up @@ -458,7 +462,49 @@ async def get_kinematics(

For more information, see `Arm component <https://docs.viam.com/reference/apis/components/arm/#getkinematics>`_.
"""
...
raise MethodNotImplementedError("get_kinematics")

async def get_kinematic_model(
self,
*,
include_collision_meshes: bool = False,
include_visual_meshes: bool = False,
extra: dict[str, Any] | None = None,
timeout: float | None = None,
**kwargs,
) -> KinematicModel:
"""
Get the kinematic model of the arm: its links, joints with their limits, output frames and properties.
Lengths are in millimeters and angles in degrees.

::

my_arm = Arm.from_robot(robot=machine, name="my_arm")

model = await my_arm.get_kinematic_model()

for joint in model.joints:
print(joint.id, joint.limits.min, joint.limits.max, joint.limits.max_velocity)

A module implements this by returning the model it holds, which ``viam.kinematics.read_kinematic_model_file`` reads from a
kinematic model file. It may ignore the two flags and return every mesh, since the server drops the bytes a request did not
ask for and fills the deprecated file fields for a client that predates the model.

Args:
include_collision_meshes (bool): Include collision mesh bytes. Otherwise a collision mesh carries only its path and
content type.
include_visual_meshes (bool): Include visual mesh bytes. Otherwise a visual mesh carries only its path and content type.
With neither flag set a server sends every mesh whole, as it did before the flags existed, so set one to leave the
other out.

Raises:
NotSupportedError: If the arm is served by a machine that predates the model and sent only the kinematics file, which
``get_kinematics`` still returns.

Returns:
KinematicModel: The kinematic model of the arm.
"""
raise MethodNotImplementedError("get_kinematic_model")

@abc.abstractmethod
async def get_3d_models(self, *, extra: dict[str, Any] | None = None, timeout: float | None = None, **kwargs) -> Mapping[str, Mesh]:
Expand Down
23 changes: 23 additions & 0 deletions src/viam/components/arm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from grpclib.client import Channel

from viam.components import KinematicsReturn
from viam.errors import NotSupportedError
from viam.proto.common import (
DoCommandRequest,
DoCommandResponse,
Expand All @@ -14,6 +15,7 @@
GetKinematicsResponse,
GetStatusRequest,
GetStatusResponse,
KinematicModel,
Mesh,
)
from viam.proto.component.arm import (
Expand Down Expand Up @@ -290,6 +292,27 @@ async def get_kinematics(
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath, response.model)
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath)

async def get_kinematic_model(
self,
*,
include_collision_meshes: bool = False,
include_visual_meshes: bool = False,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> KinematicModel:
md = kwargs.get("metadata", self.Metadata()).proto
request = GetKinematicsRequest(
name=self.name,
include_collision_meshes=include_collision_meshes,
include_visual_meshes=include_visual_meshes,
extra=dict_to_struct(extra),
)
response: GetKinematicsResponse = await self.client.GetKinematics(request, timeout=timeout, metadata=md)
if not response.HasField("model"):
raise NotSupportedError(f"arm {self.name} sent no kinematic model, only the kinematics file that get_kinematics returns")
return response.model

async def get_3d_models(
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
) -> Mapping[str, Mesh]:
Expand Down
18 changes: 2 additions & 16 deletions src/viam/components/arm/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from grpclib import GRPCError, Status
from grpclib.server import Stream

from viam.kinematics import kinematics_response
from viam.proto.common import (
DoCommandRequest,
DoCommandResponse,
Expand Down Expand Up @@ -249,22 +250,7 @@ async def GetKinematics(self, stream: Stream[GetKinematicsRequest, GetKinematics
assert request is not None
arm = self.get_resource(request.name)
timeout = stream.deadline.time_remaining() if stream.deadline else None
kinematics = await arm.get_kinematics(
include_collision_meshes=request.include_collision_meshes,
include_visual_meshes=request.include_visual_meshes,
extra=struct_to_dict(request.extra),
timeout=timeout,
metadata=stream.metadata,
)
model = None
if len(kinematics) == 2:
format, kinematics_data = kinematics
meshes = {}
elif len(kinematics) == 3:
format, kinematics_data, meshes = kinematics
else:
format, kinematics_data, meshes, model = kinematics
response = GetKinematicsResponse(format=format, kinematics_data=kinematics_data, meshes_by_urdf_filepath=meshes, model=model)
response = await kinematics_response(arm, request, timeout=timeout, metadata=stream.metadata)
await stream.send_message(response)

async def Get3DModels(self, stream: Stream[Get3DModelsRequest, Get3DModelsResponse]) -> None:
Expand Down
23 changes: 23 additions & 0 deletions src/viam/components/gantry/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from grpclib.client import Channel

from viam.components import KinematicsReturn
from viam.errors import NotSupportedError
from viam.proto.common import (
DoCommandRequest,
DoCommandResponse,
Expand All @@ -11,6 +12,7 @@
GetKinematicsResponse,
GetStatusRequest,
GetStatusResponse,
KinematicModel,
)
from viam.proto.component.gantry import (
GantryServiceStub,
Expand Down Expand Up @@ -151,6 +153,27 @@ async def get_kinematics(
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath, response.model)
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath)

async def get_kinematic_model(
self,
*,
include_collision_meshes: bool = False,
include_visual_meshes: bool = False,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> KinematicModel:
md = kwargs.get("metadata", self.Metadata()).proto
request = GetKinematicsRequest(
name=self.name,
include_collision_meshes=include_collision_meshes,
include_visual_meshes=include_visual_meshes,
extra=dict_to_struct(extra),
)
response: GetKinematicsResponse = await self.client.GetKinematics(request, timeout=timeout, metadata=md)
if not response.HasField("model"):
raise NotSupportedError(f"gantry {self.name} sent no kinematic model, only the kinematics file that get_kinematics returns")
return response.model

async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]:
md = kwargs.get("metadata", self.Metadata())
return await get_geometries(self.client, self.name, extra, timeout, md)
50 changes: 48 additions & 2 deletions src/viam/components/gantry/gantry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from viam.components import KinematicsReturn
from viam.components.component_base import ComponentBase
from viam.errors import MethodNotImplementedError
from viam.proto.common import KinematicModel
from viam.resource.types import API, RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT


Expand Down Expand Up @@ -155,7 +157,6 @@ async def is_moving(self) -> bool:
"""
...

@abc.abstractmethod
async def get_kinematics(
self,
*,
Expand All @@ -168,6 +169,9 @@ async def get_kinematics(
"""
Get the kinematics information associated with the gantry.

.. deprecated:: 0.86.0
Use ``get_kinematic_model``, which returns the typed model. A new gantry implements that method and leaves this one alone.

::

my_gantry = Gantry.from_robot(robot=machine, name="my_gantry")
Expand Down Expand Up @@ -195,4 +199,46 @@ async def get_kinematics(

For more information, see `Arm component <https://docs.viam.com/reference/apis/components/arm/#getkinematics>`_.
"""
...
raise MethodNotImplementedError("get_kinematics")

async def get_kinematic_model(
self,
*,
include_collision_meshes: bool = False,
include_visual_meshes: bool = False,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> KinematicModel:
"""
Get the kinematic model of the gantry: its links, joints with their limits, output frames and properties.
Lengths are in millimeters and angles in degrees.

::

my_gantry = Gantry.from_robot(robot=machine, name="my_gantry")

model = await my_gantry.get_kinematic_model()

for joint in model.joints:
print(joint.id, joint.limits.min, joint.limits.max, joint.limits.max_velocity)

A module implements this by returning the model it holds, which ``viam.kinematics.read_kinematic_model_file`` reads from a
kinematic model file. It may ignore the two flags and return every mesh, since the server drops the bytes a request did not
ask for and fills the deprecated file fields for a client that predates the model.

Args:
include_collision_meshes (bool): Include collision mesh bytes. Otherwise a collision mesh carries only its path and
content type.
include_visual_meshes (bool): Include visual mesh bytes. Otherwise a visual mesh carries only its path and content type.
With neither flag set a server sends every mesh whole, as it did before the flags existed, so set one to leave the
other out.

Raises:
NotSupportedError: If the gantry is served by a machine that predates the model and sent only the kinematics file, which
``get_kinematics`` still returns.

Returns:
KinematicModel: The kinematic model of the gantry.
"""
raise MethodNotImplementedError("get_kinematic_model")
18 changes: 2 additions & 16 deletions src/viam/components/gantry/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from grpclib.server import Stream

from viam.kinematics import kinematics_response
from viam.proto.common import (
DoCommandRequest,
DoCommandResponse,
Expand Down Expand Up @@ -126,22 +127,7 @@ async def GetKinematics(self, stream: Stream[GetKinematicsRequest, GetKinematics
assert request is not None
gantry = self.get_resource(request.name)
timeout = stream.deadline.time_remaining() if stream.deadline else None
kinematics = await gantry.get_kinematics(
include_collision_meshes=request.include_collision_meshes,
include_visual_meshes=request.include_visual_meshes,
extra=struct_to_dict(request.extra),
timeout=timeout,
metadata=stream.metadata,
)
model = None
if len(kinematics) == 2:
format, data = kinematics
meshes = {}
elif len(kinematics) == 3:
format, data, meshes = kinematics
else:
format, data, meshes, model = kinematics
response = GetKinematicsResponse(format=format, kinematics_data=data, meshes_by_urdf_filepath=meshes, model=model)
response = await kinematics_response(gantry, request, timeout=timeout, metadata=stream.metadata)
await stream.send_message(response)

async def GetGeometries(self, stream: Stream[GetGeometriesRequest, GetGeometriesResponse]) -> None:
Expand Down
23 changes: 23 additions & 0 deletions src/viam/components/gripper/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from grpclib.client import Channel

from viam.components import KinematicsReturn
from viam.errors import NotSupportedError
from viam.proto.common import (
DoCommandRequest,
DoCommandResponse,
Expand All @@ -11,6 +12,7 @@
GetKinematicsResponse,
GetStatusRequest,
GetStatusResponse,
KinematicModel,
)
from viam.proto.component.gripper import (
GetCurrentInputsRequest,
Expand Down Expand Up @@ -138,6 +140,27 @@ async def get_kinematics(
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath, response.model)
return (response.format, response.kinematics_data, response.meshes_by_urdf_filepath)

async def get_kinematic_model(
self,
*,
include_collision_meshes: bool = False,
include_visual_meshes: bool = False,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
) -> KinematicModel:
md = kwargs.get("metadata", self.Metadata()).proto
request = GetKinematicsRequest(
name=self.name,
include_collision_meshes=include_collision_meshes,
include_visual_meshes=include_visual_meshes,
extra=dict_to_struct(extra),
)
response: GetKinematicsResponse = await self.client.GetKinematics(request, timeout=timeout, metadata=md)
if not response.HasField("model"):
raise NotSupportedError(f"gripper {self.name} sent no kinematic model, only the kinematics file that get_kinematics returns")
return response.model

async def get_current_inputs(
self,
*,
Expand Down
Loading
Loading