RethinkRobotics-opensource / rosnodejs

Client library for writing ROS nodes in JavaScript with nodejs
http://wiki.ros.org/rosnodejs/overview
Apache License 2.0
197 stars 74 forks source link

How to handle UInt64 array (and header with timestamp) when publishing things ? #59

Closed tutorgaming closed 7 years ago

tutorgaming commented 7 years ago

I need to publish "mavros_msgs/Mavlink" from rosnodejs. which has an array of UINT64 (Uint64[]) inside the message.

All I've done is receiving raw data packet as a buffer, split it and assign to the others field.

For receiving and reading, I've followed the code and found that it used "BN.JS" , I used it to translate and the use them successfully.

but for the UInt64[], I still have no idea how to pack it as ROSMSG.

Should I pack it as Array of Bignum [ , , .... ] or send it as the array of buffer?

Also, it has a header which requires timestamp. Again, I've followed the code and found that I can call rosnode.Time to get now() from that util, is that the correct thing to do?

chris-smith commented 7 years ago

To create an uint64 array field you would simply assign an array of bignums to the field. I believe we also accept anything that can be turned into a bignum (new BN(x)) when publishing.

To send it as a ros message, you can either specify all the fields yourself and send it as JSON or you can create a Mavlink message by pulling it in through rosnodejs.

const mavros_msgs = rosnodejs.require('mavros_msgs');
const Mavlink = mavros_msgs.msg.Mavlink;

const pub = rosnodejs.nh.advertise('/my_topic', Mavlink);
pub.publish(new Mavlink());
// or
pub.publish({
  header: {
    stamp: { secs: 0, nsecs: 0 },
    seq: 0,
    frame_id: 'base'
  },
  framing_status: 1,
  // the rest of the fields
});

rosnodejs.Time.now() will give you the correctly formatted ROS time or sim time though you can also construct it yourself. In general, rosnodejs doesn't care if you publish a Message or JSON. It does care that the message is fully formed - if fields (or nested fields) in your message are missing it may fail to publish your message. Creating the message from the class makes this easier as it guarantees a fully formed object.

tutorgaming commented 7 years ago

Thank you for you very well inform. Now it works like a charm !

Thanks for sharing us the good lib :)