Ask Your Question
0

custom messge usage in python

asked 2020-10-27 05:01:42 -0600

duck-development gravatar image

updated 2020-10-27 05:39:11 -0600

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)
edit retag flag offensive close merge delete

Comments

1

Hi @duck-development have you include in the CMakeLists.txt the message_generation and message_runtime dependencies? Also have are you using the generate_messages directive? Just as said here.

Weasfas gravatar image Weasfas  ( 2020-10-27 05:23:45 -0600 )edit

Yes i did otherwise this message would not be listed in rosmsg, I add the pgk.xml and cmake file to the quetion

duck-development gravatar image duck-development  ( 2020-10-27 05:32:18 -0600 )edit
1

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.

Weasfas gravatar image Weasfas  ( 2020-10-30 05:10:25 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-10-30 14:30:32 -0600

duck-development gravatar image

One reboot of the system did help, it was a vm I never rebooted. Sorry for disturbing @Weasfas.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-10-27 05:01:42 -0600

Seen: 29 times

Last updated: Oct 30