Subscriber and publisher in the same node not working with costum msg
Hello,
I know that this question might be asked before, but I did not find the final solution for it. Here is the question:
I want to write a node that subscribe for data from a topic then publish these data to another topic. So, mainly I want a publisher and subscriber in the same node. So What I did so far is the following:
#include "ros/ros.h"
#include <geometry_msgs/Twist.h>
#include "pctx_control/Control.h"
#include "std_msgs/String.h"
#include "pctx_control/pctx.h"
#include <sstream>
pctx_control::Control controlmsg ;
void function_callback (const geometry_msgs::Twist &msg)
{
controlmsg.ch = 0 ;
controlmsg.values[0] = msg.linear.x;
controlmsg.values[1] = msg.linear.y;
controlmsg.values[2] = msg.linear.z;
std::cout << "d1"<<controlmsg.values[0] << std::endl ;
std::cout << "d2" <<controlmsg.values[1]<< std::endl ;
std::cout << "d3" <<controlmsg.values[2]<< std::endl ;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "my_node");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<pctx_control::Control>("topic2", 100);
ros::Subscriber sub = n.subscribe("topic1", 1000, function_callback);
ros::Rate loop_rate(10);
ros::spinOnce();
int count = 0;
while (ros::ok())
{
pub.publish(controlmsg);
ros::spin();
loop_rate.sleep();
}
return 0;
}
I want to save data that I want to publish in a "custom msg" pctx_control::Control type;
This msg has the following format:
int32 ch
int32 value
int16[] values