Hello @danimoura53 ,
Let’s go by parts. First of all, you need to launch the navigation nodes. You can do this with the following commands:
source ros2_ws/install/setup.bash
ros2 launch nav2_course nav2_complete.launch.py
Next, in order to start navigation, you need to provide an initial Pose for the robot in RViz. You can do this from RViz, using the 2D Pose Estimate tool. See below GIF:
Check where the robot is in your simulation, and set this Pose in the map in RViz. After you do this, you will see all the costmaps appear in RVIZ. Now you are ready to navigate.
At this point, you can already interact with the navigation nodes from your own programs. Let’s now fix a couple problems you have in your code.
- First of all, update your setup.py file, in the entry_points section, to add a comma separating the 2 executables that you are generating, like this:
entry_points={
'console_scripts': [
'clicked_point_sub = exercise44.clicked_point_sub:main',
'goal_pose_pub = exercise44.goal_pose_pub:main'
],
},
-
First, you don’t have the constructor for your class (def __init__(self)
), as you can see it is in the other script.
-
Second, the indentation of the callback()
function is wrong. It should be indented INSIDE the class Publisher
(indentation in Python is very important)
-
Finally, you are also missing the import of the PoseStamped
message. Putting all this together, your code should be something like this:
import sys
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import PoseStamped
class Publisher(Node):
def __init__(self):
super().__init__('goal_pose_pub_node')
self.publisher_ = self.create_publisher(PoseStamped, 'goal_pose', 1)
timer_period = 0.5 #Seconds
self.i = 0.0
self.timer_ = self.create_timer(timer_period, self.callback)
def callback(self):
msg = PoseStamped()
msg.header.frame_id = 'map'
msg.pose.position.x = 1.9
msg.pose.position.y = -0.5
msg.pose.orientation.w = 0.0
self.get_logger().info('Publishing Goal Position \n X= 2.2 \n Y=0.0 \n W = 1.0')
self.publisher_.publish(msg)
def main(args=None):
rclpy.init(args=args)
publisher = Publisher()
rclpy.spin_once(publisher)
publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
You can also find this code in the Solutions in the notebook.
Ok, now you can compile your workspace:
cd ~/ros2_ws
colcon build
source install/setup.bash
After compiling, you can then run your programs. For instance, you can launch the goal publisher, which will send a goal to the robot:
ros2 run exercise44 goal_pose_pub
You should see your robot start navigating to the goal position provided in the code (x=1.9, y=-0.5
).
You can also run the subscriber code:
ros2 run exercise44 clicked_point_sub
With this 2nd program, you can click somewhere in the map in RViz using the Publish Point option:
and you will get the coordinates of the map in the output of your program:
Analyzing the situation, I don’t think that the problem is that you are dumb as you say. I think that the problem is that you are not ready for doing this course. It’s impossible to try to learn something more complex without the proper base. I strongly recommend that you first complete the following courses:
- Python3 for robotics (you will learn how to program in Python)
- ROS2 Basics in Python (you will get the basis of ROS2 and practice some Python)
Best,