编写launch文件
项目根目录下创建 /launch/robot.launch.py
# 导入库
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
motion_control_node = Node(
package="motion_control",
executable="motion_control_node"
)
remote_control_node = Node(
package="remote_control_service",
executable="remote_control_service_node"
)
# 创建LaunchDescription对象launch_description,用于描述launch文件
launch_description = LaunchDescription(
[motion_control_node, remote_control_node])
# 返回让ROS2根据launch描述执行节点
return launch_description
创建执行脚本ros2_startup.sh
#!/bin/bash
export ROS_LOG_DIR=/home/ubuntu/ArslanDev/arslan-saturn-ros2/log
source /opt/ros/humble/setup.sh
source /home/ubuntu/ArslanDev/arslan-saturn-ros2/install/setup.bash
ros2 launch /home/ubuntu/ArslanDev/arslan-saturn-ros2/launch/robot.launch.py
添加执行权限
sudo chmod +x ros2_startup.sh
创建服务文件
注意一定要添加WorkingDirectory,对应修改为你的工程目录
sudo nano /etc/systemd/system/robot_run.service
# or
sudo vim /etc/systemd/system/robot_run.service
[Unit]
Description=ArslanRobot
After=network.target
[Service]
Type=simple
User=ubuntu
ExecStart=/home/ubuntu/ArslanDev/arslan-saturn-ros2/tool/ros2/ros2_startup.sh
WorkingDirectory=/home/ubuntu/ArslanDev/arslan-saturn-ros2
[Install]
WantedBy=multi-user.target
# 重新加载服务配置文件
systemctl daemon-reload
# 启动服务
sudo systemctl start robot_run.service
# 设置开机自启动
sudo systemctl enable robot_run.service
# 查看运行状态
sudo systemctl status robot_run.service
# 停止服务
sudo systemctl stop robot_run.service
# 移除开机自启动
sudo systemctl disable robot_run.service
网友评论