Skip to content

Commit 46b75ea

Browse files
viambotnjooma
authored andcommitted
[WORKFLOW] AI update based on proto changes from commit fe78ead
1 parent fe78ead commit 46b75ea

File tree

5 files changed

+99
-6
lines changed

5 files changed

+99
-6
lines changed

src/viam/components/gantry/client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Any, Dict, List, Mapping, Optional
1+
from typing import Any, Dict, List, Mapping, Optional, Tuple
22

33
from grpclib.client import Channel
44

5-
from viam.proto.common import DoCommandRequest, DoCommandResponse, Geometry
5+
from viam.proto.common import DoCommandRequest, DoCommandResponse, Geometry, GetKinematicsRequest, GetKinematicsResponse, KinematicsFileFormat
66
from viam.proto.component.gantry import (
77
GantryServiceStub,
88
GetLengthsRequest,
@@ -110,6 +110,18 @@ async def do_command(
110110
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout, metadata=md)
111111
return struct_to_dict(response.result)
112112

113+
async def get_kinematics(
114+
self,
115+
*,
116+
extra: Optional[Dict[str, Any]] = None,
117+
timeout: Optional[float] = None,
118+
**kwargs,
119+
) -> Tuple[KinematicsFileFormat.ValueType, bytes]:
120+
md = kwargs.get("metadata", self.Metadata()).proto
121+
request = GetKinematicsRequest(name=self.name, extra=dict_to_struct(extra))
122+
response: GetKinematicsResponse = await self.client.GetKinematics(request, timeout=timeout, metadata=md)
123+
return response.format, response.kinematics_data
124+
113125
async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]:
114126
md = kwargs.get("metadata", self.Metadata())
115127
return await get_geometries(self.client, self.name, extra, timeout, md)

src/viam/components/gantry/gantry.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import abc
2-
from typing import Any, Dict, Final, List, Optional
2+
from typing import Any, Dict, Final, List, Optional, Tuple
33

4+
from viam.components.arm import KinematicsFileFormat
45
from viam.resource.types import API, RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT
56

67
from ..component_base import ComponentBase
@@ -154,3 +155,43 @@ async def is_moving(self) -> bool:
154155
For more information, see `Gantry component <https://docs.viam.com/dev/reference/apis/components/gantry/#ismoving>`_.
155156
"""
156157
...
158+
159+
@abc.abstractmethod
160+
async def get_kinematics(
161+
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
162+
) -> Tuple[KinematicsFileFormat.ValueType, bytes]:
163+
"""
164+
Get the kinematics of the gantry.
165+
166+
::
167+
168+
my_gantry = Gantry.from_robot(robot=machine, name="my_gantry")
169+
170+
# Get the kinematics of the gantry.
171+
format, data = await my_gantry.get_kinematics()
172+
173+
Returns:
174+
Tuple[KinematicsFileFormat.ValueType, bytes]: The kinematics file format and the data.
175+
176+
For more information, see `Gantry component <https://docs.viam.com/dev/reference/apis/components/gantry/#getkinematics>`_.
177+
"""
178+
...
179+
180+
@abc.abstractmethod
181+
async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]:
182+
"""
183+
Get geometries of the gantry.
184+
185+
::
186+
187+
my_gantry = Gantry.from_robot(robot=machine, name="my_gantry")
188+
189+
# Get the geometries of the gantry.
190+
geometries = await my_gantry.get_geometries()
191+
192+
Returns:
193+
List[Geometry]: A list of geometries of the gantry.
194+
195+
For more information, see `Gantry component <https://docs.viam.com/dev/reference/apis/components/gantry/#getgeometries>`_.
196+
"""
197+
...

src/viam/components/gantry/service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from grpclib.server import Stream
22

3-
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse
3+
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse, GetKinematicsRequest, GetKinematicsResponse
44
from viam.proto.component.gantry import (
55
GantryServiceBase,
66
GetLengthsRequest,
@@ -103,6 +103,16 @@ async def DoCommand(self, stream: Stream[DoCommandRequest, DoCommandResponse]) -
103103
response = DoCommandResponse(result=dict_to_struct(result))
104104
await stream.send_message(response)
105105

106+
async def GetKinematics(self, stream: Stream[GetKinematicsRequest, GetKinematicsResponse]) -> None:
107+
request = await stream.recv_message()
108+
assert request is not None
109+
name = request.name
110+
gantry = self.get_resource(name)
111+
timeout = stream.deadline.time_remaining() if stream.deadline else None
112+
format, data = await gantry.get_kinematics(extra=struct_to_dict(request.extra), timeout=timeout, metadata=stream.metadata)
113+
response = GetKinematicsResponse(format=format, kinematics_data=data)
114+
await stream.send_message(response)
115+
106116
async def GetGeometries(self, stream: Stream[GetGeometriesRequest, GetGeometriesResponse]) -> None:
107117
request = await stream.recv_message()
108118
assert request is not None

tests/mocks/components.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ def __init__(self, name: str, position: List[float], lengths: List[float]):
556556
self.position = position
557557
self.lengths = lengths
558558
self.is_stopped = True
559+
self.kinematics = (KinematicsFileFormat.KINEMATICS_FILE_FORMAT_SVA, b"\x00\x01\x02")
559560
self.extra = None
560561
self.homed = True
561562
self.speeds = Optional[List[float]]
@@ -602,6 +603,13 @@ async def stop(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optiona
602603
async def is_moving(self) -> bool:
603604
return not self.is_stopped
604605

606+
async def get_kinematics(
607+
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
608+
) -> Tuple[KinematicsFileFormat.ValueType, bytes]:
609+
self.extra = extra
610+
self.timeout = timeout
611+
return self.kinematics
612+
605613
async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> List[Geometry]:
606614
self.extra = extra
607615
self.timeout = timeout

tests/test_gantry.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from viam.components.gantry import GantryClient
44
from viam.components.gantry.service import GantryRPCService
5-
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse
5+
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse, GetKinematicsRequest, GetKinematicsResponse, KinematicsFileFormat
66
from viam.proto.component.gantry import (
77
GantryServiceStub,
88
GetLengthsRequest,
@@ -64,6 +64,11 @@ async def test_extra(self):
6464
await self.gantry.move_to_position([1, 2, 3], [4, 5, 6], extra=extra)
6565
assert self.gantry.extra == extra
6666

67+
async def test_get_kinematics(self):
68+
format, data = await self.gantry.get_kinematics()
69+
assert format == self.gantry.kinematics[0]
70+
assert data == self.gantry.kinematics[1]
71+
6772
async def test_timeout(self):
6873
assert self.gantry.timeout is None
6974

@@ -160,6 +165,15 @@ async def test_do(self):
160165
result = struct_to_dict(response.result)
161166
assert result == {"command": command}
162167

168+
async def test_get_kinematics(self):
169+
async with ChannelFor([self.service]) as channel:
170+
client = GantryServiceStub(channel)
171+
request = GetKinematicsRequest(name=self.gantry.name)
172+
response: GetKinematicsResponse = await client.GetKinematics(request, timeout=1.1)
173+
assert response.format == self.gantry.kinematics[0]
174+
assert response.kinematics_data == self.gantry.kinematics[1]
175+
assert self.gantry.timeout == loose_approx(1.1)
176+
163177
async def test_get_geometries(self):
164178
async with ChannelFor([self.service]) as channel:
165179
client = GantryServiceStub(channel)
@@ -234,8 +248,16 @@ async def test_extra(self):
234248
await client.move_to_position([1, 2, 3], [4, 5, 6], extra=extra)
235249
assert self.gantry.extra == extra
236250

251+
async def test_get_kinematics(self):
252+
async with ChannelFor([self.service]) as channel:
253+
client = GantryClient(self.gantry.name, channel)
254+
format, data = await client.get_kinematics(timeout=1.1)
255+
assert format == self.gantry.kinematics[0]
256+
assert data == self.gantry.kinematics[1]
257+
assert self.gantry.timeout == loose_approx(1.1)
258+
237259
async def test_get_geometries(self):
238260
async with ChannelFor([self.service]) as channel:
239261
client = GantryClient(self.gantry.name, channel)
240262
geometries = await client.get_geometries()
241-
assert geometries == GEOMETRIES
263+
assert geometries == GEOMETRIES

0 commit comments

Comments
 (0)