Skip to content

Commit

Permalink
Merge pull request #51 from mROS-base/custom_msgs
Browse files Browse the repository at this point in the history
add commonly used msg type files as a part of mros2 library (this repo)
  • Loading branch information
takasehideki authored Sep 19, 2023
2 parents 3ab8274 + b2d6bde commit 7a8606d
Show file tree
Hide file tree
Showing 14 changed files with 1,352 additions and 10 deletions.
84 changes: 83 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Here are the functionalities that mROS 2 offers for you, and current limitations
- `wstring` (UTF-16) is not provided due to its difficulty in verification, but it is unlikely to be used.
- `array` types are not supported
- Some custom message types (e.g., Twist, Pose)
- Please check [mros2-mbed#generating-header-files-for-custom-msgtypes](https://github.com/mROS-base/mros2-mbed#generating-header-files-for-custom-msgtypes) for more details.
- Please check [#generating-header-files-for-custom-msgtypes](https://github.com/mROS-base/mros2#generating-header-files-for-custom-msgtypes) for more details.
- Fragmented message types (that exceed one packet) are experimentally supported. See [PR#36](https://github.com/mROS-base/mros2/pull/36) for more details.
- We think variable-length types cannot be handled, probably due to the limitation of lwIP.
- Service, Actions, and Parameters are not supported
Expand All @@ -45,6 +45,88 @@ Please see each repository to learn how to use it.

Please let us know if you have a request for support for other boards/kernels, or if you could implement this layer on other platforms.

## Generating header files for custom MsgTypes

You can use almost any [built-in-types in ROS 2](https://docs.ros.org/en/rolling/Concepts/About-ROS-Interfaces.html#field-types) on the embedded device.
Not that `array` types are not supported yet.

You can also use following message types that are commonly used. We have prepared and located them in `mros2_msgs/`.

- [geometry_msgs/msg/Twist](https://docs.ros2.org/latest/api/geometry_msgs/msg/Twist.html)
- [geometry_msgs/msg/Vector3](https://docs.ros2.org/latest/api/geometry_msgs/msg/Vector3.html)
- [geometry_msgs/msg/Pose](https://docs.ros2.org/latest/api/geometry_msgs/msg/Pose.html)
- [geometry_msgs/msg/Point](https://docs.ros2.org/latest/api/geometry_msgs/msg/Point.html)
- [geometry_msgs/msg/Quaternion](https://docs.ros2.org/latest/api/geometry_msgs/msg/Quaternion.html)
- [sensor_msgs/msg/Image](https://docs.ros2.org/latest/api/sensor_msgs/msg/Image.html) (experimental)

In additon, you can define a customized message type in the same way as in ROS 2, and use its header file for your application.
The rest of this section describes how to generate header files for your own MsgTypes.
The example assumes the location as `<this_repo_dir>/mros2_msgs/` and target as `geometry_msgs::msg::Twist`.
The location is arbitrary, but be careful with the paths of the Python script and the .msg file.

### Prepare .msg files

`.msg` files are simple text files that describe the fields of a ROS message (see [About ROS 2 interface](https://docs.ros.org/en/rolling/Concepts/About-ROS-Interfaces.html)). In mros2, they are used to generate header files for messages in embedded applications.

Prepare `Twist.msg` file and make sure it is in `<this_repo_dir>/mros2_msgs/geometry_msgs/msg/`.

```
$ pwd
<this_repo_dir>/mros2_msgs
$ cat geometry_msgs/msg/Twist.msg
geometry_msgs/msg/Vector3 linear
geometry_msgs/msg/Vector3 angular
```

In this example, `Twist` has a nested structure with `Vector3` as a child element. So you also need to prepare its file.

```
$ cat geometry_msgs/msg/Vector3.msg
float64 x
float64 y
float64 z
```

### Generate header files

To generate header files for `Twist` and `Vector3`, run the following command in `<this_repo_dir>/mros2_msgs/` (again, be careful about the paths!).

```
$ cd <this_repo_dir>/mros2_msgs
$ python3 ../mros2_header_generator/header_generator.py geometry_msgs/msg/Twist.msg
```

Make sure header files for custom MsgType are generated in `geometry_msgs/`.

```
$ ls -R geometry_msgs/
geometry_msgs:
msg
geometry_msgs/msg:
twist.hpp vector3.hpp Twist.msg Vector3.msg
```

You can now use them in your applicaton like this.

```
#include "mros2.hpp"
#include "mros2-platform.hpp"
#include "geometry_msgs/msg/vector3.hpp"
#include "geometry_msgs/msg/twist.hpp"
int main(int argc, char * argv[])
{
<snip.>
pub = node.create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
<snip.>
```

When you generated your own header files at an arbitrary location, you need to add its path as an include path for build (e.g., CMakeLists.txt).

If you generated new header files at `<this_repo_dir>/mros2_msgs/`, we are very welcome to your PR!!

## License

The source code of this repository itself is published under [Apache License 2.0](https://github.com/mROS-base/mros2/blob/main/LICENSE).
Expand Down
8 changes: 2 additions & 6 deletions mros2_header_generator/header_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ def main():
template = env.get_template('header_template.tpl')
datatext = template.render({ "msg": msg })

msgPkgPath = "custom_msgs" + "/" + msg['pkg']
msgPkgPath = msg['pkg']

if not(os.path.isdir("custom_msgs")):
os.mkdir("custom_msgs")
if not(os.path.isdir(msgPkgPath)):
os.mkdir(msgPkgPath)
if not(os.path.isdir(msgPkgPath + "/msg")):
Expand All @@ -48,10 +46,8 @@ def genDepMsgHeader(genMsg):
template = env.get_template('header_template.tpl')
datatext = template.render({ "msg": msg })

msgPkgPath = "custom_msgs" + "/" + msg['pkg']
msgPkgPath = msg['pkg']

if not(os.path.isdir("custom_msgs")):
os.mkdir("custom_msgs")
if not(os.path.isdir(msgPkgPath)):
os.mkdir(msgPkgPath)
if not(os.path.isdir(msgPkgPath + "/msg")):
Expand Down
4 changes: 2 additions & 2 deletions mros2_header_generator/msg_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

def msgDataGenerator(line):
dependingFileNames = []
if os.path.isfile("custom_msgs/" + line):
with open("custom_msgs/" + line, 'r') as m_f:
if os.path.isfile(line):
with open(line, 'r') as m_f:
arr = m_f.readlines()
msgDef = []
for m_line in arr:
Expand Down
2 changes: 1 addition & 1 deletion mros2_header_generator/msg_def_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def msgDefGenerator(msgDefStr, dependingFileNames):
}

else:
if os.path.isfile("custom_msgs/" + msgType + ".msg"): # when custom type
if os.path.isfile(msgType + ".msg"): # when custom type
dependingFileName = toSnakeCase(msgType) + ".hpp"
depFileArr = dependingFileName.split("/")
if depFileArr[2][0] == "_":
Expand Down
3 changes: 3 additions & 0 deletions mros2_msgs/geometry_msgs/msg/Point.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float64 x
float64 y
float64 z
2 changes: 2 additions & 0 deletions mros2_msgs/geometry_msgs/msg/Pose.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
geometry_msgs/msg/Point position
geometry_msgs/msg/Quaternion orientation
4 changes: 4 additions & 0 deletions mros2_msgs/geometry_msgs/msg/Quaternion.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
float64 x
float64 y
float64 z
float64 w
2 changes: 2 additions & 0 deletions mros2_msgs/geometry_msgs/msg/Twist.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
geometry_msgs/msg/Vector3 linear
geometry_msgs/msg/Vector3 angular
3 changes: 3 additions & 0 deletions mros2_msgs/geometry_msgs/msg/Vector3.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float64 x
float64 y
float64 z
Loading

0 comments on commit 7a8606d

Please # to comment.