Hello,
While trying to publish to the custom message Age
, I have run into a problem.
First of all, I have already successfully compiled a separate package to hold the custom message Age
, as can be attributed by the below console output:
user:~$ rosmsg list | grep Age
topic_ex/Age
When I try to publish to a custom topic using this custom message (see code below), I see that the IDE is able to locate the Age.h
header file (that had been generated earlier when I had compiled the package containing the custom message).
#include <iostream>
#include <list>
#include <map>
#include <ros/ros.h>
#include <string>
// Include the custom message types
// https://people.eng.unimelb.edu.au/pbeuchat/asclinic/software/ros_define_and_use_custom_message_types.html
#include "topic_ex/Age.h"
using namespace std;
int main(int argc, char **argv) {
// Initialize a ros-node
ros::init(argc, argv, "rosbot_age_node");
// Initialize a publisher to publish the time
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<topic_ex::Age>("/rob_age", 1000);
// At what rate should data be steamed into the new topic
ros::Rate loop_rate(2);
// Initialize a age-type variable to hold the data in
topic_ex::Age rob_age;
// Initialize the sub-variables with 0
rob_age.days = 0;
rob_age.months = 0;
rob_age.years = 0;
// A temp variable to hold the time from rosbot
unsigned long int temp_time = 0;
while (ros::ok()) {
// Convert the time appropriately for their respective fields for Age
// message
rob_age.days = temp_time / 60; // For minutes
rob_age.months = rob_age.days / 60; // For hours
rob_age.years = rob_age.months / 24; // For days
// Publish the message
pub.publish(rob_age);
ros::spinOnce();
loop_rate.sleep();
++temp_time;
}
return 0;
}
However, when I tried to compile the publisher package, I got the following error on console:
[ 93%] Built target topic_ex_generate_messages
/home/user/catkin_ws/src/age_publisher_pkg/src/publish_age.cpp:9:10: fatal error: topic_ex/Age.h: No such file or directory
9 | #include "topic_ex/Age.h"
| ^~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [age_publisher_pkg/CMakeFiles/publish_age.dir/build.make:63: age_publisher_pkg/CMakeFiles/publish_age.dir/src/publish_age.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:528: age_publisher_pkg/CMakeFiles/publish_age.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j8 -l8" failed
, which is strange because the IDE affirmed that the header file exists at the mentioned location.
I realized later that maybe the CMake file needs to know that it should include the custom-message package topic_ex
. So I made the following CMakeList
:
cmake_minimum_required(VERSION 3.0.2)
project(age_publisher_pkg)
# In package.xml, you have to state them as build_depend.
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
topic_ex # custom-message package
)
catkin_package(
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(publish_age src/publish_age.cpp)
add_dependencies(publish_age ${publish_age_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(publish_age topic_ex_generate_messages_cpp)
target_link_libraries(publish_age
${catkin_LIBRARIES}
)
With this, the publisher compiled.
This unit should be modified to include the following pieces of information:
- What format should the custom-message header file should be:
#include "topic_ex/Age.h"
→ `#include “/.h”, and - What changes must be made in the
CMake
file of the custom subscriber: add the<custom message package name>
underfind_package()
and asbuild_depend
in thepackage.xml
file.
This is just for your information.