ros-2

前奏


root@huirou:~# free -h
              total        used        free      shared  buff/cache   available
Mem:           5.8G        1.0G        3.1G        5.8M        1.7G        4.6G
Swap:          2.0G          0B        2.0G


root@huirou:~# fdisk -l | grep -w '/dev/sda'
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors


root@huirou:~# hostname -I
192.168.1.102 


# 修改固定IP
~# cp /etc/netplan/01-network-manager-all.yaml{,-`date +'%Y%m%d%H%M'`}
~# vim /etc/netplan/01-network-manager-all.yaml
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens33:
      addresses: [192.168.92.129/24]
      gateway4: 192.168.92.2
      dhcp4: false
      optional: true
      nameservers:
        addresses: [192.168.92.2, 114.114.114.114]


# 重启网络服务使配置生效
~# sudo netplan apply


# 安装ssh
root@huirou:~# apt install -y openssh-server
root@huirou:~# systemctl start sshd
root@huirou:~# systemctl enable sshd


# 配置源
root@huirou:~# mv /etc/apt/sources.list{,-bak}
root@huirou:~# vi /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

# 清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
 
# 中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse


root@huirou:~# sudo apt update 

安装ROS

~#   是root用户
~$   是普通用户
使用普通用户执行命令前必须在命令前sudo,使用root用户执行命令时则不用添加,如加上也可能执行


# 系统用户
muzhi用户密码: aaaaaa
root用户密码: aaaaaa


Ubuntu 16.04对应ROS的 Kinetic 版本
Ubuntu 18.04对应ROS的 Melodic 版本
XTDrone平台目前支持这两个发行版


# 添加ROS软件源
~# sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
# 添加密钥
~# sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
# 安装ROS
~# sudo apt update                  ===> 这步如不执行会出现下一步安装不成功!!!

==== 参考项 ==========
apt search ros-melodic      # 搜索安装包
=====================

~# sudo apt install -y ros-melodic-desktop-full


# 设置环境变量(安装了多个版本则需要参考上述Ubuntu官方安装文档)
~# echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
~# source ~/.bashrc

# 安装依赖
~# sudo apt install -y python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential

# 初始化rosdep
~# sudo apt install -y python-rosdep
~# sudo rosdep init
ERROR: Rosdep experienced an error: ('The read operation timed out',)
Please go to the rosdep page [1] and file a bug report with the stack trace below.
[1] : http://www.ros.org/wiki/rosdep

rosdep version: 0.21.0

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/rosdep2/main.py", line 146, in rosdep_main
    exit_code = _rosdep_main(args)
  File "/usr/lib/python2.7/dist-packages/rosdep2/main.py", line 441, in _rosdep_main
    return _no_args_handler(command, parser, options, args)
  File "/usr/lib/python2.7/dist-packages/rosdep2/main.py", line 450, in _no_args_handler
    return command_handlers[command](options)
  File "/usr/lib/python2.7/dist-packages/rosdep2/main.py", line 594, in command_init
    data = download_default_sources_list()
  File "/usr/lib/python2.7/dist-packages/rosdep2/sources_list.py", line 340, in download_default_sources_list
    f = urlopen(url, timeout=DOWNLOAD_TIMEOUT)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1248, in https_open
    context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1208, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1178, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 452, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 408, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
  File "/usr/lib/python2.7/ssl.py", line 754, in recv
    return self.read(buflen)
  File "/usr/lib/python2.7/ssl.py", line 641, in read
    v = self._sslobj.read(len)
SSLError: ('The read operation timed out',)




解决:
接下来就是克隆项目,克隆项目前需要将ubuntu的公钥传到github上,现在github打不开
~# ssh-keygen -t rsa -P ''        ===> 一路回车
~# cat ~/.ssh/id_rsa.pub          ===> 查看公钥
注: 将公钥复制到github即可


~# apt install -y git
~# git clone --depth=1 https://github.com/ros/rosdistro.git
~# mkdir -p /etc/ros/rosdep/sources.list.d/
~# cp  rosdistro/rosdep/sources.list.d/20-default.list   /etc/ros/rosdep/sources.list.d/
~# mv /etc/ros/rosdep/sources.list.d/20-default.list{,-bak}
~# vim /etc/ros/rosdep/sources.list.d/20-default.list         ====> 该条是命令,往下是20-default.list这个文件中的内容
# os-specific listings first
yaml file:///root/rosdistro/rosdep/osx-homebrew.yaml osx
# generic
yaml file:///root/rosdistro/rosdep/base.yaml
yaml file:///root/rosdistro/rosdep/python.yaml
yaml file:///root/rosdistro/rosdep/ruby.yaml
gbpdistro file:///root/rosdistro/releases/fuerte.yaml fuerte


~# cp /usr/lib/python2.7/dist-packages/rosdep2/gbpdistro_support.py{,-bak}
~# vim  /usr/lib/python2.7/dist-packages/rosdep2/gbpdistro_support.py
FUERTE_GBPDISTRO_URL = 'file:///root/rosdistro/releases/fuerte.yaml'


~# cp /usr/lib/python2.7/dist-packages/rosdep2/rep3.py{,-bak}
~# vim /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
REP3_TARGETS_URL = 'file:///root/rosdistro/releases/targets.yaml'


~# cp /usr/lib/python2.7/dist-packages/rosdistro/__init__.py{,-bak}
~# vim /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
DEFAULT_INDEX_URL = 'file:///root/rosdistro/index-v4.yaml'


#更新本地 rosdep 数据库
~# sudo rosdep fix-permissions
~# rosdep update
注: 这里要根据自己的网速而定,我重复执行了20来次才正常

# 查看ros版本
~# rosversion -d
melodic      				===> 本次安装的是melodic版本


运行小海龟示例
~# roscore         ===> 在图形化界面中运行该命令,运行后该不要关闭该窗口

# 再在图形化界面中使用root用户新开一个窗口(第二个窗口)
用途: turtlesim_node节点和turtle_teleop_key节点之间是通过一个ROS话题来互相通信
语法:rosrun [package_name] [node_name]
实操
~# rosrun  turtlesim  turtlesim_node

~/rosdistro# rosnode list
返回如下信息:
/rosout
/turtlesim

# rqt_graph命令来显示当前运行的节点和话题(可选项)
~# rqt_graph

# 再在图形化界面中使用root用户再新开一个窗口(第三个窗口)
用途:turtle_teleop_key在一个话题上发布按键输入消息,而turtlesim则订阅该话题以接收该消息
语法:rosrun  [package_name]   turtle_teleop_key
实操
~# rosrun  turtlesim  turtle_teleop_key

注:执行该命令时不能在小乌龟全屏下执行,也不能在终端全屏下执行,专业用语叫 "光标处于激活状态",
也就是既能显示终端也能看到小乌龟界面,这2个画面都能看到时  在终端中(非小乌龟界面)  
通过方向键即可控制小海龟的移动,如下图所示

安装Gazebo并与ROS连接

以下操作需要在上述都完成后进行

上面安装ros时是安装的 ros-melodic-desktop-full 版本,ros-melodic-desktop-full因为安装的是全版本(包含Gazebo),所以这里安装Gazebo时要

root@muzhi:~# apt list|grep gazebo-*
root@muzhi:~# apt install -y gazebo9 libgazebo9-dev

# Gazebo与ROS连接
(1) 安装ROS-Gazebo接口
root@muzhi:~# sudo apt install -y ros-melodic-gazebo-ros-pkgs  ros-melodic-gazebo-msgs  ros-melodic-gazebo-plugins  ros-melodic-gazebo-ros-control

(2) 验证是否连接成功
# 图形化界面上新打开第一个窗口输入:
# root@muzhi:~# roscore

图形化界面上新打开第二个窗口输入:
root@muzhi:~# vim ~/.ignition/fuel/config.yaml
把 api.ignitionfuel.org 改成 fuel.ignitionrobotics.org,而后再运行

root@muzhi:~# rosrun gazebo_ros gazebo
报错:
VMware: vmw_ioctl_command error 无效的参数.
Aborted (core dumped)
解决:
右击虚拟机===设置===显示器===去掉3D加速


root@muzhi:~# rosrun gazebo_ros gazebo
再一次报错
VMware: vmw_ioctl_command error 无效的参数.
Aborted (core dumped)

# 解决
https://blog.csdn.net/weixin_43059285/article/details/108012524
~# echo "export SVGA_VGPU10=0" >> ~/.bashrc
~# source ~/.bashrc

~# rosrun gazebo_ros gazebo
如果在上一步骤发现Gazebo打开后长时间停留在启动界面,且显示黑屏(有网格),关掉Gazebo,并依次执行以下语句
~# mkdir -p ~/.gazebo/models && cd ~/.gazebo/models/
~/.gazebo/models# wget http://file.ncnynl.com/ros/gazebo_models.txt
~/.gazebo/models# wget -i gazebo_models.txt
~/.gazebo/models# ls model.tar.g* | xargs -n1 tar xzvf
~/.gazebo/models# killall gzserver
~/.gazebo/models# killall gzclient

~/.gazebo/models# which gzserver gzclient          ===> 查看服务端和客户端的位置


# 再来执行该命令
~/.gazebo/models# rosrun gazebo_ros gazebo

# 如果Gazebo打开界面如下图所示,且可看到终端显示下面代码块中的内容,则说明Gazebo与ROS连接成功!
~/.gazebo/models# rostopic list
/clock
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose


# 退出到家目录
~/.gazebo/models# cd

# 常用命令
~# rostopic list
~# rosnode list
~# rostopic echo <主题>          ===> 主题从上条命令中获取
~# rostopic info <主题>          


posted @ 2022-04-17 11:07  huirou  阅读(57)  评论(0编辑  收藏  举报