Robotics StackExchange | Archived questions

example.interfaces worked without it being in package.xml

How could that be?

I'm doing the ROS2 for Beginners taught by Renard, writing my first Python publisher and it worked.
Or maybe it did not, it compiled and ran but I did not have a subscriber yet so could not see the message being published. But it compiled. So don't count on colcon for any compile time errors?

My publisher code

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node

from example_interfaces.msg import String


class RobotNewsStationNode(Node):

    def __init__(self):
        super().__init__(node_name="robot_news_station")
        self.robot_name_ = "R2D3"
        # publish msg on topic robot_news
        self.publisher_ = self.create_publisher(String, "robot_news", 10)
        self.timer_ = self.create_timer(0.5, self.publish_news)
        self.get_logger().info("Robot News Station has been started")

    def publish_news(self):
        msg = String()
        msg.data = "Hi " + str(self.robot_name_) + " from the robot news station."
        self.publisher_.publish(msg)


def main(args=None):
    rclpy.init(args=args)
    node = RobotNewsStationNode()
    #callbacks called from spin function
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == "__main__":
    main()

package.xml with comment

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>my_py_pkg</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="nyck33@todo.todo">nyck33</maintainer>
  <license>TODO: License declaration</license>

  <depend>rclpy</depend>
  <depend>example_interfaces</depend>  //the above publisher "ran" with no errors without this line

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

even without that line exampleinterfaces my publisher and subscriber seem to "work".
I can even do ros2 topic echo /robot
news and get the message How is this possible? Why even bother with including that line in package.xml if it's unneeded?

My subscriber just in case

#!/usr/bin/env python3

import rclpy
from rclpy.node import Node

from example_interfaces.msg import String

class SmartphoneNode(Node): # modify name
    def __init__(self):
        super().__init__("smartphone") # modify name
        # subscribe to topic
        self.subscriber = self.create_subscription(String, "robot_news", self.callback_robot_news, 10)
        self.get_logger().info("Smarphone has been started")

    def callback_robot_news(self, msg):
        self.get_logger().info(msg.data)



def main(args=None):
    rclpy.init(args=args)
    node = SmartphoneNode() # modify name
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ =="__main__":
    main()

Asked by nyck3333 on 2023-06-05 08:45:36 UTC

Comments

Quick comment: there doesn't appear to be sufficient information in your post to be able to help you.

I'm doing the ROS2 for Beginners taught by Renard

that's great, but we are not. We can't see what you see, and we're not going to sift through the course you mention to find the exact code you reference. Always include the code you're looking at, using, writing, debugging, etc. Copy errors/warnings/output verbatim and clearly describe what you expect to happen, isn't happening, what is happening and why you believe that should not happen.

So don't count on colcon for any compile time errors?

Please refrain from these kinds of premature conclusions. They're not helpful.

Additionally: Python is not compiled and Colcon does not compile anything, so you should indeed not see any compilation errors in this case.

Asked by gvdhoorn on 2023-06-05 09:08:19 UTC

My bad @gvdhoorn, I just heard make the python file an executable with chmod +x and then mistakingly connected that to "compiling" a python file when in fact it's a bash shebang one liner at the top plus the chmod command. Yes I know Python is interpreted. But in the build process, colcon does not show any errors like that? That to me is kind of strange. I mean, it's nice that it's a universal build tool that works with many build systems but how would I debug that in a large system then? I'd have to go through every package and unit test each one I guess.

Asked by nyck3333 on 2023-06-05 20:41:38 UTC

But in the build process, colcon does not show any errors like that? That to me is kind of strange. I mean, it's nice that it's a universal build tool that works with many build systems but how would I debug that in a large system then? I'd have to go through every package and unit test each one I guess.

please stop guessing.

This seems like a situation where assumptions lead to an incorrect understanding of a fundamental tool, which will then be very difficult to change afterwards.

If you want something cleared up, ask about it.

We still don't have sufficient information to help explain what you attempt to describe in your OP. If you'd still like help, please provide the information asked for.

Asked by gvdhoorn on 2023-06-05 22:34:16 UTC

@gvdhoorn, I added all the code and it seems to work without that line.

Asked by nyck3333 on 2023-06-06 00:38:32 UTC

Answers

From your updated question, I guess your main question is:

Even without <depend>example_interfaces</depend> [in my package.xml] my publisher and subscriber seem to "work". I can even do ros2 topic echo /robot_news and get the message. How is this possible?

The short answer would be: your Python interpreter does not care about package.xml files.

As long as all the Python packages your script requires are present on your system (or, more specifically: can be found on the PYTHONPATH), your script will run. It's likely example_interfaces was installed, either through aptor as part of a build of your Colcon workspace, and thus the modules in it would be available to your script.

Why even bother with including that line in package.xml if it's unneeded?

Your conclusion can't be drawn from this n==1 observation.

Package manifests have different purposes, and the one you are referring to (stating dependencies) is mostly used when setting up a build and/or runtime environment. See #q220531 for a similar question, and the linked #q217475 for a more in-depth Q&A.

But, as I wrote above, if the runtime environment in which you execute a script already contains all dependencies needed, not stating dependencies will of course not affect anything. Everything is already present, so missing lines in your package.xml will not cause problems.

Not updating your manifest will become a problem for anyone else trying to build or install your package.

Best practice in ROS package development would be to run something like rosdep install --from-paths /path/to/colcon_ws --ignore-from-source when building from source. Without the dependencies stated, rosdep would not know what to install (or: it would not be able to determine whether any dependencies are missing).

For apt-based installs, .deb package dependency relationships are based on the information in package.xml. So again, without those lines, it will all not work.

And from your comments:

But in the build process, colcon does not show any errors like that? That to me is kind of strange. I mean, it's nice that it's a universal build tool that works with many build systems but how would I debug that in a large system then?

As mentioned in the comments, Python is not a compiled language. "All" Colcon does, is kick-off the build of your Python package and then wait for it to complete (with "all" in quotes, as it does quite a bit more, but that's not relevant for this answer).

If "the build" of your Python package does not depend on the dependencies in your package.xml, no problems will occur. And even if there were errors, they will not be coming from Colcon, but from whatever tool performs the actual build.

The line you mention (a <depend>...</depend>) will affect other things Colcon cares about (such as the order in which packages should be built), but as example_interfaces was already present on your system, it all "just worked".

Finally, for a C++ node, and a CMake build type, you can be sure you will see errors (generated by the compiler and/or CMake itself) when you don't state your dependencies correctly. And Colcon will happily forward those to your terminal.

Asked by gvdhoorn on 2023-06-06 04:54:33 UTC

Comments