Skip to content

Commit

Permalink
all changes
Browse files Browse the repository at this point in the history
  • Loading branch information
susmitaSanyal committed Jan 4, 2024
1 parent 49c354f commit 1a89e41
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 83 deletions.
90 changes: 18 additions & 72 deletions src/viam/components/movement_sensor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,131 +37,77 @@ def __init__(self, name: str, channel: Channel):
self.client = MovementSensorServiceStub(channel)
super().__init__(name)

async def get_position(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Tuple[GeoPoint, float]:
async def get_position(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Tuple[GeoPoint, float]:
if extra is None:
extra = {}
request = GetPositionRequest(name=self.name, extra=dict_to_struct(extra))
response: GetPositionResponse = await self.client.GetPosition(request, timeout=timeout)
return response.coordinate, response.altitude_m

async def get_linear_velocity(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Vector3:
async def get_linear_velocity(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Vector3:
if extra is None:
extra = {}
request = GetLinearVelocityRequest(name=self.name, extra=dict_to_struct(extra))
response: GetLinearVelocityResponse = await self.client.GetLinearVelocity(request, timeout=timeout)
return response.linear_velocity

async def get_angular_velocity(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Vector3:
async def get_angular_velocity(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Vector3:
if extra is None:
extra = {}
request = GetAngularVelocityRequest(name=self.name, extra=dict_to_struct(extra))
response: GetAngularVelocityResponse = await self.client.GetAngularVelocity(request, timeout=timeout)
return response.angular_velocity

async def get_linear_acceleration(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Vector3:
async def get_linear_acceleration(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Vector3:
if extra is None:
extra = {}
request = GetLinearAccelerationRequest(name=self.name, extra=dict_to_struct(extra))
response: GetLinearAccelerationResponse = await self.client.GetLinearAcceleration(request, timeout=timeout)
return response.linear_acceleration

async def get_compass_heading(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> float:
async def get_compass_heading(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> float:
if extra is None:
extra = {}
request = GetCompassHeadingRequest(name=self.name, extra=dict_to_struct(extra))
response: GetCompassHeadingResponse = await self.client.GetCompassHeading(request, timeout=timeout)
return response.value

async def get_orientation(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Orientation:
async def get_orientation(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Orientation:
if extra is None:
extra = {}
request = GetOrientationRequest(name=self.name, extra=dict_to_struct(extra))
response: GetOrientationResponse = await self.client.GetOrientation(request, timeout=timeout)
return response.orientation

async def get_properties(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> MovementSensor.Properties:
async def get_properties(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> MovementSensor.Properties:
if extra is None:
extra = {}
request = GetPropertiesRequest(name=self.name, extra=dict_to_struct(extra))
response: GetPropertiesResponse = await self.client.GetProperties(request, timeout=timeout)
return MovementSensor.Properties.from_proto(response)

async def get_accuracy(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Mapping[str, float]:
async def get_accuracy(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> MovementSensor.Accuracy:
if extra is None:
extra = {}
request = GetAccuracyRequest(name=self.name, extra=dict_to_struct(extra))
response: GetAccuracyResponse = await self.client.GetAccuracy(request, timeout=timeout)
return response.accuracy

async def get_readings(
self,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**__,
) -> Mapping[str, SensorReading]:
return MovementSensor.Accuracy(
accuracy=response.accuracy,
position_hdop=response.position_hdop,
position_vdop=response.position_vdop,
position_nmea_gga_fix=response.position_nmea_gga_fix,
compass_degrees_error=response.compass_degrees_error,
)

async def get_readings(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Mapping[str, SensorReading]:
if extra is None:
extra = {}
request = GetReadingsRequest(name=self.name, extra=dict_to_struct(extra))
response: GetReadingsResponse = await self.client.GetReadings(request, timeout=timeout)

return sensor_readings_value_to_native(response.readings)

async def do_command(
self,
command: Mapping[str, ValueTypes],
*,
timeout: Optional[float] = None,
**__,
) -> Mapping[str, ValueTypes]:
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
return struct_to_dict(response.result)
Expand Down
16 changes: 11 additions & 5 deletions src/viam/components/movement_sensor/movement_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class MovementSensor(ComponentBase):
This cannot be used on its own. If the ``__init__()`` function is overridden, it must call the ``super().__init__()`` function.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "movement_sensor"
)
SUBTYPE: Final = Subtype(RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "movement_sensor")

@dataclass
class Properties:
Expand Down Expand Up @@ -54,6 +52,14 @@ def from_proto(cls, proto: GetPropertiesResponse) -> Self:
linear_velocity_supported=proto.linear_velocity_supported,
)

@dataclass
class Accuracy:
accuracy: Mapping[str, float]
position_hdop: float
position_vdop: float
position_nmea_gga_fix: int
compass_degrees_error: float

@abc.abstractmethod
async def get_position(
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
Expand Down Expand Up @@ -124,11 +130,11 @@ async def get_properties(self, *, extra: Optional[Dict[str, Any]] = None, timeou
@abc.abstractmethod
async def get_accuracy(
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
) -> Mapping[str, float]:
) -> Accuracy:
"""Get the accuracy of the various sensors
Returns:
Dict[str, float]: The accuracy
MovementSensor.Accuracy: The accuracies of the movement sensor
"""
...

Expand Down
12 changes: 9 additions & 3 deletions src/viam/components/movement_sensor/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from viam.utils import dict_to_struct, sensor_readings_native_to_value, struct_to_dict


class MovementSensorRPCService(MovementSensorServiceBase, ResourceRPCServiceBase[MovementSensor]):
class MovementSensorRPCService(MovementSensorServiceBase, ResourceRPCServiceBase):
"""
gRPC Service for a MovementSensor
"""
Expand Down Expand Up @@ -114,8 +114,14 @@ async def GetAccuracy(self, stream: Stream[GetAccuracyRequest, GetAccuracyRespon
name = request.name
sensor = self.get_resource(name)
timeout = stream.deadline.time_remaining() if stream.deadline else None
accuracy = await sensor.get_accuracy(extra=struct_to_dict(request.extra), timeout=timeout, metadata=stream.metadata)
response = GetAccuracyResponse(accuracy=accuracy)
accuracy = await sensor.get_accuracy(timeout=timeout, metadata=stream.metadata)
response = GetAccuracyResponse(
accuracy=accuracy.accuracy,
position_hdop=accuracy.position_hdop,
position_vdop=accuracy.position_vdop,
position_nmea_gga_fix=accuracy.position_nmea_gga_fix,
compass_degrees_error=accuracy.compass_degree_error,
)
await stream.send_message(response)

async def DoCommand(self, stream: Stream[DoCommandRequest, DoCommandResponse]) -> None:
Expand Down
5 changes: 3 additions & 2 deletions tests/mocks/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ async def home(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optiona
self.homed = True
self.extra = extra
self.timeout = timeout
print("called home")
return self.homed

async def get_lengths(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[float]:
Expand Down Expand Up @@ -810,7 +811,7 @@ def __init__(
heading: float,
orientation: Orientation,
properties: MovementSensor.Properties,
accuracy: Mapping[str, float],
accuracy: MovementSensor.Accuracy,
readings: Mapping[str, float],
):
super().__init__(name)
Expand Down Expand Up @@ -871,7 +872,7 @@ async def get_properties(

async def get_accuracy(
self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
) -> Mapping[str, float]:
) -> MovementSensor.Accuracy:
self.extra = extra
self.timeout = timeout
return self.accuracy
Expand Down
9 changes: 8 additions & 1 deletion tests/test_movement_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@
position_supported=True,
compass_heading_supported=False,
)
ACCURACY = {"foo": 0.1, "bar": 2, "baz": 3.14}
ACCURACY = MovementSensor.Accuracy(
accuracy={"foo": 0.1, "bar": 2, "baz": 3.14},
position_hdop=1.03,
position_vdop=2.09,
position_nmea_gga_fix=0,
compass_degrees_error=0.0,

)
EXTRA_PARAMS = {"foo": "bar", "baz": [1, 2, 3]}
READINGS = {"a": 1, "b": 2, "c": 3}

Expand Down

0 comments on commit 1a89e41

Please sign in to comment.