Put data from one topic into an dynamic array in the Callback function and do calculation in ROS
Hi, I am actually reading my hand pose from a Leap Motion sensor and I want to calculate how fast the hand moves(by calculating derivativex = dx / dt) in X direction. My solution is to put 100 hand pose values in an array and keep updating this array with new values when new messages (msg->palmpos.x) arrive in the callback function through topic leapmotion/data.
My question is when I print the derivativex = dx / dt with ROS_ERROR("Hello %f", "derivativex") the output is always: Can you please kindly tell me what I've been doing wrong? Thanks a miliion Btw, plese find the link for the topic that my callback is listening if it is helpful for you the topic publisher. Thanks a million.
Please take a look at my call back function:
#include "geometry_msgs/TwistStamped.h"
#include "jog_msgs/JogJoint.h"
#include "jog_msgs/leapros.h"
#include "ros/ros.h"
#include <ros/console.h>
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
namespace to_twist
{
class spaceNavToTwist
{
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();
};
const int arraySize = 100;// constant variable can be used to specify array size
double vectorx[ arraySize ] = {};// initialize elements of array n to 0
int resolution = 10;
double derivativex = 0;
double dx = 0;
int dt = 0;
private:
ros::NodeHandle n_;
ros::Subscriber joy_sub_;
ros::Publisher twist_pub_, joint_delta_pub_;
ros::AsyncSpinner spinner_;
// Convert incoming joy commands to TwistStamped commands for jogging.
void joyCallback(const jog_msgs::leapros::ConstPtr& msg)
{
for ( int count = 0; count < arraySize; ++count ) {// store the values of poses
vectorx[ count ] = msg->palmpos.x;
if (count>resolution) {
dx = vectorx[ count-1 ] - vectorx[ count-(resolution-1) ];
dt = resolution;
derivativex = dx / dt;
ROS_ERROR("Hello %f", derivativex);
}
if (count == arraySize) {
count=0;
}
}