publisher and subscriber in the same node
Hello, i have a subscriber that reads the values of a flex sensor that rosserial_arduino is reading, and have another file with a publisher that writes values in a topic of the shadow hand i need to send the flex sensor values to shadow hand, the question is, can i create only one .cpp file with the subscriber and the publisher there?
already i have this two files:
Subscriber:
#include <ros/ros.h>
#include <std_msgs/Float32.h>
void indiceCallback(const std_msgs::Float32::ConstPtr& msg)
{
ROS_INFO("Valor indice: [%f]", msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "indice");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
ros::spin();
return 0;
}
Publisher file:
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "prueba");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
std_msgs::Float64 msg;
msg.data = 1.5;
ROS_INFO("%f", msg.data);
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
As you can see the publisher file publish the data value, but there this value i change manually, i need to put there the value that i have reading in the subscriber.
Thank you a lot people!!
I already do this:
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include "std_msgs/Float32.h"
#include <sstream>
void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
ROS_INFO("Valor indice: [%f]", msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "indice");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
ros::spin();
ros::init(argc, argv, "prueba");
//ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 500);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
std_msgs::Float64 msg;
//msg.data
ROS_INFO("%f", msg.data);
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
When i run everything the subscriber shows to me what is listening, now i need to deliver this vlues to the publisher because i need to send this values to other topic. and if i do
rostopic echo /sh_ffj3_mixed_position_velocity_controller/command
it shoot me:
WARNING: no messages received and simulated time is active.
Is /clock being published?
Any Idea??
///////////////////////////////////Edit
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <sstream>
void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
ROS_INFO("Valor indice: [%f]", msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "indice");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
ros::spin();
ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 500);
ros::Rate loop_rate(10);
ros::spinOnce();
int count = 0;
while (ros::ok())
{
std_msgs::Float64 msg;
//msg.data
ROS_INFO("%f", msg.data);
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
EDIT///////////////////////////////////////////////////////////
the code is like that:
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <sstream>
void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
ROS_INFO("Valor indice: [%f]", msg->data);
}
int main ...
You only need the spinOnce in the main while loop.
OK, it already works, but i need to take the value that i reading with the subscriber and deliver this value to the publisher, how can i do that??
You added a a different question that is not really related to your original one. It's better to create a new question for that. In your case, the problem is that you are trying to access a pointer that hasn't been initialized yet in your main loop. Add an
if(mensaje) { ... }
around your publish.Thank you everybody, the problem was solved, i did what Lorenz said, thank you!!