Ask Your Question
1

Subscribing to a Float32MultiArray and printing each value

asked 2018-11-18 10:54:42 -0500

th6262 gravatar image

Hey everyone, I'm trying to do exactly what the title says , subcribe to a Multi Array with Float32 values (7 elements), and print each value for testing purposes. For some reason, what worked with integer values before, doesn't apply to Float32 Multi Arrays

The topic I'm subscribing to is called "/Test".

The error code when running this program:

0, 0, 0, 0, 0, 0, 0, 0, terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error>

' what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument Aborted (core dumped)

My Code:

ros::Subscriber controllerSub;
float Arr[7];

void arrayCallback(const std_msgs::Float32MultiArray::ConstPtr& array);

int main (int argc, char** argv)
{
  // Initialize ROS
  ros::init (argc, argv, "controller");
  ros::NodeHandle n;

  // Create a ROS subscriber for the input point cloud

  controllerSub = n.subscribe("/Test", 100, arrayCallback);

  ros::spinOnce();




  int j = 0;
  for(j = 0; j < 8; j++)
    {
        printf("%d, ", Arr[j]);
    }

  printf("\n");
  return 0;

}

void arrayCallback(const std_msgs::Float32MultiArray::ConstPtr& array)
{

        int i = 0;

        // print all the remaining numbers
        for(std::vector<float>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)
        {
                Arr[i] = *it;
                i++;
        }

        return;
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-11-18 11:59:21 -0500

adysalas gravatar image

Hello,

I have a similar issue sending also float32 arrays, and I solved creating a file.msg with my array variable like this float32[] array1

And later on the subscriber I did something like this

std::vector<float> waypointsX;

void chatterCallback(const robo_move_tpc::waypoints::ConstPtr& waypointsData)

waypointsX.push_back(waypointsData-> array1[i]);

And later I put all the values on the vector and work with the vector, obviously you can work directly with the waypointData, but for me was more easy work of that way. Hope this help you.

edit flag offensive delete link more

Comments

Thank you for the suggestion, do you have any idea what's wrong with the way I'm doing it ? Because I can't change the type of message that's being published . My goal is simply to subscribe to it so I can access the individual float values for further processing

th6262 gravatar imageth6262 ( 2018-11-18 15:30:56 -0500 )edit
1

The only thing that I see out of context is the initial declaration of ros::Subscriber controllerSub; out of the main, out of that I didn't see anything wrong.

adysalas gravatar imageadysalas ( 2018-11-18 16:16:31 -0500 )edit
1

I see 3 possible issues. (1) you are calling ros::spinOnce immediately after subscribing to the data. Likely by the time you hit the spin, you won't have received a message and there will be nothing on your callback queue. This is why you are printing zeros (most likely). (2) you are using %d...

jarvisschultz gravatar imagejarvisschultz ( 2018-11-18 17:30:33 -0500 )edit
1

for your printf formatting, but you have float values. (3) you are iterating past the end of the Arr in your for loop. I wouldn't think any of these issues would cause the error you posted, but they should all be fixed. If you want to subscribe to one message you could use waitForMessage

jarvisschultz gravatar imagejarvisschultz ( 2018-11-18 17:31:52 -0500 )edit
1
1

In fact I didn’t see the printf . It should be %f and not the %d. About the rospin, I don’t see any problem because the callback is going to be made and the information stored in Arr[]

adysalas gravatar imageadysalas ( 2018-11-18 17:45:23 -0500 )edit

Thx everyone involved, I applied all the tips! This is what ultimately fixed it:

ros::Rate loop_rate(10);
while (ros::ok())
  {
    // here the for loop to print the array content

    printf("\n");
    ros::spinOnce();
    loop_rate.sleep();
  }
th6262 gravatar imageth6262 ( 2018-11-18 19:52:13 -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

2 followers

Stats

Asked: 2018-11-18 10:54:42 -0500

Seen: 371 times

Last updated: Nov 18 '18