0
$\begingroup$

Rosanswers logo

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


Originally posted by kyubot on ROS Answers with karma: 60 on 2022-03-30

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by ljaniec with karma: 3064 on 2022-03-30

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by kyubot on 2022-03-31:
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!

Comment by ljaniec on 2022-03-31:
I am happy to help! You can mark the answer as accepted then

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.