项目需求:机器人开机上电后工控机首先运行机械臂控制代码,再运行算法代码
1.终端执行以下代码
gnome-session-properties
2.设置开机自启动选项
在弹出界面点击添加,名称随便填,命令填入要启动的脚本,注释随便填。
gnome-terminal -- /home/txz/start.sh
3.开机自启动脚本start.sh
开机后首先ping机械臂的ip地址,每隔5s ping一次,能ping通后执行机械臂蜷缩操作命令,然后再打开一个终端窗口,启动算法代码。
#!/bin/bash
echo "$(date "+%Y-%m-%d %H:%M:%S") ceshila" >> /home/txz/screen.log
while true;doif ping -c 1 10.7.5.18 &> /dev/null;thenecho "$(date "+%Y-%m-%d %H:%M:%S") Ping successul. Starting Python script."echo "$(date "+%Y-%m-%d %H:%M:%S") Ping successful. Starting Python script." >> /home/txz/screen1.logpython3 /home/txz/arm_server/quansuo_position.pysleep 3python3 /home/txz/arm_server/quansuo_position.pysleep 1gnome-terminal -- /home/txz/8.17/401_serch-tcp_4/1_main_frame/build/test_demosleep 1python3 /home/txz/arm_server/server_nano.pybreakelseecho "$(date "+%Y-%m-%d %H:%M:%S") Ping unsuccessful.Retrying in 5 seconds."echo "$(date "+%Y-%m-%d %H:%M:%S") Ping unsuccessful.Retrying in 5 seconds." >> /home/txz/screen2.logsleep 5fi
done
4.关机前操作代码stop.sh
找到开机启动的代码命令,然后kill掉,再让机械臂恢复零位
#!/bin/bash# 查询命令的ID
command_name="python3" # 替换为你要查询的命令名称
command_pid=$(pgrep "$command_name")if [ -z "$command_pid" ]; thenecho "未找到运行的命令: $command_name"
elseecho "找到运行的命令: $command_name (PID: $command_pid)"# 杀死命令kill -9 "$command_pid"echo "已杀死命令: $command_name (PID: $command_pid)"
fi
python3 /home/txz/arm_server/zero_position.py
sleep 3
python3 /home/txz/arm_server/zero_position.py