ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Linking Classes in 2 user defined packages

asked 2019-02-11 12:18:54 -0600

houssemDZ-25 gravatar image

updated 2019-02-11 18:53:14 -0600

Hello.

I'm beggeing with ROS I'm studing CMakeLists.txt using diffrent approches to execute same code. I have a problem with the linking phase of compilation of two user defined packages :

pkg1 : sphero_imu_reader I've defined a Class ImuTopicReader

pkg 2 : sphero_crash_server I've defined a Class CrashServer

I want to use the Class ImuTopicReader inside CrashServer class

the files:

  • sphero_imu_reader :

    ->src/topic_reader.h
    ->src/topic_reader.cpp
    
    • sphero_crash_server :

       ->/include/sphero_crash_server/topic_reader.h
       ->src/topic_reader.h
       ->src/topic_reader.cpp
      

I've got these problems :

fatal error: topic_reader.h: no such file or directory

CMakeLists.txt for sphero_topic_reader :


    cmake_minimum_required(VERSION 2.8.3)
    project(sphero_imu_reader)

    ## Compile as C++11, supported in ROS Kinetic and newer
    add_compile_options(-std=c++11)

    ## Find catkin macros and libraries
    ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
    ## is used, also find other catkin packages
    find_package(catkin REQUIRED COMPONENTS
      sensor_msgs
      roscpp
    )
    catkin_package(
    # INCLUDE_DIRS include
    #  LIBRARIES imu_topic_reader
      CATKIN_DEPENDS sensor_msgs roscpp
    #  DEPENDS system_lib
    )
    ## Specify additional locations of header files
    ## Your package locations should be listed before other locations
    include_directories(
    # include
      ${catkin_INCLUDE_DIRS}
    )

    add_executable(${PROJECT_NAME}_node src/topic_reader.cpp)
    add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS})   
    target_link_libraries(${PROJECT_NAME}_node
       ${catk
    ## Mark cpp header files for installation
     install(DIRECTORY include/${PROJECT_NAME}/
       DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
       FILES_MATCHING PATTERN "*.h"
       PATTERN ".svn" EXCLUDE
     )

package.xml for Sphero_imu_reader

      <buildtool_depend>catkin</buildtool_depend>
      <build_depend>sensor_msgs</build_depend>
      <build_depend>roscpp</build_depend>
      <build_export_depend>sensor_msgs</build_export_depend>
      <build_export_depend>roscpp</build_export_depend>
      <exec_depend>sensor_msgs</exec_depend>
      <exec_depend>roscpp</exec_depend>

topic_reader.h for sphero_imu_reader pkg

    #ifndef SPHERO_IMU_READER
    #define SPHERO_IMU_READER
    #include "ros/ros.h"
    #include "sensor_msgs/Imu.h"
    #endif
    class ImuTopicReader
    {
        private:
            std::string name_;
            ros::NodeHandle n_;
            ros::Subscriber subs_;
            sensor_msgs::Imu imu_;
        public:

            ImuTopicReader(std::string name)
            {
                name_ = name;
                subs_ = n_.subscribe(name_,1,&ImuTopicReader::imuCallback,this);
            }   
            void imuCallback(const sensor_msgs::Imu::ConstPtr& msg);    
            sensor_msgs::Imu getImu();
    };

CMakeLists.txt for sphero_crash_server :

    cmake_minimum_required(VERSION 2.8.3)
    project(sphero_crash_server)
    add_compile_options(-std=c++11)
    find_package(catkin REQUIRED COMPONENTS
      roscpp
      sensor_msgs
      sphero_imu_reader
    )
    catkin_package(
     CATKIN_DEPENDS roscpp sphero_imu_reader sensor_msgs
    )

    include_directories(
    # include
      ${catkin_INCLUDE_DIRS}  
    )
    add_executable(${PROJECT_NAME}_node src/server.cpp)
    add_dependencies(${PROJECT_NAME}_node } ${catkin_EXPORTED_TARGETS} ${catkin_LIBRARIES})
    target_link_libraries(${PROJECT_NAME}_node
       ${catkin_LIBRARIES}
    )
    install(DIRECTORY include/${PROJECT_NAME}/
      DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
      FILES_MATCHING PATTERN "*.h"
      PATTERN ".svn" EXCLUDE
    )

package.xml for sphero_crash_server :

      <buildtool_depend>catkin</buildtool_depend>
      <build_depend>roscpp</build_depend>
      <build_depend>sphero_imu_reader</build_depend>
      <build_depend>sensor_msgs</build_depend>
      <build_export_depend>roscpp</build_export_depend>
      <build_export_depend>sphero_imu_reader</build_export_depend>
      <build_export_depend>sensor_msgs</build_export_depend>
      <exec_depend>roscpp</exec_depend>
      <exec_depend>sphero_imu_reader</exec_depend>
      <exec_depend>sensor_msgs</exec_depend>

server.h in pgk sphero_crash_server :

    #include "ros/ros.h"
    #include "sphero_crash_server/topic_reader.h"
    #include "std_srvs/Trigger.h"
    #include "sensor_msgs/Imu.h"
    #include <algorithm>
    #include <vector>

    class CrashServer
    {
        private:

            ros::NodeHandle n_;
            ImuTopicReader imu_topic_;
            float threshold_;
            ros::ServiceServer service_;
            std::string name_;
        public:
            CrashServer (std::string name,float threshold)
            {

                name_ = name;
                imu_topic_ = ImuTopicReader(name_);
                threshold_ = threshold;
                service_ = n_.advertiseService("detect_crash_action_serv",&CrashServer::detectionCallback,this);
                std::cout << "done" <<std::endl;
            }   
            std::string sectorDetection();
            bool detectionCallback(std_srvs::Trigger::Request  &req , std_srvs::Trigger::Response &res);
    };
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-02-12 03:37:06 -0600

Delb gravatar image

updated 2019-02-12 03:39:05 -0600

You should change a litlle bit your layout to have things working (and respect ROS standards), organize your files like that first :

catkin_ws/
  src/
    sphero_imu_reader/
      src/
        topic_reader.cpp
      include/
        sphero_imu_reader/
          topic_reader.h
    sphero_crash_server/
      src/
        server.cpp
      include/
        sphero_crash_server/
          server.h

Now for your both CMakeLists.txt, just add this inside catkin_package() function :

INCLUDE_DIRS include

Now in server.h you can add this line and use the class ImuTopicReader inside the class CrashServer :

#include "sphero_imu_reader/topic_reader.h"

Note : You might have cpp issues when calling the constructor but your first error should be gone.

edit flag offensive delete link more

Comments

I've edited the layout its didn't even compile for the ImuTopicReader pakacge. I checked many projects on github they haven't used that convention of putting .h only in the include folder

houssemDZ-25 gravatar image houssemDZ-25  ( 2019-02-13 05:33:55 -0600 )edit

its didn't even compile for the ImuTopicReader pakacge What is the error exactly ?

Also have you made the changes in CmakeLists.txt to tell that the headers are in the include file as I told you at the end of my answer ?

Delb gravatar image Delb  ( 2019-02-13 07:29:31 -0600 )edit

I checked many projects on github they haven't used that convention

Were they official ros packages ? You can see this layout in packages like move_base, there is even a ros_best_practices package using this layout. Can you tell me one of those package ?

Delb gravatar image Delb  ( 2019-02-13 07:35:11 -0600 )edit
  • I've changed the CmaleLists.txt and I try to compile ImuTopicReader and I've got this error : fatal error: sphero_imu_reader/topic_reader.h: no such file or dir
houssemDZ-25 gravatar image houssemDZ-25  ( 2019-02-13 15:42:38 -0600 )edit

Can you edit your question to show us the new CMakeLists.txt files and also the source files please ? And aalso the description of your layout too, for both packages.

Delb gravatar image Delb  ( 2019-02-13 15:53:23 -0600 )edit

Question Tools

Stats

Asked: 2019-02-11 12:18:54 -0600

Seen: 236 times

Last updated: Feb 12 '19