ROS机器人底盘(39)-ROS程序开机自启动(2)

1.前言

前文ROS机器人底盘(33)-ROS程序开机自启动(1)使用了ROSrobot_upstart包设置了pibot_bringup开启启动的方法,在统一用到其他的launch上会发现无法启动的问题,比如在启动gmapping时候,没有相关环境变量等,本文介绍使用systemd设置启动项

2.使用systemctrl创建服务

2.1新建服务

/etc/systemd/system创建pibot.service

# pibot.serive
[Unit]
Description=this is pibot service

[Service]
Type=simple
ExecStart=/usr/bin/pibot_start
ExecStop=/usr/bin/pibot_stop
ExecRestart=/usr/bin/pibot_restart

[Install]
WantedBy=multi-user.target

2.2 添加执行脚本

看到我们对应的项目添加脚本即可,这里的脚本启动,这里需要设置ROS的相关环境和PIBOT的相关环境及变量

我们手动输入命令启动时候的环境变量是写在~/.basrhc,该文件会在打开bash shell的时候被加载

#pibot_start
#!/bin/bash

log_file=/tmp/pibot-upstart.log
echo "$DATE: pibot-start" >> $log_file
pibotrc=/etc/pibotrc
. $pibotrc

code_name=$(lsb_release -sc)

if [ "$code_name" = "trusty" ]; then
    ros_version="indigo"
elif [ "$code_name" = "xenial" ]; then
    ros_version="kinetic"
elif [ "$code_name" = "bionic" ]; then
    ros_version="melodic"
else
    echo "PIBOT not support $code_name" >> $log_file
    exit
fi

echo "SYS_DIST:                $code_name" >> $log_file
echo "ROS_DIST:                $ros_version" >> $log_file
echo "LOCAL_IP:                $LOCAL_IP" >> $log_file
echo "ROS_MASTER_URI:          $ROS_MASTER_URI" >> $log_file
echo "ROS_IP:                  $ROS_IP" >> $log_file
echo "HOSTNAME:                $ROS_HOSTNAME" >> $log_file
echo "PIBOT_MODEL:             $PIBOT_MODEL" >> $log_file
echo "PIBOT_LIDAR:             $PIBOT_LIDAR" >> $log_file
echo "PIBOT_BOARD:             $PIBOT_BOARD" >> $log_file

# 这里写需要添加的启动launch命令
roslaunch pibot_navigation gmapping.launch

以hera为例

#pibotenv
#source ros
source /opt/ros/kinetic/setup.bash
LOCAL_IP=192.168.12.1 #这里IP需要设置本机IP,连接外网需要在路由器设置好固定`IP`
export ROS_IP=$LOCAL_IP
export ROS_HOSTNAME=$LOCAL_IP
export PIBOT_MODEL=hera
export PIBOT_LIDAR=rplidar
export PIBOT_BOARD=stm32f4
export ROS_MASTER_URI=http://$LOCAL_IP:11311

#source pibot
source /home/pibot/pibot_ros/ros_ws/devel/setup.bash

2.3 测试

  • 启动
    sudo systemctl start pibot
  • 停止
    sudo systemctl stop pibot
  • 重启
    sudo systemctl restart pibot
  • 开启开机启动
    sudo systemctl enable pibot
  • 关闭开机启动
    sudo systemctl disable pibot
  • 查看是否开机启动
    systemctl is-enabled pibot
  • 刷新配置
    sudo systemctl daemon-reload
  • 查看状态
    sudo systemctl status pibot

3.验证

重启后,查看状态,没有错误显示则为正常


ROS机器人底盘(39)-ROS程序开机自启动(2)_第1张图片

4.其他

4.1 一键配置

pibot提供一间配置脚本pibot_add_upstart.sh

#修改 ~/pibot_ros/pibot_upstart/pibotenv修改参数

cd ~/pibot_ros
./pibot_add_upstart.sh

你可能感兴趣的:(ROS机器人底盘(39)-ROS程序开机自启动(2))