#!/bin/bash
#########################################################################
# Author: zhangshuai #
# Date: 2019.03.21 #
# Description: Automated generation of launch and world #
# Usage: ./Distributed_Gazebo configuration_file #
#########################################################################
configuration_file=$1
world="WORLD:"
node="GAZEBO_NODE:"
ip="GAZEBO_IP:"
port="SOCKET_PORT:"
hector="HECTOR_NAME:"
world_name=0
idex=(0 0)
id0=0
id1=1
node_0_hector_count=0
node_1_hector_count=0
gazebo_idex_count=0
# gazebo_count
# gazebo_id
# local_hector_
gazebo_param_0=(0 0)
gazebo_param_1=(0 0)
hector_node_0=(0)
hector_node_1=(0)
echo "begin"
while read line
do
if [[ $line =~ $world ]]
then
temp=${line#*\"}
temp=${temp%\"*}
world_name=$temp
echo ${temp}
fi
if [[ $line =~ $node ]]
then
temp=${line#*\"}
temp=${temp%\"*}
idex[gazebo_idex_count]=$temp
echo ${temp}
echo ${idex[gazebo_idex_count]}
echo "gazebo_idex_count:$gazebo_idex_count"
let gazebo_idex_count++
fi
if [[ $line =~ $ip ]]
then
temp=${line#*\"}
temp=${temp%\"*}
if [ "${idex[gazebo_idex_count-1]}" == "$id0" ]
then
gazebo_param_0[0]=$temp
# echo "gazebo_param_0[0]:${gazebo_param_0[0]}"
fi
if [ "${idex[gazebo_idex_count-1]}" == "$id1" ]
then
gazebo_param_1[0]=$temp
# echo "gazebo_param_1[0]:${gazebo_param_1[0]}"
fi
echo ${temp}
fi
if [[ $line =~ $port ]]
then
temp=${line#*\"}
temp=${temp%\"*}
if [ "${idex[gazebo_idex_count-1]}" == "$id0" ]
then
gazebo_param_0[1]=$temp
fi
if [ "${idex[gazebo_idex_count-1]}" == "$id1" ]
then
gazebo_param_1[1]=$temp
fi
echo ${temp}
fi
if [[ $line =~ $hector ]]
then
temp=${line#*\"}
temp=${temp%\"*}
if [ "${idex[gazebo_idex_count-1]}" == "$id0" ]
then
hector_node_0[node_0_hector_count]=$temp
let node_0_hector_count++
fi
if [ "${idex[gazebo_idex_count-1]}" == "$id1" ]
then
hector_node_1[node_1_hector_count]=$temp
let node_1_hector_count++
fi
echo ${temp}
fi
done < $configuration_file
echo "end"
# echo ${gazebo_param_0[0]}
# echo ${gazebo_param_0[1]}
# echo ${gazebo_param_1[0]}
# echo ${gazebo_param_1[1]}
# echo ${hector_node_0[0]}
# echo ${hector_node_0[1]}
# echo ${hector_node_1[0]}
# echo ${hector_node_1[1]}
echo "node0:${node_0_hector_count}"
echo "node1:${node_1_hector_count}"
file="var/log/messages"
echo "${file#*/}"
echo "${file##*/}"
# 开始写文件
launch_file=(0 0)
world_file=(0 0)
# node_0的launch文件
launch_file[0]="src/hector_quadrotor/hector_quadrotor_gazebo/launch/hector_node_0_${node_0_hector_count}_${node_1_hector_count}.launch"
if [ -f ${launch_file[0]} ]
then
`rm -rf ${launch_file[0]}`
fi
`touch ${launch_file[0]}`
# node_1的launch文件
launch_file[1]="src/hector_quadrotor/hector_quadrotor_gazebo/launch/hector_node_1_${node_0_hector_count}_${node_1_hector_count}.launch"
if [ -f ${launch_file[1]} ]
then
`rm -rf ${launch_file[1]}`
fi
`touch ${launch_file[1]}`
# node_0的world文件
world_file[0]="src/hector_quadrotor/hector_quadrotor_gazebo/worlds/${world_name}_node_0.world"
if [ -f ${world_file[0]} ]
then
`rm -rf ${world_file[0]}`
fi
`touch ${world_file[0]}`
# node_1的world文件
world_file[1]="src/hector_quadrotor/hector_quadrotor_gazebo/worlds/${world_name}_node_1.world"
if [ -f ${world_file[1]} ]
then
`rm -rf ${world_file[1]}`
fi
`touch ${world_file[1]}`
# 写world文件
for((i=0;i<$gazebo_idex_count;i++))
do
echo "" >> ${world_file[i]}
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " model://sun" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " model://${world_name}" >> ${world_file[i]}
echo " 0 0 0 0 0 0" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " 0.68 0.68 0.68 1.0" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " 0" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
tmp_int=$i
if [ "$tmp_int" == "$id0" ]
then
echo " " >> ${world_file[i]}
fi
if [ "$tmp_int" == "$id1" ]
then
echo " " >> ${world_file[i]}
fi
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo " 0" >> ${world_file[i]}
echo " 0.001" >> ${world_file[i]}
echo " 0" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
echo " " >> ${world_file[i]}
echo "" >> ${world_file[i]}
done
# 写launch文件
for((i=0;i<$gazebo_idex_count;i++))
do
echo "" >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
x=0
y=0
t_x=0
t_y=0
# tmp0=$node_0_hector_count+$node_1_hector_count
# tmp1=$node_1_hector_count
tmp_int=$i
if [ "$tmp_int" == "$id0" ]
then
while [ $x -lt $(($node_0_hector_count + $node_1_hector_count)) ]
do
for((j=0;j<$node_0_hector_count;j++))
do
if [ $y -eq 10 ]
then
let t_y=0
let t_x=t_x+5
fi
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
let t_y=t_y+5
let y++;
let x++;
done
for((j=0;j<$node_1_hector_count;j++))
do
if [ $y -eq 10 ]
then
let t_y=0
let t_x=t_x+5
fi
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
let t_y=t_y+5
let y++;
let x++;
done
done
fi
if [ "$tmp_int" == "$id1" ]
then
while [ $x -lt 4 ]
do
for((j=0;j<$node_0_hector_count;j++))
do
if [ $y -eq 10 ]
then
let t_y=0
let t_x=t_x+5
fi
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
let t_y=t_y+5
let y++;
let x++;
done
for((j=0;j<$node_1_hector_count;j++))
do
if [ $y -eq 10 ]
then
let t_y=0
let t_x=t_x+5
fi
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
let t_y=t_y+5
let y++;
let x++;
done
done
fi
echo " " >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
echo "" >> ${launch_file[i]}
done