# ROS Day1

参考自ROS Wiki

在UnderstandingTopics这一章遇到了问题,wiki中为了使用动态图显示系统中正在发生的事情,运行rqt_graph,然而我在运行命令

rosrun rqt_graph rqt_graph 

时报错

先是没有rospkg,这个在Anaconda目录下的“apt-get 和conda install将包安装到哪去了“中提过了。后来又遇到No module named catkin_pkg.packages 查到一篇博客说:

Make sure that you have installed " catkin_pkg " , it is up to date and on the PYTHONPATH.

于是我用

rospack find catkin_pkg

没找到,想了一下是在import时报错,所以catkin_pkg应该是个python的模块,于是pip install catkin_pkg,安装成功。 但是结合前面提到的“apt-get 和conda install将包安装到哪去了“,使用pip和conda install效果是一样的,我再次运行pip install catkin_pkg, 提示已经安装:catkin_pkg in ./anaconda3/lib/python3.8/site-packages(0.4.23)

# 有一个疑问

结合之前那篇文章中的内容,有一个疑问, 假如我在anaconda下create了3个环境,第一个是python2.7的,叫py27; 第二个和第三个都是python3.8的分别叫py38和test,假设我在py38下使用pip install安装了包,那么test下是否能找到这个包?

catkin_pkg in ./anaconda3/lib/python3.8/site-packages(0.4.23)

并没有对py38和test进行区分,是否在import时会有差异呢?

pip应当是对应虚拟环境的python版本,之所以会出现上面的问题是因为下面的bug,当解决了这个bug后,可以通过 which pip 命令查看使用的是哪个虚拟环境下的pip

# bug

遇到了新的问题,使用conda create 创建新的环境后,例如

conda create -n py27 python=2.7

成功执行了,而且输出的结果也显示

The Following NEW packages will be INSTALLED:
…
python pkgs/main/linux-64/python-2.7.18-ha1903f6_2
…

结果使用 python --version 发现版本还是3.8.8

解决方案:

使用命令 conda deactivate 退出base环境,然后重新激活环境 conda activate env_name

参考https://stackoverflow.com/questions/36733179/why-conda-cannot-call-correct-python-version-after-activating-the-environment

# 顺带一提

我在pip时使用的是py27,环境是python2.7。但是第二次pip显示已安装的路径是python3.8的。catkin_pkg in ./anaconda3/lib/python3.8/site-packages(0.4.23),通过pip -V可以看到pip的版本以及它来自哪个python版本。

如何切换pip?假设我当前使用python2.7的环境py27,我想用pip安装应该怎么做

网上说可以修改site.py文件下的USER_SITE和USER_BASE分别来修改下载的package存放路径和python,pip等模块的存放地址。


文章里也说了,PYTHONPATH是python解释器搜索包的路径,也就是说需要确保包的路径在环境变量PYTHONPATH中才行。而我执行echo $PYTHONPATH发现返回结果是 /opt/ros/kinetic/lib/python2.7/dist-packages

在之前那篇文章中也有过向环境变量添加路径的操作,但是那次是apt-get下载的模块,而且下载的也是python2.7的模块,所以现在得解决python2.7使用pip的问题

# No module named typing

https://stackoverflow.com/questions/67278017/pip-command-line-importerror-no-module-named-typing

解决方法

wget  https://bootstrap.pypa.io/pip/2.7/get-pip.py

python get-pip.py

相当于重新安装了python2.7的pip,再使用

pip install typing

即可

# 后面还会遇到

No module named catkin_pkg.packages
pip install catkin_pkg
No module named PySide2.QtCore
pip install PySide2
Qt.qpa.plugin:Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized.Reinstalling the application may fix this problem.
sudo apt-get install libxcb-xinerama0

当运行 rosrun rqt_graph rqt_graph 时

PluginManager._discover() force discovery of plugins
RospkgPluginProvider._find_plugins() crawling for plugins of type 'qt_gui'
Segmentation fault(core dumped)

参考: https://answers.ros.org/question/260316/rqt-rqt_graph-rqt_plot-doest-work-segmentation-fault-core-dumped/

I had the same problem. When I checked the Ubuntu crash report I realized that the error was coming from Qt library. I had a software package which was referencing QT libs from a different path. Once I setup the environment variable LD_LIBRARY_PATH=<qt home="" directory="">/5.7/gcc_64/lib I was able to run rqt without segfault. Hope this helps.

提到的环境变量LD_LIBRARY_PATH,似乎是没有安装Qt库的问题

使用命令 qmake -v 查看Qt版本,显示:

Qmake version 3.1
Using Qt version 5.9.7 in /home/winka/anaconda3/lib

看来是之前已经安装了Qt

echo $LD_LIBRARY_PATH
/opt/ros/kinetic/lib:/opt/kenetic/lib/x86_64-linux-gnu

并没有这个路径

那么现在要将Qt的路径添加到LD_LIBRARY_PATH下

执行命令

export LD_LIBRARY_PATH=/home/winka/anaconda3/lib:$LD_LIBRARY_PATH

注意等号前后不要加空格!!

直接运行命令 ros_graph即可出现图形界面