Issue with reading sensor_msgs
Hello every one, since, i want to read messages from the topic /cartesian/solution
which has sensor_msgs
and want to then some random topic /panda/pand
. For confirmation, i am also printing out one member of topic /cartesian/solution
But all the time its showing me zero. It means my callback function is not updating the values to one declared in public class. Because all the time it is showing me zeros for all of these values, which are their inherent assignments. I want to read these from this topic but unable to do it.
Updated Code
using namespace std;
class server {
public:
std_msgs::Float64 joint_position0, joint_position1, joint_position2, joint_position3, joint_position4, joint_position5, joint_position6, joint_position7, joint_position8, joint_position9, joint_position10, joint_position11, joint_velocity0, joint_velocity1, joint_velocity2, joint_velocity3, joint_velocity4, joint_velocity5, joint_velocity6, joint_velocity7, joint_velocity8, joint_velocity9, joint_velocity10, joint_velocity11;
void jointStateCallback(const sensor_msgs::JointState::ConstPtr& msg);
}
void server::jointStateCallback(const sensor_msgs::JointState::ConstPtr& msg) {
joint_position0.data = msg->position[0];
joint_position1.data = msg->position[1];
joint_position2.data = msg->position[2];
joint_position3.data = msg->position[3];
joint_position4.data = msg->position[4];
joint_position5.data = msg->position[5];
joint_position6.data = msg->position[6];
joint_position7.data = msg->position[7];
joint_position8.data = msg->position[8];
joint_position9.data = msg->position[9];
joint_position10.data = msg->position[10];
joint_position11.data = msg->position[11];
joint_velocity0.data = msg->velocity[0];
joint_velocity1.data = msg->velocity[1];
joint_velocity2.data =msg->velocity[2];
joint_velocity3.data = msg->velocity[3];
joint_velocity4.data = msg->velocity[4];
joint_velocity5.data = msg->velocity[5];
joint_velocity6.data = msg->velocity[6];
joint_velocity7.data = msg->velocity[7];
joint_velocity8.data = msg->velocity[8];
joint_velocity9.data = msg->velocity[9];
joint_velocity10.data = msg->velocity[10];
joint_velocity11.data = msg->velocity[11];
}
int main(int argc, char** argv) {
server objserver;
ros::init(argc, argv, "mover_node");
ros::NodeHandle n;
//ros::AsyncSpinner spinner(1);
//spinner.start();
//bool success;
ros::Subscriber joint_sub = n.subscribe("/catersian/solution", 100, &server::jointStateCallback, &objserver);
while (ros::ok())
{
ros::spinOnce();
cout << objserver.joint_position8.data;
cout<<"\n";
}
return(0);
}
Can please make sure that your code is displayed correctly in the question and clarify what exactly the problem is. What are you trying to do? What do you expect to happen? What happens instead?
And sorry, but maybe you want to look at something like this: http://www.cplusplus.com/reference/ve...
I would highly suggest you try and use a object oriented (class) approach to doing this instead of relying on global variables.
See the following example of something like this:
The header for the class with pubs and subs: https://github.com/ACSLaboratory/phee...
The src file for the the class: https://github.com/ACSLaboratory/phee...
and finally, a src file with the main: https://github.com/ACSLaboratory/phee...
Dear ct2034 and zmk5, thanks for your message. Now, i have changed the syntax of node with class defined instead of using global variables. But, i am unable to get the data from the topic. It is giving me some strange values, may be due to time lapse? when i used float, it was given me some random values and then i replaced ith std_msgs/Float64 and now, it is giving me zeros, all the time.
Code#
using namespace std; class server { public:
std_msgs::Float64 joint_position0, joint_position1, .......etc
void jointStateCallback(const sensor_msgs::JointState::ConstPtr& msg); };
void server::jointStateCallback(const sensor_msgs::JointState::ConstPtr& msg) {
joint_position0.data = msg->position[0];
... for all joint values
ros::Publisher joint_pub = n.advertise<trajectory_msgs::jointtrajectory>("/panda/pand", 100, true); ros::Subscriber joint_sub = n.subscribe("/catersian/solution", 100, &server::jointStateCallback, &objserver);
Can you please help me, where i am making mistake
Can you please edit your original question to contain the latest code and make sure that all is syntax highlighted. And add: What are you trying to do? What do you expect to happen? What happens instead?
Dear ct2034, i have updated my post. Since, i want to read the values from topic "/Cartesian/solution" and print out it values. But all the time its showing zeros. I do not where i am mistakes. Thank you
Sorry, i did not highlight the code. I have highlighted it now.
Thank you