Home>

###
I want to exchange messages between publishers and subscribers using ROS.

I am studying ROS based on a reference book called ROS Robot Programming Bible.
And I am learning the basics of ROS while running the program.

I am doing the exercise of implementing and communicating with the publisher and subscriber nodes.
Therefore. There is a situation where the build does not pass.
The sample program of the reference book is used as it is except the information such as the author and URL of the package.xml.

Workspace catkin_ws
Directly under catkin_ws src, dev, build
Directly below src ros_tutorials_topic, CMakeLists.txt, my_first_ros_pkg (probably not related, but listed for the time being)
Directly under ros_tutorials_topic include, src, msg, CMakeLists.txt, package.xml
Directly under src topic_publisher.cpp, topic_publisher.cpp

*
The msg directly under ros_tutorials_topic contains the following message MsgTutorial.msg.
time stamp
int32 data

After passing the build, the following screen will be displayed (partially posted).
The ros :: part of the cpp file is an undefined reference.

Perhaps I made a mistake in CMakeLists.txt.
I will post all the codes.

Error message

[72%] Linking CXX executable/home/taku/catkin_ws/devel/lib/ros_tutorials_topic/topic_publisher
CMakeFiles/topic_publisher.dir/src/topic_publisher.cpp.o: In function `main ':
topic_publisher.cpp :(. text + 0x70): `ros :: init (int&, char **, std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator<char>>const&, unsigned int) 'is an undefined reference
topic_publisher.cpp :(. text + 0xcc): `ros :: NodeHandle :: NodeHandle (std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator<char>>const&, std: : map<std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator<char>>, std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std: : allocator<char>>, std :: less<std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator<char>>>, std :: allocator<std :: pair<std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator<char>>const, std :: __ cxx11 :: basic_string<char, std :: char_traits<char>, std :: allocator undefined reference to '<char>>>>>>const&)'
topic_publisher.cpp :(. text + 0x182): Undefined reference to `ros :: Rate :: Rate (double) '
topic_publisher.cpp :(. text + 0x1a0): undefined reference to `ros :: ok () '

New error message

[rosrun] Couldn't find executable named topic_publisher below/home/taku/catkin_ws/src/ros_tutorials_topic
source code:(4 files: txt, pkg, cpp × 2)

** CMakeLists.txt **
cmake_minimum_required (VERSION 2.8.3)
project (ros_tutorials_topic)

find_package (catkin REQUIRED COMPONENTS
message_generation
std_msgs
roscpp
)

add_message_files (FILES MsgTutorial.msg)

generate_messages (DEPENDENCIES std_msgs)

catkin_package (
INCLUDE_DIRS include
LIBRARIES ros_tutorials_topic
CATKIN_DEPENDS std_msgs roscpp
DEPENDS system_lib
)

include_directories (${catkin_INCLUDE_DIRS})

add_executable (topic_publisher src/topic_publisher.cpp)
add_dependencies (topic_publisher ${${PROJECT_NAME} _EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries (topic_publisher ${catkin_LIBRALIES})

add_executable (topic_subscriber src/topic_subscriber.cpp)
add_dependencies (topic_subscriber ${${PROJECT_NAME} _EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries (topic_subscriber ${catkin_LIBRALIES})

**** package.xml ****<? xml version = "1.0"?><package>  <name>ros_tutorials_topic</name>  <version>0.1.0</version>  <description>The ros_tutorials_topic package</description>  <maintainer email = "[email protected]">taku</maintainer>  <license>apache license 2.0</license>Jane Doe</author><url type = "website">http://wiki.ros.org/ros_tutorials_topic</url>  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>  <build_depend>std_msgs</build_depend>  <build_depend>message_generation</build_depend>  <run_depend>message_runtime</run_depend>  <run_depend>roscpp</run_depend>  <run_depend>std_msgs</run_depend>  <export></export></package>
Publisher node creation cpp
**** topic_publisher.cpp ****
#include<ros/ros.h>// ROS header file
#include<ros_tutorials_topic/MsgTutorial.h>int main (int argc, char ** argv) {
  ros :: init (argc, argv, "topic_publisher");// node initialization (set node name to "hello")
  ros :: NodeHandle nh;// Create node handle

// ros_tutorial_pub is the publisher
// Topic name is ros_tutorial_msg
// set the publisher queue size to 100
// A queue is a type of data structure. In a data string, take new data after the string, and before fetching it
ros :: Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic :: MsgTutorial>("ros_tutorial_msg", 100);ros :: Rate loop_rate (10);// Period setting (10Hz = 0.1 seconds)
// message file
// Declaration of message msg described in MsgTutorial.msg format
// time and int32 type variables were stored
ros_tutorials_topic :: MsgTutorial msg;
int count = 0;

  while (ros :: ok ()) {
msg.stamp = ros :: Time :: now ();// current time
msg.data = count;// count variable to data
// Display the stamp.sec message
ROS_INFO ("send msg =% d", msg.stamp.sec);
// Display the stamp.nsec message
ROS_INFO ("send msg =% d", msg.stamp.nsec);
ROS_INFO ("count =% d", msg.data);

// Publish the message msg in the created ros_tutorial_pub publisher
ros_tutorial_pub.publish (msg);
loop_rate.sleep ();// Wait for the specified cycle (wait for the time less other processing time in the loop)
++ count;
  }
return 0;
}

Subscriber node creation cpp
**** topic_subscriber.cpp ****
#include<ros/ros.h>// ROS header file
#include<ros_tutorials_topic/MsgTutorial.h>// message file
// Declaration of message msg described in MsgTutorial.msg format
// time and int32 type variables were stored
// ros_tutorials_topic :: MsgTutorial msg;
// message callback function
// Function that runs when a message is received in the ros_tutorial_msg topic
// Argument is MsgTutorial message of ros_tutorials_topic topic
void msgCallback (const ros_tutorials_topic :: ConstPtr&msg) {
// Display the stamp.sec message
ROS_INFO ("receive msg =% d", msg.stamp.sec);
// Display the stamp.nsec message
ROS_INFO ("receive msg =% d", msg.stamp.nsec);
ROS_INFO ("receive msg =% d", msg.data);
}
int main (int argc, char ** argv) {
  ros :: init (argc, argv, "topic_subscriber");// node initialization (set node name to "hello")ros :: NodeHandle nh;// Create node handle

// ros_tutorial_pub is the publisher
// Topic name is ros_tutorial_msg
// set the publisher queue size to 100
// A queue is a type of data structure. In a data string, take new data after the string, and before fetching it
ros :: Subscriber ros_tutorial_sub = nh.subscribe ("ros_tutorial_msg", 100, msgCallback)
// Call the callback function when a message is received
ros :: spin ();
return 0;
}
What I tried

https://qiita.com/srs/items/de7bf71b96dc26b1f53b

Is there a problem with CMakeLists.txt based on the link above? I thought.
However, I can't think of an approach from that, so I would like to ask for your advice.

Supplemental information (FW/tool version, etc.)

In windows10, using VMware Workstation 15 Player, it is running with ubuntu of "16.04.6 LTS (Xenial Xerus)".
ROS is "Kinetic Kame".

Both are as per the reference book ROS Robot Programming Bible (except using virtual machines).


http://forestofazumino.web.fc2.com/ros/ros_simple_program.html
I was able to run the above sample.

I ran turtlesim according to the book

Thanks to you, I was able to build it, but I could not execute it with the following command.
rosrun ros_tutorials_topic topic_publisher

[rosrun] Couldn't find executable named topic_publisher below/home/taku/catkin_ws/src/ros_tutorials_topic

An executable file was created in the following directory.
/ home/taku/catkin_ws/devel/lib/ros_tutorials_topic

Question 1
-"Source ~/catkin_ws/devel/setup.bash" has already been added to .bashrc.

-When you execute "echo $ROS_PACKAGE_PATH" in the terminal, the following is returned.
/ home/taku/sample_ws/src:/home/taku/catkin_ws/src:/opt/ros/kinetic/share

-When TAB completion was performed when typing rosrun ros_tuto, the following was completed.
rosrun ros_tutorials_topic

-When roscd ros_tutorials_topic is executed, the directory changes as follows.
~/catkin_ws/src/ros_tutorials_topic

Question 2
CMakeLists.txt has not changed except the spelling of LIBRALIES.

Question 3
I was able to execute the reference link "rosrun sample hello". "Hello World!" Was displayed at regular intervals.

  • Answer # 1

    It's a misspelling.
    LIBRARIES, not LIBRALIES.

    target_link_libraries (topic_publisher ${catkin_LIBRALIES})