ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Issue with reading sensor_msgs

asked 2019-08-09 09:01:14 -0500

SunnyKatyara gravatar image

updated 2019-08-10 21:22:15 -0500

jayess gravatar image

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);
}
edit retag flag offensive close merge delete

Comments

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?

ct2034 gravatar image ct2034  ( 2019-08-09 10:31:15 -0500 )edit

And sorry, but maybe you want to look at something like this: http://www.cplusplus.com/reference/ve...

ct2034 gravatar image ct2034  ( 2019-08-09 10:32:51 -0500 )edit

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...

zmk5 gravatar image zmk5  ( 2019-08-09 15:34:16 -0500 )edit

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

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 03:42:07 -0500 )edit
1

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?

ct2034 gravatar image ct2034  ( 2019-08-10 04:17:24 -0500 )edit

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

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 04:37:57 -0500 )edit

Sorry, i did not highlight the code. I have highlighted it now.

Thank you

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 05:13:08 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-08-10 09:29:45 -0500

ct2034 gravatar image

If you want to read values from /cartesian/solution, you have a typo in your code. Is says /cATersian/.... Everything else works. I tested it.

Although I would recommend using arrays or vectors for the data instead of 24 individual variables. I also like the conversion to a class. But there it is generally good practise to have the variables protected.

edit flag offensive delete link more

Comments

but ct2034, when i run the code, it gives me zeros only. It means my callback function is not working. Because all the time, it prints 0s only, which are default in the class. The output from /cartesian/solution is :

sunny@sunny-ThinkPad-Edge-E531:~/catkin_ws/src/tutorial_iros2018/launch$ rostopic echo /cartesian/solution 
header: 
  seq: 0
  stamp: 
    secs: 23
    nsecs: 456000000
  frame_id: ''
name: [panda_joint_x, panda_joint_y, panda_joint_theta, schunk_pw70_joint_pan, schunk_pw70_joint_tilt,
  panda_joint_1, panda_joint_2, panda_joint_3, panda_joint_4, panda_joint_5, panda_joint_6,
  panda_joint_7]
position: [0.0, 0.0, 0.0, 0.0, 0.0, 0.27218156462835696, -0.23239725174321993, 0.06414067279654619, -0.41568844865564586, -0.20909900886432725, 0.6249307486805932, -1.7535359390708531]

But i am not getting these value in the ouput, with this script code. This is the data published by other node on this topic.

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 09:53:49 -0500 )edit

yes, because your code is subscribing to /catersian/solution instead of /cartesian/solution. You can also add some output to the callback function to see when it is called. (which it won't be with that code)

ct2034 gravatar image ct2034  ( 2019-08-10 10:11:42 -0500 )edit

i can also recommend to add something like ros::Duration(1).sleep(); to your loop. Otherwise, it may just spam you terminal with too many outputs

ct2034 gravatar image ct2034  ( 2019-08-10 10:14:58 -0500 )edit

Thank you so much. It was really as mistake with topic name. I did not put head to spelling. But thank you so much. It is working now :)

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 10:17:28 -0500 )edit

One last thing, i want to ask you that, how can i read the time from this topic and send it on other topic "/panda/pand".

Thank you

SunnyKatyara gravatar image SunnyKatyara  ( 2019-08-10 10:20:50 -0500 )edit

Cool. Happy it works now. Can you please accept my answer. the time is in the header. (You can see it in your rotopic echo ouput) just copy that to your new message.

ct2034 gravatar image ct2034  ( 2019-08-10 10:24:51 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-08-09 09:01:14 -0500

Seen: 287 times

Last updated: Aug 10 '19