Repository navigation
Add type mapping info to documents - #190
Conversation
| | -------- | --------- | ----------- | -------------------- | | ||
| | bool | \_Bool | bool | bool | | ||
| | octet | uint8\_t | uint8\_t | bytes with length 1 | | ||
| | char | char | char | str with length 1 | |
There was a problem hiding this comment.
There was a problem hiding this comment.
As this is a string, the question of the encoding may propagate here as well
There was a problem hiding this comment.
I threw in wchar and wstring as well. c9ddd20
I'm not yet sure if this doc should link to a separate unicode support design doc, or if that should be included here.
| | octet | uint8\_t | uint8\_t | bytes with length 1 | | ||
| | char | char | char | str with length 1 | | ||
| | float | float | float | float | | ||
| | double | double | double | float | |
There was a problem hiding this comment.
should we add long double here ?
| IDL types mapped to different types in different languages. | ||
|
|
||
|
|
||
| | IDL type | C type | C++ type | Python type | |
There was a problem hiding this comment.
There seem to be differences between IDL types and DDS types. Would it make sense to have a DDS Type column here?
There was a problem hiding this comment.
By DDS types do you mean DDS-XTYPES 1.2?
There was a problem hiding this comment.
Might be able to avoid adding DDS-XTYPES since the 1.2 spec has a mapping of DDS to IDL types already.
There was a problem hiding this comment.
Yes I was thinking of DDS-XTYPES, to have a reference of what will end up on the wire when using a given IDL type.
|
|
||
| | ROS type | IDL type | | ||
| | ----------------------- | -------------- | | ||
| | static array | T\[N\] | |
There was a problem hiding this comment.
It looks like these IDL types are named array, unbounded sequence etc in the article above.
| | unbounded sequence of string | rcutils\_string\_array | | | | ||
| | bounded sequence of string | rcutils\_string\_array | | | | ||
| | array of octet | | | bytes | | ||
| | array of char | char * | std::string | str | |
There was a problem hiding this comment.
It may not make sense to have array of char map to char * in C because that means it couldn't contain a value of zero.
There was a problem hiding this comment.
currently in C we have a custom data structure for this that has a size component, so I think it is possible for array elements to have any value in the char range in them, we would just not be able to treat it as a string (strlen etc).
There was a problem hiding this comment.
array of char and wchar no longer strings in cd1e3b2
| | bool | \_Bool | bool | bool | | ||
| | octet | uint8\_t | uint8\_t | bytes with length 1 | | ||
| | char | char16\_t | char16\_t | str with length 1 | | ||
| | wchar | signed char | char | str with length 1 | |
| | string | char * | std::string | str | | ||
| | string<N> | char * | std::string | str | | ||
| | wstring | char16\_t * | std::u16string | str | | ||
| | wstring<N> | char16\_t * | std::u16string | str | |
There was a problem hiding this comment.
I was wondering about the different handling of bounded sequences compared to bounded (w)strings in C++. For the first we go through the hassle to provide our own data structure (to enforce the upper bound) for the other two we don't (atm).
Maybe we should use the same approach homogeneously - it could be either of the two, The effort and "customness" of types we define vs. the delayed check until the data structure is being passed to the lower levels.
There was a problem hiding this comment.
yeah I was thinking the same for the C part, currently in C we have custom data structure for all the dynamic types (bounded and unbounded). AFAICT in C++ we have a custom container for bounded vectors but nothing for strings ATM (as described in previous comment).
In the delayed check approach, when / how would the user be notified that the data doesnt fit?
There was a problem hiding this comment.
when / how would the user be notified that the data doesnt fit?
The rclpy_publish function converts the Python object into a ROS C message. If any of the constraints are violated it can signaled through an exception.
Optionally we can offer a validation function which the user could invoke explicitly.
There was a problem hiding this comment.
In Python I would expect the user to not be allowed to place data bigger than the message specification at the Python level already (what we currently do with custom setters).
My question was about the C language that doesn't provide exceptions. If we map to a char(16_t) * I am not sure how we would restrict the memory allocation or report the error.
But your point still holds: it can be done when publish is called rather than when filling the message datastructure.
There was a problem hiding this comment.
In Python I would expect the user to not be allowed to place data bigger than the message specification at the Python level already (what we currently do with custom setters).
That is true but only covers the case where a field is being "assigned". But e.g. a list is mutable and new items can be added after the initial assignment with the property of the class being able to intercept (assuming we don't mock all method of the list instance).
My question was about the C language that doesn't provide exceptions. If we map to a char(16_t) * I am not sure how we would restrict the memory allocation or report the error.
In a future rclc client library the publish call can perform the validation the same way as in Python. While the publish in rcl could also check the constraints I would rather not do that and rely in the client library on top to do that. All of these functions have return code to signal error already (if exceptions are not available / used).
There was a problem hiding this comment.
While the publish in rcl could also check the constraints I would rather not do that and rely in the client library on top to do that.
Just adding it looks like either client libraries or the rmw implementations must check the constraints because rcl_publish sees the message as a void *.
In Python I would expect the user to not be allowed to place data bigger than the message specification at the Python level already (what we currently do with custom setters).
I think ros2/ros2#509 is a hint that ros2 should consider only checking the validity of the message once. From the perspective of ROS 2 as a library the message constraints don't matter until the message is used. From the perspective of a developer writing code constraint checking when a field is set is nice because it errors closer to the bug, but from the perspective of a user running the code it can be a significant performance hit for little gain when most of the time everything is working fine.
| | char | signed char | char | str with length 1 | | ||
| | wchar | char16\_t | char16\_t | str with length 1 | | ||
| | bool | \_Bool | bool | bool | | ||
| | octet | uint8\_t | uint8\_t | bytes with length 1 | |
There was a problem hiding this comment.
Looking at newer specs I was wondering if the C++ mapping should be byte (available as of C++17) instead. To match the semantic C could then be mapped to unsigned char.
[We would need to check for availability across all targeted platforms. Even if not all support the type yet we could still make the decision today (using a custom typedef or using char directly) and switch to the standard type once available.]
There was a problem hiding this comment.
Looking at cppreference and Visual Studio conformance announcement it looks like all compilers support it
| | -------------- | ---------------------- | ------------------- | ----------- | | ||
| | T[N] | T[N] | std::array<T, N> | list | | ||
| | sequence<T> | struct {size\_t, T * } | std::vector<T> | list | | ||
| | sequence<T, N> | struct {size\_t, T * } | custom\_class<T, N> | list | |
There was a problem hiding this comment.
should this be struct {size\_t, T * , N } instead? (where N would be the max capacity)
There was a problem hiding this comment.
I think max should to be a const variable; because if it's non-const then it would need to be in two places. First in the struct that can be modified by a user, and second in code used to validate a message.
If that sounds reasonable then it implies the struct with a field for max capacity would be generated for every field. Alternatively a struct definition could be reused if the max capacity was a separate const variable.
const size_t example_msgs__example_bounded__sensor_readings__max_capacity = 64;
// ...
struct example_msgs__example_bounded
{
// ...
rcutils_float_array * sensor_readings = NULL;
}|
1c4fa2f defines a custom mapping to |
| | unbounded sequence of string | rcutils\_string\_array | | | | ||
| | bounded sequence of string | rcutils\_string\_array | | | | ||
| | array of char | char * | std::string | str | | ||
| | array of wchar | char16\_t * | std::u16string | str | |
There was a problem hiding this comment.
The C and C++ mapping for arrays of (w)char need to be different since both don't allow for null characters which isn't forbidden for these IDL types.
| If a cell is blank then the default mapping is used. | ||
|
|
||
| | IDL type | C type | C++ type | Python type | | ||
| | ---------------------------- | ---------------------- | -------------- | ----------- | |
There was a problem hiding this comment.
Do we want to specify the mapping to use numpy for scalar type? E.g.
uint32[N]:numpy.ndarray(shape=(N, ), dtype=numpy.uint32)sequence<uint32 [, N]\>:numpy.ndarray(shape=(0, ), dtype=numpy.uint32)
There was a problem hiding this comment.
+1 for octet[N] being np.array(). It would make it easier to support multiple dimension arrays in the future. numpy seems like a minimal enough dependency. pip freeze after installing numpy in a venv only shows numpy and pkg-resources (which was in the env by default).
There was a problem hiding this comment.
How about other scalar types and sequences?
+1 for octet[N] being np.array()
For octet[N] bytes already provides a native mapping (without the usual Python overhead) so I am not sure about that case.
|
|
||
| ### Constraint Checking | ||
|
|
||
| Generated code for messages is guaranteed to enforce constraints only when a message is published. |
There was a problem hiding this comment.
I think this sounds ambiguous:
guaranteed to enforce ... only when ... published
I don't think we want to indicate that the want to guarantee that we don't check it before. Maybe rephrase to:
Generated code for messages is only guaranteed to enforce constraints when a message is published.
| ### Constraint Checking | ||
|
|
||
| Generated code for messages is guaranteed to enforce constraints only when a message is published. | ||
| For example, there might not be an error when a field constrained to be a fixed size array is assigned an array with the wrong size. |
There was a problem hiding this comment.
Inject the following?
For example, depending on the language there ...
In Python we could (and probably will) check this trivially in the setter.
There was a problem hiding this comment.
Added another sentence saying constraint checks could be inconsistent depending on language features and performance cost 46ec109
|
|
||
| Generated code for messages is guaranteed to enforce constraints only when a message is published. | ||
| For example, there might not be an error when a field constrained to be a fixed size array is assigned an array with the wrong size. | ||
| In the future constraints might be checked when a field is set. |
There was a problem hiding this comment.
Maybe append the following?
... and/or mutated.
Same for the next line.
| | -------------- | --------------------------------- | ----------------- | ----------- | | ||
| | T[N] | T[N] | std::array<T, N> | list | | ||
| | sequence<T> | struct {size\_t, T * } | std::vector<T> | list | | ||
| | sequence<T, N> | struct {size\_t, T * }, size\_t N | std::vector<T, N> | list | |
There was a problem hiding this comment.
std::vector doesn't have a second template argument N.
| | int64 | int64\_t | int64\_t | int | | ||
| | uint64 | uint64\_t | uint64\_t | int | | ||
|
|
||
| 1. <a name="byte"></a>If std::byte is not available then uint8\_t is used instead. |
There was a problem hiding this comment.
Instead of uint8_t I would suggest unsigned char since that is how it is defined in C++17.
There was a problem hiding this comment.
Side note, I learned that in C and C++ an unsigned char is guaranteed to be 1 byte, but 1 byte may be more than 8 bits. It doesn't look like any modern systems do that, but if they do the number of bits in a byte is defined in <limits.h> or <climits> as CHAR_BIT.
| | sequence<octet> | | | bytes | | ||
| | sequence<octet, N> | | | bytes | | ||
| | array of string | struct {size\_t, T * }, size\_t N | | | | ||
| | int8[N] | | | numpy.array(dtype=numpy.int8) | |
There was a problem hiding this comment.
Should the fixed-size arrays also have shape=(N, )?
There was a problem hiding this comment.
It looks like it doesn't accept a keyword argument of shape, and the text of repr() doesn't show it either. As far as I can tell the shape is inferred from the data passed in.
There was a problem hiding this comment.
The following lines work for me:
import numpy
numpy.ndarray(shape=(5, ), dtype=numpy.uint32)
There was a problem hiding this comment.
Ah, I was trying numpy.array(...). It looks like the underlying type is ndarray and array is just a helper to construct one. I'll change the types.
| If a cell is blank then the default mapping is used. | ||
|
|
||
| | IDL type | C type | C++ type | Python type | | ||
| | ---------------------------- | --------------------------------- | -------------- | -------------------------------- | |
There was a problem hiding this comment.
I would suggest to use the same order as in the first "Type Mapping" table (currently lines 56-70).
|
|
||
| | IDL type | C type | C++ type | Python type | | ||
| | ---------------------------- | --------------------------------- | -------------- | -------------------------------- | | ||
| | float[N] | | | numpy.array(dtype=numpy.float\_) | |
There was a problem hiding this comment.
float_? Isn't that the same as float64?
There was a problem hiding this comment.
Oops it is. 4838992 makes float -> numpy.float32, double -> numpy.float64 and long double -> list(float)
There was a problem hiding this comment.
Should we add special mapping for long doubles be arrays of numpy.float128 as well?
| | -------- | ----------- | ------------------------------ | -------------------- | | ||
| | float | float | float | float | | ||
| | double | double | double | float | | ||
| | long double | long double | long double | float | |
There was a problem hiding this comment.
Thoughts on not initially supporting long double? It seems inconsistent across platforms and DDS vendors.
Visual Studio typedef'd long double to double which is 64bit
https://docs.microsoft.com/en-us/cpp/c-language/type-long-double?view=vs-2017
Connext works around this by mapping DDS_LongDouble to a char array (of size 16?) on some platforms and long double on others.
https://community.rti.com/static/documentation/connext-dds/5.2.3/doc/manuals/connext_dds/html_files/RTI_ConnextDDS_CoreLibraries_UsersManual/Content/UsersManual/Translations_for_IDL_Types.htm#Note2_LongDouble
Opensplice doesn't appear to support it. The grammar on page 589 / pdf page 617
only defines <floating_pt_type> as "float" and "double".
Fast-CDR uses the C++ type long double and serializes it as 16 bytes on the wire. https://github.com/eProsima/Fast-CDR/blob/1df42bbd1ea86a9c99bb8fa72104f19872235986/src/cpp/Cdr.cpp#L528
There was a problem hiding this comment.
My understanding from original discussion was that we should support all IDL types. @dirk-thomas is that correct?
Looking a bit more into it:
- It looks like Connext and Fast-RTPS agree that it should be a 16-byte thing on the wire
The issues we face right now:
- FastRTPS doesn't have special handling to account for the fact that the type has a different size on windows
- OpenSplice doesn't support this type
Looking at Opensplice it appears that wchar and wstring are not supported either https://github.com/ADLINK-IST/opensplice/blob/36e4f9c250a640ade72d5af92084f5f5243ce96c/src/tools/idlpp/code/idl_unsupported.h.
Not convinced it should be a blocker for supporting it though. We may just need to have implementation specific limitations or workarounds
There was a problem hiding this comment.
after talking with @mikaelarguedas I think this can be resolved by saying long double will be supported, but users will have to be cautious of middleware support for it.
I'm not sure if we should use the C and C++ type long double on all platforms, but that seems like the easiest thing to do for now.
There was a problem hiding this comment.
Added warning about long double on some platforms and middleware in
2082ba0
4838992 to
44ac062
Compare
| | -------- | ------------------ | | ||
| | bool | boolean | | ||
| | byte | octet | | ||
| | char | char | |
There was a problem hiding this comment.
IDL 7.2.6.2.1 says A char is an 8-bit quantity with a numerical value between 0 and 255 (decimal). The ROS 2 interface definition says char is [-128, 127]. Changing this type mapping to idl type int8 after discussing with @mjcarroll
| | float | float | float | float | | ||
| | double | double | double | float | | ||
| | long double | long double | long double[<sup>2</sup>](#ld) | float | | ||
| | char | signed char | char | str with length 1 | |
There was a problem hiding this comment.
IDL 7.2.6.2.1 says A char is an 8-bit quantity with a numerical value between 0 and 255 (decimal). Changing to unsigned char in c and c++
| | bool | boolean | | ||
| | byte | octet | | ||
| | char | char | | ||
| | char | short | |
There was a problem hiding this comment.
would an octet make more sense than a short in this case ?
There was a problem hiding this comment.
I think short is a better fit than octet. IDL 4.2 section 7.4.1.4.3 says Octet literals have integer value in the range 0…255. char is an integer in range [-128, 27] in ros 2 .msg files #190 (comment)
There was a problem hiding this comment.
It's still unclear why it is treated differently than the int8 type.
My understanding was that they both fit in 8-bit and can represent signed values, so I'm not sure why we should use a 16-bit type to store them
There was a problem hiding this comment.
Got the int8 types mixed up. It should be fixed in e7c99ea
| | char | short | | ||
| | float32 | float | | ||
| | float64 | double | | ||
| | int8 | octet | |
There was a problem hiding this comment.
This should map to idl type int8
| | float32 | float | | ||
| | float64 | double | | ||
| | int8 | octet | | ||
| | uint8 | octet | |
There was a problem hiding this comment.
This should map to idl type uint8
| | string | char * | std::string | str | | ||
| | string<N> | char * | std::string | str | | ||
| | wstring | char16\_t * | std::u16string | str | | ||
| | wstring<N> | char16\_t * | std::u16string | str | |
There was a problem hiding this comment.
from previous discussion, I think string and wstring would be structs in C (like sequences) and not just pointers, isnt' it ?
There was a problem hiding this comment.
Possibly referring to this: #190 (comment) where array of char and wchar should be structs because they can contain zero?
I don't remember a discussion about wstring or string being structs instead of pointers though.
There was a problem hiding this comment.
A string / wstring must not contain null characters. Therefore a raw pointer is sufficient in this case.
The opposite case is char[] / wchar[] which doesn't have that restriction and therefore is stored as data ptr / size pair.
|
As far as I understand the current concept is to generate a ROS 2 specific C++ type from an IDL. This leads to the fact that two types with the same data exist.
I have two questions:
Please also note that RTI and OpenSplice have (AFAIK) common IDL to C/C++ etc. type translations available here. Thank you! |
This is not a change to how the system already works. The user code always only interacts with a ROS type - it must not be coupled with a vendor specific type.
Even when using shared memory DDS implementations still need to serialize the published sample into the shared memory and on the other end deserialize it into a sample class. In the current typesupport implementations the overhead is that the ROS type is first converted to the vendor specific type which then gets serialized. Nothing should prevent a RMW implementation to skip the inbetween type and implement serialization logic from the ROS type directly. But again: this is already the same in the current code and doesn't change in any way with the transition to using
I think I don't understand the question. DDS systems define the type of exchanged sample and that is the "contract" between both endpoints. Since the data is exchanged in serialized form it is not relevant how the object representation looks like on both sides.
Did you notice and differences between these translations and the ones proposed in ROS? |
|
@dirk-thomas: Thank you for your reply.
No, but I was hoping the IDL transition might enable a better interoperability between ROS 2 and native DDS application. A lot of people ask us for all the DDS features. We want to provide that while still offering the simplicity of ROS 2. This is what makes things complicated.
I was hoping for ROS 2 types having the following structure: This would allow transparent use of DDS and ROS 2 types.
I'll try to elaborate with an example. Assuming I write an algorithm taking in a Now someone wants to reuse this code in a pure DDS application. To be able to do that smoothly the output of the IDLs from ROS 2 should have the exact same API as DDS types so that I can reuse the code. Another independent question:
|
The transition to use IDL will allow at least all the supported types to be exchanged transparently (e.g. by consensus on the encoding of strings). Other than that I don't know which exact part you would like to see "better interoperability". Maybe you can describe specific cases.
Making every ROS 2 data type a template based on the concrete implementation is certainly undesired. This would require compile time knowledge of the chosen RMW impl as well as increase complexity of every code using ROS messages. One of the design goals is that the ROS data types are not tied to a specific implementation and can be used even without any RMW impl. in e.g. library API. Also your example implies that all members of the data structure are only to be accessed by methods. In earlier discussions the community strongly expressed the desire to keep member-based access to all the fields.
Not even two different DDS implementations offer the very same API for the generated data structures. So how should ROS offer that across DDS vendors (and potentially non-DDS RMW impl)? That being said most types are very close to each other (if not even identical). So nothing prevents you from implementing two signatures of the function which then use a shared function to perform the logic or use a templated function if the content of the functions works for both types. |
I'd avoid using the ROS type or the DDS type in a library unless you're willing to live with the consequences of that decision (a dependency on one or the other). So I don't agree with the conclusion that:
In ROS 1 (and planned for ROS 2) we have something where you can pub/sub whatever type you want so long as you tell us how to serialize and deserialize it, see: I think this is the right way to address the concern of wanting to use data structures different from ROS's message data structures and without incurring copying costs (for both runtime and development time). For instance, this allows us to pub/sub PCL point clouds rather than our typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
void callback(const PointCloud::ConstPtr& msg)
{
printf ("Cloud: width = %d, height = %d\n", msg->width, msg->height);
BOOST_FOREACH (const pcl::PointXYZ& pt, msg->points)
printf ("\t(%f, %f, %f)\n", pt.x, pt.y, pt.z);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "sub_pcl");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe<PointCloud>("points2", 1, callback);
ros::spin();
}So, from your example, if you'd like to use |
| | uint64[N] | | | numpy.ndarray(shape=(N, ), dtype=numpy.uint64) | | ||
| | sequence<uint64> | | | numpy.ndarray(dtype=numpy.uint64) | | ||
| | sequence<uint64, N> | | | numpy.ndarray(dtype=numpy.uint64) | | ||
| | array of string | struct {size\_t, T * }, size\_t N | | | |
There was a problem hiding this comment.
"array" seems wrong here. The mapping suggests that it is a sequence (non static size).
Also the mapping doesn't seem to be different than the generic definition of arrays and sequences above? Should this line be removed?
There was a problem hiding this comment.
This line probably should be removed, I think it made sense when the mapping was rcutils_string_array. array of string was trying to say string[N] from T[N] in the generic table, so the default type would be char * [N].
There was a problem hiding this comment.
✔️ removed in 828db25.
The same commit also consolidates the numpy mapping into a more readable "templated" form - while the cells get wider I hope it makes it more readable / comprehensible than the verbose version before.
dirk-thomas
left a comment
There was a problem hiding this comment.
The current state looks solid to me. I propose to get it merged as is.
|
@mjcarroll Please review / approve. |
|
LGTM, merging |
This adds mappings between IDL types and other types.