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

ROS2 cannot load custom my_package.srv

asked 2022-03-30 05:26:02 -0600

kyubot gravatar image

I am trying to add my custom service to my_package. I created a 'srv' folder under 'my_package' and add "Battery.srv" as below

---
float64 volt
float64 SOC
float64 current

and added followings to package.xml

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

and in CMakeLists.txt

find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/Battery.srv"
)

Also in my setup.py

setup(
    name=package_name,
    version='0.0.1',
    packages=find_packages(exclude=[]),
    data_files=[
        ('share/ament_index/resource_index/packages', ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*.launch.py'))),
        (os.path.join('share', package_name, 'param'), glob(os.path.join('param', '*.yaml'))),
        (os.path.join('share', package_name, 'srv'), glob(os.path.join('srv', '*.srv'))),
    ],

Finally in my package_node.py I tried to import

from my_package.srv import Battery

After colcon build this package successfully, to checked my service I entered below

 $ ros2 interface show my_package/srv/Battery
---
float64 volt
float64 SOC
float64 current

Now when I tried to run my package node I got error like this

File "/home/my/ros2_ws/build/my_package/my_package/scripts/my_package_node.py", line 14, in <module>
    from my_package.srv import Battery
ModuleNotFoundError: No module named 'my_package.srv'

Is there anything wrong with my practice? Any advice would be much appreciated. Kyu

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-03-30 17:02:38 -0600

ljaniec gravatar image

updated 2022-03-30 17:17:18 -0600

Just to be sure - did you source install/setup.bash in your workspace every time (once for the client, once for the server)? You can try to you delete your build and install folders there and rebuild everything too.

Did you create your package with this tutorial?? Because usually messages and services are defined in another package called your_project_interfaces and you import everything from there. Only this part differs in your approach (e.g. based on this and previous link) - you try to do everything in one package, both srv/ and scripts.

edit flag offensive delete link more

Comments

As you said, source ~/bashrc did the trick and it works now. I also realized srv file should not include any Capital letter. Thank you for your comment!

kyubot gravatar image kyubot  ( 2022-03-31 01:11:00 -0600 )edit

I am happy to help! You can mark the answer as accepted then

ljaniec gravatar image ljaniec  ( 2022-03-31 02:05:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-03-30 05:26:02 -0600

Seen: 762 times

Last updated: Mar 30 '22