robot slam service (09):脚本自动启动
1. 开机启动
1.1 方式一 Add an Upstart job
1.2 方式二 Add to /etc/rc.local
1.3 方式三 Add an init script
2. 图形界面
gnome-terminal的快捷方式
3. ROS途径
robot_upstart方式
4 upstart自启动服务中的串口通讯
5. REFs:
multi subscriber topic one node parallel callback spin block site:ros.org
https://answers.ros.org/question/185153/ros-multithreading-example/
https://answers.ros.org/question/208587/shared-variable-between-callbacks/
https://answers.ros.org/question/241561/ros-threading-asyncspinners-and-control/
https://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning
1. 开机启动
I have a script in a folder: /path/to/my/script.sh, I need this script to run every time the system starts (even if no one logs in to the system).
1.1 方式一 Add an Upstart job
Create /etc/init/myjob.conf
$ vi /etc/init/myjob.conf
with content like the following:
description “my job”
start on startup
task
exec /path/to/my/script.sh
Okay !
1.2 方式二 Add to /etc/rc.local
$ sudo vi /etc/rc.local
with content like the following:
# This script is executed at the end of each multiuser runlevel
/usr/sbin/openvpn –config /home/dehaou1404/openv/client.conf > /dev/null & #others
/path/to/my/script.sh || exit 1 # Added new one
exit 0
Okay !
1.3 方式三 Add an init script
Create a new script in /etc/init.d/myscript.
$ vi /etc/init.d/myscript.sh
Make it executable.
$ chmod ugo+x /etc/init.d/myscript
#!/bin/bash
# … here do whatever you want to do …
exit 0
Configure the init system to run this script at startup:
$ cd /etc/init.d/
$ sudo update-rc.d myscript.sh defaults 98
Adding system startup for /etc/init.d/00.start.sh …
/etc/rc0.d/K9800.start.sh -> ../init.d/00.start.sh
/etc/rc1.d/K9800.start.sh -> ../init.d/00.start.sh
/etc/rc6.d/K9800.start.sh -> ../init.d/00.start.sh
/etc/rc2.d/S9800.start.sh -> ../init.d/00.start.sh
/etc/rc3.d/S9800.start.sh -> ../init.d/00.start.sh
/etc/rc4.d/S9800.start.sh -> ../init.d/00.start.sh
/etc/rc5.d/S9800.start.sh -> ../init.d/00.start.sh
when you want to remove:
$ sudo update-rc.d -f myscript.sh remove
Removing any system startup links for /etc/init.d/00.start.sh …
/etc/rc0.d/K9800.start.sh
/etc/rc1.d/K9800.start.sh
/etc/rc2.d/S9800.start.sh
/etc/rc3.d/S9800.start.sh
/etc/rc4.d/S9800.start.sh
/etc/rc5.d/S9800.start.sh
/etc/rc6.d/K9800.start.sh
update-rc.d install and remove System-V style init script links. update-rc.d automatically updates the System V style init script links /etc/rcrunlevel.d/NNname to scripts /etc/init.d/name. by init, 0123456789S, and NN is the two-digit sequence code used by init to decide which order to run the scripts in.
bash sample:
#!/bin/bash
# like to log things, so this creates a new log for this script
LOG=”/home/funbot/funbot.log”
# Time/Date stamp the log
echo -e “n$(date +%Y:%m:%d-%T) – Starting ROS daemon at startup” >> $LOG
# For bootup time calculations
START=$(date +%s.%N)
#This is important to wait until the IP address of the machine is actually configured
while true; do
IP=”`ifconfig | grep ‘inet addr:’172.168.100.137”| cut -d: -f2 | awk ‘{ print $1}’`”
if [ “$IP” ] ; then
echo “Found”
break
fi
done
END=$(date +%s.%N)
echo ” Use $(echo “$END – $START”|bc ) seconds for IP come up” >> $LOG
# … here do whatever you want to do …
exit 0
2. 图形界面
gnome-terminal的快捷方式
2.1 created bash file including start roslaunch command.
$ vi 00.start.sh
2.2. Step 2: Make the script executable by using command
$ sudo chmod +x /path/to/00.start.sh
2.3. System->preference->startup application, Add:
gnome-terminal -x /home/funstep/00.start.sh
we executed the command to autostart the bash when Ubuntu boots.
2.4. reboot system and your application would auto start on boot every time.
Notice 1:
It is not possible or rather difficult to open multiple terminals from a single script,
so it is recommended to use a single launch file to include everything.
Notice 2:
if using & ampersand or wait to run task in background:
{ command1 & command2 & } &
To execute a group of commands
( command1 & command2 ) &
execute commands in a list (subshell)
Notice 3:
It is bash rather than sh!
IF DEBUG needed, On one terminal, Edit –>profile preferences–>title and command tab , select when command exits hold the terminal open.
3. ros途径
robot_upstart方式
3.1 Intro
The robot_upstart package may be used to install/uninstall Ubuntu Linux upstart jobs which launch groups of roslaunch files.
This package aims to assist with creating simple platform-specific jobs to start robot’s ROS launch files when its PC powers up.
This package allows robot administrators to quickly and easily generate one or more background jobs to run roslaunch invocations on system startup.
关于robot_upstart主要优点:
automatically takes care of waiting for network and any other required services to come up.
roslaunch will automatically start a roscore if not already one running.
所以,Unless you have very specific reasons (ie: no upstart support), it is the best choise.
3.2 Install
$ unzip robot-upstart-src.zip ./
$ cartkin …
$ rosrun robot-upstart install xxx/launch/yyy.launch
you will meet fail, then:
$ mv ./robot-upstart-src/ ~/
$ sudo apt-get install ros-indigo-robot-upstart
$ rosrun robot-upstart install xxx/launch/yyy.launch
meet failed again, then:
$ mv ~/robot-upstart-src ./
$ cartkin …
Be Okay!
$ sudo apt-get install ros-indigo-robot-upstart
WARNING: The following packages cannot be authenticated!
ros-indigo-robot-upstart
Install these packages without verification? [y/N]
y
3.3 Usage
The basic or one-off usage is with the install script:
$ rosrun robot_upstart install funbot/launch/r_rbt.launch
or
$ rosrun robot_upstart install funbot/launch/r_rbt.launch –interface wlan2
——
Install files to the following paths:
/etc/init/funbot.conf
/etc/ros/indigo/funbot.d/.installed_files
/etc/ros/indigo/funbot.d/r_rbt.launch
/usr/sbin/funbot-start
/usr/sbin/funbot-stop
Now calling: /usr/bin/sudo /opt/ros/indigo/lib/robot_upstart/mutate_files
[sudo] password for funstep:
Filesystem operation succeeded.
这样把launch作为服务,开机自动启动。
uninstall?
$ rosrun robot_upstart uninstall funbot
If the job is crashing on startup or otherwise want to see what is being output to the terminal on startup:
$ sudo vi /var/log/upstart/funbot.log
It will start automatically when you next start your machine.
You can bring it up and down manually, or via a simple Python API:
$ sudo service funbot start
$ sudo service funbot stop
3.4 upstart自启动服务中的串口通讯
The robot_upstart executes as root and uses setuidgid to run the roslaunch process as the appropriate unprivileged user:
https://github.com/clearpathrobotics/robot_upstart/blob/hydro-devel/tmpl/start#L80
Unfortunately, setuidgid does not pick up the user’s group memberships, so it need a udev rule to either make the device world RW, or to specifically chown it to your ROS user (administrator, on Clearpath-configured robot PCs).
首先,为解决当前用户没有打开串口权限问题,要把当前用户加到串口用户组。
查看串口所在组
$ ls -l /dev/ttyS0
crw-rw—- 1 root dialout 4, 64 1月 27 21:52 /dev/ttyS0
查看当前用户所在组
$ id -Gn
funstep adm lp cdrom floppy sudo audio video plugdev users netdev lpadmin scanner
将当前用户加入到串口所在组dialout
$ sudo adduser funstep dialout
注销重新登录
其次可以用临时方案:
Short term solution, this command would allow full read/write access to serial port ttyS0:
$ sudo chmod 666 /dev/ttyS0
或者永久解决:
Long term solution, it is to install a udev rule using custom udev rules
$ cd /etc/udev/rules.d
(sudo) create a new file
$ sudo vi 40-permissions.rules
KERNEL==”ttyS0″, MODE=”0666″
Then run this command to update your udev rules:
$ sudo service udev restart
All users should now have full access to ttyS0.
发表评论
Want to join the discussion?Feel free to contribute!