roscore源码

执行which roscore的结果是/opt/ros/kinetic/bin/roscore,本身是个python脚本。

roscore的源代码在https://github.com/ros/ros_comm/blob/melodic-devel/tools/roslaunch/scripts/roscore,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

import sys
from optparse import OptionParser
from rosmaster.master_api import NUM_WORKERS

NAME = 'roscore'

def _get_optparse():

parser = OptionParser(usage="usage: %prog [options]",
prog=NAME,
description="roscore will start up a ROS Master, a ROS Parameter Server and a rosout logging node",
epilog="See http://wiki.ros.org/roscore"
)
parser.add_option("-p", "--port",
dest="port", default=None,
help="master port. Only valid if master is launched", metavar="PORT")
parser.add_option("-v", action="store_true",
dest="verbose", default=False,
help="verbose printing")
parser.add_option("-w", "--numworkers",
dest="num_workers", default=NUM_WORKERS, type=int,
help="override number of worker threads", metavar="NUM_WORKERS")
parser.add_option("-t", "--timeout",
dest="timeout",
help="override the socket connection timeout (in seconds).", metavar="TIMEOUT")
parser.add_option("--master-logger-level",
dest="master_logger_level", default=False, type=str,
help="set rosmaster.master logger level ('debug', 'info', 'warn', 'error', 'fatal')")
return parser


parser = _get_optparse()
(options, args) = parser.parse_args(sys.argv[1:])
#从这里知道roscore 并不支持参数
if len(args) > 0:
parser.error("roscore does not take arguments")

#roscore实际只调用roslanch.main
import roslaunch
roslaunch.main(['roscore', '--core'] + sys.argv[1:])

roslaunch.main

roslaunch.main 其实主要做了几件事,

  • 创建保存log的文件夹
  • 启动roslaunch server
  • 启动roscores master和rosout

roslaunch脚本只有两句话,其实是导入了roslaunch包,执行了roslaunch.main()函数:

1
2
import roslaunch
roslaunch.main()

roslaunch.main()的源码在http://docs.ros.org/kinetic/api/roslaunch/html/ 的217行。