- 论坛徽章:
- 0
|
本帖最后由 ch122633 于 2017-07-31 22:01 编辑
- int mavlink_main(int argc, char *argv[])
- {
- if (argc < 2) {
- usage(); //使用说明
- return 1;
- }
- if (!strcmp(argv[1], "start")) {
- return Mavlink::start(argc, argv); //在这里启动了mavlink
- } else if (!strcmp(argv[1], "stop")) { //命令是stop-all
- PX4_WARN("mavlink stop is deprecated, use stop-all instead");
- usage();
- return 1;
- } else if (!strcmp(argv[1], "stop-all")) {
- return Mavlink::destroy_all_instances(); //关闭start里创建的任务
- } else if (!strcmp(argv[1], "status")) {
- return Mavlink::get_status_all_instances(); //打印start中创建的任务信息
- } else if (!strcmp(argv[1], "stream")) {
- return Mavlink::stream_command(argc, argv);
- } else if (!strcmp(argv[1], "boot_complete")) {
- Mavlink::set_boot_complete();
- return 0;
- } else {
- usage();
- return 1;
- }
- return 0;
- }
复制代码- Mavlink::start(int argc, char *argv[])
- {
- MavlinkULog::initialize();
- MavlinkCommandSender::initialize();
- // Wait for the instance count to go up one
- // before returning to the shell
- int ic = Mavlink::instance_count();
- if (ic == Mavlink::MAVLINK_MAX_INSTANCES) { //确认不超过最大个数
- warnx("Maximum MAVLink instance count of %d reached.",
- (int)Mavlink::MAVLINK_MAX_INSTANCES);
- return 1;
- }
- // Instantiate thread
- char buf[24];
- sprintf(buf, "mavlink_if%d", ic);
- // This is where the control flow splits
- // between the starting task and the spawned
- // task - start_helper() only returns
- // when the started task exits. //这里创建了start_helper的守护进程
- px4_task_spawn_cmd(buf,
- SCHED_DEFAULT,
- SCHED_PRIORITY_DEFAULT,
- 2500,
- (px4_main_t)&Mavlink::start_helper,
- (char *const *)argv);
- // Ensure that this shell command
- // does not return before the instance
- // is fully initialized. As this is also
- // the only path to create a new instance,
- // this is effectively a lock on concurrent
- // instance starting. XXX do a real lock.
- // Sleep 500 us between each attempt
- const unsigned sleeptime = 500;
- // Wait 100 ms max for the startup.
- const unsigned limit = 100 * 1000 / sleeptime;
- unsigned count = 0;
- while (ic == Mavlink::instance_count() && count < limit) { //轮询等待
- ::usleep(sleeptime);
- count++;
- }
- return OK;
- }
复制代码- int Mavlink::start_helper(int argc, char *argv[])
- {
- /* create the instance in task context */
- Mavlink *instance = new Mavlink();
- int res;
- if (!instance) {
- /* out of memory */
- res = -ENOMEM;
- warnx("OUT OF MEM");
- } else {
- /* this will actually only return once MAVLink exits */
- res = instance->task_main(argc, argv); //这里是运行了task_main函数
- instance->_task_running = false;
- }
- return res;
- }
复制代码
|
|