0
$\begingroup$

Rosanswers logo

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_implboost::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;
}

Originally posted by th6262 on ROS Answers with karma: 50 on 2018-11-18

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by adysalas with karma: 26 on 2018-11-18

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by th6262 on 2018-11-18:
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

Comment by adysalas on 2018-11-18:
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.

Comment by jarvisschultz on 2018-11-18:
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...

Comment by jarvisschultz on 2018-11-18:
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

Comment by jarvisschultz on 2018-11-18:
http://docs.ros.org/melodic/api/roscpp/html/namespaceros_1_1topic.html

Comment by adysalas on 2018-11-18:
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[]

Comment by th6262 on 2018-11-18:
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();
  }
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.