ROS常用设置

ROS1

安装ROS1

如果是在深圳,推荐使用腾讯的官方源,该教程默认安装noetic版本的ROS

1
2
3
4
5
6
7
sudo sh -c 'echo "deb https://mirrors.tencent.com/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt install curl
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update
sudo apt install ros-noetic-ros-base python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo rosdep init
rosdep update

ROS主从机配置

ros的主从机只需要配置ROS_IP和ROS_MASTER_URI就可以。
主机配置的ROS_IP是主机的局域网的IP, ROS_MASTER_URI是应该是使用局域网的IP加端口,如 ROS_MASTER_URI=“http://192.168.23.10:11311”,
如果主机端没有配置ROS_IP,那么主机的xmlrpc会直接返回hostname,这时候如果从机并不知道该hostname是对应什么IP,这样从机就无法建立有效链接。
如果不想配置死环境变量,可以使用roslaunch对针对性应用配置临时环境变量,比如:

1
2
3
4
5
6
<launch>
<node pkg="rviz" type="rviz" name="rviz" >
<env name="ROS_IP" value="192.168.23.100" />
<env name="ROS_MASTER_URI" value="http://192.168.23.10:11311" />
</node>
</launch>

切记,ROS_MASTER_URI就是局域网内主机的IP,两侧机器是一样的。但是ROS_IP是各自主机的IP。一切配hostname的教程都是大忽悠!!!!

编译相关

1
2
3
rosdep install --from-paths src --ignore-src -r -y #根据package.xml安装依赖
catkin_make install -DCATKIN_WHITELIST_PACKAGES="clean_robot_base;ultrasonic;tof_pointcloud" -DCMAKE_BUILD_TYPE=Debug
catkin_create_pkg 包名 依赖1 依赖2 ...

有时候为了方便,也可以编辑Makefile,添加一下特定的环境变量

1
2
3
4
5
6
7
8
ARCH = $(shell arch)
all:
@echo CPU ARCH is ${ARCH}
catkin_make install -DCATKIN_WHITELIST_PACKAGES="clean_robot_base;ultrasonic;tof_pointcloud" \
-DCMAKE_BUILD_TYPE=Release \
-DCATKIN_DEVEL_PREFIX=./devel_${ARCH} \
-DCMAKE_INSTALL_PREFIX=./install_${ARCH} \
--build ./build_${ARCH}

CMakeLists.txt的install选项

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
# 安装可执行文件
install(TARGETS ${PROJECT_NAME}_node
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

# 安装用户动态库
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)

# 安装文件
install(FILES
myfile1
myfile2
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

# 安装文件夹
install(DIRECTORY launch
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
PATTERN ".svn" EXCLUDE)

install(DIRECTORY params
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
PATTERN ".svn" EXCLUDE)

安装部署

ROS提供了一个robot_upstart的特殊节点作为安装服务。我们先看一下robot_upstart的用法:

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
28
> rosrun robot_upstart install -h
usage: install [-h] [--job JOB] [--interface ethN] [--user NAME] [--setup path/to/setup.bash] [--rosdistro DISTRO] [--master http://MASTER:11311]
[--logdir path/to/logs] [--augment] [--provider [upstart|systemd]] [--symlink] [--wait] [--systemd-after After=]
pkg/path [pkg/path ...]

Use this tool to quickly and easily create system startup jobs which run one or more ROS launch files as a daemonized background process on your computer.
More advanced users will prefer to access the Python API from their own setup scripts, but this exists as a simple helper, an example, and a compatibility
shim for previous versions of robot_upstart which were bash-based.

positional arguments:
pkg/path 包名/需要安装的launch文件路劲,比如包名是test,launch文件路径是launch/test.launch,那么这个选项就是test/launch/test.launch
optional arguments:
-h, --help show this help message and exit
--job JOB 给服务取一个指定的名字,可以不设置
--interface ethN Specify network interface name to associate job with.
--user NAME Specify user to launch job as.
--setup path/to/setup.bash 这里是指向一个包的setup.bash路径,为了避免不必要的麻烦,使用绝对路径
--rosdistro DISTRO Specify ROS distro this is for.
--master http://MASTER:11311
Specify an alternative ROS_MASTER_URI for the job launch context.
--logdir path/to/logs
Specify an a value for ROS_LOG_DIR in the job launch context.
--augment Bypass creating the job, and only copy user files. Assumes the job was previously created.
--provider [upstart|systemd]
Specify provider if the autodetect fails to identify the correct provider
--symlink 不复制launch文件,使用软链接指向包的launch文件
--wait Pass a wait flag to roslaunch.
--systemd-after test.service 设置这个服务在test.service之后启动

参考指令

1
rosrun robot_upstart install --job kaylor --setup $(pwd)/install/setup.bash --systemd-after "test.service network.target" test/launch/talker.launch

上面的指令,注意setup.bash一定是bash!!!如果需要加入环境变量,可以自己添加一个.bash文件,设置环境变量之后,再source这个ros的setup.bash。当然,也可以通过修改service文件引入环境变量

ROS2

安装ROS2 Humble

安装官方通用通用源(这里使用腾讯的镜像源)

1
2
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://mirrors.cloud.tencent.com/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

如果你的系统是ubuntu20.04,仍然想使用humble,做如下设置:

1
2
sudo curl -sSL http://apt.kaylordut.cn/kaylor-keyring.gpg -o /usr/share/keyrings/kaylor-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/kaylor-keyring.gpg] http://apt.kaylordut.cn/ros2/ubuntu/ focal main" | sudo tee /etc/apt/sources.list.d/kaylor-ros2.list > /dev/null

最后,安装你需要的版本:

1
2
3
4
5
sudo apt update
sudo apt upgrade
sudo apt install ros-humble-desktop
sudo apt install ros-humble-ros-base
sudo apt install ros-dev-tools

如果你需要的安装包找不到,请邮件到[email protected],我会抽空做好适配

常用代码

只订阅一次topic

1
2
msg_type tmp;
bool ret = rclcpp::wait_for_message(tmp, this->shared_from_this(), topic_name, std::chrono::seconds(3));

注意,这个方法不能在自身构造函数里使用,所以一般可以写成成员函数调用。

常用指令

  • 创建包

    1
    ros2 pkg create --build-type ament_cmake --dependencies geometry_msgs rclcpp tf2 tf2_ros turtlesim -- learning_tf2_cpp
  • 安装依赖包

    1
    rosdep install -i --from-path src --rosdistro foxy -y

编译相关

cmake 设置相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Install launch files.
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)

# Install parameters files.
install(DIRECTORY
params
DESTINATION share/${PROJECT_NAME}/
)

install(TARGETS
clean_robot_base
DESTINATION lib/${PROJECT_NAME})

添加依赖选项

  • 第一,需要在package.xml里添加tf2_geometry_msgs
  • 第二,在cmake里添加
    1
    2
    find_package(tf2_geometry_msgs REQUIRED)
    ament_target_dependencies(target tf2_geometry_msgs)

colcon build

  • 使用symlink选项,install文件夹的文件生成软连接,否则生成的是拷贝

    1
    colcon build --symlink-install
  • 编译指定的包

    1
    colcon build --packages-select package_name
  • 编译指定的包和它的依赖

    1
    colcon build --packages-up-to package_name
  • 编译时忽略的包

    1
    colcon build --packages-ignore package_name
  • 编译的时候添加cmake参数选项

    1
    colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug/Release
  • 编译一个package 并且把log 显示在屏幕上

    1
    colcon build --packages-select rmw_coredds_shared_cpp --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Debug --event-handlers=console_direct+
  • 编译之前构建失败的包,不包括先前已经中止的包

    1
    colcon build --packages-select-build-failed
  • 跳过之前之前已经完成构建的包

    1
    colcon build --packages-skip-build-finished
  • 指定编译的build base

    1
    colcon build --build-base path
  • 指定安装目录

    1
    colcon build --install-base path
  • 并行编译参数

    1
    colcon build --parallel-worker Number
  • 安装不添加子目录

    1
    colcon build --merge-install

部署

  • 安装robot-upstart包
  • source自己的包的setup.bash

    这里的目的是为了下一步的运行的指令能找到要安装的包

  • 添加启动任务
    1
    2
    # 举例说明
    ros2 run robot_upstart install --job test --setup your_package_setup.bash_path your_package_name/launch/test.launch.py --logdir logdir_path

注意那个launch文件的命名一定要launch.py结尾,不然启动到时候会有问题。
–setup选项需要的是一个路径,但是后面的launch文件路径是需要“包名/launch/launch文件名”
如果ROS需要操作一些外设,可能存在权限问题,这时候生成启动任务的指令,最好在root用户下进行

0%