Why can't I import custom message module in ROS?
Hi, I'm a student studying ROS.
I want to use custom message. So, I searched some of materials, but it doesn't work.
Please help me...
this is error message.
process[both_of_motor-3]: started with pid [5985]
Traceback (most recent call last):
File "/home/hs/catkin_ws2/src/motor/src/both_of_motor.py", line 5, in <module>
from motor.msg import motor
ImportError: No module named msg
[ERROR] [1613469949.149284]: Error opening serial: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'
[both_of_motor-3] process has died [pid 5985, exit code 1, cmd /home/hs/catkin_ws2/src/motor/src/both_of_motor.py __name:=both_of_motor __log:=/home/hs/.ros/log/8aa06470-703e-11eb-8ea6-f8a2d66c8c55/both_of_motor-3.log].
and below is source code.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import rospy
5 from motor.msg import motor
6
7 angle = 90
8 speed = 90
9
10 def talker():
11 global angle, speed
12
13 pub = rospy.Publisher('motor', motor)
14 rospy.init_node('motorControl', anonymous=True)
15
16 msg = motor()
17
18 while not rospy.is_shutdown():
19 msg.angle = angle
20 msg.speed = speed
21
22 pub.publish(msg)
23
24 angle += 10
25 speed += 2
26
27 rospy.sleep(2)
28
29
30 if __name__ == '__main__':
31 try:
32 talker()
33 except rospy.ROSInterruptException:
34 pass
It is my directory structure.
├── motor
│ ├── CMakeLists.txt
│ ├── include
│ │ └── motor
│ ├── launch
│ │ ├── both_of_motor.launch
│ │ └── motor.launch
│ ├── msg
│ │ └── motor.msg
│ ├── package.xml
│ └── src
│ ├── both_of_motor.py
│ ├── .motor_pub.py.swp
│ ├── motor.py
│ └── motor.pyc
and it is my CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(motor)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
motor.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES motor
CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
and it is package.xml
<?xml version="1.0"?>
<package format="2">
<name>motor</name>
<version>0.0.0</version>
<description>The motor package</description>
<maintainer email="hs@todo.todo">hs</maintainer>
<license>TODO</license>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
</package>
next is $export | grep ROS
hs@hs-800G5H-800G5S:~/catkin_ws2/src/motor$ export | grep ROS
declare -x ROSLISP_PACKAGE_DIRECTORIES="/home/hs/catkin_ws2/devel/share/common-lisp"
declare -x ROS_DISTRO="melodic"
declare -x ROS_ETC_DIR="/opt/ros/melodic/etc/ros"
declare -x ROS_HOSTNAME="127.0.0.1"
declare -x ROS_MASTER_URI="http://127.0.0.1:11311"
declare -x ROS_PACKAGE_PATH="/home/hs/catkin_ws2/src:/opt/ros/melodic/share"
declare -x ROS_PYTHON_VERSION="2"
declare -x ROS_ROOT="/opt/ros/melodic/share/ros"
declare -x ROS_VERSION="1"
it is $ echo $PYTHONPATH
hs@hs-800G5H-800G5S:~/catkin_ws2/src/motor$ echo $PYTHONPATH
/home/hs/catkin_ws2/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages
it is python -> sys.path
hs@hs-800G5H-800G5S:~/catkin_ws2/src/motor$ python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0 ...
In the future, when you post CMakeLists.txt and/or package.xml files (which can be very helpful!), please don't include line numbers, and take the effort to remove all of the bolierplate comments. The files are really long and contain a bunch of unnecessary information as you've written it.
I just edited your question to reflect the above suggestions. Hope that helps!
First step would be to verify that you've actually run
catkin_make
and to just see if the messages were actually generated into C++ headers and Python classes. So, re-runcatkin_make
in the root of your workspace. You should actually see lines in thecatkin_make
output like:Generating Python from MSG motor/motor
. Then you could check that the auto-generated Python files actually exist. They should be in/home/hs/catkin_ws2/devel/lib/python2.7/dist-packages/motor
. Does that directory exist? Is there an__init__.py
file there and a directory calledmsg
?Also, FWIW, it looks to me like you are missing a
CATKIN_DEPENDS
onmessage_runtime
in yourcatkin_package
macroFirst of all, thanks for all of your advice. It was my first post, so I think I was unskilled at posting. I did
catkin_make
. Also, I can check the file, __init__.py and msg. Look at here.hs@hs-800G5H-800G5S:~/catkin_ws2/devel/lib/python2.7/dist-packages/motor$ ls __init__.py __init__.pyc msg hs@hs-800G5H-800G5S:~/catkin_ws2/devel/lib/python2.7/dist-packages/motor$ ls msg __init__.py __init__.pyc _motor.py _motor.pyc _my_motor.py _my_motor.pyc
And just a moment ago, I added
message_runtime
in CMakeLists.txt. So, the file was edited like this.catkin_package(CATKIN_DEPENDS roscpp rospy std_msgs message_runtime)
but it doesn't work....Additionally, I made another package named 'hello' for testing. All of the source code is perfectly same with the 'motor' package except the package name. And it use custom message defined in the 'motor' package('hello' package is differs only package name from the 'motor' package. it uses motor package's custom message(motor) same with 'motor' package.). But the python file in the 'hello' package works without problem... I don't know what is different.
In contrast, in 'motor' package, 'motor' package can import custom message which is defined in the 'hello' package(the message contents is same). It can be executed. Also, 'hello' package can import custom message defined in the 'hello' package.