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

how to publish int8,float ...and function ? [closed]

asked 2014-09-12 02:13:01 -0600

turtle gravatar image

i can't publish int8 .. can you help me?

  #include "ros/ros.h"
#include "std_msgs/Int8.h"

#include <sstream>


int main(int argc, char **argv)
{

  ros::init(argc, argv, "talker");

  ros::NodeHandle n;


  ros::Publisher chatter_pub = n.advertise<std_msgs::Int8>("mytopic", 1000);

  ros::Rate loop_rate(10);

    int8_t count = 0;
  while (ros::ok())
  {

    std_msgs::Int8 msg;


    msg.data = count++;

    ROS_INFO("%s", msg.data);


    chatter_pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();

  }


  return 0;
}

display : NULL

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by turtle
close date 2014-09-12 04:43:04.004741

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-09-12 02:23:33 -0600

gvdhoorn gravatar image
ROS_INFO("%s", msg.data);

For std_msgs::Int8, the data field is of the int8 type (see the msg doc). The %s format string is for strings (or char*), so that won't work. Try %d.


PS: are you sure you aren't ignoring some compiler warning?

PPS: the question title says you cannot publish an Int8, but in your actual question it would seem you only have difficulty logging the data field using ROS_INFO(..). Perhaps you should update the title to reflect your actual issue.

edit flag offensive delete link more

Comments

Thanhk you ! i done

turtle gravatar image turtle  ( 2014-09-12 04:37:54 -0600 )edit
1

answered 2014-09-12 02:17:49 -0600

bvbdort gravatar image
ROS_INFO("%d", msg.data);  // msg.data is int data type so %d
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-09-12 02:13:01 -0600

Seen: 6,384 times

Last updated: Sep 12 '14