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

subscribing to custom message type

asked 2015-07-10 14:16:40 -0500

saikrishnagv gravatar image

updated 2015-07-14 04:00:06 -0500

gvdhoorn gravatar image

I have created a custom message type. I am able to successfully publish it with the topic name "topic".

int8 seq_id
std_msgs/Header timestamp
int8 frame_id
float64 myheight
float64 mywidth
int16 step_value
sensor_msgs/Image my_left
sensor_msgs/Image my_right

is my custom message.

I created a new package. I want to subscribe to the node publishing "topic".

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener");

    ros::NodeHandle n;

        std::cout << "hello disparity" << std::endl;

        ros::Subscriber sub = n.subscribe("mycustomtopic",1000,chatterCallback);

        std::cout << "after subscription disparity" << std::endl;

        ros::spin();

        std::cout << "after spin disparity" << std::endl;

        return 0;
}

is my main function.

void chatterCallback(const swahana_v2::rectified_imageConstPtr &msg){
    std::cout << "checker " << std::endl;
    return;
}

is my callback function written in the same node.

and the output is

hello disparity

after subscription disparity

^Cafter spin disparity

following is my CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)

project(compute_disparity)

set(CMAKE_BUILD_TYPE Release)

find_package(catkin REQUIRED COMPONENTS
  rectifier

  roscpp

  rospy

  std_msgs

  swahana_v2

  sensor_msgs
)

catkin_package()

include_directories( ${catkin_INCLUDE_DIRS})

include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(hsvgmmN src/hsvgmmN.cpp)

target_link_libraries(hsvgmmN ${catkin_LIBRARIES})

add_dependencies(hsvgmmN compute_disparity_generate_messages_cpp)

target_link_libraries(hsvgmmN ${catkin_LIBRARIES})

target_link_libraries (hsvgmmN elas viso ${PCL_LIBRARIES} ${OpenCV_LIBS} )

find_package(message_generation)

catkin_package(CATKIN_DEPENDS message_runtime)

add_message_files(

  DIRECTORY ../swahana_v2/msg

  FILES rectified_image.msg
)

find_package(catkin REQUIRED COMPONENTS swahana_v2)

add_dependencies(hsvgmmN swahana_v2_generate_messages_cpp)

but, "checker" is not getting printed. This means, there is still some problem with the callback function. please help!

edit retag flag offensive close merge delete

Comments

Have you had a look on rqt_graph like I wrote in the answer? Are the nodes connected by topic?

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-13 12:52:10 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2015-07-11 12:12:50 -0500

updated 2015-07-14 03:26:04 -0500

You need to add the old package as dependency to your new package.

Inside the CMakeLists.txt and as run and build dependency in package.xml

And in the Callback you need your const <package>::<message>::ConstPtr &msg

CMakeLists.txt:

find_package(catkin REQUIRED COMPONENTS
   ... 
   your_old_package
)

package.xml:

   <run_depend>your_old_package</run_depend>
   <build_depend>your_old_package</build_depend>

I guess you are also missing to link your node inside CMakeLists.txt with roscpp by:

add_executable(<executable_name> <dependencies>)
target_link_libraries(<executable_name> ${catkin_LIBRARIES})

Have you checked rqt_graph if the topics are connected?

(deselect hide checkboxes to see unconnected topics rectangular boxes)

If not you might need to create a launchfile with remap: http://wiki.ros.org/roslaunch/XML/remap

Have you checked that the publisher is really publishing?

rostopic echo /mycustomtopic

alternatively you could try to publish with rostopic

rostopic pub /mycustomtopic package/MessageType <press tab twice (auto completion)>

edit flag offensive delete link more

Comments

void chatterCallback(const rectified_imageConstPtr &msg) is my callback function.. and I got the following error message:

/home/krish/architecture_ws/src/compute_disparity/src/hsvgmmN.cpp:116:28: error: ‘rectified_imageConstPtr’ does not name a type

I have the rectified_image.msg as my custom

saikrishnagv gravatar image saikrishnagv  ( 2015-07-12 04:42:17 -0500 )edit

Sorry messed something up, just checked back one of my own callbacks.

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-12 10:26:44 -0500 )edit

that also did not work! :(

saikrishnagv gravatar image saikrishnagv  ( 2015-07-12 12:27:21 -0500 )edit

Please post the function signature that doesn't work.

BennyRe gravatar image BennyRe  ( 2015-07-13 02:11:49 -0500 )edit

void chatterCallback(const swahana_v2::rectified_image::ConstPtr &msg){ std::cout << "checker" << std::endl; }

saikrishnagv gravatar image saikrishnagv  ( 2015-07-13 02:45:41 -0500 )edit
1

Is it a typo? chattercallback -> chatterCallback (in your line above you write it with lower case letters and here you do camelcase)

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-13 03:39:16 -0500 )edit

Please also always provide the compiler message.

BennyRe gravatar image BennyRe  ( 2015-07-13 03:45:15 -0500 )edit

Linking CXX executable /home/krish/architecture_ws/devel/lib/compute_disparity/hsvgmmN CMakeFiles/hsvgmmN.dir/src/hsvgmmN.cpp.o: In function `ros::Subscriber ros::NodeHandle::subscribe<swahana_v2::rectified_image_<std::allocator<void> > >(std::basic_string<char, std::char_traits<char="">, std::allocato

saikrishnagv gravatar image saikrishnagv  ( 2015-07-13 04:07:02 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-10 14:16:40 -0500

Seen: 6,861 times

Last updated: Jul 14 '15