Ask Your Question
0

how to calculate difference of two messages in callback function on same topic

asked 2018-12-20 03:36:03 -0600

stevensu1838 gravatar image

updated 2018-12-20 03:52:48 -0600

Hi Sir, I am actually reading my hand pose from a Leap Motion sensor and I want to calculate how much the hand moves in X direction. My question is how to read the message in callback function(as you know, the message is coming through a normal topic ) and show the difference between the previous message and the current one? Please take a look at my call back function:

public:
  spaceNavToTwist() : spinner_(1)
  {
    joy_sub_ = n_.subscribe("leapmotion/data", 1, &spaceNavToTwist::joyCallback, this);
    // Changed "spacenav/joy" to topic "/leapmotion/data"
    twist_pub_ = n_.advertise<geometry_msgs::TwistStamped>("jog_arm_server/delta_jog_cmds", 1);
    joint_delta_pub_ = n_.advertise<jog_msgs::JogJoint>("jog_arm_server/joint_delta_jog_cmds", 1);

    spinner_.start();
    ros::waitForShutdown();
  };

private:
  ros::NodeHandle n_;
  ros::Subscriber joy_sub_;
  ros::Publisher twist_pub_, joint_delta_pub_;
  ros::AsyncSpinner spinner_;

    void joyCallback(const jog_msgs::leapros::ConstPtr& msg)
{
// Variables to store the previous values of handpose along X,Y and Z-Axes
double previousX, previousY, previousZ;
// Variables to store the current values of handpose along X,Y and Z-Axes
double currentX, currentY, currentZ;
// Get the initial values of handpose along X,Y and Z-Axes
previousX = msg->palmpos.x;
// Read the current values of handpose along X,Y and Z-Axes

currentX = msg->palmpos.x;
ROS_ERROR("previous %f", previousX);
ROS_ERROR("current %f", currentX);
ROS_ERROR("difference %f", currentX-previousX);

I don't know why when I print the previous message and the current one out, they are always the same and difference is 0. Thanks a lot

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2018-12-20 04:10:59 -0600

Delb gravatar image

updated 2018-12-20 04:13:56 -0600

There is something wrong here :

// Get the initial values of handpose along X,Y and Z-Axes
previousX = msg->palmpos.x;
// Read the current values of handpose along X,Y and Z-Axes
currentX = msg->palmpos.x;

You are actually doing previousX = currentX since you assigned them the same value.

What you want to do is to use global variables so that when the callback is called again you still have the value stored somewhere, i'll give you an exemple :

//global variable that needs to be set to a default value
double previousX = 0;
void joyCallback(const jog_msgs::leapros::ConstPtr& msg)
{
    double difference;
    difference = msg->palmpos.x - previousX;
    ROS_INFO("previous %f", previousX);
    previousX = msg->palmpos.x;
    ROS_INFO("current %f", previousX);
    ROS_INFO("difference %f", difference);
}
edit flag offensive delete link more

Comments

Hi buddy, thank you so much for your help. And I am right now calculating the speed of my hand in the following link. Can you please take a look?link

stevensu1838 gravatar imagestevensu1838 ( 2018-12-22 03:22:35 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-12-20 03:36:03 -0600

Seen: 83 times

Last updated: Dec 20 '18