how to calculate difference of two messages in callback function on same topic
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