|
12 | 12 |
|
13 | 13 | #include "dynamic_graph_bridge/ros.hpp"
|
14 | 14 |
|
15 |
| -namespace dynamic_graph_bridge |
16 |
| -{ |
| 15 | +namespace dynamic_graph_bridge { |
17 | 16 | /**
|
18 | 17 | * @brief Client of the RosPythonInterpreterServer through rosservices.
|
19 | 18 | */
|
20 |
| -class RosPythonInterpreterClient |
21 |
| -{ |
22 |
| -public: |
23 |
| - /** |
24 |
| - * @brief Construct a new RosPythonInterpreterClient object. |
25 |
| - */ |
26 |
| - RosPythonInterpreterClient(); |
27 |
| - |
28 |
| - /** |
29 |
| - * @brief Destroy the RosPythonInterpreterClient object. |
30 |
| - */ |
31 |
| - ~RosPythonInterpreterClient() |
32 |
| - { |
| 19 | +class RosPythonInterpreterClient { |
| 20 | + public: |
| 21 | + /** |
| 22 | + * @brief Construct a new RosPythonInterpreterClient object. |
| 23 | + */ |
| 24 | + RosPythonInterpreterClient(); |
| 25 | + |
| 26 | + /** |
| 27 | + * @brief Destroy the RosPythonInterpreterClient object. |
| 28 | + */ |
| 29 | + ~RosPythonInterpreterClient() {} |
| 30 | + |
| 31 | + /** |
| 32 | + * @brief Call the rosservice of the current running DynamicGraphManager. |
| 33 | + * |
| 34 | + * @param code_string To be executed in the embeded python interpreter. |
| 35 | + * @return std::string |
| 36 | + */ |
| 37 | + std::string run_python_command(const std::string& code_string); |
| 38 | + |
| 39 | + /** |
| 40 | + * @brief Call the rosservice of the current running DynamicGraphManager. |
| 41 | + * |
| 42 | + * @param filename |
| 43 | + */ |
| 44 | + std::string run_python_script(const std::string& filename); |
| 45 | + |
| 46 | + private: |
| 47 | + /** |
| 48 | + * @brief Connect to a designated service. |
| 49 | + * |
| 50 | + * @tparam RosServiceType |
| 51 | + * @param service_name |
| 52 | + * @param client |
| 53 | + * @param timeout |
| 54 | + */ |
| 55 | + template <typename RosServiceType> |
| 56 | + typename rclcpp::Client<RosServiceType>::SharedPtr connect_to_rosservice( |
| 57 | + const std::string& service_name, const DurationSec& timeout) { |
| 58 | + typename rclcpp::Client<RosServiceType>::SharedPtr client; |
| 59 | + try { |
| 60 | + RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
| 61 | + "Waiting for service %s ...", service_name.c_str()); |
| 62 | + // let use wait for the existance of the services |
| 63 | + client = ros_node_->create_client<RosServiceType>(service_name.c_str()); |
| 64 | + if (client->wait_for_service(timeout)) { |
| 65 | + RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
| 66 | + "Successfully connected to %s", service_name.c_str()); |
| 67 | + } else { |
| 68 | + RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
| 69 | + "Failed to connect to %s", service_name.c_str()); |
| 70 | + } |
| 71 | + } catch (...) { |
| 72 | + throw std::runtime_error(service_name + " not available."); |
33 | 73 | }
|
34 |
| - |
35 |
| - /** |
36 |
| - * @brief Call the rosservice of the current running DynamicGraphManager. |
37 |
| - * |
38 |
| - * @param code_string To be executed in the embeded python interpreter. |
39 |
| - * @return std::string |
40 |
| - */ |
41 |
| - std::string run_python_command(const std::string& code_string); |
42 |
| - |
43 |
| - /** |
44 |
| - * @brief Call the rosservice of the current running DynamicGraphManager. |
45 |
| - * |
46 |
| - * @param filename |
47 |
| - */ |
48 |
| - std::string run_python_script(const std::string& filename); |
49 |
| - |
50 |
| -private: |
51 |
| - /** |
52 |
| - * @brief Connect to a designated service. |
53 |
| - * |
54 |
| - * @tparam RosServiceType |
55 |
| - * @param service_name |
56 |
| - * @param client |
57 |
| - * @param timeout |
58 |
| - */ |
59 |
| - template <typename RosServiceType> |
60 |
| - typename rclcpp::Client<RosServiceType>::SharedPtr connect_to_rosservice( |
61 |
| - const std::string& service_name, const DurationSec& timeout) |
62 |
| - { |
63 |
| - typename rclcpp::Client<RosServiceType>::SharedPtr client; |
64 |
| - try |
65 |
| - { |
66 |
| - RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
67 |
| - "Waiting for service %s ...", |
68 |
| - service_name.c_str()); |
69 |
| - // let use wait for the existance of the services |
70 |
| - client = |
71 |
| - ros_node_->create_client<RosServiceType>(service_name.c_str()); |
72 |
| - if (client->wait_for_service(timeout)) |
73 |
| - { |
74 |
| - RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
75 |
| - "Successfully connected to %s", |
76 |
| - service_name.c_str()); |
77 |
| - } |
78 |
| - else |
79 |
| - { |
80 |
| - RCLCPP_INFO(rclcpp::get_logger("RosPythonInterpreterClient"), |
81 |
| - "Failed to connect to %s", |
82 |
| - service_name.c_str()); |
83 |
| - } |
84 |
| - } |
85 |
| - catch (...) |
86 |
| - { |
87 |
| - throw std::runtime_error(service_name + " not available."); |
88 |
| - } |
89 |
| - return client; |
90 |
| - } |
91 |
| - |
92 |
| - /** |
93 |
| - * @brief Connects to the RunCommand rosservice. |
94 |
| - * |
95 |
| - * @param timeout [in] |
96 |
| - */ |
97 |
| - void connect_to_rosservice_run_python_command( |
98 |
| - const DurationSec& timeout = DurationSec(-1)) |
99 |
| - { |
100 |
| - command_client_ = connect_to_rosservice<RunPythonCommandSrvType>( |
101 |
| - run_command_service_name_, timeout); |
102 |
| - } |
103 |
| - |
104 |
| - /** |
105 |
| - * @brief Connects to the RunPythonFile rosservice. |
106 |
| - * |
107 |
| - * @param timeout [in] |
108 |
| - */ |
109 |
| - void connect_to_rosservice_run_python_script( |
110 |
| - const DurationSec& timeout = DurationSec(-1)) |
111 |
| - { |
112 |
| - script_client_ = connect_to_rosservice<RunPythonFileSrvType>( |
113 |
| - run_script_service_name_, timeout); |
114 |
| - } |
115 |
| - |
116 |
| -private: |
117 |
| - /** |
118 |
| - * @brief Name of the local ros_node; |
119 |
| - */ |
120 |
| - std::string ros_node_name_; |
121 |
| - |
122 |
| - /** @brief Handle for manipulating ROS objects. */ |
123 |
| - RosNodePtr ros_node_; |
124 |
| - |
125 |
| - /** @brief Name of the DynamicGraphManager RosPythonInterpreterServer |
126 |
| - * rosservice for running a python command. */ |
127 |
| - std::string run_command_service_name_; |
128 |
| - |
129 |
| - /** @brief Rosservice to run a python command. |
130 |
| - * @see run_command_service_name_ */ |
131 |
| - RunPythonCommandClientPtr command_client_; |
132 |
| - |
133 |
| - /** @brief Input of the rosservice. */ |
134 |
| - RunPythonCommandRequestPtr run_command_request_; |
135 |
| - |
136 |
| - /** @brief Name of the DynamicGraphManager RosPythonInterpreterServer |
137 |
| - * rosservice for running a python script. */ |
138 |
| - std::string run_script_service_name_; |
139 |
| - |
140 |
| - /** @brief Rosservice to run a python script. |
141 |
| - * @see run_command_service_name_ */ |
142 |
| - RunPythonFileClientPtr script_client_; |
143 |
| - |
144 |
| - /** @brief Input of the rosservice. */ |
145 |
| - RunPythonFileRequestPtr run_file_request_; |
146 |
| - |
147 |
| - /** @brief timeout used during the connection to the rosservices in |
148 |
| - * seconds. */ |
149 |
| - DurationSec timeout_connection_s_; |
| 74 | + return client; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Connects to the RunCommand rosservice. |
| 79 | + * |
| 80 | + * @param timeout [in] |
| 81 | + */ |
| 82 | + void connect_to_rosservice_run_python_command( |
| 83 | + const DurationSec& timeout = DurationSec(-1)) { |
| 84 | + command_client_ = connect_to_rosservice<RunPythonCommandSrvType>( |
| 85 | + run_command_service_name_, timeout); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief Connects to the RunPythonFile rosservice. |
| 90 | + * |
| 91 | + * @param timeout [in] |
| 92 | + */ |
| 93 | + void connect_to_rosservice_run_python_script( |
| 94 | + const DurationSec& timeout = DurationSec(-1)) { |
| 95 | + script_client_ = connect_to_rosservice<RunPythonFileSrvType>( |
| 96 | + run_script_service_name_, timeout); |
| 97 | + } |
| 98 | + |
| 99 | + private: |
| 100 | + /** |
| 101 | + * @brief Name of the local ros_node; |
| 102 | + */ |
| 103 | + std::string ros_node_name_; |
| 104 | + |
| 105 | + /** @brief Handle for manipulating ROS objects. */ |
| 106 | + RosNodePtr ros_node_; |
| 107 | + |
| 108 | + /** @brief Name of the DynamicGraphManager RosPythonInterpreterServer |
| 109 | + * rosservice for running a python command. */ |
| 110 | + std::string run_command_service_name_; |
| 111 | + |
| 112 | + /** @brief Rosservice to run a python command. |
| 113 | + * @see run_command_service_name_ */ |
| 114 | + RunPythonCommandClientPtr command_client_; |
| 115 | + |
| 116 | + /** @brief Input of the rosservice. */ |
| 117 | + RunPythonCommandRequestPtr run_command_request_; |
| 118 | + |
| 119 | + /** @brief Name of the DynamicGraphManager RosPythonInterpreterServer |
| 120 | + * rosservice for running a python script. */ |
| 121 | + std::string run_script_service_name_; |
| 122 | + |
| 123 | + /** @brief Rosservice to run a python script. |
| 124 | + * @see run_command_service_name_ */ |
| 125 | + RunPythonFileClientPtr script_client_; |
| 126 | + |
| 127 | + /** @brief Input of the rosservice. */ |
| 128 | + RunPythonFileRequestPtr run_file_request_; |
| 129 | + |
| 130 | + /** @brief timeout used during the connection to the rosservices in |
| 131 | + * seconds. */ |
| 132 | + DurationSec timeout_connection_s_; |
150 | 133 | };
|
151 | 134 |
|
152 | 135 | } // namespace dynamic_graph_bridge
|
0 commit comments