X5之Mavlink

来源:互联网 发布:简阳市环保局何知云 编辑:程序博客网 时间:2024/04/28 18:09



1、int Mavlink::task_main(int argc, char *argv[])

函数里循环体里有

        /* update streams */
        MavlinkStream *stream;
        LL_FOREACH(_streams, stream) {
            stream->update(t);
        }

其中int
MavlinkStream::update(const hrt_abstime t)
{
    uint64_t dt = t - _last_sent;
    unsigned int interval = _interval;

    if (!const_rate()) {
        interval /= _mavlink->get_rate_mult();
    }

    if (dt > 0 && dt >= interval) {
        /* interval expired, send message */
        send(t);

        if (const_rate()) {
            _last_sent = (t / _interval) * _interval;

        } else {
            _last_sent = t;
        }


注意对变量MavlinkStream        *_streams的赋值是在下面的函数中进行的。

int
Mavlink::configure_stream(const char *stream_name, const float rate)
{
    /* calculate interval in us, 0 means disabled stream */
    unsigned int interval = interval_from_rate(rate);

    /* search if stream exists */
    MavlinkStream *stream;
    LL_FOREACH(_streams, stream) {
        if (strcmp(stream_name, stream->get_name()) == 0) {
            if (interval > 0) {
                /* set new interval */
                stream->set_interval(interval);

            } else {
                /* delete stream */
                LL_DELETE(_streams, stream);
                delete stream;
            }

            return OK;
        }
    }

    if (interval == 0) {
        /* stream was not active and is requested to be disabled, do nothing */
        return OK;
    }

    /* search for stream with specified name in supported streams list */
    for (unsigned int i = 0; streams_list[i] != nullptr; i++) {

        if (strcmp(stream_name, streams_list[i]->get_name()) == 0) {
            /* create new instance */
            stream = streams_list[i]->new_instance(this);
            stream->set_interval(interval);
            LL_APPEND(_streams, stream);

            return OK;
        }
    }

    /* if we reach here, the stream list does not contain the stream */
    warnx("stream %s not found", stream_name);

    return ERROR;
}


        return 0;
    }

    return -1;
}

send()函数是类MavlinkStream的一个纯虚函数。

在这里实际上是根据 stream指向的实际类调用相应类的send()函数。这些实际的send()函数在文件里src\modules\mavlink\mavlink_messages.cpp里。

比如心跳包消息MavlinkStreamHeartbeat 就是这样发送的,显然在这里周期发送的。

0 0
原创粉丝点击