Skip to content

Commit

Permalink
ROS2: Normalize byte as uint8 instead of int8 (#49)
Browse files Browse the repository at this point in the history
### Changelog
<!-- Write a one-sentence summary of the user-impacting change (API,
UI/UX, performance, etc) that could appear in a changelog. Write "None"
if there is no user-facing change -->

### Description

In
[ROS2](https://docs.ros.org/en/iron/Concepts/Basic/About-Interfaces.html#field-types),
`byte` fields should be treated as `uint8` types:


> | Type name | C++     | Python          | DDS type |
> |-----------|---------|-----------------|----------|
> | byte      | uint8_t | builtins.bytes* | octet    |

Prior to this change we were treating `byte` fields as `int8` which
causes errors when parsing a byte field with a default value (e.g.
`255`) which is out of the range of a int8.
  • Loading branch information
achim-k authored Apr 24, 2024
1 parent 464dfe0 commit 3f13e0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/buildRos2Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function normalizeType(type: string): string {
case "char":
return "uint8";
case "byte":
return "int8";
return "uint8";
case "builtin_interfaces/Time":
case "builtin_interfaces/msg/Time":
return "time";
Expand Down
34 changes: 28 additions & 6 deletions src/parse.ros2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("parseMessageDefinition", () => {
isArray: false,
isComplex: false,
name: "y",
type: "int8",
type: "uint8",
},
],
name: undefined,
Expand Down Expand Up @@ -803,35 +803,35 @@ uint32 line`;
{
definitions: [
{
type: "int8",
type: "uint8",
name: "DEBUG",
isConstant: true,
value: 10,
valueText: "10",
},
{
type: "int8",
type: "uint8",
name: "INFO",
isConstant: true,
value: 20,
valueText: "20",
},
{
type: "int8",
type: "uint8",
name: "WARN",
isConstant: true,
value: 30,
valueText: "30",
},
{
type: "int8",
type: "uint8",
name: "ERROR",
isConstant: true,
value: 40,
valueText: "40",
},
{
type: "int8",
type: "uint8",
name: "FATAL",
isConstant: true,
value: 50,
Expand Down Expand Up @@ -1176,4 +1176,26 @@ string<=10[<=5] up_to_five_strings_up_to_ten_characters_each
},
]);
});

it("handles bounded byte field with default value", () => {
// Can be found in /opt/ros/<distro>/share/test_msgs/msg/BoundedSequences.msg
const messageDefinition = `
byte[<=3] byte_values_default [0, 1, 255]
`;
const types = parse(messageDefinition, { ros2: true });
expect(types).toEqual([
{
definitions: [
{
type: "uint8",
isArray: true,
name: "byte_values_default",
isComplex: false,
defaultValue: [0, 1, 255],
arrayUpperBound: 3,
},
],
},
]);
});
});

0 comments on commit 3f13e0c

Please sign in to comment.