0
$\begingroup$

Rosanswers logo

Hi, I am having trouble compiling my code using the message_filters Synchronizer in ROS2 Foxy. The tests included in the library compile and run fine but the code below does not want to compile on multiple systems with the following configurations:

OS: Ubuntu 20.04

Distro: ROS2 Foxy

message_filters: 3.2.5 (apt version)

My code:

#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
#include <message_filters/subscriber.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <message_filters/sync_policies/exact_time.h>
#include <message_filters/synchronizer.h>

using namespace std::placeholders;

class SyncNode : rclcpp::Node{

private:

    using approximate_policy = message_filters::sync_policies::ApproximateTime<std_msgs::msg::String, std_msgs::msg::String>;
    typedef message_filters::Synchronizer<approximate_policy> Synchronizer;

    message_filters::Subscriber<std_msgs::msg::String> sub1;
    message_filters::Subscriber<std_msgs::msg::String> sub2;
    std::unique_ptr<Synchronizer> sync;
    
    void callback(const std_msgs::msg::String::SharedPtr msg1, const std_msgs::msg::String::SharedPtr msg2);


public:

    SyncNode() : Node("test_message_filters_node"){

        sub1.subscribe(this, "/message_1");
        sub2.subscribe(this, "/message_2");

        sync.reset(new message_filters::Synchronizer<approximate_policy>(approximate_policy(10), sub1, sub2));
        sync->registerCallback(std::bind(&SyncNode::callback, this, _1, _2));

    }

};

void SyncNode::callback(const std_msgs::msg::String::SharedPtr msg1, const std_msgs::msg::String::SharedPtr msg2){
    RCLCPP_INFO(this->get_logger(), "Messages synced: Callback activated"); }

Compiling with GCC and G++ 9.4, I get the following error:

This code block was moved to the following github gist: https://gist.github.com/answers-se-migration-openrobotics/bc8479199b6272c53ecfcf236b219485

Trying to compile with Clang and Clang++ 10 yields the following errors:

In file included from /home/benjamin/Documents/Mechatronics/ros2/dev_ws/src/test_message_filters/src/test_sync.cpp:3:
In file included from /home/benjamin/Documents/Mechatronics/ros2/dev_ws/src/test_message_filters/include/test_message_filters/msg_node.hpp:4:
In file included from /opt/ros/foxy/include/message_filters/sync_policies/approximate_time.h:52:
/opt/ros/foxy/include/message_filters/signal9.h:272:12: error: no matching member function for call to 'addCallback'
    return addCallback<const M0ConstPtr&,
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/ros/foxy/include/message_filters/synchronizer.h:298:20: note: in instantiation of function template specialization 'message_filters::Signal9<std_msgs::msg::String_<std::allocator<void> >, std_msgs::msg::String_<std::allocator<void> >, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType>::addCallback<const std::_Bind<void (SyncNode::*(SyncNode *, std::_Placeholder<1>, std::_Placeholder<2>))(std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >, std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >)> >' requested here
    return signal_.addCallback(callback);
                   ^
/home/benjamin/Documents/Mechatronics/ros2/dev_ws/src/test_message_filters/include/test_message_filters/msg_node.hpp:32:15: note: in instantiation of function template specialization 'message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<std_msgs::msg::String_<std::allocator<void> >, std_msgs::msg::String_<std::allocator<void> >, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType> >::registerCallback<std::_Bind<void (SyncNode::*(SyncNode *, std::_Placeholder<1>, std::_Placeholder<2>))(std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >, std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >)> >' requested here
        sync->registerCallback(std::bind(&SyncNode::callback, this, _1, _2));
              ^
/opt/ros/foxy/include/message_filters/signal9.h:170:14: note: candidate function template not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<const _Bind<void (SyncNode::*(SyncNode *, _Placeholder<1>, _Placeholder<2>))(shared_ptr<String_<allocator<void> > >, shared_ptr<String_<allocator<void> > >)> &>::value, const _Bind<void (SyncNode::*(SyncNode *, _Placeholder<1>, _Placeholder<2>))(shared_ptr<String_<allocator<void> > >, shared_ptr<String_<allocator<void> > >)> &, const _Placeholder<1> &, const _Placeholder<2> &, const _Placeholder<3> &, const _Placeholder<4> &, const _Placeholder<5> &, const _Placeholder<6> &, const _Placeholder<7> &, const _Placeholder<8> &, const _Placeholder<9> &>::type' (aka '_Bind<std::_Bind<void (SyncNode::*(SyncNode *, std::_Placeholder<1>, std::_Placeholder<2>))(std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >, std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >)> (std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>, std::_Placeholder<4>, std::_Placeholder<5>, std::_Placeholder<6>, std::_Placeholder<7>, std::_Placeholder<8>, std::_Placeholder<9>)>') to 'const std::function<void (const shared_ptr<const String_<allocator<void> > > &, const shared_ptr<const String_<allocator<void> > > &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &, const shared_ptr<const NullType> &)>' for 1st argument
  Connection addCallback(const std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)>& callback)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:222:14: note: candidate function template not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<const _Bind<void (SyncNode::*(SyncNode *, _Placeholder<1>, _Placeholder<2>))(shared_ptr<String_<allocator<void> > >, shared_ptr<String_<allocator<void> > >)> &>::value, const _Bind<void (SyncNode::*(SyncNode *, _Placeholder<1>, _Placeholder<2>))(shared_ptr<String_<allocator<void> > >, shared_ptr<String_<allocator<void> > >)> &, const _Placeholder<1> &, const _Placeholder<2> &, const _Placeholder<3> &, const _Placeholder<4> &, const _Placeholder<5> &, const _Placeholder<6> &, const _Placeholder<7> &, const _Placeholder<8> &, const _Placeholder<9> &>::type' (aka '_Bind<std::_Bind<void (SyncNode::*(SyncNode *, std::_Placeholder<1>, std::_Placeholder<2>))(std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >, std::shared_ptr<std_msgs::msg::String_<std::allocator<void> > >)> (std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>, std::_Placeholder<4>, std::_Placeholder<5>, std::_Placeholder<6>, std::_Placeholder<7>, std::_Placeholder<8>, std::_Placeholder<9>)>') to 'void (*)(const std::shared_ptr<const std_msgs::msg::String_<std::allocator<void> > > &, const std::shared_ptr<const std_msgs::msg::String_<std::allocator<void> > > &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &, const std::shared_ptr<const message_filters::NullType> &)' for 1st argument
  Connection addCallback(void(*callback)(P0, P1, P2, P3, P4, P5, P6, P7, P8))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:180:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:186:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:192:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2, P3))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:198:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2, P3, P4))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:204:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2, P3, P4, P5))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:210:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2, P3, P4, P5, P6))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:216:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback(void(*callback)(P0, P1, P2, P3, P4, P5, P6, P7))
             ^
/opt/ros/foxy/include/message_filters/signal9.h:270:14: note: candidate template ignored: substitution failure: too many template arguments for function template 'addCallback'
  Connection addCallback( C& callback)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:264:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2, P3, P4, P5, P6, P7), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:258:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2, P3, P4, P5, P6), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:252:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2, P3, P4, P5), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:246:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2, P3, P4), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:240:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2, P3), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:234:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1, P2), T* t)
             ^
/opt/ros/foxy/include/message_filters/signal9.h:228:14: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  Connection addCallback(void(T::*callback)(P0, P1), T* t)

I have no idea what is wrong with the code, which is based on multiple ROS2 Foxy examples that work fine. Could someone please point out what I am missing?

Thank you!


Originally posted by BVM97 on ROS Answers with karma: 13 on 2022-10-09

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The arguments for your callback should be const std::shared_ptr<const...>&, so in your case const std_msgs::msg::String::ConstSharedPtr msg1. So ConstSharedPtr instead of SharedPtr.


Originally posted by Wilco Bonestroo with karma: 159 on 2022-10-10

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.