NexDroid与 ROS 结合

2021-12-29

视频

我们制作了NexDroid系统连接到ROS的视频教程。

NexDroid与ROS结合-连接和使用

一、初始化 ROS 操作环境

·ROS 系统的安装

包括完整的安装步骤和出现错误的解决方式。

1.设置sources.list

设置电脑以安装来自packages.ros.org的软件。

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

速度过慢配置清华源镜像

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

PS:下载失败,尝试对 ubuntu 配置阿里源、清华源等。下面提供一个阿里源的配置方案。

  • 备份源列表

    Ubuntu配置的默认源并不是国内的服务器,下载更新软件都比较慢。首先备份源列表文件sources.list

    sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
  • 打开sources.list文件修改

    sudo gedit /etc/apt/sources.list

    编辑/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-get update
    sudo apt-get upgrade
    sudo apt-get install build-essential

2.设置密钥

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

3.安装

sudo apt update
sudo apt install ros-melodic-desktop-full

4.初始化rosdep

初始化失败,按照步骤 6 修改,初始化完成再去配置环境变量

sudo rosdep init
rosdep update

5.设置环境变量

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

6.rosdeq出错

因为服务器的原因,会遇到下面的问题,需要对配置文件进行修改

sudo rosdep init
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down.
rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
ERROR: error loading sources list:
('The read operation timed out')
  • 打开包含资源下载函数的文件:
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py

添加

url="https://ghproxy.com/"+url

img
预览

  • 修改/usr/lib/python2.7/dist-packages/rosdistro/__init__.py文件中的DEFAULT_INDEX_URL

    sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
    DEFAULT_INDEX_URL = 'https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml'

    img
    预览

  • 修改其余(4 个)文件中的地址,在地址 https://raw.githubusercontent.com/ 前添加https://ghproxy.com/

    $ sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/gbpdistro_support.py
    //修改第36行的地址
    $ sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py 72行
    //修改第72行
    $ sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
    //修改第39行
    $ sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/manifest_provider/github.py
    //修改第68行、119行
  • 解决 Hit 第五个地址的报错

    sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/gbpdistro_support.py
    //在第204行添加如下代码(即在该函数块下的第一行处)
    gbpdistro_url = "https://ghproxy.com/" + gbpdistro_url
    //注意,原网址中代理地址的双引号是中文,直接粘贴复制会报字符识别错误
  • 如果依然不能解决 Hit 第五个地址的报错,则修改

    sudo gedit /etc/resolv.conf

    将原有的 nameserver 这一行注释,并添加以下两行:

    nameserver 8.8.8.8 #google域名服务器
    nameserver 8.8.4.4 #google域名服务器

    保存退出,执行

    sudo  apt-get update

    再执行

    sudo rosdep init
    ERROR: default sources list file already exists:
    /etc/ros/rosdep/sources.list.d/20-default.list
    Please delete if you wish to re-initialize

    解决办法:

    执行以下命令,删除已经存在的初始化文件:

    sudo rm /etc/ros/rosdep/sources.list.d/20-default.list

    然后再重新运行

    sudo rosdep init

·ROS 功能包的安装

sudo apt-get install ros-melodic-moveit
sudo apt-get install ros-melodic-joint-state-publisher
sudo apt-get install ros-melodic-robot-state-publisher
sudo apt install ros-melodic-gazebo-ros-pkgs
sudo apt-get install ros-melodic-gazebo-ros-control
sudo apt-get install  ros-melodic-joint-trajectory-controller

·ROS 创建工作空间

mkdir -p ~/inexbot/src
cd ~/inexbot/src
catkin_init_workspace
cd ../
catkin_make

修改~/.bashrc文件

gedit ~/.bashrc

在文件末尾添加

source ~/inexbot/devel/setup.bash

把所有 inexbot 的功能包复制到inexbot/src目录下

二、对moveit和机器人的进行配置

​ 连接之前,要调整 ros 与机器人之间的各轴的转动方向关节角度上下限值关节转动最大的速度和加速度,使它们统一。

​ 在示教器的设置界面上可以对机器人进行设置。

ros可以修改 xacro 文件中的各个关节角的axislimit标签下的参数,使得模型和机器人的转动方向和转动限值相统一。修改qj_config/config/joint_limits 中的max_velocitymax_acceleration,改变 ros 模型的关节转动最大的速度和加速度

​ 如果转动方向、关节角度上下限值、关节转动最大的速度和加速度不统一,运行过程示教器会报角度超限伺服警报的错误

......
<joint
    name="robot_joint2"        <!--关节名称-->
    type="revolute">
    <origin
      xyz="0 0.18 -0.0525"
      rpy="0 0 0" />
    <parent
      link="robot_Link1" />
    <child
      link="robot_Link2" />
    <axis
      xyz="1 0 0" />    <!--取1为正方向,-1为负方向-->
    <limit
      lower="-3.14"
      upper="3.14"
      effort="100"
      velocity="1" />  <!--lower是关节转角下限,upper是上限,ros为弧度制,示教器为角度制-->
    <dynamics damping="0" friction="1"/>
  </joint>
......
joint_limits:
   robot_joint1:
    has_velocity_limits: true
    max_velocity: 1
    has_acceleration_limits: ture
    max_acceleration: 1
<!--max_velocity关节转动的最大速度,max_acceleration关节转动的最大加速度,单位是弧度制-->
......

三、ROS 与控制器通信

以六轴机器人为例,介绍 Ros 如何与 NRC 机器人控制器连接

1.启动控制器

2.启动 Rviz 和 Gazebo 的联合仿真环境。

roslaunch inexbot_qj robot_bringup.launch

3.添加机器人模型

点击 displays 右下角的 Add ,选择 Robot Model 添加机器人模型。

img
预览
img
预览

4.启动 ros node 与控制器连接

rosrun inexbot_service rostopic_joint5

四、通过 moveit 控制机器人

1.设置 Goal state 控制

img
预览

​ 点击 plan 产生规划轨迹,底部会显示计算用时 Time。

img
预览

​ 点击 execute 执行,也可以点击 plan&execute 规划完立即执行。

2.拖拽规划器控制

​ 拖拽规划器前端的绿色小球,光标放在小球上时,下方出现一个三维坐标,拖拽到目标点,点击 plan 产生规划轨迹,点击 execute 执行,也可以点击 plan&execute 规划完立即执行。

img
预览

五、C++编程实现对机器人的控制

1.创建 ros 功能包,存放程序文件

cd ~/inexbot/src
catkin_create_pkg inexbot_code std_msgs rospy roscpp
cd ../
catkin_make

2.关节空间规划

创建moveit_joint_demo.c

将文件放置到~/inexbot/src/inexbot_code/src路径下

//moveit_joint_demo.c
#include <ros/ros.h>
#include <moveit/move_group_interface/move_group_interface.h>

int main(int argc, char **argv)
{
    //ros的初始化
    ros::init(argc, argv, "moveit_joint_demo");
    ros::AsyncSpinner spinner(1);
    spinner.start();

    //manipulator是通过moveit设置的规划组
    moveit::planning_interface::MoveGroupInterface arm("manipulator");

    //容许误差
    arm.setGoalJointTolerance(0.001);
    //最大加速度
    arm.setMaxAccelerationScalingFactor(0.2);
    //最大速度
    arm.setMaxVelocityScalingFactor(0.2);

    // 设置goal_state,控制机械臂先回到初始化位置
    //home为moveit预设的位置
    arm.setNamedTarget("home");
    //运行
    arm.move();
    sleep(1);
    //设置关节空间的六个轴的角度
    double targetPose[6] = {0.391410, -0.676384, -0.376217, 0.0, 1.052834, 0.454125};
    std::vector<double> joint_group_positions(6);
    joint_group_positions[0] = targetPose[0];
    joint_group_positions[1] = targetPose[1];
    joint_group_positions[2] = targetPose[2];
    joint_group_positions[3] = targetPose[3];
    joint_group_positions[4] = targetPose[4];
    joint_group_positions[5] = targetPose[5];

    arm.setJointValueTarget(joint_group_positions);
    arm.move();
    sleep(1);

    // 控制机械臂先回到初始化位置
    arm.setNamedTarget("home");
    arm.move();
    sleep(1);

    ros::shutdown();

    return 0;
}

修改CMakeLists.txt文件

在 install 标签上面添加

add_executable(moveit_joint_demo src/moveit_joint_demo.cpp)
target_link_libraries(moveit_joint_demo ${catkin_LIBRARIES})
#############
## Install ##
#############

编译代码文件

cd ~/inexbot
catkin_make

运行

rosrun inexbot_code moveit_joint_demo
如果本文有错误请向我们反馈,我们很珍惜您的意见或建议。