Ask Your Question

How to log float64 on arduino?

asked 2017-02-17 11:00:02 -0500

danielsnider gravatar image

updated 2017-02-17 11:16:30 -0500

I'm having trouble logging a float64 value. Here's my arduino code (I'm using rosserial):

void jt_callback(const trajectory_msgs::JointTrajectory &msg) {
  trajectory_msgs::JointTrajectoryPoint *points = msg.points;  <-- is this correct?
  sprintf(log_msg,"points[0].positions[0] = %f",points[0].positions[0]);  <-- correct?


[INFO] [1487350593.639400, 5453.955000]: points[0].positions[0] = ?

The correct value should be 0.1 not ?.

Also, when I try %d or %i the output value is -13108.

Any help is much appreciated!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-17 11:53:37 -0500

danielsnider gravatar image

I fixed my own problem! I found that I needed to convert float to char* using a function in the standard Arduino library called dtostrf(). Think of it as "Decimal to String Float".

This answer has a better description:

Working code:

void jt_callback(const trajectory_msgs::JointTrajectory &msg) {
  nh.loginfo("msg received");
  // Print points.positions[0]
  char result[8]; // Buffer big enough for 7-character float
  dtostrf(points[0].positions[0], 6, 2, result); // Leave room for too large numbers!
  sprintf(log_msg,"points[0].positions[0] =%s", result);

Correct output:

[INFO] [1487353743.896876, 6929.022000]: points[0].positions[0] = 0.10

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-02-17 11:00:02 -0500

Seen: 3,643 times

Last updated: Feb 17 '17