如何在 Ros-python 脚本中导入 Webots 控制器

How to import Webots controller in a Ros-python script

我想在 Python 中编写一个控制器并尝试导入 Webots 控制器库:

#!/usr/bin/env python

import rospy
from std_msgs.msg import String
from time import sleep
from controller import Robot

并得到错误

Traceback (most recent call last):
  File "./mine_controller.py", line 6, in <module>
    from controller import Robot
  File "/snap/webots/current/usr/share/webots/lib/controller/python27/controller.py", line 17, in <module>
    _controller = swig_import_helper()
  File "/snap/webots/current/usr/share/webots/lib/controller/python27/controller.py", line 16, in swig_import_helper
    return importlib.import_module('_controller')
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: libCppController.so: cannot open shared object file: No such file or directory

我的环境变量:

PYTHONPATH=/snap/webots/current/usr/share/webots/lib/controller/python27:/home/boand/workspace_ros/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages
LD_LIBRARY_PATH=/home/boand/workspace_ros/devel/lib:/opt/ros/melodic/lib:/snap/webots/current/usr/share/webots/lib

看来你的LD_LIBRARY_PATH不对,/snap/webots/current/usr/share/webots/lib应该换成/snap/webots/current/usr/share/webots/lib/controller