Skip to content

Commit 581d0bd

Browse files
author
Javier Moreno
committed
Added common demos
1 parent 680a706 commit 581d0bd

12 files changed

Lines changed: 269 additions & 0 deletions

File tree

‎int32_publisher/CMakeLists.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=*)
3+
project(int32_publisher)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclc QUIET)
7+
find_package(std_msgs REQUIRED)
8+
9+
# Do not compile package if rclcpp is not found
10+
if (NOT rclc_FOUND)
11+
message(WARNING "${PROJECT_NAME} will be ignored due to rclc is not installed")
12+
return()
13+
endif()
14+
15+
add_executable(${PROJECT_NAME} main.c)
16+
ament_target_dependencies(${PROJECT_NAME} rclc std_msgs)
17+
18+
install(TARGETS
19+
${PROJECT_NAME}
20+
DESTINATION lib/${PROJECT_NAME}
21+
)
22+
23+
ament_package()

‎int32_publisher/main.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <rclc/rclc.h>
2+
#include <std_msgs/msg/int32.h>
3+
4+
#include <stdio.h>
5+
6+
int main(int argc, char *argv[])
7+
{
8+
(void)argc;
9+
(void)argv;
10+
rclc_init(0, NULL);
11+
rclc_node_t* node = rclc_create_node("publisher_node", "");
12+
rclc_publisher_t* publisher = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "publisher_example", 1);
13+
14+
std_msgs__msg__Int32 msg;
15+
msg.data = -1;
16+
17+
while (rclc_ok())
18+
{
19+
printf("Sending: '%i'\n", msg.data++);
20+
rclc_publish(publisher, (const void*)&msg);
21+
rclc_spin_node_once(node, 500);
22+
}
23+
rclc_destroy_publisher(publisher);
24+
rclc_destroy_node(node);
25+
return 0;
26+
}

‎int32_publisher/package.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>int32_publisher</name>
5+
<version>0.0.1</version>
6+
<description>Example of a publisher using int32</description>
7+
<maintainer email="borjaouterelo@eprosima.com">borjaouterelo</maintainer>
8+
<license>Apache License 2.0</license>
9+
<buildtool_depend>ament_cmake</buildtool_depend>
10+
<build_depend>rclc</build_depend>
11+
<build_depend>std_msgs</build_depend>
12+
<export>
13+
<build_type>ament_cmake</build_type>
14+
</export>
15+
</package>
16+

‎int32_subscriber/CMakeLists.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=*)
3+
project(int32_subscriber)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclc QUIET)
7+
find_package(std_msgs REQUIRED)
8+
9+
# Do not compile package if rclcpp is not found
10+
if (NOT rclc_FOUND)
11+
message(WARNING "${PROJECT_NAME} will be ignored due to rclc is not installed")
12+
return()
13+
endif()
14+
15+
add_executable(${PROJECT_NAME} main.c)
16+
ament_target_dependencies(${PROJECT_NAME} rclc std_msgs)
17+
18+
install(TARGETS
19+
${PROJECT_NAME}
20+
DESTINATION lib/${PROJECT_NAME}
21+
)
22+
23+
ament_package()

‎int32_subscriber/main.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <rclc/rclc.h>
2+
#include <std_msgs/msg/int32.h>
3+
4+
#include <stdio.h>
5+
6+
void on_message(const void* msgin)
7+
{
8+
const std_msgs__msg__Int32* msg = (const std_msgs__msg__Int32*)msgin;
9+
printf("I heard: [%i]\n", msg->data);
10+
}
11+
12+
int main(int argc, char* argv[])
13+
{
14+
(void)argc;
15+
(void)argv;
16+
rclc_init(0, NULL);
17+
rclc_node_t* node = rclc_create_node("subscription_node", "");
18+
rclc_subscription_t* sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
19+
"subscription_example", on_message, 1, false);
20+
21+
rclc_spin_node(node);
22+
23+
rclc_destroy_subscription(sub);
24+
rclc_destroy_node(node);
25+
return 0;
26+
}

‎int32_subscriber/package.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>int32_subscriber</name>
5+
<version>0.0.1</version>
6+
<description>Example of a subscriber using int32</description>
7+
<maintainer email="borjaouterelo@eprosima.com">borjaouterelo</maintainer>
8+
<license>Apache License 2.0</license>
9+
<buildtool_depend>ament_cmake</buildtool_depend>
10+
<build_depend>rclc</build_depend>
11+
<build_depend>std_msgs</build_depend>
12+
<export>
13+
<build_type>ament_cmake</build_type>
14+
</export>
15+
</package>
16+

‎string_publisher/CMakeLists.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=*)
3+
project(string_publisher)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclc QUIET)
7+
find_package(std_msgs REQUIRED)
8+
9+
# Do not compile package if rclcpp is not found
10+
if (NOT rclc_FOUND)
11+
message(WARNING "${PROJECT_NAME} will be ignored due to rclc is not installed")
12+
return()
13+
endif()
14+
15+
add_executable(${PROJECT_NAME} main.c)
16+
ament_target_dependencies(${PROJECT_NAME} rclc std_msgs)
17+
18+
install(TARGETS
19+
${PROJECT_NAME}
20+
DESTINATION lib/${PROJECT_NAME}
21+
)
22+
23+
ament_package()

‎string_publisher/main.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <rclc/rclc.h>
2+
#include <std_msgs/msg/string.h>
3+
4+
#include <stdio.h>
5+
6+
int main(int argc, char* argv[])
7+
{
8+
(void)argc;
9+
(void)argv;
10+
rclc_init(0, NULL);
11+
rclc_node_t* node = rclc_create_node("publisher_node", "");
12+
rclc_publisher_t* publisher =
13+
rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "std_msgs_msg_String", 1);
14+
15+
std_msgs__msg__String msg;
16+
char buff[128] = {0};
17+
msg.data.data = buff;
18+
msg.data.capacity = sizeof(buff);
19+
msg.data.size = 0;
20+
21+
int num = 0;
22+
while (rclc_ok())
23+
{
24+
msg.data.size = snprintf(msg.data.data, msg.data.capacity, "Hello World %i", num++);
25+
if (msg.data.size > msg.data.capacity)
26+
msg.data.size = 0;
27+
28+
printf("Sending: '%s'\n", msg.data.data);
29+
rclc_publish(publisher, (const void*)&msg);
30+
rclc_spin_node_once(node, 500);
31+
}
32+
rclc_destroy_publisher(publisher);
33+
rclc_destroy_node(node);
34+
return 0;
35+
}

‎string_publisher/package.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>string_publisher</name>
5+
<version>0.0.1</version>
6+
<description>Example of a string publisher</description>
7+
<maintainer email="borjaouterelo@eprosima.com">borjaouterelo</maintainer>
8+
<license>Apache License 2.0</license>
9+
<buildtool_depend>ament_cmake</buildtool_depend>
10+
<build_depend>rclc</build_depend>
11+
<build_depend>std_msgs</build_depend>
12+
<export>
13+
<build_type>ament_cmake</build_type>
14+
</export>
15+
</package>
16+

‎string_subscriber/CMakeLists.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=*)
3+
project(string_subscriber)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclc QUIET)
7+
find_package(std_msgs REQUIRED)
8+
9+
# Do not compile package if rclcpp is not found
10+
if (NOT rclc_FOUND)
11+
message(WARNING "${PROJECT_NAME} will be ignored due to rclc is not installed")
12+
return()
13+
endif()
14+
15+
add_executable(${PROJECT_NAME} main.c)
16+
ament_target_dependencies(${PROJECT_NAME} rclc std_msgs)
17+
18+
install(TARGETS
19+
${PROJECT_NAME}
20+
DESTINATION lib/${PROJECT_NAME}
21+
)
22+
23+
ament_package()

0 commit comments

Comments
 (0)