rosros 0.2.5
Simple unified interface to ROS1 / ROS2 Python API
Loading...
Searching...
No Matches
rosros.api Namespace Reference

Utilities for ROS built-in types, and message and service types. More...

Functions

 canonical (typename)
 Returns "pkg/Type" for "pkg/subdir/Type".
 
 deserialize_message (raw, cls_or_typename)
 Returns ROS message or service request/response instantiated from serialized binary.
 
 dict_to_message (dct, msg_or_type)
 Returns ROS message populated from Python dictionary.
 
 format_param_name (name)
 Returns parameter name with correct separator for ROS version, and leading sigils stripped.
 
 get_alias_type (typename)
 Returns ROS built-in type for alias like "char", if any; reverse of get_type_alias().
 
 get_message_class (msg_or_type)
 Returns ROS message / service class object.
 
 get_message_definition (msg_or_type, full=True)
 Returns ROS message or service request/response type definition text.
 
 get_message_fields (val)
 Returns {field name: field type name} if ROS message or service request/response, else {}.
 
 get_message_header (val)
 Returns message `std_msgs/Header`-attribute if any, else `None`.
 
 get_message_type (msg_or_cls)
 Returns ROS message / service canonical type name, like "std_msgs/Header".
 
 get_message_type_hash (msg_or_type)
 Returns ROS message / service type MD5 hash.
 
 get_message_value (msg, name, default=...)
 Returns object attribute value, with numeric arrays converted to lists.
 
 get_service_definition (srv_or_type)
 Returns ROS service type definition text.
 
 get_service_request_class (srv_or_type)
 Returns ROS service request class object.
 
 get_service_response_class (srv_or_type)
 Returns ROS service response class object.
 
 get_type_alias (typename)
 Returns alias like "char" for ROS built-in type, if any; reverse of get_alias_type().
 
 is_ros_message (val)
 Returns whether value is a ROS message or service request/response class or instance.
 
 is_ros_service (val)
 Returns whether value is a ROS service class object.
 
 is_ros_time (val)
 Returns whether value is a ROS time/duration class or instance.
 
 make_duration (secs=0, nsecs=0)
 Returns a ROS duration.
 
 make_full_typename (typename, category="msg")
 Returns "pkg/msg/Type" for "pkg/Type".
 
 make_time (secs=0, nsecs=0)
 Returns a ROS time.
 
 message_to_dict (msg, replace=None)
 Returns ROS message as nested Python dictionary.
 
 message_to_str (msg, indent=None)
 Returns ROS message as an evaluatable string, e.g.
 
 scalar (typename)
 Returns scalar type from ROS message data type, like "uint8" from uint8-array.
 
 serialize_message (msg)
 Returns ROS message or service request/response as a serialized binary of `bytes()`.
 
 time_category (msg_or_type)
 Returns "time" or "duration" for time/duration type, else original value.
 
 time_message (val, to_message=True, clock_type=None)
 Converts ROS2 time/duration between `rclpy` and `builtin_interfaces` objects.
 
 to_datetime (val)
 Returns value as datetime.datetime if value is ROS time/duration, else value.
 
 to_decimal (val)
 Returns value as decimal.Decimal if value is ROS time/duration, else value.
 
 to_duration (val)
 Returns value as ROS duration if convertible (int/float/time/datetime/decimal), else value.
 
 to_nsec (val)
 Returns value in nanoseconds if value is ROS time/duration, else value.
 
 to_sec (val)
 Returns value in seconds if value is ROS time/duration, else value.
 
 to_sec_nsec (val)
 Returns value as (seconds, nanoseconds) if value is ROS time/duration, else value.
 
 to_time (val)
 Returns value as ROS time if convertible (int/float/duration/datetime/decimal), else value.
 

Variables

 FAMILY = ros.FAMILY
 ROS Python module family, "rospy" or "rclpy".
 
 PARAM_SEPARATOR = ros.PARAM_SEPARATOR
 Separator char between ROS parameter namespace parts.
 
 PRIVATE_PREFIX = ros.PRIVATE_PREFIX
 Prefix for "private" names, auto-namespaced under current namespace.
 
 ros = ros1 or ros2
 
 ros1 = None
 
 ROS_ALIAS_TYPES = ros.ROS_ALIAS_TYPES
 Mapping between type aliases and real types, like {"byte": "int8"} in ROS1.
 
dict ROS_BUILTIN_CTORS
 Python constructors for ROS built-in types, as {ROS name: type class}.
 
list ROS_BUILTIN_TYPES = ROS_NUMERIC_TYPES + ROS_STRING_TYPES
 All built-in basic types in ROS.
 
list ROS_COMMON_TYPES = ROS_BUILTIN_TYPES + ROS_TIME_TYPES
 All built-in basic types plus time types in ROS.
 
list ROS_NUMERIC_TYPES
 All built-in numeric types in ROS.
 
list ROS_STRING_TYPES = ["string", "wstring"]
 All built-in string types in ROS.
 
 ROS_TIME_CLASSES = ros.ROS_TIME_CLASSES
 ROS time/duration types mapped to type names.
 
 ROS_TIME_TYPES = ros.ROS_TIME_TYPES
 ROS time/duration types.
 
 ROSLogHandler = ros.ROSLogHandler
 Logging handler that forwards logging messages to ROS logger.
 

Detailed Description

Utilities for ROS built-in types, and message and service types.

Function Documentation

◆ canonical()

rosros.api.canonical (   typename)

Returns "pkg/Type" for "pkg/subdir/Type".

Definition at line 69 of file api.py.

◆ deserialize_message()

rosros.api.deserialize_message (   raw,
  cls_or_typename 
)

Returns ROS message or service request/response instantiated from serialized binary.

Definition at line 347 of file api.py.

◆ dict_to_message()

rosros.api.dict_to_message (   dct,
  msg_or_type 
)

Returns ROS message populated from Python dictionary.

Raises TypeError on attribute value type mismatch.

Parameters
msg_or_typecanonical or full class name like "std_msgs/Bool" or "std_msgs/msg/Bool", or class instance like `std_msgs.msg.Bool()`, or class object like `std_msgs.msg.Bool`

Definition at line 262 of file api.py.

◆ format_param_name()

rosros.api.format_param_name (   name)

Returns parameter name with correct separator for ROS version, and leading sigils stripped.

Definition at line 74 of file api.py.

◆ get_alias_type()

rosros.api.get_alias_type (   typename)

Returns ROS built-in type for alias like "char", if any; reverse of get_type_alias().

In ROS1, byte and char are aliases for int8 and uint8; in ROS2 the reverse.

Definition at line 208 of file api.py.

◆ get_message_class()

rosros.api.get_message_class (   msg_or_type)

Returns ROS message / service class object.

Parameters
msg_or_typefull or canonical class name, like "std_msgs/Bool" or "std_srvs/SetBool" or "std_srvs/SetBoolRequest"; or class instance like `std_msgs.msg.Bool()`
Returns
ROS message / service class object, like `std_msgs.msg.Bool` or `std_srvs.srv.SetBool` or `std_srvs.srv.SetBoolRequest`, or None if not found

Definition at line 88 of file api.py.

◆ get_message_definition()

rosros.api.get_message_definition (   msg_or_type,
  full = True 
)

Returns ROS message or service request/response type definition text.

Text will include subtype definitions by default.

Parameters
msg_or_typecanonical or full class name like "std_msgs/Bool" or "std_msgs/msg/Bool", or class instance like `std_msgs.msg.Bool()`, or class object like `std_msgs.msg.Bool`
fullinclude definitions of nested types, separated by "\n---\nMSG: pkg/Type\n" (ignored for service request/response types)
Returns
message type definition text

Definition at line 103 of file api.py.

◆ get_message_fields()

rosros.api.get_message_fields (   val)

Returns {field name: field type name} if ROS message or service request/response, else {}.

Parameters
valROS message or service request/response instance, or class object

Definition at line 112 of file api.py.

◆ get_message_header()

rosros.api.get_message_header (   val)

Returns message `std_msgs/Header`-attribute if any, else `None`.

Parameters
valROS message or service request/response instance

Definition at line 121 of file api.py.

◆ get_message_type()

rosros.api.get_message_type (   msg_or_cls)

Returns ROS message / service canonical type name, like "std_msgs/Header".

Returns "*" for `AnyMsg`.

Parameters
msg_or_clsclass instance like `std_msgs.msg.Bool()`, or class object like `std_msgs.msg.Bool`
Returns
canonical name, or `None` if not ROS message / service

Definition at line 133 of file api.py.

◆ get_message_type_hash()

rosros.api.get_message_type_hash (   msg_or_type)

Returns ROS message / service type MD5 hash.

Parameters
msg_or_typefull or canonical class name like "std_msgs/Bool" or "std_srvs/SetBool" or "std_srvs/SetBoolRequest", or class instance like `std_msgs.msg.Bool()`, or class object like `std_msgs.msg.Bool`

Definition at line 144 of file api.py.

◆ get_message_value()

rosros.api.get_message_value (   msg,
  name,
  default = ... 
)

Returns object attribute value, with numeric arrays converted to lists.

Parameters
namemessage attribute name; may also be (nested, path) or "nested.path"
defaultvalue to return if attribute does not exist; raises exception otherwise

Definition at line 154 of file api.py.

◆ get_service_definition()

rosros.api.get_service_definition (   srv_or_type)

Returns ROS service type definition text.

Parameters
srv_or_typecanonical or full class name like "std_srvs/SetBool" or "std_srvs/srv/SetBool", or class instance like `std_srvs.srv.SetBool()`, or class object like `std_srvs.srv.SetBool`
Returns
ROS service type definition text

Definition at line 166 of file api.py.

◆ get_service_request_class()

rosros.api.get_service_request_class (   srv_or_type)

Returns ROS service request class object.

Parameters
srv_or_typecanonical or full class name like "std_srvs/SetBool" or "std_srvs/srv/SetBool", or class instance like `std_srvs.srv.SetBool()`, or class object like `std_srvs.srv.SetBool`
Returns
ROS service request class object, like `std_srvs.srv.SetBoolRequest`

Definition at line 178 of file api.py.

◆ get_service_response_class()

rosros.api.get_service_response_class (   srv_or_type)

Returns ROS service response class object.

Parameters
srv_or_typecanonical or full class name like "std_srvs/SetBool" or "std_srvs/srv/SetBool", or class instance like `std_srvs.srv.SetBool()`, or class object like `std_srvs.srv.SetBool`
Returns
ROS service response class object, like `std_srvs.srv.SetBoolResponse`

Definition at line 190 of file api.py.

◆ get_type_alias()

rosros.api.get_type_alias (   typename)

Returns alias like "char" for ROS built-in type, if any; reverse of get_alias_type().

In ROS1, byte and char are aliases for int8 and uint8; in ROS2 the reverse.

Definition at line 199 of file api.py.

◆ is_ros_message()

rosros.api.is_ros_message (   val)

Returns whether value is a ROS message or service request/response class or instance.

Parameters
vallike `std_msgs.msg.Bool()` or `std_srvs.srv.SetBoolRequest`
Returns
True if value is a ROS message or service request/response class or instance, False otherwise

Definition at line 218 of file api.py.

◆ is_ros_service()

rosros.api.is_ros_service (   val)

Returns whether value is a ROS service class object.

Definition at line 222 of file api.py.

◆ is_ros_time()

rosros.api.is_ros_time (   val)

Returns whether value is a ROS time/duration class or instance.

Definition at line 227 of file api.py.

◆ make_duration()

rosros.api.make_duration (   secs = 0,
  nsecs = 0 
)

Returns a ROS duration.

Definition at line 232 of file api.py.

◆ make_full_typename()

rosros.api.make_full_typename (   typename,
  category = "msg" 
)

Returns "pkg/msg/Type" for "pkg/Type".

Parameters
categorytype category like "msg" or "srv"

Definition at line 247 of file api.py.

◆ make_time()

rosros.api.make_time (   secs = 0,
  nsecs = 0 
)

Returns a ROS time.

Definition at line 237 of file api.py.

◆ message_to_dict()

rosros.api.message_to_dict (   msg,
  replace = None 
)

Returns ROS message as nested Python dictionary.

Parameters
replacemapping of {value: replaced value}, e.g. {math.nan: None, math.inf: None}

Definition at line 292 of file api.py.

◆ message_to_str()

rosros.api.message_to_str (   msg,
  indent = None 
)

Returns ROS message as an evaluatable string, e.g.

"std_msgs.msg.UInt8(data=0)".

Parameters
indentmulti-line indentation level to use if not returning one-liner; as the number of spaces or the string to indent with

Definition at line 316 of file api.py.

◆ scalar()

rosros.api.scalar (   typename)

Returns scalar type from ROS message data type, like "uint8" from uint8-array.

Returns type unchanged if an ordinary type. In ROS2, returns unbounded type, e.g. "string" from "string<=10[<=5]".

Definition at line 358 of file api.py.

◆ serialize_message()

rosros.api.serialize_message (   msg)

Returns ROS message or service request/response as a serialized binary of `bytes()`.

Definition at line 342 of file api.py.

◆ time_category()

rosros.api.time_category (   msg_or_type)

Returns "time" or "duration" for time/duration type, else original value.

Parameters
msg_or_typefull or canonical class name like "duration" or "builtin_interfaces/Time", or class instance like `rospy.Time()`, or class object like `rclpy.time.Time`

Definition at line 369 of file api.py.

◆ time_message()

rosros.api.time_message (   val,
  to_message = True,
  clock_type = None 
)

Converts ROS2 time/duration between `rclpy` and `builtin_interfaces` objects.

Parameters
valROS2 time/duration object from `rclpy` or `builtin_interfaces`
to_messagewhether to convert from `rclpy` to `builtin_interfaces` or vice versa
clock_typeClockType for converting to `rclpy.Time`, defaults to `ROS_TIME`
Returns
value converted to appropriate type, or original value if not convertible

Definition at line 384 of file api.py.

◆ to_datetime()

rosros.api.to_datetime (   val)

Returns value as datetime.datetime if value is ROS time/duration, else value.

Definition at line 389 of file api.py.

◆ to_decimal()

rosros.api.to_decimal (   val)

Returns value as decimal.Decimal if value is ROS time/duration, else value.

Definition at line 395 of file api.py.

◆ to_duration()

rosros.api.to_duration (   val)

Returns value as ROS duration if convertible (int/float/time/datetime/decimal), else value.

Definition at line 402 of file api.py.

◆ to_nsec()

rosros.api.to_nsec (   val)

Returns value in nanoseconds if value is ROS time/duration, else value.

Definition at line 407 of file api.py.

◆ to_sec()

rosros.api.to_sec (   val)

Returns value in seconds if value is ROS time/duration, else value.

Definition at line 412 of file api.py.

◆ to_sec_nsec()

rosros.api.to_sec_nsec (   val)

Returns value as (seconds, nanoseconds) if value is ROS time/duration, else value.

Definition at line 417 of file api.py.

◆ to_time()

rosros.api.to_time (   val)

Returns value as ROS time if convertible (int/float/duration/datetime/decimal), else value.

Definition at line 422 of file api.py.

Variable Documentation

◆ FAMILY

rosros.api.FAMILY = ros.FAMILY

ROS Python module family, "rospy" or "rclpy".

Definition at line 63 of file api.py.

◆ PARAM_SEPARATOR

rosros.api.PARAM_SEPARATOR = ros.PARAM_SEPARATOR

Separator char between ROS parameter namespace parts.

Definition at line 57 of file api.py.

◆ PRIVATE_PREFIX

rosros.api.PRIVATE_PREFIX = ros.PRIVATE_PREFIX

Prefix for "private" names, auto-namespaced under current namespace.

Definition at line 60 of file api.py.

◆ ros

rosros.api.ros = ros1 or ros2

Definition at line 25 of file api.py.

◆ ros1

rosros.api.ros1 = None

Definition at line 20 of file api.py.

◆ ROS_ALIAS_TYPES

rosros.api.ROS_ALIAS_TYPES = ros.ROS_ALIAS_TYPES

Mapping between type aliases and real types, like {"byte": "int8"} in ROS1.

Definition at line 54 of file api.py.

◆ ROS_BUILTIN_CTORS

dict rosros.api.ROS_BUILTIN_CTORS
Initial value:
1= {"byte": int, "char": int, "int8": int, "int16": int,
2 "int32": int, "int64": int, "uint8": int, "uint16": int,
3 "uint32": int, "uint64": int, "float32": float, "float64": float,
4 "bool": bool, "string": str, "wstring": str}

Python constructors for ROS built-in types, as {ROS name: type class}.

Definition at line 39 of file api.py.

◆ ROS_BUILTIN_TYPES

list rosros.api.ROS_BUILTIN_TYPES = ROS_NUMERIC_TYPES + ROS_STRING_TYPES

All built-in basic types in ROS.

Definition at line 36 of file api.py.

◆ ROS_COMMON_TYPES

list rosros.api.ROS_COMMON_TYPES = ROS_BUILTIN_TYPES + ROS_TIME_TYPES

All built-in basic types plus time types in ROS.

Definition at line 51 of file api.py.

◆ ROS_NUMERIC_TYPES

list rosros.api.ROS_NUMERIC_TYPES
Initial value:
1= ["byte", "char", "int8", "int16", "int32", "int64", "uint8",
2 "uint16", "uint32", "uint64", "float32", "float64", "bool"]

All built-in numeric types in ROS.

Definition at line 29 of file api.py.

◆ ROS_STRING_TYPES

list rosros.api.ROS_STRING_TYPES = ["string", "wstring"]

All built-in string types in ROS.

Definition at line 33 of file api.py.

◆ ROS_TIME_CLASSES

rosros.api.ROS_TIME_CLASSES = ros.ROS_TIME_CLASSES

ROS time/duration types mapped to type names.

Definition at line 48 of file api.py.

◆ ROS_TIME_TYPES

rosros.api.ROS_TIME_TYPES = ros.ROS_TIME_TYPES

ROS time/duration types.

Definition at line 45 of file api.py.

◆ ROSLogHandler

rosros.api.ROSLogHandler = ros.ROSLogHandler

Logging handler that forwards logging messages to ROS logger.

Definition at line 66 of file api.py.