Suppose I'm playing a character with Craft as a class skill. I put 1 rank in it, select Weaponsmith to fit my background and end up with Craft (Weapons) +4. Now, if I later decide I want to be able to make bows and arrows, do I also get another +3 bonus on Craft (Bows) when I invest a new rank? There are different kinds of craft, just like there are different kinds of knowledge, but the class skill bonus for craft is completely unspecified.


Question: ros send message on startup doesn't seem to work
Answer: I experimented a bit and here's what I think happens.

When you run

$ rostopic echo /questions


The /questions topic doesn't exist before you run the program. Maybe you cheat it by first running your programming, running this command and then running your program again, but this assertion is still true. So at the point when you run your program, rostopic is not subscribed to any topic.

I don't know internally how this is handled by roscore and rostopic, but somewhere along the line, this is what's happening as the observed behavior of rostopic:

forever

    /* establish connection */
    while not subscribed to topic
        try to subscribe
        if failed
            sleep a bit

    /* intercept messages */
    while topic still exists
        wait for message
        print message


This means that rostopic (or more likely roscore on its behalf) waits for a topic to become available and subscribes to it.  It then proceeds to print incoming messages.  If the topic becomes unavailable, rostopic (or again more likely roscore on its behalf) goes back to waiting for the topic to become available again.

That said, here's what happens when this runs in parallel with your code:

    rostopic                       you

  wait for topic
      sleep







  wait for topic
      sleep

                                  start
                                ros::init
                             advertise topic
                          publish first message


subscribe to topic
 wait for message
      sleep


As you can see, your program publishes its first message immediately after advertising the topic. The unsuspecting rostopic program is not yet aware of this advertisement so is not actually subscribed to the topic yet. You publish the first message before rostopic gets a chance to subscribe to the topic.

You can verify this simply by putting a relatively large sleep after you advertise the topics and before you publish to them, i.e. between nc.init() and nc.spin().



To address your real problem (as stated and responded in the comments), which is to understand when the node has started up and use this as a trigger for further actions, I'd advise to consider another strategy.

Generally, you would want to make sure a node is up to make sure the other nodes that depend on it can run correctly. However, this doesn't mean that the original node has to be run just before the depending nodes. It could in theory be run long before them, or since ROS doesn't run in real-time and as you have observed, the timing between when topics are advertised, subscribed and the messages going around are quite unpredictable. Add network to this and they are really unpredictable.

So instead, you could devise a mechanism to query whether your node is available or not. This is simply done with a ROS service. You would essentially do all the necessary startup and then put up a service that always returns true. Any node that needs to ensure this node is available before proceeding can poll this service and as soon as a call to this service succeeds, you know the node is running.

As an added bonus, this means that you don't have to take this node down (and all depending nodes) just because you need to restart one of the depending nodes.