Ask Your Question
0

accessing std_msgs::Float64MultiArray data

asked 2013-07-14 23:02:56 -0500

Mudassir Khan gravatar image

updated 2013-07-14 23:04:42 -0500

I have a 2D Eigen matrix

MatrixXd map = MatrixXd::Zero(mapSizeX,mapSizeY);

I read data from a text file and update the matrix, which works fine.

ifstream myFile ("map_test.txt");
int xCord = 0;
int yCord = 0;
int val;
if (myFile.is_open())
{
    while (myFile >> val)
    {
        map(xCord,yCord) = val;
        yCord ++;
        if (yCord == mapSizeY)
        {
            yCord = 0;
            xCord ++;
        }
    }
    myFile.close();
}
else
{
    ROS_INFO("could not open file");
}

then I convert the Eigen matrix to std_msgs::Float64MultiArray

std_msgs::Float64MultiArray map_info;
tf::matrixEigenToMsg(map,map_info );

now when I try to access the data it gives unexpected results. Like I try to access first 6 elements of the map_info

for (double i=0;i<6;i++)
{
       ROS_INFO("value at index %d = %f",i,map_info.data[i]);
}

and it output

[ INFO] [1373878614.190914794]: value at index 18 = 0.000000
[ INFO] [1373878614.191085167]: value at index 18 = 1.000000
[ INFO] [1373878614.191230635]: value at index 18 = 2.000000
[ INFO] [1373878614.191324653]: value at index 18 = 3.000000
[ INFO] [1373878614.191438539]: value at index 18 = 4.000000
[ INFO] [1373878614.191529321]: value at index 18 = 5.000000

but if I replace %d with %f in ROS_INFO line or I define i as int then it gives correct result like

[ INFO] [1373878704.169522260]: value at index 0.000000 = 1.000000
[ INFO] [1373878704.169687096]: value at index 1.000000 = 1.000000
[ INFO] [1373878704.169760705]: value at index 2.000000 = 1.000000
[ INFO] [1373878704.169881303]: value at index 3.000000 = 1.000000
[ INFO] [1373878704.169971869]: value at index 4.000000 = 1.000000
[ INFO] [1373878704.170037066]: value at index 5.000000 = 1.000000

I can't understand what is the issue here. Why it would give me this unexpected results. One thing I would mention, when I compile the code it give the following warning.

warning: format ‘%d’ expects argument of type ‘int’, but argument 9 has type ‘double’ [-Wformat]
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2013-07-14 23:30:55 -0500

iterator 'i' in for loop is declared as double instead of int, therefore u see this kind of output and a warning at the end.

edit flag offensive delete link more

Comments

i did that on purpose, even if i declare i as int and put %d instead of %f for map_info.data[] in ROS_INFO, it returns huge unknown numbers like [ INFO] [1373888581.936912465]: value at index 0 = -490863752

Mudassir Khan gravatar imageMudassir Khan ( 2013-07-15 01:42:54 -0500 )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

Stats

Asked: 2013-07-14 23:02:56 -0500

Seen: 1,373 times

Last updated: Jul 14 '13