Storing 2 messages received from 2 publishers in 1 vector.
Hello, I would like to compile a ROS node that takes some Pose2D(x,y,theta) information sent by some publisher node and some Float64 value sent by an other publisher node and store it in some vector. I have the code as below.
#include "ros/ros.h"
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Pose2D.h>
#include <std_msgs/Float64.h>
using namespace message_filters;
using namespace std_msgs;
using namespace std;
class EKF_node
{
public:
// constructor
EKF_node()
: y(4)
{
ros::NodeHandle n;
sub_camera = n.subscribe("chatter", 1, &EKF_node::chatterCallback, this);
sub_imu = n.subscribe("chatter2", 1, &EKF_node::chatterCallback2, this);
}
// callback1
void chatterCallback(const geometry_msgs::Pose2D::ConstPtr& msg)
{
ROS_INFO(msg->x);
y[0] = msg->x;
y[1] = msg->y;
y[2] = msg->theta;
}
// callback2
void chatterCallback2(const std_msgs::Float64::ConstPtr& msg)
{
ROS_INFO("I heard from the second chatter: ", msg->data);
y[4] = msg->data;
}
//private:
// vector containing the outputs
vector<std_msgs::Float64> y;
ros::Subscriber sub_camera;
ros::Subscriber sub_imu;
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "listenerMultipleTopics");
EKF_node myNode;
ros::Rate r(10);
while(ros::ok)
{
ros::spinOnce();
cout << myNode.y[0] << endl << myNode.y[1] << endl << myNode.y[2] << endl << myNode.y[3] << endl;
r.sleep();
}
return 0;
}
Unfortunately, I get some error message as below (ROS doesn't seem to like something about the equalities). Do you know what could possibly go wrong?
/home/alic/catkin_ws/src/beginner_tutorials/src/listenerMult.cpp: In member function ‘void EKF_node::chatterCallback(const ConstPtr&)’:
/home/alic/catkin_ws/src/beginner_tutorials/src/listenerMult.cpp:32:16: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std_msgs::Float64_<std::allocator<void> > > >::value_type {aka std_msgs::Float64_<std::allocator<void> >}’ and ‘const _x_type {aka const double}’)
y[0] = msg->x;
^
In file included from /home/alic/catkin_ws/src/beginner_tutorials/src/listenerMult.cpp:8:0:
/opt/ros/lunar/include/std_msgs/Float64.h:22:8: note: candidate: constexpr std_msgs::Float64_<std::allocator<void> >& std_msgs::Float64_<std::allocator<void> >::operator=(const std_msgs::Float64_<std::allocator<void> >&)
struct Float64_
^~~~~~~~
/opt/ros/lunar/include/std_msgs/Float64.h:22:8: note: no known conversion for argument 1 from ‘const _x_type {aka const double}’ to ‘const std_msgs::Float64_<std::allocator<void> >&’
/opt/ros/lunar/include/std_msgs/Float64.h:22:8: note: candidate: constexpr std_msgs::Float64_<std::allocator<void> >& std_msgs::Float64_<std::allocator<void> >::operator=(std_msgs::Float64_<std::allocator<void> >&&)
/opt/ros/lunar/include/std_msgs/Float64.h:22:8: note: no known conversion for argument 1 from ‘const _x_type {aka const double}’ to ‘std_msgs::Float64_<std::allocator<void> >&&’
/home/alic/catkin_ws/src/beginner_tutorials/src/listenerMult.cpp:33:16: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std_msgs::Float64_<std::allocator<void> > > >::value_type {aka std_msgs::Float64_<std::allocator<void> >}’ and ‘const _y_type {aka const double}’)
y[1] = msg->y;
^
In file included from /home/alic/catkin_ws/src/beginner_tutorials/src/listenerMult.cpp:8:0:
/opt/ros/lunar/include/std_msgs/Float64.h:22:8: note: candidate: constexpr std_msgs::Float64_<std::allocator ...
Can you please repost your code with smaller indentations so that one line of code isn't spanning multiple lines of the question? Also, please use the preformatted text
101010
button for your terminal output.@edamondo: can you explain how this is related to #q282259? It seems like almost a duplicate.
Furthermore:
nitpick maybe, but this is not about ROS, but C++. The C++ compiler is telling you that your C++ source contains errors and it cannot continue.
Hi gvdhoorn, It i just that when I posted an update on the initial question, Thomas D suggested to put it in a new question, so here I put this in a new question. Makes sense?