免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2512 | 回复: 0

[应用] mavlink_main.cpp学习 [复制链接]

论坛徽章:
0
发表于 2017-07-31 21:57 |显示全部楼层
本帖最后由 ch122633 于 2017-07-31 22:01 编辑
  1. int mavlink_main(int argc, char *argv[])
  2. {
  3.         if (argc < 2) {
  4.                 usage();                                                                                                                      //使用说明
  5.                 return 1;
  6.         }

  7.         if (!strcmp(argv[1], "start")) {
  8.                 return Mavlink::start(argc, argv);                               //在这里启动了mavlink            

  9.         } else if (!strcmp(argv[1], "stop")) {                                      //命令是stop-all
  10.          PX4_WARN("mavlink stop is deprecated, use stop-all instead");
  11.                 usage();
  12.                 return 1;

  13.         } else if (!strcmp(argv[1], "stop-all")) {
  14.                 return Mavlink::destroy_all_instances();                               //关闭start里创建的任务

  15.         } else if (!strcmp(argv[1], "status")) {
  16.                 return Mavlink::get_status_all_instances();                          //打印start中创建的任务信息

  17.         } else if (!strcmp(argv[1], "stream")) {
  18.                 return Mavlink::stream_command(argc, argv);

  19.         } else if (!strcmp(argv[1], "boot_complete")) {
  20.                 Mavlink::set_boot_complete();
  21.                 return 0;

  22.         } else {
  23.                 usage();
  24.                 return 1;
  25.         }

  26.         return 0;
  27. }
复制代码
  1. Mavlink::start(int argc, char *argv[])
  2. {
  3.         MavlinkULog::initialize();
  4.         MavlinkCommandSender::initialize();

  5.         // Wait for the instance count to go up one
  6.         // before returning to the shell
  7.         int ic = Mavlink::instance_count();

  8.         if (ic == Mavlink::MAVLINK_MAX_INSTANCES) {                    //确认不超过最大个数
  9.                 warnx("Maximum MAVLink instance count of %d reached.",
  10.                       (int)Mavlink::MAVLINK_MAX_INSTANCES);
  11.                 return 1;
  12.         }

  13.         // Instantiate thread
  14.         char buf[24];
  15.         sprintf(buf, "mavlink_if%d", ic);

  16.         // This is where the control flow splits
  17.         // between the starting task and the spawned
  18.         // task - start_helper() only returns
  19.         // when the started task exits.                                                //这里创建了start_helper的守护进程
  20.         px4_task_spawn_cmd(buf,
  21.                            SCHED_DEFAULT,
  22.                            SCHED_PRIORITY_DEFAULT,
  23.                            2500,
  24.                            (px4_main_t)&Mavlink::start_helper,
  25.                            (char *const *)argv);

  26.         // Ensure that this shell command
  27.         // does not return before the instance
  28.         // is fully initialized. As this is also
  29.         // the only path to create a new instance,
  30.         // this is effectively a lock on concurrent
  31.         // instance starting. XXX do a real lock.

  32.         // Sleep 500 us between each attempt
  33.         const unsigned sleeptime = 500;

  34.         // Wait 100 ms max for the startup.
  35.         const unsigned limit = 100 * 1000 / sleeptime;

  36.         unsigned count = 0;

  37.         while (ic == Mavlink::instance_count() && count < limit) {                        //轮询等待
  38.                 ::usleep(sleeptime);
  39.                 count++;
  40.         }

  41.         return OK;
  42. }
复制代码
  1. int Mavlink::start_helper(int argc, char *argv[])
  2. {
  3.         /* create the instance in task context */
  4.         Mavlink *instance = new Mavlink();

  5.         int res;

  6.         if (!instance) {

  7.                 /* out of memory */
  8.                 res = -ENOMEM;
  9.                 warnx("OUT OF MEM");

  10.         } else {
  11.                 /* this will actually only return once MAVLink exits */
  12.                 res = instance->task_main(argc, argv);                                                                //这里是运行了task_main函数
  13.                 instance->_task_running = false;

  14.         }

  15.         return res;
  16. }
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP