Skip to content

Commit d8f2c9e

Browse files
committed
Demonstration
1 parent 2dc70f2 commit d8f2c9e

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/agnocast_sample_application/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ target_include_directories(sim_time_timer PRIVATE
9696
${agnocastlib_INCLUDE_DIRS}
9797
)
9898

99+
add_executable(ros_client src/ros_client.cpp)
100+
ament_target_dependencies(ros_client rclcpp agnocast_sample_interfaces)
101+
99102
add_library(listener_component SHARED src/minimal_subscriber.cpp)
100103
ament_target_dependencies(listener_component rclcpp rclcpp_components agnocastlib agnocast_sample_interfaces)
101104
target_include_directories(listener_component PRIVATE ${agnocastlib_INCLUDE_DIRS})
@@ -160,6 +163,9 @@ install(TARGETS no_rclcpp_client
160163
install(TARGETS sim_time_timer
161164
DESTINATION lib/${PROJECT_NAME})
162165

166+
install(TARGETS ros_client
167+
DESTINATION lib/${PROJECT_NAME})
168+
163169
install(DIRECTORY launch
164170
DESTINATION share/${PROJECT_NAME}/
165171
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<launch>
2+
<node pkg="agnocast_sample_application" exec="ros_client" name="ros_client_node" output="screen">
3+
<remap from="/sum_int_array" to="/srv/sum_int_array" />
4+
</node>
5+
</launch>

src/agnocast_sample_application/launch/server.launch.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<launch>
2-
<node pkg="agnocast_sample_application" exec="server" name="server_node" output="screen">
2+
<node pkg="agnocast_sample_application" exec="server" name="server_node" output="screen" emulate_tty="true">
33
<remap from="/sum_int_array" to="/srv/sum_int_array" />
44

55
<env name="LD_PRELOAD" value="libagnocast_heaphook.so:$(env LD_PRELOAD '')" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "agnocast_sample_interfaces/srv/sum_int_array.hpp"
2+
#include "rclcpp/rclcpp.hpp"
3+
4+
#include <thread>
5+
6+
using namespace std::chrono_literals;
7+
8+
constexpr size_t ARRAY_SIZE = 100;
9+
10+
int main(int argc, char * argv[])
11+
{
12+
rclcpp::init(argc, argv);
13+
14+
auto node = rclcpp::Node::make_shared("ros_client");
15+
std::thread spin_thread([node]() mutable {
16+
rclcpp::executors::SingleThreadedExecutor executor;
17+
executor.add_node(node);
18+
executor.spin();
19+
});
20+
21+
auto client = node->create_client<agnocast_sample_interfaces::srv::SumIntArray>("sum_int_array");
22+
23+
while (!client->wait_for_service(1s)) {
24+
if (!rclcpp::ok()) {
25+
RCLCPP_ERROR(node->get_logger(), "Interrupted while waiting for the service. Exiting.");
26+
return 0;
27+
}
28+
RCLCPP_INFO(node->get_logger(), "Service not available, waiting again...");
29+
}
30+
31+
auto request1 =
32+
std::make_shared<typename agnocast_sample_interfaces::srv::SumIntArray::Request>();
33+
for (size_t i = 1; i <= ARRAY_SIZE; ++i) {
34+
request1->data.push_back(i);
35+
}
36+
client->async_send_request(
37+
std::move(request1),
38+
[node = node.get()](
39+
rclcpp::Client<agnocast_sample_interfaces::srv::SumIntArray>::SharedFuture future) {
40+
RCLCPP_INFO(node->get_logger(), "Result1: %ld", future.get()->sum);
41+
});
42+
43+
auto request2 =
44+
std::make_shared<typename agnocast_sample_interfaces::srv::SumIntArray::Request>();
45+
for (size_t i = 0; i < ARRAY_SIZE; ++i) {
46+
request2->data.push_back(i);
47+
}
48+
auto future = client->async_send_request(std::move(request2));
49+
RCLCPP_INFO(node->get_logger(), "Result2: %ld", future.get()->sum);
50+
51+
spin_thread.join();
52+
rclcpp::shutdown();
53+
return 0;
54+
}

0 commit comments

Comments
 (0)