Creating generic topic subscribers with ShapeShifter
Hi all, I'm having trouble creating a generic topic subcriber that simply records the last time a message was published. Here are the relevant parts of my current code:
void cb_record_message_time(const ros::MessageEvent<topic_tools::ShapeShifter>& msg, uint64_t& message_time){
ros::Time time = ros::Time::now();
message_time = 1000 * (uint64_t)time.sec + (uint64_t)time.nsec / 1e6;
}
int main(int argc, char **argv) {
ros::init(argc, argv, "topic_health_check");
ros::NodeHandle nh;
std::vector<std::string> topic_names = {
"/topic/1",
"/topic/2",
"/topic/3"
};
uint64_t topic_message_times[256]; // if this is a std::vector then std::ref(topic_message_times[i]) doesn't work
std::vector<ros::Subscriber> topic_subs;
for(int i = 0; i < topic_names.size(); i++) {
topic_message_times[i] = 0;
ros::Subscriber sub = nh.subscribe<topic_tools::ShapeShifter>(topic_names[i], 1, boost::bind(cb_record_message_time, _1, std::ref(topic_message_times[i])));
topic_subs.push_back(sub);
}
}
The ShapeShifter docs are sparse, the given example at http://wiki.ros.org/ros_type_introspection/Tutorials/GenericTopicSubscriber doesn't compile, and most other examples on the web use nh.subscribe without the template type <> which also doesn't compile (I'm on Kinetic, not sure if that makes a difference). Any thoughts? Thanks!
Edit:: This is the error I get:
CMakeFiles/health_check.dir/src/health_check.cpp.o: In function ros::serialization::PreDeserialize<topic_tools::ShapeShifter>::notify(ros::serialization::PreDeserializeParams<topic_tools::ShapeShifter> const&)': health_check.cpp:(.text._ZN3ros13serialization14PreDeserializeIN11topic_tools12ShapeShifterEE6notifyERKNS0_20PreDeserializeParamsIS3_EE[_ZN3ros13serialization14PreDeserializeIN11topic_tools12ShapeShifterEE6notifyERKNS0_20PreDeserializeParamsIS3_EE]+0x24a): undefined reference to topic_tools::ShapeShifter::morph(std::__cxx11::basic_string<char, std::char_traits<char="">, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char="">, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char="">, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char="">, std::allocator<char> > const&)' CMakeFiles/health_check.dir/src/health_check.cpp.o: In function boost::detail::sp_if_not_array<topic_tools::ShapeShifter>::type boost::make_shared<topic_tools::ShapeShifter>()': health_check.cpp:(.text._ZN5boost11make_sharedIN11topic_tools12ShapeShifterEJEEENS_6detail15sp_if_not_arrayIT_E4typeEDpOT0_[_ZN5boost11make_sharedIN11topic_tools12ShapeShifterEJEEENS_6detail15sp_if_not_arrayIT_E4typeEDpOT0_]+0x77): undefined reference to topic_tools::ShapeShifter::ShapeShifter()' CMakeFiles/health_check.dir/src/health_check.cpp.o: In function ros::SubscriptionCallbackHelperT<boost::shared_ptr<topic_tools::ShapeShifter const> const&, void>::getTypeInfo()': health_check.cpp:(.text._ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv[_ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv]+0x9): undefined reference totypeinfo for topic_tools::ShapeShifter' collect2: error: ld returned 1 exit status
Asked by dheeranet on 2018-05-30 21:43:10 UTC
Answers
That is a linker error, not a compilation problem. You're not linking against the libraries that are required.
Edit: since this is now an answer: make sure to link against topic_tools
(ie: add it to find_package(catkin REQUIRED COMPONENTS ..)
. That's what provides ShapeShifter
.
Asked by gvdhoorn on 2018-05-31 01:41:52 UTC
Comments
Demangled symbol:
ros::SubscriptionCallbackHelperT<boost::shared_ptr<topic_tools::ShapeShifter const> const&, void>::getTypeInfo()[ros::SubscriptionCallbackHelperT<boost::shared_ptr<topic_tools::ShapeShifter const> const&, void>::getTypeInfo()] 0x9)
Asked by gvdhoorn on 2018-05-31 01:42:35 UTC
@gvdhoorn Thanks! I got it to work.
Asked by dheeranet on 2018-05-31 04:31:26 UTC
@dheeranet How did you solve the problem? I'm stuck with a similar problem
Asked by teshansj on 2018-08-31 01:55:21 UTC
@gvdhoorn Can you please explain in detail that where to change because I am also getting same error.
void topicCallback(const ShapeShifter::ConstPtr &msg, const std::string &topic_name)
{
uint32_t length = msg->size();
sizes+=length;
auto time = std::chrono::system_clock::now();
double diff = std::chrono::duration<double>(time-last).count();
last=time;
times+=diff;
}
boost::function<void(const ShapeShifter::ConstPtr &)> callback;
callback = [topic_name](const ShapeShifter::ConstPtr &msg)
{
topicCallback(msg, topic_name);
};
ros::Subscriber subscriber = nh.subscribe(topic_name, 10, callback);
Asked by Aakin on 2021-09-29 16:46:09 UTC
The problem is most likely in your CMakeLists.txt
, which we don't see.
In any case, just to clarify: my answer here wasn't actually an answer, it was a comment. It appears to have been converted to an answer. That's why there is nothing more than a quick hint.
Asked by gvdhoorn on 2021-09-30 02:04:21 UTC
CMakeLists.txt:-
cmake_minimum_required(VERSION 2.8.3)
project(roshz)
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package(
INCLUDE_DIRS
CATKIN_DEPENDS roscpp
)
include_directories(include ${catkin_INCLUDE_DIRS})
link_directories(${catkin_LIBRARY_DIRS})
add_definitions(${catkin_DEFINITIONS})
add_compile_options(-std=c++11)
add_executable(${PROJECT_NAME}
src/roshz.cpp
src/statistics.cpp
src/tp.cpp
)
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
@gvdhoorn
Asked by Aakin on 2021-09-30 09:45:57 UTC
You already have #q387669. Why keep posting here as well?
Asked by gvdhoorn on 2021-09-30 13:12:34 UTC
Might make sense to post in both places. Google seems to rank this post higher so more people land here I think
Asked by dheeranet on 2021-09-30 13:49:27 UTC
Seeing as you've now commented @dheeranet: could you mark the question as answered by clicking the checkmark if you feel it has been answered?
Asked by gvdhoorn on 2021-09-30 14:11:47 UTC
Comments
What part of your code isn't working? what error are you getting?
Asked by ahendrix on 2018-05-30 22:48:31 UTC
._ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv[_ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv]+0x9): undefined reference to `typeinfo for topic_tools::ShapeShifter'
Asked by dheeranet on 2018-05-31 00:13:51 UTC