我在python中创建了一个ros2节点,它在
rrt_trajectory_JERK.csv
中调用数据。该文件保存在 ros2ws/src/foo_package/resource/data/
文件夹中,但尽管使用这些辅助函数读取数据,节点仍找不到脚本:
@staticmethod
def get_file_location(filename, location='data'):
directory_path = os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), location)
os.makedirs(directory_path, exist_ok=True)
return os.path.join(directory_path, filename)
def read_data(filename, order=-1, dimensions=-1):
filename = get_file_location(filename, 'resource/data')
with open(filename, 'r') as file:
return read_file(file, order, dimensions)
@staticmethod
def read_file(file, order=-1, dimensions=-1):
reader = csv.reader(file)
data_list = list(reader)
data_array = np.array(data_list).astype(np.float64)
return data_array[:, order:order+dimensions] if order > -1 and dimensions > -1 else data_array
辅助函数与 ros 节点位于同一目录中。
这是我收到的错误消息:
/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
OIAC controller
[Errno 2] No such file or directory: '~/ros2ws/install/ros_package/lib/python3.10/site-packages/resource/data/rrt_trajectory_JERK.csv'
我的节点正在寻找
~/ros2ws/install/ros_package/lib/python3.10/site-packages/resource/data/
中的脚本而不是 ~/ros2ws/src/foo_package/resource/data/
中导致的问题
为什么会出现这种情况,有解决办法吗?
这用于将 src 文件与资源分开。使用此方法还有其他原因。如果您确实想使用 src 文件夹中的数据,您可以在脚本中添加类似的内容:
import os
cwd = os.getcwd()
file_path = os.path.join(cwd,'src/foo_package/resource/data/rrt_trajectory_JERK.csv')
建议的方法是使用 setup.py 将目录安装在共享文件夹中,然后访问它。