使用 Ubuntu18.04 安装 ROS Melodic
$ sudo nano /etc/apt/sources.list修改该文件,将该文件中原来的内容全部注释,添加如下内容(此处以阿里源作为示例)
# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse然后更新一下
sudo apt update && sudo apt upgrade参考官网教程安装即可
官网教程
rosdep update 报错
输入
sudo gedit /etc/hosts在最后一行加入
151.101.84.133 raw.githubusercontent.comsudo rosdep init 报错
sudo: rosdep:找不到命令原因:python-rosdep 这个包没有装。
解决办法:安装 python-rosdep
sudo apt-get install python-rosdep其余坑待填
mkdir ros_ws
cd ros_wsmkdir -p first_ws/src创建一个
first_ws的工作空间,并且在其中创建了src目录first_ws就是工作空间的名称
cd ..
catkin_make来到创建的工作空间目录下,调用ros的名命令
catkin_make,将工作空间进行编译。 编译后,会得到工作空间的文件结构,build,devel,CMakeLists.txt都会自动生成catkin_make是ROS的编译工具,我们会经常用到。
在workspace的src目录下
catkin_create_pkg hello_ros rospy roscpp rosmsg
catkin_create_pkg是创建package的命令。第一个参数hello_ros是指创建的package名称,后面的参数roscpp,rospy,rosmsg是指当前创建的这个package需要提供哪些环境依赖。roscpp是对c++的一种依赖,有了它就可以用c++开发ros程序rospy是对python的一种依赖,有了它就可以用python开发ros程序rosmsg是Node间通讯的消息协议依赖,有了它就可以让节点间进行消息通讯 注意:通常在开发过程中,这三个依赖都是必备的。
见另一篇文章:Ubuntu自用软件记录
cd ros_ws/first_ws
source devel/setup.bash此操作非常重要。 devel目录中的setup.bash是用于开发环境中,方便找到开发依赖的。
cd ~/devtools/clion-2019.3.2/bin/
./clion.sh依次打开 File -> Setting -> Editor -> File and Code Templates -> Python Script,添加
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : ${DATE} ${TIME}
# @Author :
# @File : ${NAME}.py
# @Software: ${PRODUCT_NAME}
#!/usr/bin/env python让系统知道当前是可执行的python脚本,避免系统当作shell脚本执行。# coding:utf-8解决编码问题
依次打开 File -> Setting -> Build,Execution,Deployment -> Python Interprete -> Add
选择 Virtualenv Environment -> Existing environment ,选择以下路径:

需要注意的是,目前ROS Melodic版本还不支持python3,要等到ROS N版才会支持。 因此,我们选择环境的时候选择python2.x版本。
在hello_ros的目录中创建scripts目录,用于写python代码。
依次打开 File -> Setting -> Build,Execution,Deployment -> CMake -> Generation path,输入
/tmp/hello_ros/cmake-build-debug在scripts目录下新建hello_node.py文件,代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 5/9/20 7:34 PM
# @Author : Chenan_Wang
# @File : hello_node.py
# @Software: CLion
# ROS 的 Python 环境
import rospy
if __name__ == '__main__':
# ros的节点,需要传入节点的名称 -- 在rosmaster中注册
rospy.init_node('itcast_node')
# 频率操作Rate,可以按照一定的频率操作循环执行。
# (hz赫兹) 10 --1秒执行10次
rate = rospy.Rate(1)
while not rospy.is_shutdown():
print 'hello ros python'
rate.sleep()
# 阻塞线程spin,可以阻塞当前的线程
rospy.spin()在文件夹中打开hello_node.py文件,右键 Properties -> Permissions -> Execute 后选中
启动命令行,来到工作空间目录下,编译项目
cd ros_ws/first_ws
catkin_make
source devel/setup.bash
rosrun hello_ros hello_node.pyCtrl + Alt + L 格式化Ctrl + Alt + V 生成局部变量Ctrl + /注释Ctrl + Y 删除当前行Ctrl + Shift + Enter 下一行Ctrl + Shift + Up 上移一行Ctrl + Shift + Down 下移一行Home 光标到行首End 光标到行末Alter + Enter 万能提示