I barely understood this part of the course and I believe many experienced the same. But now my question is that: Why do I need to add this to my code?
import rospkg
rospack = rospkg.RosPack()
This rospack.get_path() works in the same way as $(find name_of_package) in the launch files.
traj = rospack.get_path(‘iri_wam_reproduce_trajectory’) + “/config/get_food.txt”
and where to add it exactly?
From your question, I infer that you are working on the course ROS Basics in 5 Days in Python.
Let me explain what the lines do:
This line basically imports the rospkg class as a module in python, so that you can use the member functions and attributes associated with that module.
This line basically instantiates the RosPack class from the rospkg module. This is done this way because modules must be declared in all-lowercase words, therefore you can’t have camel-cased module names like RosPack as module names.
This line gets the path of the compiled package from the list of all ROS package paths, hence the function name get_path, you are basically getting the path of the specified pakage using rospack module in python. This will yield a path string to the folder, something like /path/to/iri_wam_reproduce_trajectory.
This line simply concatenates the package path with the respective file path suffix. So the outcome would be something like /path/to/iri_wam_reproduce_trajectory/config/get_food.txt. This will get stored in the traj variable.
In summary, you need to add “that” line to reference any pre-built ROS packages inside another ROS package program.