ROS 重名(已整理)

ROS 节点名称重名

1. rosrun 设置命名空间与重映射

实现打开两个相同节点

  1. 方式1:设置命名空间

rosrun turtlesim turtlesim_node __ns:=ergouzi
rosrun turtlesim turtlesim_node __ns:=molvzi

rosnode list

/erGouZi/turtlesim
/molvzi/turtlesim
/rosout
/turtlesim

  1. 方式2:名称重映射

rosrun turtlesim turtlesim_node __name:=daqiang
rosrun turtlesim turtlesim_node __name:=xioaqiang

rosnode list

/daqiang
/erGouZi/turtlesim
/molvzi/turtlesim
/rosout
/turtlesim
/xioaqiang

2. launch文件设置命名空间与重映射

<!-- 需要启动多个乌龟GUI节点 -->
<launch>
    <node pkg = "turtlesim" type = "turtlesim_node" name = "turtlesim" /> 
    <!-- 名称重映射 -->
    <node pkg = "turtlesim" type = "turtlesim_node" name = "t1" /> 
    <!-- 命名空间 -->
    <node pkg = "turtlesim" type = "turtlesim_node" name = "turtlesim" ns = "ergouzi" /> 
    <!-- 命名空间 + 名称重映射 -->
    <node pkg = "turtlesim" type = "turtlesim_node" name = "t2" ns = "maolvzi"/> 
</launch>

3. 编码设置命名空间与重映射

ROS 话题名称设置

1. rosrun设置话题重映射

rosrun turtlesim turtlesim_node 与 rosrun teleop_twist_keyboard teleop_twist_keyboard.py
不能通信
rostopic list

/cmd_vel——keyboard
/rosout
/rosout_agg
/turtle1/cmd_vel ——node
/turtle1/color_sensor
/turtle1/pose

使用下面

rosrun teleop_twist_keyboard teleop_twist_keyboard.py /cmd_vel:=/turtle1/cmd_vel

或者

rosrun turtlesim turtlesim_node /turtle1/cmd_vel:=/cmd_vel

2. launch文件设置话题重映射

rename_topic——start.launch文件

<!-- 键盘控制乌龟运动 -->
<launch>
    <!-- 将乌龟的话题设置为与键盘控制一致 -->
    <node pkg = "turtlesim" type = "turtlesim_node" name = "t1" >
        <remap from = "/turtle1/cmd_vel" to = "/cmd_vel" />
    </node>
    <nodep pkg = "teleop_twist_keyboard" type = "teleop_twist_keyboard.py" name = "key1" />
    
    <!-- 将键盘控制的话题设置为与乌龟一致 -->
</launch>

3. 编码设置话题名称

在这里插入图片描述
rosrun rename02_topic topic_name __ns:=xxx(命名空间)
rosnode list 与rostopic list
可以自己定义命名空间

#include"ros/ros.h"
#include"std_msgs/String.h"

/*
    需求: 演示不同类型的话题名称设置
          设置话题名称与命名空间
*/

int main(int argc, char *argv[])
{
    ros::init(argc,argv,"hello");//hello是节点
    // ros::NodeHandle nh;

    //核心:设置不同类型的话题
    //1. 全局 ---- 话题名称以 / 开头(也可以设置自己的命名空间),这种情况下和节点(命名空间以及名称)没有关系
    ros::Publisher pub1 = nh.advertise<std_msgs::String>("/chatter",1000);
    // ros::Publisher pub1 = nh.advertise<std_msgs::String>("/yyy/chatter",1000);
    //全局话题是/chatter,也可以用/yyy/chatter
    
    //2. 相对 ---- 非 / 开头
    ros::Publisher pub2 = nh.advertise<std_msgs::String>("chatter",1000);
    // ros::Publisher pub2 = nh.advertise<std_msgs::String>("yyy/chatter",1000);


    //3. 私有 ---- 需要创建特定的NodeHandle nh("~")
    ros::NodeHandle nh("~");
    // ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",1000);
    ros::Publisher pub = nh.advertise<std_msgs::String>("yyy/chatter",1000);

    // 注意:如果私有的NH创建的话题以 / 开头(全局话题),生成的话题是全局的,非私有的
    // 全局话题的优先级更高


    while(ros::ok())
    {

    }

    return 0;
}

python实现

#! /usr/bin/env python

import rospy
from std_msgs.msg import String

if __name__ == "__main__":
    rospy.init_node("hello")
    """
        需求: 实现不同类型的话题设置
    """

    # 1. 全局
    # pub = rospy.Publisher("/chatter",String,queue_size = 10)

    # 2. 相对
    pub = rospy.Publisher("chatter",String,queue_size = 10)

    # 3. 私有
    pub = rospy.Publisher("~chatter",String,queue_size = 10)


    while not rospy.is_shutdown():
        pass

ROS 参数名称设置

1. launch文件设置参数

<!-- 设置参数 -->
<launch>
    <!-- 格式1: 全局 -->
    <param name = "radius" value = "0.2" />
    
    <!-- 格式2: 私有 -->
    <node pkg = "turtlesim" type = "turtlesim_node" name = "t1" ns = "xxx">
        <param name = "radius" value = "0.08" />
    </node>
</launch>

2. 编码设置参数

C++实现

#include"ros/ros.h"

int main(int argc, char *argv[])
{
    ros::init(argc,argv,"hello");
    ros::NodeHandle nh;

    /*
        使用 ros::param 来设置参数
    */

    // 1. 全局
    ros::param::set("/radiusA",100);
    // 2. 相对
    ros::param::set("radiusB",100);
    // 3. 私有
    ros::param::set("~radiusC",100);

    /*
        使用 NodeHandle 设置参数
    */

    // 全局
    nh.setParam("/radius_nh_A",1000);

    // 相对
    nh.setParam("radius_nh_B",1000);

    // 私有
    ros::NodeHandle nh_private("~");
    nh_private.setParam("radius_nh_C",1000);
    
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2021 Nqq

你的鼓励是我学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值