run_ros.sh 3.76 KB
Newer Older
chenyulei's avatar
chenyulei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#!/bin/bash

# How to use: (only accept three params below)
# 1.start lv nodes and record lv rosbag(the number represents the recording time): ./run_ros.sh lv 10
# 2.only start lv node: ./run_ros.sh lv
# 3.only start all nodes: ./run_ros.sh jfx
# 4. stop all nodes: ./run_ros.sh stop
# 5. restart all nodes: ./run_ros.sh restart


function getpids(){
    local res=`ps -ef | grep -v grep | grep $1 | sed 's/[ ][ ]*/ /g' | cut -d' ' -f8`
    echo $res
}

function killpids(){
    local res=`ps -ef | grep -v grep | grep $1 | sed 's/[ ][ ]*/ /g' | cut -d' ' -f2,8`
    local arr=($res)
    for i in ${!arr[@]}
    do
        if [ `expr $i % 2` -eq 0 ];then
            `echo "123" | sudo -S kill -9 ${arr[$i]}`
            echo "\tprocess \033[36m${arr[$i+1]}\033[0m killed\n"
        fi
    done
}

function stopapp(){
	source /home/nvidia/catkin_ws/devel/setup.sh
	rosnode list
	rosnode kill --all
	rosnode cleanup
    `killall roslaunch`
	echo "============ ALL ROS NODE HAS BEEN SHUTDOWN ============"

}

function startapp(){
	echo "====== ROSNODE     KILL      ALL======"
	rosnode kill --all
	clear
	sleep 2

	echo "====== START            ROSCORE ======"
	cd /home/nvidia/catkin_ws/
	roscore_logname="roscore-$(date +%Y%m%d)_$(date +%H%M%S).log"
   	nohup roscore > ../log/$roscore_logname 2>&1 &

   	sleep 3

	echo  "====== START PANDAR64 ROS NODES ======"
	cd /home/nvidia/catkin_ws/
	source devel/setup.bash
	lidar_logname="lidar-$(date +%Y%m%d)_$(date +%H%M%S).log"
	nohup roslaunch hesai_lidar hesai_lidar.launch > ../log/$lidar_logname 2>&1 &
   
	echo  "====== START  CAMERA  ROS NODES ======"
	cd /home/nvidia/catkin_ws/
	source devel/setup.bash
	camera_logname="camera-$(date +%Y%m%d)_$(date +%H%M%S).log"
	nohup roslaunch spinnaker_sdk_camera_driver node_acquisition.launch  > ../log/$camera_logname 2>&1 &
     
	if [ $1 == "jfx" ];then

		echo  "====== Start PROJECT ROS NODES ========"
		cd /home/nvidia/catkin_ws/
		source devel/setup.bash
		rosproject_logname="project-$(date +%Y%m%d)_$(date +%H%M%S).log"
		nohup roslaunch jfxrosperceiver project.launch  > ../log/$rosproject_logname 2>&1 &
	fi
	echo -e "----------------------------------------------------------------------------\n\n"
	sleep 4
	if [ $2 != " " ];then
		for j in 1 2 3; do
			if [ $j == 1 ];then
				sleep 4
				rm -rf /home/nvidia/rosbag/*
			fi
			echo "++++++ RECORD  ROSBAG(LV)  $j  TIMES ++++++"
			record_rosbag $2 j
		done
		echo "RECORD ROSBAG IS FINISHED"
	fi
}

function record_rosbag(){
	mkdir -p /home/nvidia/rosbag
	cd /home/nvidia/rosbag
        rosbag_logname="rosbag-$(date +%Y%m%d)_$(date +%H%M%S).log"
	nohup rosbag record --duration=$1 $(rostopic list | grep 'pandar$') $(rostopic list | grep 'compressed$') > ../log/$rosbag_logname 2>&1 &
	tm="scale=1; $1/100" | bc
	export tm=$(echo "scale=1;  $1/100" | bc)
	i=0
	icon=''
	arr=('|' '/' '-' '\\')
	index=0
	while [ $i -le 100 ]
	do
    		index=`echo $i%4`
    		printf "[%-74s][%d%%][%c]\r" "$icon" "$i" "${arr[$index]}"
    		icon='#'$icon
   		 ((i++))
   		 sleep $tm
	done
	echo

	echo "++++++ RECORD ROSBAG(LV) $j TIMES OK ++++++"
	sleep 3
	save_path="/media/nvidia/Data/7-1/"
	rosbag_name=$( ls | grep '.bag$')
	rosbag info $rosbag_name
	echo "++++++ SCP $rosbag_name TO $save_path(POINT 1-1) ++++++"
	scp ./*.bag nvidia@10.50.108.1:$save_path
	rm -rf ./*.bag
	echo -e "\n"
}

MODE="start"
if [ $# -ge 1 ];then
MODE=$1
fi
clear
mkdir -p /home/nvidia/log
if [ $MODE == "stop" ];then
    stopapp
    echo "mec app stopped at " `date` >> /home/nvidia/log/rosstop.log
elif [ $MODE == "start" ];then
    startapp $2 $3
    echo "mec app started at " `date` >> /home/nvidia/log/rosstart.log
elif [ $MODE == "restart" ];then
    stopapp
    startapp $2 $3
    echo "mec app restarted at " `date` >> /home/nvidia/log/rosrestart.log
else
    echo "Unexpected param(start stop or restart)"

fi