Skip to content

Commit cd9fded

Browse files
committed
simple_pub simple_sub as libraries; added back multihtread examples
1 parent e16fec4 commit cd9fded

23 files changed

+127
-208
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ Running `simple_client/timer_main` contains an alternative implementation for t
5555
$ ros2 run simple_logger logger_main
5656
```
5757

58-
Log some data. Change the logger verbosity level at run time. Log some other data.
58+
Log some messages. Change the logger verbosity level at run time. Log some other messages.
5959

60+
#### ROS2 Multithread
61+
```
62+
$ ros2 run simple_multithread multithread_main
63+
```
64+
65+
Run multiple nodes in separate threads from the same process.
6066

6167
#### Time Synchronization message filters
6268
```

simple_client/timer_client.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
#include "timer_client.hpp"
44

5-
65
using GetImageSrv = simple_interfaces::srv::GetImage;
76
using namespace std::chrono_literals;
87

9-
10-
118
TimerClientNode::TimerClientNode(std::string name) : Node(name)
129
{
1310

simple_multithread/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ endif()
1313
find_package(ament_cmake REQUIRED)
1414
find_package(rclcpp REQUIRED)
1515
find_package(simple_publisher REQUIRED)
16+
find_package(simple_subscriber REQUIRED)
1617

18+
add_executable(multithread_main multithread_main.cpp)
19+
ament_target_dependencies(multithread_main rclcpp simple_publisher simple_subscriber)
1720

18-
add_executable(main main.cpp)
19-
ament_target_dependencies(main rclcpp simple_publisher)
21+
add_executable(executor_main executor_main.cpp)
22+
ament_target_dependencies(executor_main rclcpp simple_publisher simple_subscriber)
2023

2124
install(TARGETS
22-
main
25+
multithread_main
26+
executor_main
2327
DESTINATION lib/${PROJECT_NAME}
2428
)
2529

simple_multithread/executor_main.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <chrono>
2+
#include <memory>
3+
#include <iostream>
4+
#include <thread>
5+
6+
#include "rclcpp/rclcpp.hpp"
7+
8+
#include "simple_publisher.hpp"
9+
#include "simple_subscriber.hpp"
10+
11+
12+
int main(int argc, char ** argv)
13+
{
14+
rclcpp::init(argc, argv);
15+
16+
std::shared_ptr<SimplePublisherNode> pub_node = std::make_shared<SimplePublisherNode>();
17+
std::shared_ptr<SimpleSubscriberNode> sub_node = std::make_shared<SimpleSubscriberNode>();
18+
19+
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor =
20+
std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
21+
22+
executor->add_node(pub_node);
23+
executor->add_node(sub_node);
24+
25+
executor->spin();
26+
27+
rclcpp::shutdown();
28+
29+
return 0;
30+
31+
}

simple_multithread/main.cpp

-17
This file was deleted.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <chrono>
2+
#include <memory>
3+
#include <iostream>
4+
#include <thread>
5+
6+
#include "rclcpp/rclcpp.hpp"
7+
8+
#include "simple_publisher.hpp"
9+
#include "simple_subscriber.hpp"
10+
11+
using namespace std::chrono_literals;
12+
13+
int main(int argc, char ** argv)
14+
{
15+
rclcpp::init(argc, argv);
16+
17+
std::shared_ptr<SimplePublisherNode> pub_node = std::make_shared<SimplePublisherNode>();
18+
std::shared_ptr<SimpleSubscriberNode> sub_node = std::make_shared<SimpleSubscriberNode>();
19+
20+
std::thread pub_thread(static_cast<void (*)(rclcpp::Node::SharedPtr)>(rclcpp::spin), pub_node);
21+
std::thread sub_thread(static_cast<void (*)(rclcpp::Node::SharedPtr)>(rclcpp::spin), sub_node);
22+
23+
24+
std::this_thread::sleep_for(10s);
25+
26+
27+
rclcpp::shutdown();
28+
29+
return 0;
30+
31+
}

simple_multithread/package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
<build_depend>rclcpp</build_depend>
1414
<build_depend>simple_publisher</build_depend>
15+
<build_depend>simple_subscriber</build_depend>
1516

1617
<exec_depend>rclcpp</exec_depend>
1718
<exec_depend>simple_publisher</exec_depend>
19+
<exec_depend>simple_subscriber</exec_depend>
1820

1921
<export>
2022
<build_type>ament_cmake</build_type>

simple_publisher/CMakeLists.txt

+23
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,34 @@ find_package(ament_cmake REQUIRED)
1414
find_package(rclcpp REQUIRED)
1515
find_package(std_msgs REQUIRED)
1616

17+
include_directories(include)
18+
19+
add_library(${PROJECT_NAME} simple_publisher.cpp)
20+
ament_target_dependencies(${PROJECT_NAME} rclcpp sensor_msgs)
21+
22+
install(
23+
TARGETS ${PROJECT_NAME}
24+
ARCHIVE DESTINATION lib
25+
LIBRARY DESTINATION lib
26+
RUNTIME DESTINATION bin
27+
)
28+
29+
install(
30+
DIRECTORY include/
31+
DESTINATION include
32+
)
1733

1834
add_executable(publisher_main publisher_main.cpp simple_publisher.cpp)
1935
ament_target_dependencies(publisher_main rclcpp std_msgs)
2036

2137
install(TARGETS publisher_main
2238
DESTINATION lib/${PROJECT_NAME})
2339

40+
41+
ament_export_include_directories(include)
42+
ament_export_libraries(${PROJECT_NAME})
43+
44+
ament_export_dependencies(rclcpp)
45+
ament_export_dependencies(std_msgs)
46+
2447
ament_package()

simple_security/CMakeLists.txt

-27
This file was deleted.
File renamed without changes.

simple_security/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Then we can populate the directory with authentication keys for our nodes.
3131

3232
Now that we have defined some keys, it's necessary to enable the ROS2 security in all the terminals that we are using.
3333

34-
$ source security_scripts/enable_security.sh
34+
$ source enable_security.sh
3535

36-
**NOTE**: you can reset these environment variables by sourcing the `security_scripts/disable_security.sh` script.
36+
**NOTE**: you can reset these environment variables with `source disable_security.sh`.
3737

3838
Now security is enabled.
3939

@@ -69,7 +69,7 @@ As we have seen, as long as security options are enabled, only nodes with autent
6969

7070
If we have our secured pub/sub system running, we can still however start a new node without security options.
7171

72-
You can do that by opening a new terminal or with `source security_scripts/disable_security.sh`.
72+
You can do that by opening a new terminal or with `source disable_security.sh`.
7373

7474
$ ros2 run simple_subscriber subscriber_main __node:=additional_subscriber
7575

File renamed without changes.

simple_security/package.xml

-23
This file was deleted.
File renamed without changes.

simple_security/publishers_main.cpp

-79
This file was deleted.

simple_security/subscribers_main.cpp

-52
This file was deleted.

0 commit comments

Comments
 (0)