若文件里包含了ros话题的发布和接收,那么设置自启动时,应该首先将roscore设置为自启动。
首先确保roscore有一个systemd服务文件。如果还没有,需要在/etc/systemd/system/下创建一个。例如,一个基本的roscore.service文件可能如下所示:
bash
[Unit]
Description=ROS Master Node
After=network.target
[Service]
Type=simple
User=your_username # 使用你的用户名
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/ros/melodic/bin # 根据需要修改
Environment=LD_LIBRARY_PATH=/opt/ros/melodic/lib:/usr/lib # 根据需要修改
ExecStart=/bin/bash -c "source /opt/ros/melodic/setup.bash && roscore"
Restart=on-failure
[Install]
WantedBy=multi-user.target
上面文件中的 PATH 路径查看方式:打开一个终端并输入echo $PATH
LD_LIBRARY_PATH:输入echo $LD_LIBRARY_PATH来查看LD_LIBRARY_PATH
上面文件中的melodic要替换成自己的ros版本号。
我的roscore.service文件为:
bash
[Unit]
Description=ROS Master Node
After=network.target
[Service]
Type=simple
User=ubuntu
Environment=PATH=/opt/ros/noetic/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Environment=LD_LIBRARY_PATH=/opt/ros/noetic/lib
ExecStart=/bin/bash -c "source /opt/ros/noetic/setup.bash && roscore"
Restart=on-failure
[Install]
WantedBy=multi-user.target
然后依次运行:
bash
sudo systemctl daemon-reload
sudo systemctl start roscore.service
sudo systemctl status roscore.service
二、设置包含ros节点的文件自启动
包含ros节点的文件为terminal2car_receive.py
bash
[Unit]
Description=Terminal to Car Receiver Service
After=network.target roscore.service
Requires=roscore.service
[Service]
Type=simple
ExecStart=/path/to/your/script_or_command_to_start_terminal2car_receive
Restart=on-failure
[Install]
WantedBy=multi-user.target
上述文件中的/path/to/your/script_or_command_to_start_terminal2car_receive 要替换成运行terminal2car_receive.py时的完整路径。
terminal2car_receive.py的自启动服务文件名称为:terminal2car_receive.service,内容为:
bash
[Unit]
Description=Terminal to Car Receiver Service
After=network.target roscore.service
Requires=roscore.service
[Service]
Type=simple
ExecStart=/usr/bin/env python3 /home/ubuntu/ccy/glove_car_bt/ros_udp_udp-main/src/udp_udp/scripts/terminal2car_receive.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
依次运行:
bash
sudo systemctl daemon-reload
sudo systemctl start terminal2car_receive.service
sudo systemctl status terminal2car_receive.service