Can you offer more details?
‘for the code that I found’ Is this our code in our repositories ? If so link please.
Normally there is an include or two at the start of a file which by itself does nothing and is there so ‘real code’ can reference message types later.
Then in starting the node there will be a line to setup a ROS subscriber to a specific topic name and that line will specify the message type expected in that topic. That line will also specify a ‘callback’ routine. Later as the code runs whenever ROS sees a message on the topic it will do a callback to the callback you specified when registering the subscriber.
So you don’t see later in the code the message being directly handled, you go to that callback function that handles that message type on the topic being subscribed and there the user does what he wants with the contents with the message so his node can use that callback data.
Your next question about 'replace LaserScan messages with Sonar messages depends on what you want to do. The LaserScan message has an array of 360 ranges so lets say you only want to see straight ahead, 45deg to right, 45deg to left, full right and full left. If that is the case you can subscribe to the /sonars topic and pull off the 5 sonar ranges.
Examples of use of sonars topic to get ranges from sonars:
For C++ see our new sonar_detector node I did a couple weeks back.
For Python sonar subscriptions that is not in our default top level ‘demos’ repository yet.
you can go into your catkin_ws/src/demos folder and first use ‘git pull’ then use ‘git checkout SonarWanderer’ and see demos/sonar_wanderer node which I did a week or so ago.
If this did not help then I really don’t understand what you want to do or perhaps I don’t know just what code you found in some other example for some other ROS robot.