publisher not publishing on topic
im trying to publish a message on a topic once but it never publishes the data at all. This is the code in main
ros::init(argc, argv, "keeper_env");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::Empty>("toggle_led", 1000);
std_msgs::Empty msg;
chatter_pub.publish(msg);
even putting it in a finite while loop dosent work
int i=0;
ros::Rate rat(20);
while(i<10)
{
chatter_pub.publish(msg);
ros::spinOnce();
i++;
rat.sleep();
}
but if i put it in an infitite loop then it publishes the data.
while(ros::ok())
{
chatter_pub.publish(msg);
ros::spinOnce();
}
If i put it in a if condition in that infinite loop then it still dosent publish. i dont want it to be called infintely. i just need it once. can someone please help me
int i=0;
while(ros::ok())
{
if(i==0)
{
printf("Doses it go iun here \n");
chatter_pub.publish(msg);
i++;
}
}