我尝试过这个方法,并发现它是有效的。如果你希望其他人能方便地运行你的代码,你可能要考虑使用相对路径。我是通过 ros2 的 .launch.py 文件实现的。
主要的方法是利用 AppendEnvironmentVariable
和 get_package_share_directory
:
import os
from ament_index_python.packages import get_package_share_directory
from launch.actions.append_environment_variable import AppendEnvironmentVariable
def generate_launch_description():
# Set Gazebo plugin path
append_environment = AppendEnvironmentVariable(
'GAZEBO_PLUGIN_PATH',
os.path.join(os.path.join(get_package_share_directory('pb_rm_simulation'), 'meshes', 'obstacles', 'obstacle_plugin', 'lib'))
)
ld = LaunchDescription()
# Set environment variables
ld.add_action(append_environment)
return ld
然后,你可以方便地使用你自己的 Gazebo 插件,如下所示:
<model name="turtlebot3_drl_obstacle1">
<plugin name="obstacle1" filename="libobstacle1.so"/>
<include>
<pose>1 -2.5 0.4 0 0 0</pose>
<uri>model://obstacles/obstacle1</uri>
</include>
</model>
希望这对你有帮助 😃