当前位置:   article > 正文

ROS入门(ROS安装+编译器配置)详细教程 ROSneotic安装 VScode的ROS开发环境的简单配置_ros neotics

ros neotics

ROS安装 + vscode简单配置

假设你已经安装好了Ubuntu 20.04,并且只有国内网络环境。

换源

  1. Ctrl + Alt + T打开终端

  2. 备份并打开

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    sudo gedit /etc/apt/sources.list
    
    • 1
    • 2
  3. 复制一下内容,替换默认的源

    #添加阿里源
    deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    #添加清华源
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  4. 更新源

    sudo apt-get update
    
    • 1
  5. 如出现错误信息,则输入依次

    sudo apt-get -f install
    sudo apt-get upgrade
    
    • 1
    • 2

安装ROS

  1. 添加 sources.list(设置你的电脑可以从 packages.ros.org 接收软件.)

    sudo sh -c '. /etc/lsb-release && echo "d{2d3b390d-f615-41d0-a830-7bbdcedbd397}eb http://mirrors.ustc.edu.cn/ros/ubuntu/ `lsb_release -cs` 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'
    
    • 1
    • 2
  2. 添加 keys

    sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
    
    • 1
  3. 更新源

    sudo apt-get update
    
    • 1

    image-20220120222109999

  4. 安装ROS Noetic

    sudo apt install ros-noetic-desktop-full
    
    • 1
    • 如果安装过程出现:E: Failed to fetch

      E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/pool/main/r/ros-noetic-topic-tools/ros-noetic-topic-tools_1.15.13-1focal.20210922.182212_amd64.deb  Undetermined Error [IP: 101.6.15.130 80]
      
      • 1

      则输入

      sudo apt-get update --fix-missing 
      sudo apt install ros-noetic-desktop-full
      
      • 1
      • 2
  5. 耐心等待安装完成

  6. 设置系统环境变量

    echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
    source ~/.bashrc
    
    • 1
    • 2

测试

运行海龟仿真

在三个Terminal中分别执行

  • roscore
  • rosrun turtlesim turtlesim_node
  • rosrun turtlesim turtle_teleop_key

image-20220120225040030

成功安装!

配置vscode

  1. vscode官网下载.deb安装包,选择保存文件。

  2. 在安装包所在目录打开终端

  3. 执行sudo dpkg -i 安装包名字.deb

  4. 终端输入code,打开vscode

  5. 安装如图插件image-20220120234244802

  6. 重启vscode

  7. 配置c++ ROS库

  8. mkdir -p ~/catkin_ws/src
    cd ~/catkin_ws/src
    catkin_init_workspace
    cd ..
    catkin_make
    catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
    code .
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  9. 在/src下新建test.cpp

    #include <bits/stdc++
    using namespace std;
    
    #include "ros/ros.h"
    #include "std_msgs/String.h"
    
    int main(int argc, char** argv)
    {
    	ros::init(argc, argv, "talker");
    	ros::NodeHandle n;
    	ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
    	ros::Rate loop_rate(10);
    	int count = 0;
    	while(ros::ok())
    	{
    		std_msgs::String msg;
    		std::stringstream ss;
    		ss << "hello world " << count;
    		msg.data = ss.str();
    		ROS_INFO("%s", msg.data.c_str());
    		chatter_pub.publish(msg);
    		ros::spinOnce();
    		loop_rate.sleep();
    		count++;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  10. 此时,在#include "ros/ros.h"处有红色波浪线报错。

  11. 在黄色灯泡处点击“Add to”即可。image-20220121000943565

  12. 若无提示,则点击Edit “includePath” setting。并使其自动创建配置文件。image-20220121001438906

  13. 在配置项目中添加

    "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    
    • 1

    image-20220121001617267

  14. 全部保存后重启,红色波浪线消失,可以使用自动补全写代码了。

参考资料:

  1. 使用VScode搭建ROS开发环境
  2. ros项目调试:vscode下配置开发ROS项目
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/776823
推荐阅读
相关标签
  

闽ICP备14008679号