ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to log float64 on arduino?

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

danielsnider gravatar image

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

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) {
  // http://docs.ros.org/kinetic/api/trajectory_msgs/html/msg/JointTrajectoryPoint.html
  trajectory_msgs::JointTrajectoryPoint *points = msg.points;  <-- is this correct?
  sprintf(log_msg,"points[0].positions[0] = %f",points[0].positions[0]);  <-- correct?
  nh.loginfo(log_msg);
}

Output:

[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
3

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

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: http://arduino.stackexchange.com/ques...

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);
  nh.loginfo(log_msg);
}

Correct output:

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

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

Seen: 4,619 times

Last updated: Feb 17 '17