Skip to content

Commit 509feab

Browse files
committed
get imu
1 parent 9ac40bd commit 509feab

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

openlch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.6.1"
1+
__version__ = "0.7.0"
22

33
from .hal import HAL

openlch/cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def cli() -> None:
2525
- start-video-stream: Start the video stream
2626
- stop-video-stream: Stop the video stream
2727
- get-video-stream-urls: Get the URLs for various video stream formats
28+
- get-imu-data: Get current IMU sensor data (gyroscope and accelerometer readings)
2829
2930
Use 'openlch COMMAND --help' for more information on a specific command.
3031
"""
@@ -268,5 +269,26 @@ def set_torque_enable(settings: List[Tuple[int, str]], ip: str) -> None:
268269
finally:
269270
hal.close()
270271

272+
@cli.command()
273+
@click.argument("ip", default=DEFAULT_IP)
274+
def get_imu_data(ip: str) -> None:
275+
"""Get current IMU sensor data (gyroscope and accelerometer readings)."""
276+
hal = HAL(ip)
277+
try:
278+
imu_data = hal.imu.get_data()
279+
click.echo("IMU Sensor Data:")
280+
click.echo("\nGyroscope (degrees/second):")
281+
click.echo(f" X: {imu_data['gyro']['x']:.2f}")
282+
click.echo(f" Y: {imu_data['gyro']['y']:.2f}")
283+
click.echo(f" Z: {imu_data['gyro']['z']:.2f}")
284+
click.echo("\nAccelerometer (g):")
285+
click.echo(f" X: {imu_data['accel']['x']:.2f}")
286+
click.echo(f" Y: {imu_data['accel']['y']:.2f}")
287+
click.echo(f" Z: {imu_data['accel']['z']:.2f}")
288+
except Exception as e:
289+
click.echo(f"An error occurred: {str(e)}")
290+
finally:
291+
hal.close()
292+
271293
if __name__ == "__main__":
272294
cli()

openlch/hal.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, host: str = '192.168.42.1', port: int = 50051) -> None:
2323
self.__stub = hal_pb_pb2_grpc.ServoControlStub(self.__channel)
2424
self.servo = self.Servo(self.__stub)
2525
self.system = self.System(self.__stub)
26+
self.imu = self.IMU(self.__stub)
2627

2728
def close(self) -> None:
2829
"""Close the gRPC channel."""
@@ -271,3 +272,26 @@ def get_video_stream_urls(self) -> Dict[str, List[str]]:
271272
'mse': list(response.mse),
272273
'rtsp': list(response.rtsp)
273274
}
275+
276+
class IMU:
277+
"""Class for IMU-related operations."""
278+
279+
def __init__(self, stub):
280+
self.__stub = stub
281+
282+
def get_data(self) -> Dict[str, Dict[str, float]]:
283+
"""
284+
Get current IMU sensor data including gyroscope and accelerometer readings.
285+
286+
Returns:
287+
Dict[str, Dict[str, float]]: A dictionary containing gyroscope and accelerometer data:
288+
{
289+
'gyro': {'x': float, 'y': float, 'z': float}, # Angular velocity in degrees/second
290+
'accel': {'x': float, 'y': float, 'z': float} # Linear acceleration in g
291+
}
292+
"""
293+
response = self.__stub.GetImuData(hal_pb_pb2.Empty())
294+
return {
295+
'gyro': {'x': response.gyro.x, 'y': response.gyro.y, 'z': response.gyro.z},
296+
'accel': {'x': response.accel.x, 'y': response.accel.y, 'z': response.accel.z}
297+
}

openlch/hal_pb_pb2.py

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openlch/hal_pb_pb2_grpc.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def __init__(self, channel):
104104
request_serializer=hal__pb__pb2.TorqueEnableSettings.SerializeToString,
105105
response_deserializer=hal__pb__pb2.Empty.FromString,
106106
_registered_method=True)
107+
self.GetImuData = channel.unary_unary(
108+
'/hal_pb.ServoControl/GetImuData',
109+
request_serializer=hal__pb__pb2.Empty.SerializeToString,
110+
response_deserializer=hal__pb__pb2.ImuData.FromString,
111+
_registered_method=True)
107112

108113

109114
class ServoControlServicer(object):
@@ -193,6 +198,12 @@ def SetTorqueEnable(self, request, context):
193198
context.set_details('Method not implemented!')
194199
raise NotImplementedError('Method not implemented!')
195200

201+
def GetImuData(self, request, context):
202+
"""Missing associated documentation comment in .proto file."""
203+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
204+
context.set_details('Method not implemented!')
205+
raise NotImplementedError('Method not implemented!')
206+
196207

197208
def add_ServoControlServicer_to_server(servicer, server):
198209
rpc_method_handlers = {
@@ -266,6 +277,11 @@ def add_ServoControlServicer_to_server(servicer, server):
266277
request_deserializer=hal__pb__pb2.TorqueEnableSettings.FromString,
267278
response_serializer=hal__pb__pb2.Empty.SerializeToString,
268279
),
280+
'GetImuData': grpc.unary_unary_rpc_method_handler(
281+
servicer.GetImuData,
282+
request_deserializer=hal__pb__pb2.Empty.FromString,
283+
response_serializer=hal__pb__pb2.ImuData.SerializeToString,
284+
),
269285
}
270286
generic_handler = grpc.method_handlers_generic_handler(
271287
'hal_pb.ServoControl', rpc_method_handlers)
@@ -654,3 +670,30 @@ def SetTorqueEnable(request,
654670
timeout,
655671
metadata,
656672
_registered_method=True)
673+
674+
@staticmethod
675+
def GetImuData(request,
676+
target,
677+
options=(),
678+
channel_credentials=None,
679+
call_credentials=None,
680+
insecure=False,
681+
compression=None,
682+
wait_for_ready=None,
683+
timeout=None,
684+
metadata=None):
685+
return grpc.experimental.unary_unary(
686+
request,
687+
target,
688+
'/hal_pb.ServoControl/GetImuData',
689+
hal__pb__pb2.Empty.SerializeToString,
690+
hal__pb__pb2.ImuData.FromString,
691+
options,
692+
channel_credentials,
693+
insecure,
694+
call_credentials,
695+
compression,
696+
wait_for_ready,
697+
timeout,
698+
metadata,
699+
_registered_method=True)

0 commit comments

Comments
 (0)