Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 5edc9ff

Browse files
committed
Refs #3845. Solves bug of publisher susbscriber examples.
1 parent 6805bd4 commit 5edc9ff

2 files changed

Lines changed: 59 additions & 23 deletions

File tree

examples/uros/publisher/publisher_main.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,37 @@ int publisher_main(int argc, char* argv[])
1111
{
1212
(void)argc;
1313
(void)argv;
14+
int result = 0;
1415
rclc_init(1, "");
1516
const rclc_message_type_support_t type_support = RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32);
16-
rclc_node_t* node = rclc_create_node("publisher_node", "");
17-
rclc_publisher_t* publisher = rclc_create_publisher(node, type_support, "publisher_example", 1);
18-
19-
std_msgs__msg__Int32 msg;
20-
std_msgs__msg__Int32__init(&msg);
17+
rclc_node_t* node = NULL;
18+
if (node = rclc_create_node("publisher_node", ""))
19+
{
20+
rclc_publisher_t* publisher = NULL;
21+
if(publisher = rclc_create_publisher(node, type_support, "std_msgs_msg_Int32", 1))
22+
{
2123

22-
while (rclc_ok())
24+
std_msgs__msg__Int32 msg;
25+
msg.data = 0;
26+
while (rclc_ok() && msg.data <= 1000)
27+
{
28+
printf("Sending: '%i'\n", msg.data++);
29+
rclc_publish(publisher, (const void*)&msg);
30+
rclc_spin_node_once(node, 500);
31+
}
32+
rclc_destroy_publisher(publisher);
33+
}
34+
else
35+
{
36+
printf("Issues creating publisher\n");
37+
result = -1;
38+
}
39+
rclc_destroy_node(node);
40+
}
41+
else
2342
{
24-
printf("Sending: '%i'\n", msg.data++);
25-
rclc_publish(publisher, (const void*)&msg);
26-
rclc_spin_node_once(node, 500);
43+
printf("Issues creating node\n");
44+
result = -1;
2745
}
28-
rclc_destroy_publisher(publisher);
29-
rclc_destroy_node(node);
30-
return 0;
46+
return result;
3147
}

examples/uros/subscriber/subscriber_main.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
#include <std_msgs/msg/int32.h>
44
#include <stdio.h>
55

6+
static int count = 0;
7+
68
void on_message(const void* msgin)
79
{
810
const std_msgs__msg__Int32* msg = (const std_msgs__msg__Int32*)msgin;
911
printf("I heard: [%i]\n", msg->data);
12+
count++;
1013
}
1114

1215
#if defined(BUILD_MODULE)
@@ -17,15 +20,32 @@ int subscriber_main(int argc, char* argv[])
1720
{
1821
(void)argc;
1922
(void)argv;
20-
rclc_init(0, NULL);
21-
rclc_node_t* node = rclc_create_node("int32_subscriber_c", "");
22-
23-
rclc_subscription_t* sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
24-
"std_msgs_msg_Int32", on_message, 1, false);
25-
26-
rclc_spin_node(node);
27-
28-
rclc_destroy_subscription(sub);
29-
rclc_destroy_node(node);
30-
return 0;
23+
int result = 0;
24+
rclc_init(1, "");
25+
rclc_node_t* node = NULL;
26+
if (node = rclc_create_node("int32_subscriber_c", ""))
27+
{
28+
rclc_subscription_t* sub = NULL;
29+
if(sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
30+
"std_msgs_msg_Int32", on_message, 1, false))
31+
{
32+
while (rclc_ok() && count <= 50)
33+
{
34+
rclc_spin_node_once(node, 500);
35+
}
36+
rclc_destroy_subscription(sub);
37+
}
38+
else
39+
{
40+
printf("Issues creating subscriber\n");
41+
result = -1;
42+
}
43+
rclc_destroy_node(node);
44+
}
45+
else
46+
{
47+
printf("Issues creating node\n");
48+
result = -1;
49+
}
50+
return result;
3151
}

0 commit comments

Comments
 (0)