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_introspe... 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
What part of your code isn't working? what error are you getting?
._ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv[_ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11topic_tools12ShapeShifterEEEvE11getTypeInfoEv]+0x9): undefined reference to `typeinfo for topic_tools::ShapeShifter'