ImportError: No module named robot_messages.msg
When I run this code I get :
ImportError: No module named robot_messages.msg
This is the code that is written on this tutorial: https://www.youtube.com/watch?v=DLVyc9hOvk8 and the person is able to run it at 27:50 min mark, but I am not.
I have no idea what could be the problem. I made two packages. One by the name locationmonitor and other by the name robotmessages. In the robot_messages I made file LandmarkDistance.msg which looks like this:
string name # Name of the landmark
float64 location # distance from landmark in meters
I tried searching for the forum and making a seperate package just for the messages, but I just don't know what the problem could be.
#!/usr/bin/env python
import math
import rospy
from nav_msgs.msg import Odometry
from robot_messages.msg import LandmarkDistance
def distance(x1,y1,x2,y2):
xd=x1-x2
yd=y1-y2
return math.sqrt(xd*xd+yd*yd)
class LandmarkMonitor(object):
def __init__(self, pub, landmarks):
self._pub=pub
self._landmarks= landmarks
def callback(self,msg):
x=msg.pose.pose.position.x
y=msg.pose.pose.position.y
z=msg.pose.pose.position.z
closest_name= None
closest_distance= None
for l_name, l_x, l_y in self._landmarks:
dist=distance(x,y,l_x,l_y)
if closest_distance is None or dist < closest_distance:
closest_name = l_name
closest_distance = dist
ld= LandmarkDistance()
ld.name= closest_name
ld.distance= closest_distance
self._pub.publish(ld)
rospy.loginfo('closest: {} and the distance is {}' .format(closest_name,closest_distance))
def main():
rospy.init_node('location_monitor')
landmarks= []
landmarks.append(("Cube", 0.31,-0.99))
landmarks.append(("Dumpster", 0.11,-2.42))
landmarks.append(("Cylinder", -1.14,-2.88))
landmarks.append(("Barrier", -2.59,-0.83))
landmarks.append(("Bookshelf", -0.09,0.53))
pub= rospy.Publisher('closest_landmark', LandmarkDistance)
monitor= LandmarkMonitor(pub, landmarks)
rospy.Subscriber("/odom", Odometry, monitor.callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
main()
Asked by AkaTomo93 on 2016-10-28 16:27:08 UTC
Answers
Make sure to add generate_messages() to your CMakeLists.txt of package,This will solve your error:)
generate_messages( DEPENDENCIES std_msgs )
then re-run catkin_make and then resource setup.bash
Asked by Samyak Jain on 2023-04-20 09:02:55 UTC
Comments
Just to make sure:
catkin_make
in the workspace root (not insrc
)catkin_make
, you ransource /path/to/catkin_ws/devel/setup.bash
And then
rosrun location_monitor ..
results in that error?Asked by gvdhoorn on 2016-10-29 04:00:35 UTC
First, make sure your msg being successfully built by doing "rosmsg show robot_messages/Landmark....". Please post the output of that command here
Asked by DavidN on 2016-10-29 05:09:02 UTC
Thank you for helping me out guys. I'm actually not sure what I did to fix this. I just woke up the next morning ran the code and it was working. I'm really not sure what I did but thank you for trying to help anyway :)
Asked by AkaTomo93 on 2016-10-29 09:15:03 UTC