custom messge usage in python
i wrote a smal python node
#!/usr/bin/env python3
import rospy
from ros_agv.msg import MotorStates
from ros_agv.msg import MotorCmds
from ros_agv.msg import MotorState
from ros_agv.msg import MotorCmd
rospy.init_node('motor')
pub = rospy.Publisher('motorStates' , MotorStates)
rate = rospy.Rate(50)
while not rospy.is_shutdown():
msg = MotorStates
motor1 = MotorState
msg.state = [motor1,motor1,motor1,motor1]
pub.publish(msg)
rate.sleep()
package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
cmakelist.txt
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
message_generation
)
add_message_files(
FILES
MotorCmd.msg
MotorState.msg
MotorCmds.msg
MotorStates.msg
)
generate_messages(
DEPENDENCIES
std_msgs # Or other packages containing msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime
# INCLUDE_DIRS include
# LIBRARIES ros_agv
CATKIN_DEPENDS rospy
# DEPENDS system_lib
)
the output of ~/ws$ rosmsg list | grep ros_agv
is
ros_agv/MotorCmd
ros_agv/MotorCmds
ros_agv/MotorState
ros_agv/MotorStates
when i try to start the node in the same terminal rosrun ros_agv motor.py
i get the output:
Traceback (most recent call last):
File "/home/anton/ws/src/ros_agv/script/motor.py", line 4, in <module>
from ros_agv.msg import MotorStates
ImportError: cannot import name 'MotorStates' from 'ros_agv.msg' (unknown location)
Hi @duck-development have you include in the CMakeLists.txt the
message_generation
andmessage_runtime
dependencies? Also have are you using thegenerate_messages
directive? Just as said here.Yes i did otherwise this message would not be listed in rosmsg, I add the pgk.xml and cmake file to the quetion
Mm, that is weird. Maybe is something related with the workspace set up or the fact that you are using
python3
and the interpreter may not be properly set up.