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

Using class to get values from callback function??

asked 2016-07-03 19:34:02 -0500

Tomm gravatar image

updated 2016-07-04 18:52:34 -0500

Hi everyone,

I have written a code and I want to use the values of linearposx,linearposy and yaw from the callback function named ComPoseCallback. As shown here, I did the changes in my program (created a class). But when I try to build it, it gives me the following error:

expected primary-expression before ‘)’ token
ros::Subscriber ComPose_sub = n.subscribe("/RosAria/pose", 1000, &PioneerData::ComPoseCallback, PioneerData);

Here is my code:

#include "ros/ros.h"
#include "nav_msgs/Odometry.h" 
#include <ros/console.h>
#include "geometry_msgs/Pose.h"
#include "geometry_msgs/Twist.h"
#include <tf/transform_datatypes.h>

double quatx;
double quaty;
double quatz;
double quatw;
double linearposx;
double linearposy;

class PioneerData
{
 public: 
 void ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg);
};



int main(int argc, char **argv)
{
    ros::init(argc, argv, "Test_FK"); 
    ros::NodeHandle n;                 
    ros::Publisher Velocity_pub = n.advertise<geometry_msgs::Twist>("/RosAria/cmd_vel",1000);
    ros::Subscriber ComPose_sub = n.subscribe("/RosAria/pose", 1000, &PioneerData::ComPoseCallback, &PioneerData);

    geometry_msgs::Twist velocity;
    velocity.linear.x =0.1;
    velocity.linear.y =0;
    velocity.linear.z =0;
    velocity.angular.x =0;
    velocity.angular.y =0;
    velocity.angular.z =0;

    geometry_msgs::Twist Stop;
    Stop.linear.x =0;
    Stop.linear.y =0;
    Stop.linear.z =0;
    Stop.angular.x =0;
    Stop.angular.y =0;
    Stop.angular.z =0;



   //std::cout<<"xPosition"<<linearposx<<std::endl;
   //ros::Rate loop_rate(10);
  //ros::Rate rate (1);

  while (ros::ok())
   {


   if(Velocity_pub.getNumSubscribers()>0)
    {
    if (linearposx==0)
    {
    Velocity_pub.publish(velocity);

    }
    else if(linearposx>=0.5)
    {
      Velocity_pub.publish(Stop);
    }
    ros::spin();
     break;
    }
   }
    //return 0;
}

void ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg)            
{
    ROS_INFO("Seq: [%d]", msg->header.seq);
    ROS_INFO("Position-> x: [%f], y: [%f], z: [%f]", msg->pose.pose.position.x,msg->pose.pose.position.y, msg->pose.pose.position.z);
    ROS_INFO("Orientation-> x: [%f], y: [%f], z: [%f], w: [%f]", msg->pose.pose.orientation.x, msg->pose.pose.orientation.y, msg->pose.pose.orientation.z, msg->pose.pose.orientation.w);

   linearposx=msg->pose.pose.position.x;
   linearposy=msg->pose.pose.position.y;
   quatx= msg->pose.pose.orientation.x;
   quaty= msg->pose.pose.orientation.y;
   quatz= msg->pose.pose.orientation.z;
   quatw= msg->pose.pose.orientation.w;

   tf::Quaternion q(quatx, quaty, quatz, quatw);
   tf::Matrix3x3 m(q);
   double roll, pitch, yaw;
   m.getEulerYPR(yaw, pitch, roll); //getRPY
   ROS_INFO("Yaw: [%f],Pitch: [%f],Roll: [%f]",yaw,pitch,roll);

   return ;
}

I can't understand the reason for the error. Can you please point out my mistake? Thanks.

Edit :Hi janindu, I did the changes suggested by you. I am getting the following error:

error: extra qualification ‘PioneerData::’ on member ‘ComPoseCallback’ [-fpermissive]
  void PioneerData::ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg);

Edit 1: I did a quick google search and found that I need to remove

Pioneer::

from:

void PioneerData::ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg)

to remove above error shown in edit . After removing that error, I get a new error as you can see below:

CMakeFiles/Test_FK.dir/src/Test_FK.cpp.o: In function `main':
Test_FK.cpp:(.text+0x17e): undefined reference to `PioneerData::ComPoseCallback(boost::shared_ptr<nav_msgs::Odometry_<std::allocator ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-07-03 19:50:58 -0500

janindu gravatar image

updated 2016-07-03 20:19:07 -0500

I think I figured out what your mistake is. The 4th argument in the n.subscribe method should be the context in which the callback should be called.

That is, instead of

ros::Subscriber ComPose_sub = n.subscribe("/RosAria/pose", 1000, &PioneerData::ComPoseCallback, PioneerData);

you should first instantiate the PioneerData class like this.

PioneerData pioneerData;

Then call the subscriber method with a pointer to the object instance.

ros::Subscriber ComPose_sub = n.subscribe("/RosAria/pose", 1000, &PioneerData::ComPoseCallback, &pioneerData);

It should work. Let me know if it doesn't.

EDIT

Also, your ComPoseCallback method definition should be changed to

void PioneerData::ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg)  { ...
edit flag offensive delete link more

Comments

Hi.Thank you for your reply. I have edited the post. Please have a look at it.

Tomm gravatar image Tomm  ( 2016-07-04 14:10:13 -0500 )edit

You have put the qualifier in ComPoseCallback declaration, not definition. Change the method definition as follows.

void PioneerData::ComPoseCallback(const nav_msgs::Odometry::ConstPtr& msg)            
{
    ROS_INFO("Seq: [%d]", msg->header.seq);
    ...
}
janindu gravatar image janindu  ( 2016-07-04 19:20:44 -0500 )edit

And you can refer http://www.cplusplus.com/doc/tutorial... to see how to implement a class. Let me know if you get further errors.

janindu gravatar image janindu  ( 2016-07-04 19:21:49 -0500 )edit

No more errors after the changes. Thanks a lot man. Cheers.

Tomm gravatar image Tomm  ( 2016-07-04 21:59:04 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-03 19:34:02 -0500

Seen: 1,135 times

Last updated: Jul 04 '16