赞
踩
假设你已经安装好了Ubuntu 20.04,并且只有国内网络环境。
Ctrl + Alt + T
打开终端
备份并打开
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo gedit /etc/apt/sources.list
复制一下内容,替换默认的源
#添加阿里源 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
更新源
sudo apt-get update
如出现错误信息,则输入依次
sudo apt-get -f install
sudo apt-get upgrade
添加 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'
添加 keys
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
更新源
sudo apt-get update
安装ROS Noetic
sudo apt install ros-noetic-desktop-full
如果安装过程出现: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]
则输入
sudo apt-get update --fix-missing
sudo apt install ros-noetic-desktop-full
耐心等待安装完成
设置系统环境变量
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
运行海龟仿真
在三个Terminal中分别执行
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
成功安装!
在vscode官网下载.deb安装包,选择保存文件。
在安装包所在目录打开终端
执行sudo dpkg -i 安装包名字.deb
终端输入code
,打开vscode
安装如图插件
重启vscode
配置c++ ROS库
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
code .
在/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; }
此时,在#include "ros/ros.h"
处有红色波浪线报错。
在黄色灯泡处点击“Add to”即可。
若无提示,则点击Edit “includePath” setting
。并使其自动创建配置文件。
在配置项目中添加
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
全部保存后重启,红色波浪线消失,可以使用自动补全写代码了。
参考资料:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。