Ask Your Question
1

ROSSerial Arduino Arrays

asked 2015-02-21 08:55:01 -0500

aak2166 gravatar image

Hi All,

I'm trying to use Rosserial Arduino to process some data from a low cost laser rangefinder. Over the course of a scan I try to add the data to an array of float in arduino:

float snd_distance = getDistance(servo_pos);
distance_array[servo_pos] = snd_distance;

Then I have a function which populates the message:

void populateMessage(float distances[145]){  
 //Set up frame data
  scanData.header.frame_id = "1";
  scanData.header.seq = scanData.header.seq++;
  scanData.header.stamp = nh.now();
  scanData.angle_min = 0.0;
  scanData.angle_max = 2.53072742;
  scanData.angle_increment = 0.0174532925; 
  scanData.range_min = 0;
  scanData.range_max = 40;  
  for(int i = 0; i <= 144; i++){
    scanData.ranges[i] = distances[i];
  }
  pub_scan.publish(&scanData);
  nh.spinOnce();
}

However, there seems to be a problem copying the values of distances over to scanData.ranges. I see no errors during compilation, however execution locks up at that step and I seem unable to get past it. Has anyone seen this before? Many thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2015-02-21 11:53:12 -0500

paulbovbel gravatar image

updated 2015-02-21 11:53:24 -0500

A few things jump out immediately:

scanData.header.seq = scanData.header.seq++;

Won't do what you expect ( http://stackoverflow.com/questions/24... )

for(int i = 0; i <= 144; i++){ scanData.ranges[i] = distances[i]; }

You can't just write into an array like this ( http://wiki.ros.org/rosserial/Overvie... ). You either need to allocate the array, and then move the data, or just set the array pointer to the appropriate destination:

scanData.ranges = &distances;

Don't forget to set the size as well:

scanData.ranges_length = 145;
edit flag offensive delete link more

Comments

Thanks for the tips. Didn't realize the ranges_length was a field I could write to looking at the message definition for laser scan. Suppose its implicit to all these arrays?

aak2166 gravatar imageaak2166 ( 2015-02-21 12:07:54 -0500 )edit

Yep. In roscpp those wouldn't be arrays either, they would be std::vectors. But rosserial API is different than roscpp.

paulbovbel gravatar imagepaulbovbel ( 2015-02-21 12:26:16 -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

1 follower

Stats

Asked: 2015-02-21 08:55:01 -0500

Seen: 1,562 times

Last updated: Feb 21 '15