ros2 how to implement subscription using RCL

asked 2020-01-31 05:29:57 -0500

marney gravatar image

I am studying RCL. I wrote this code and made a simple subscriber. I'd like a subscriber to take a message when any message is published. How do I edit it ?

int main(int argc,const* argv[]){    
rcl_init_options_t initOptions = rcl_get_zero_initialized_init_options();
rcl_init_options_init(&initOptions, rcutils_get_default_allocator());
rcl_context_t context = rcl_get_zero_initialized_context();
rcl_init(argc, argv, &initOptions, &context);
rcl_init_options_fini(&initOptions);

rcl_node_t node = rcl_get_zero_initialized_node();
rcl_node_options_t nodeOptions = rcl_node_get_default_options();
rcl_node_init(&node, "node_name", "node_namespace", &context, &nodeOptions);
const rosidl_message_type_support_t * typeSupport = ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String);
rcl_subscription_t subscription = rcl_get_zero_initialized_subscription();
rcl_subscription_options_t subscriptionOptions = rcl_subscription_get_default_options();
rcl_subscription_init(&subscription, &node, typeSupport, "test_topic", &subscriptionOptions);
std_msgs__msg__String msg;
rmw_message_info_t messageInfo;
rcl_take(&subscription, &msg, &messageInfo);
rcl_subscription_fini(&subscription, &node);
rcl_node_fini(&node);
return 0;
}

Thanks.

edit retag flag offensive close merge delete

Comments

Any particular reason to use rcl instead of rclcpp? Could you also clarify what you mean by any message?

MCornelis gravatar image MCornelis  ( 2020-02-03 08:16:26 -0500 )edit

You could have a look here https://github.com/micro-ROS/executor... . For the implementation of the LET executor made by Bosch for micro-ROS. It uses most aspects of the RCL layer in a simple to follow way using C. Hope this helps you ^^

MCornelis gravatar image MCornelis  ( 2020-02-03 08:25:17 -0500 )edit

Thank you so much for a comment. Actually, I try to make client library in other language by wrapping RCL, so I submitted this question. I also checked micro-ROS implementation, and it seems that functions related to rcl_wait_set_t are suitable for my purpose. Thanks.

marney gravatar image marney  ( 2020-02-04 20:14:23 -0500 )edit

There was also some talk about work on RCLC, so a C layer sitting on top of RCL, in the RealTime working group on ROS discourse. I'm not sure if work on this has already started/continued or not. I believe this is also an effort by Bosch. They are looking at C, because they are dealing with micro-controllers/-processors. Depending on what language you want to wrap you could maybe seek contact with the RT WG people. They do a lot of things with RCL and helped me a lot when I was looking at the C++ executor. Make sure to not post straight up questions on Discourse though (that's what ROS answers is for) and please don't derail the working group with off topic discussions :P. Just saying there are some smart people there that could have useful info in case you get stuck.

MCornelis gravatar image MCornelis  ( 2020-02-05 03:57:44 -0500 )edit