1313
1414#include < dynamic-graph/pool.h>
1515
16+ #include < boost/make_shared.hpp>
17+ #include < boost/shared_ptr.hpp>
1618#include < exception>
19+ #include < sot/core/robot-utils.hh>
20+ #include < stdexcept>
1721
18- #include " dynamic_graph_bridge/ros_parameter.hpp"
22+ #include " pinocchio/multibody/model.hpp"
23+ #include " pinocchio/parsers/urdf.hpp"
1924
2025// POSIX.1-2001
2126#include < dlfcn.h>
@@ -24,15 +29,15 @@ using namespace std;
2429using namespace dynamicgraph ::sot;
2530namespace po = boost::program_options;
2631
27- SotLoaderBasic::SotLoaderBasic (const std::string & aNodeName)
32+ SotLoaderBasic::SotLoaderBasic (const std::string& aNodeName)
2833 : rclcpp::Node(aNodeName),
2934 dynamic_graph_stopped_(true ),
35+ sotController_(NULL ),
3036 sotRobotControllerLibrary_(0 ) {
3137 RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge::" ),
3238 " Beginning of SotLoaderBasic" );
33- // nh_ = dynamic_graph_bridge::get_ros_node("SotLoaderBasic");
3439 RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
35- " End of SotLoaderBasic" );
40+ " End of SotLoaderBasic" );
3641}
3742
3843SotLoaderBasic::~SotLoaderBasic () {
@@ -41,23 +46,23 @@ SotLoaderBasic::~SotLoaderBasic() {
4146
4247void SotLoaderBasic::initialize () {}
4348
44- // rclcpp::Node::SharedPtr SotLoaderBasic::returnsNodeHandle() { return nh_; }
4549int SotLoaderBasic::initPublication () {
4650 // Prepare message to be published
4751 RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
4852 " SotLoaderBasic::initPublication - create joint_pub" );
49-
53+
5054 joint_pub_ =
5155 create_publisher<sensor_msgs::msg::JointState>(" joint_states" , 1 );
5256
5357 RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
54- " SotLoaderBasic::initPublication - after create joint_pub" );
58+ " SotLoaderBasic::initPublication - after create joint_pub" );
5559 return 0 ;
5660}
5761
5862void SotLoaderBasic::initializeServices () {
59- RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
60- " SotLoaderBasic::initializeServices - Ready to start dynamic graph." );
63+ RCLCPP_INFO (
64+ rclcpp::get_logger (" dynamic_graph_bridge" ),
65+ " SotLoaderBasic::initializeServices - Ready to start dynamic graph." );
6166
6267 using namespace std ::placeholders;
6368 service_start_ = create_service<std_srvs::srv::Empty>(
@@ -74,8 +79,7 @@ void SotLoaderBasic::initializeServices() {
7479 RCLCPP_INFO (rclcpp::get_logger (" dynamic_graph_bridge" ),
7580 " SotLoaderBasic::initializeServices - stopped dynamic graph." );
7681
77- rclcpp::Node::SharedPtr a_node_ptr (this );
78- dynamicgraph::parameter_server_read_robot_description (a_node_ptr);
82+ parameter_server_read_robot_description ();
7983}
8084
8185void SotLoaderBasic::setDynamicLibraryName (std::string& afilename) {
@@ -106,8 +110,7 @@ int SotLoaderBasic::readSotVectorStateParam() {
106110 RCLCPP_INFO (get_logger (), " state_vector_map parameter size %ld" ,
107111 stateVectorMap_.size ());
108112 } catch (exception& e) {
109- std::throw_with_nested (
110- std::logic_error (" Unable to call nh_->get_parameter" ));
113+ std::throw_with_nested (std::logic_error (" Unable to call get_parameter" ));
111114 }
112115
113116 nbOfJoints_ = static_cast <int >(stateVectorMap_.size ());
@@ -233,17 +236,19 @@ void SotLoaderBasic::CleanUp() {
233236 // SignalCaster singleton could probably be destroyed.
234237
235238 // Load the symbols.
236- destroySotExternalInterface_t* destroySot =
237- reinterpret_cast <destroySotExternalInterface_t*>(reinterpret_cast <long >(
238- dlsym (sotRobotControllerLibrary_, " destroySotExternalInterface" )));
239- const char * dlsym_error = dlerror ();
240- if (dlsym_error) {
241- std::cerr << " Cannot load symbol destroy: " << dlsym_error << ' \n ' ;
242- return ;
243- }
239+ if (sotController_ != NULL ) {
240+ destroySotExternalInterface_t* destroySot =
241+ reinterpret_cast <destroySotExternalInterface_t*>(reinterpret_cast <long >(
242+ dlsym (sotRobotControllerLibrary_, " destroySotExternalInterface" )));
243+ const char * dlsym_error = dlerror ();
244+ if (dlsym_error) {
245+ std::cerr << " Cannot load symbol destroy: " << dlsym_error << ' \n ' ;
246+ return ;
247+ }
244248
245- destroySot (sotController_);
246- sotController_ = NULL ;
249+ destroySot (sotController_);
250+ sotController_ = NULL ;
251+ }
247252
248253 // / Uncount the number of access to this library.
249254 try {
@@ -265,3 +270,44 @@ void SotLoaderBasic::stop_dg(
265270 std::shared_ptr<std_srvs::srv::Empty::Response>) {
266271 dynamic_graph_stopped_ = true ;
267272}
273+
274+ bool SotLoaderBasic::parameter_server_read_robot_description () {
275+ if (!has_parameter (" robot_description" )) {
276+ declare_parameter (" robot_description" , std::string (" " ));
277+ }
278+
279+ std::string robot_description;
280+ std::string parameter_name (" robot_description" );
281+ get_parameter (parameter_name, robot_description);
282+ if (robot_description.empty ()) {
283+ RCLCPP_FATAL (rclcpp::get_logger (" dynamic_graph_bridge" ),
284+ " Parameter robot_description is empty" );
285+ return false ;
286+ }
287+
288+ std::string model_name (" robot" );
289+
290+ // Search for the robot util related to robot_name.
291+ RobotUtilShrPtr aRobotUtil = getRobotUtil (model_name);
292+ // If does not exist then it is created.
293+ if (aRobotUtil == RefVoidRobotUtil ())
294+ aRobotUtil = createRobotUtil (model_name);
295+
296+ // If the creation is fine
297+ if (aRobotUtil != RefVoidRobotUtil ()) {
298+ // Then set the robot model.
299+ aRobotUtil->set_parameter (parameter_name, robot_description);
300+ RCLCPP_INFO (
301+ rclcpp::get_logger (" dynamic_graph_bridge" ),
302+ " parameter_server_read_robot_description : Set parameter_name : %s." ,
303+ parameter_name.c_str ());
304+ // Everything went fine.
305+ return true ;
306+ }
307+ RCLCPP_FATAL (rclcpp::get_logger (" dynamic_graph_bridge" ),
308+ " Wrong initialization of parameter_name %s" ,
309+ parameter_name.c_str ());
310+
311+ // Otherwise something went wrong.
312+ return false ;
313+ }
0 commit comments