Skip to content

Instantly share code, notes, and snippets.

@danielsnider
Last active August 26, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsnider/13aa8c21e4fb12621b7d8ba59a762e75 to your computer and use it in GitHub Desktop.
Save danielsnider/13aa8c21e4fb12621b7d8ba59a762e75 to your computer and use it in GitHub Desktop.
Team R3 has developed a script to automatically set your ROS_IP environment variable and to detect an online robot using ping and set your ROS_MASTER_URI environment variable (assumes a static IP for your robot). If your robot is not online your own ROS_IP will be used in your ROS_MASTER_URI. To use this script run 'source ~/set_robot_as_ROS_mas…
#!/bin/bash
# Get my IP
MY_IP=$(hostname -I | grep 192.168.137 | cut -f1 -d' ')
exit_status=$?
if [ $exit_status -eq 1 ]; then
echo "ERROR: you do not have an IP on the robot network (192.168.137.x). Please set one. Aborting..."
return 1
fi
# Check for online robots or default to localhost
echo -n "Searching for available robots"
ROBOT1=192.168.137.212
ROBOT2=192.168.137.213
# Ping robots
ping -c1 -W1 -q $ROBOT1 > /dev/null 2>&1
ROBOT1_UP=$?
echo -n "." # progress indication
ping -c1 -W1 -q $ROBOT2 > /dev/null 2>&1
ROBOT2_UP=$?
echo -n "." # progress indication
# Descision tree: based on ping, use IP of ROBOT1 or ROBOT2 or localhost or let the user pick between ROBOT1 and ROBOT2 if they're both available
if [ $ROBOT1_UP -eq 0 ] && [ $ROBOT2_UP -eq 0 ]; then
echo "Detected both ROBOT1 and ROBOT2 are ONLINE."
echo "Please pick (1) or (2)."
read -p "Use ROBOT1 or ROBOT2?: " USER_PICK
if [ $USER_PICK -eq 1 ]; then
MASTER_PICK=$ROBOT1
elif [ $USER_PICK -eq 2 ]; then
MASTER_PICK=$ROBOT2
fi
elif [ $ROBOT1_UP -eq 0 ]; then
MASTER_PICK=$ROBOT1
elif [ $ROBOT2_UP -eq 0 ]; then
MASTER_PICK=$ROBOT2
else
MASTER_PICK=$MY_IP
fi
echo "Setting your ROS_IP to $MY_IP and ROS_MASTER to $MASTER_PICK!"
export ROS_IP=$MY_IP
export ROS_MASTER_URI=http://$MASTER_PICK:11311/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment