【TOOL】 提升ROS配置主从机效率的小工具

一、简介

  因经常需要对不同机器配置主从机(不同机器的主机名一致,IP地址为 “192.168.101.*** ”格式),故整理此脚本来提高自己频繁切换机器和主从机/单机模式的切换效率,进行一个命令行配置/取消主从机模式。

二、配置步骤

1.编辑脚本文件

  脚本文件名为 set_master_slave.sh,内容如下:

#!/bin/bash
# Author:   chatgpt
# Verifier: ZYG
# Date:     2023.06.29


local_ip=$(hostname -I|awk '{print $1}')

IFS='.' read -ra ip_parts <<< "${local_ip}";
ip_pre="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}."
ip_post="${ip_parts[3]}"

ip_master_post="145"
ip_slave_post="135"
ip_master=${ip_pre}${ip_master_post}
ip_slave=${ip_pre}${ip_slave_post}

remote_name="gene"
locate_name=$(hostname)

set_master() {
    
    
  # Check if ~/.bashrc contains existing ROS master and slave configuration
  if grep -qE "ROS_MASTER_URI|ROS_HOSTNAME" ~/.bashrc; then
    # Remove existing ROS master and slave configuration
    sed -i '/export ROS_MASTER_URI=/d' ~/.bashrc
    sed -i '/export ROS_HOSTNAME=/d' ~/.bashrc
    #echo "Existing ROS master and slave configuration removed from ~/.bashrc."
  fi

  # Check if /etc/hosts contains existing ROS master and slave configuration
  if grep -qE ".*${remote_name}|.*${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/.*(${remote_name}|${locate_name})/d" /etc/hosts
    #echo "Existing ROS master and slave configuration removed from /etc/hosts."
  fi

  # Configuring ROS ROS_MASTER_URI IP
  echo "export ROS_MASTER_URI=http://$1:11311" >> ~/.bashrc

  # Configuring ROS_HOSTNAME IP
  echo "export ROS_HOSTNAME=$1" >> ~/.bashrc

  # Update /etc/hosts file
  if grep -qE "${remote_name}|${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/(${remote_name}|${locate_name})/d" /etc/hosts
  fi
  echo "$2 ${remote_name}" | sudo tee -a /etc/hosts >/dev/null

  echo "ROS master and slave configuration updated."
  echo "ROS master: ${locate_name} ${ip_master}"
  echo "ROS slave:  ${remote_name} ${ip_slave}"
}

set_slave() {
    
    
  # Check if ~/.bashrc contains existing ROS master and slave configuration
  if grep -qE "ROS_MASTER_URI|ROS_HOSTNAME" ~/.bashrc; then
    # Remove existing ROS master and slave configuration
    sed -i '/export ROS_MASTER_URI=/d' ~/.bashrc
    sed -i '/export ROS_HOSTNAME=/d' ~/.bashrc
    #echo "Existing ROS master and slave configuration removed from ~/.bashrc."
  fi

  # Check if /etc/hosts contains existing ROS master and slave configuration
  if grep -qE ".*${remote_name}|.*${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/.*(${remote_name}|${locate_name})/d" /etc/hosts
    #echo "Existing ROS master and slave configuration removed from /etc/hosts."
  fi

  # Configuring ROS remote master IP
  echo "export ROS_MASTER_URI=http://$1:11311" >> ~/.bashrc

  # Configuring ROS local slave IP
  echo "export ROS_HOSTNAME=$2" >> ~/.bashrc

  # Update /etc/hosts file
  if grep -qE "${remote_name}|${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/(${remote_name}|${locate_name})/d" /etc/hosts
  fi
  echo "$1 ${remote_name}" | sudo tee -a /etc/hosts >/dev/null

  echo "ROS master and slave configuration updated."
  echo "ROS master: ${remote_name} ${ip_master}"
  echo "ROS slave:  ${locate_name} ${ip_slave}"
}

# Check the number of arguments
if [[ $# -eq 2 ]]; then
  if [ "$1" == "m" ]; then
    ip_master=${local_ip}
    ip_slave=${ip_pre}$2
    set_master ${ip_master} ${ip_slave}
  else
    # Two arguments provided, configure ROS master and slave IPs
    ip_master=${ip_pre}$1
    ip_slave=${ip_pre}$2
    set_slave ${ip_master} ${ip_slave}
  fi
elif [[ $# -eq 3 ]]; then
  if [ "$1" == "m" ]; then
    ip_master=${local_ip}
    ip_slave=${ip_pre}$2
    remote_name=$3
    set_master ${ip_master} ${ip_slave}
  else
    # Two arguments provided, configure ROS master and slave IPs
    ip_master=${ip_pre}$1
    ip_slave=${ip_pre}$2
    remote_name=$3
    set_slave ${ip_master} ${ip_slave}
  fi
elif [[ $# -eq 1 ]]; then
  if [ "$1" == "n" ]; then
    # Check if ~/.bashrc contains existing ROS master and slave configuration
    if grep -qE "ROS_MASTER_URI|ROS_HOSTNAME" ~/.bashrc; then
      # Remove existing ROS master and slave configuration
      sed -i '/export ROS_MASTER_URI=/d' ~/.bashrc
      sed -i '/export ROS_HOSTNAME=/d' ~/.bashrc
      echo "Removed ROS master and slave configuration from ~/.bashrc."
    fi

    # Check if /etc/hosts contains existing ROS master and slave configuration
    if grep -qE ".*${remote_name}|.*${locate_name}" /etc/hosts; then
      # Remove existing ROS master and slave configuration
      sudo sed -i -E "/.*(${remote_name}|${locate_name})/d" /etc/hosts
      echo "Removed ROS master and slave configuration from /etc/hosts."
    fi
  else
    # One argument provided, configure default ROS master and slave IPs
    ip_master=${ip_pre}$1
    #ip_slave=${ip_pre}$1

    set_slave ${ip_master} ${ip_slave}
  fi
else
  # No arguments provided
  echo "Invalid number of arguments."
  echo "Usage: bash set_master_slave.sh [master_ip] [slave_ip]"
  echo "Example: bash set_master_slave.sh 100 143"
  exit 1
fi



# Source the updated ~/.bashrc file
source ~/.bashrc

内容适配说明

  • ip_pre=“192.168.101.” 修改成需要的IP前缀
  • remote_name=“robot” 修改成远程主机名,作为ROS主机
  • locate_name=“gene” 修改成本地主机名,作为ROS从机

2.脚本使用说明

  注意:进行主从机配置时,请先在同一网络再执行此脚本!!

2.1 配置从机模式

2.1.1 单参数

命令格式:./set_master_slave.sh [master_ip]
具体用法:./set_master_slave.sh 116
实例:
在这里插入图片描述

2.1.2 双参数

命令格式:./set_master_slave.sh [master_ip] [slave_ip]
具体用法:./set_master_slave.sh 125 145
实例:
在这里插入图片描述

2.1.3 三参数

命令格式:./set_master_slave.sh [master_ip] [slave_ip] [remote_name]
具体用法:./set_master_slave.sh 125 145 gene
实例:
在这里插入图片描述

2.2 配置主机模式

2.2.1 双参数

命令格式:./set_master_slave.sh m [remote_ip] 具体用法:./set_master_slave.sh m 145`
实例:
在这里插入图片描述

2.2.1 三参数

命令格式:./set_master_slave.sh m [remote_ip] [remote_name] 具体用法:./set_master_slave.sh m 135 gene`
实例:
在这里插入图片描述

2.2 取消主从机模式

命令格式:./set_master_slave.sh n (后跟参数是字母n)
具体用法:./set_master_slave.sh n
实例:
在这里插入图片描述

3.小建议

  可以将该脚本文件放在一不常翻看的文件路径下,结合使用别名的形式,打开终端即可使用,上述实例即是取别名的形式调用,真香~

猜你喜欢

转载自blog.csdn.net/qq_38429958/article/details/131462827