[TOOL] A small tool to improve the efficiency of master-slave configuration in ROS

1. Introduction

  Because it is often necessary to configure the master-slave machine for different machines (the host names of different machines are the same, and the IP address is in “192.168.101.*** ”the format), this script is organized to improve the switching efficiency of frequent machine switching and master-slave/stand-alone mode, and a command line configuration / Cancel master-slave mode.

2. Configuration steps

1. Edit the script file

  The script file name set_master_slave.shis as follows:

#!/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

Content adaptation instructions

  • ip_pre="192.168.101." Modify to the required IP prefix
  • remote_name="robot" is changed to the remote host name as the ROS host
  • locate_name="gene" is modified to the local host name as a ROS slave

2. Script instructions

  Note: When performing master-slave configuration, please execute this script on the same network first! !

2.1 Configure slave mode

2.1.1 Single parameter

Command format: ./set_master_slave.sh [master_ip]
Specific usage: ./set_master_slave.sh 116
Example:
insert image description here

2.1.2 Two parameters

Command format: ./set_master_slave.sh [master_ip] [slave_ip]
Specific usage: ./set_master_slave.sh 125 145
Example:
insert image description here

2.1.3 Three parameters

Command format: ./set_master_slave.sh [master_ip] [slave_ip] [remote_name]
Specific usage: ./set_master_slave.sh 125 145 gene
Example:
insert image description here

2.2 Configure host mode

2.2.1 Two parameters

Command format: ./set_master_slave.sh m [remote_ip] 具体用法:./set_master_slave.sh m 145`Example
:
insert image description here

2.2.1 Three parameters

Command format: ./set_master_slave.sh m [remote_ip] [remote_name] 具体用法:./set_master_slave.sh m 135 gene`Example
:
insert image description here

2.2 Cancel the master-slave mode

Command format: ./set_master_slave.sh n( followed by parameters are lettersn )
Specific usage: ./set_master_slave.sh n
Example:
insert image description here

3. Tips

  You can put the script file under a file path that you don’t often browse, and use the alias form to open the terminal and use it. The above example is called in the form of an alias. It’s really fragrant~

Guess you like

Origin blog.csdn.net/qq_38429958/article/details/131462827