Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

PX4 mixer 結構剖析:以 UAVCAN 為例

編輯:關於 Android 開發

PX4 mixer 結構剖析:以 UAVCAN 為例

mixer 是 PX4 系統層級的 app 指令,原始碼位於 Firmware/src/systemcmds/mixer。它的職責是把 mix 檔中的有效內容載入到具體裝置裡,再由裝置內的 MixerGroups 去解析這些定義。

本文以 UAVCAN 為例。系統啟動後,裝置節點的名稱是 /dev/uavcan/esc,UAVCAN 的定義中包含 MixerGroup 與 Output 的實例。

三種 Mixer 類型

  • NullMixer:為未分組的輸出通道佔位。
  • SimpleMixer:把零個或多個輸入融合成一個輸出。
  • MultirotorMixer:把輸入量(ROLL、PITCH、YAW、THROTTLE)依預先定義的 geometry 融合成一組輸出量。

mix 檔是怎麼被讀進來的

讀檔函式位於 Firmware/src/modules/systemlib/mixer_load/mixer_load.c:

int load_mixer_file(const char *fname, char *buf, unsigned maxlen)

fname 是 mix 檔在系統中的路徑,buf 是暫存內容的緩衝區,maxlen 是緩衝區上限。

這個函式會剔除任何符合下列條件的行:

  1. 行長度小於 2 個字元
  2. 首字元不是大寫字母
  3. 第二個字元不是 :

換句話說,只有形如 X: 開頭的行才算合法。過濾之後剩下的內容會被完整寫入 buf,所以撰寫 mix 檔時務必遵守這個格式。

接著透過 ioctl 把緩衝區交給裝置處理:

int ret = ioctl(dev, MIXERIOCLOADBUF, (unsigned long)buf);

相關結構的定義

/** simple channel scaler */
struct mixer_scaler_s {
    float negative_scale; // 負向縮放,mix 檔 O: 後第 1 個整數 / 10000.0f
    float positive_scale; // 正向縮放,O: 後第 2 個整數 / 10000.0f
    float offset;         // 偏移量,O: 後第 3 個整數 / 10000.0f
    float min_output;     // 最小輸出,O: 後第 4 個整數 / 10000.0f
    float max_output;     // 最大輸出,O: 後第 5 個整數 / 10000.0f
};

/** mixer input */
struct mixer_control_s {
    uint8_t control_group;        // 輸入讀取的所屬群組
    uint8_t control_index;        // 群組內的索引
    struct mixer_scaler_s scaler; // 使用前套用的縮放
};

/** simple mixer */
struct mixer_simple_s {
    uint8_t control_count;              // 輸入數量
    struct mixer_scaler_s output_scaler;// 輸出的縮放設定
    struct mixer_control_s controls[0]; // 實際長度由 control_count 決定
};

這三個結構的層級關係一目了然:mixer_scaler_s 描述單一控制量的縮放參數;mixer_control_s 在此之上多了「這個輸入來自哪一組、組內第幾個」;mixer_simple_s 則是完整的控制實體——因為一個 mixer 只有一個輸出、可有零到多個輸入,所以 control_count 指明輸入數量,實際資料存放在 controls[0] 這個柔性陣列裡,輸出則由 output_scaler 掌控。

把這些結構和 mix 檔的語法擺在一起對照,格式規則就完全說得通了。

uavcan_main.cpp 中的解析入口

int UavcanNode::ioctl(file *filp, int cmd, unsigned long arg)
{
    ...
    case MIXERIOCLOADBUF: {
        const char *buf = (const char *)arg;
        unsigned buflen = strnlen(buf, 1024);

        if (_mixers == nullptr) {
            _mixers = new MixerGroup(control_callback, (uintptr_t)_controls);
        }

        if (_mixers == nullptr) {
            _groups_required = 0;
            ret = -ENOMEM;
        } else {
            ret = _mixers->load_from_buf(buf, buflen); // 開始解析資料
            if (ret != 0) {
                warnx("mixer load failed with %d", ret);
                delete _mixers;
                _mixers = nullptr;
                _groups_required = 0;
                ret = -EINVAL;
            } else {
                _mixers->groups_required(_groups_required);
            }
        }
        break;
    }
    ...
}

MixerGroup::load_from_buf —— 用首字元決定型別

int MixerGroup::load_from_buf(const char *buf, unsigned &buflen)
{
    int ret = -1;
    const char *end = buf + buflen;

    while (buflen > 0) {
        Mixer *m = nullptr;
        const char *p = end - buflen;
        unsigned resid = buflen;

        // 用該行的第一個字母決定要建構哪一種 mixer
        switch (*p) {
        case 'Z':
            m = NullMixer::from_text(p, resid);
            break;
        case 'M':
            m = SimpleMixer::from_text(_control_cb, _cb_handle, p, resid);
            break;
        case 'R':
            m = MultirotorMixer::from_text(_control_cb, _cb_handle, p, resid);
            break;
        default:
            /* 大概是空白或垃圾資料,跳過一個 byte 再試 */
            buflen--;
            continue;
        }

        if (m != nullptr) {
            add_mixer(m);
            ret = 0;
            buflen = resid; // 只有解析成功才前移指標
            debug("SUCCESS - buflen: %d", buflen);
        } else {
            /* 有資料卻解析不出來,放棄 */
            break;
        }
    }
    return ret;
}

這裡的設計巧思在於:Z / M / R 三個字母就對應 NullMixer、SimpleMixer(M 應該是早期 Mixed 的沿用)、MultirotorMixer,用一個 switch 完成型別分派。

SimpleMixer::from_text —— 解析 M: 開頭的定義

格式規定 M: 後面跟著一個數字,用來指明輸入訊號源的數量,也就是 struct mixer_control_s 的個數。

SimpleMixer *
SimpleMixer::from_text(Mixer::ControlCallback control_cb, uintptr_t cb_handle,
                       const char *buf, unsigned &buflen)
{
    SimpleMixer *sm = nullptr;
    mixer_simple_s *mixinfo = nullptr;
    unsigned inputs;
    int used;
    const char *end = buf + buflen;

    // 取得 mixer 的基礎資訊
    if (sscanf(buf, "M: %u%n", &inputs, &used) != 1) {
        debug("simple parse failed on '%s'", buf);
        goto out;
    } // 把 M: 後第一個數值複製到 inputs,並把已處理的字元數寫入 used

    buf = skipline(buf, buflen); // 讓 buf 指向下一行
    if (buf == nullptr) {
        debug("no line ending, line is incomplete");
        goto out;
    }

    // M: 後面的數字即 controls 陣列的長度
    // MIXER_SIMPLE_SIZE(n) = sizeof(mixer_simple_s) + n * sizeof(mixer_control_s)
    mixinfo = (mixer_simple_s *)malloc(MIXER_SIMPLE_SIZE(inputs));
    if (mixinfo == nullptr) {
        debug("could not allocate memory for mixer info");
        goto out;
    }

    mixinfo->control_count = inputs;

    // 解析輸出域,填充到 output_scaler
    if (parse_output_scaler(end - buflen, buflen, mixinfo->output_scaler)) {
        debug("simple mixer parser failed parsing out scaler tag");
        goto out;
    }

    // 解析輸入量,因為已知數量所以要跑 inputs 次
    for (unsigned i = 0; i < inputs; i++) {
        if (parse_control_scaler(end - buflen, buflen,
                                 mixinfo->controls[i].scaler,
                                 mixinfo->controls[i].control_group,
                                 mixinfo->controls[i].control_index)) {
            debug("simple mixer parser failed parsing ctrl scaler tag");
            goto out;
        }
    }

    sm = new SimpleMixer(control_cb, cb_handle, mixinfo);
    if (sm != nullptr) {
        mixinfo = nullptr; // 所有權轉移,別再 free
        debug("loaded mixer with %d input(s)", inputs);
    } else {
        debug("could not allocate memory for mixer");
    }

out:
    if (mixinfo != nullptr) {
        free(mixinfo);
    }
    return sm;
}

O: 行 —— 解析輸出縮放

int SimpleMixer::parse_output_scaler(const char *buf, unsigned &buflen,
                                     mixer_scaler_s &scaler)
{
    int ret;
    int s[5];
    int n = -1;

    buf = findtag(buf, buflen, 'O'); // 尋找 "O:" 控制符,回傳指向 'O' 的指標

    // 12 表示這行至少有 12 個字元("O: " 加上五個單字位整數)
    // 最短的合法定義就是:O: 0 0 0 0 0
    if ((buf == nullptr) || (buflen < 12)) {
        debug("output parser failed finding tag, ret: '%s'", buf);
        return -1;
    }

    // O: 後必須有 5 個整數,彼此以空白分隔
    if ((ret = sscanf(buf, "O: %d %d %d %d %d %n",
                      &s[0], &s[1], &s[2], &s[3], &s[4], &n)) != 5) {
        debug("out scaler parse failed on '%s' (got %d, consumed %d)", buf, ret, n);
        return -1;
    }

    buf = skipline(buf, buflen);
    if (buf == nullptr) {
        debug("no line ending, line is incomplete");
        return -1;
    }

    // 從這裡的赋值可看出 O: 後 5 個數的含義,且全部要除以 10000
    // 這也解釋了 mix 格式文件為何說這些值是被放大 10000 倍後的整數
    scaler.negative_scale = s[0] / 10000.0f;
    scaler.positive_scale = s[1] / 10000.0f;
    scaler.offset         = s[2] / 10000.0f;
    scaler.min_output     = s[3] / 10000.0f;
    scaler.max_output     = s[4] / 10000.0f;

    return 0;
}

用整數再除以 10000 來表達浮點參數,是嵌入式系統常見的做法:避開了早期工具鏈對浮點解析支援不足的問題,也讓 mix 檔保持純文字、易於手寫。

S: 行 —— 解析每個輸入通道

int SimpleMixer::parse_control_scaler(const char *buf, unsigned &buflen,
                                      mixer_scaler_s &scaler,
                                      uint8_t &control_group,
                                      uint8_t &control_index)
{
    unsigned u[2];
    int s[5];

    buf = findtag(buf, buflen, 'S'); // 找到第一個 'S' 並讓 buf 指向該行行首

    // 16 表示該 S: 行至少有 16 個字元,也就是至少 7 個整數
    if ((buf == nullptr) || (buflen < 16)) {
        debug("control parser failed finding tag, ret: '%s'", buf);
        return -1;
    }

    if (sscanf(buf, "S: %u %u %d %d %d %d %d",
               &u[0], &u[1], &s[0], &s[1], &s[2], &s[3], &s[4]) != 7) {
        debug("control parse failed on '%s'", buf);
        return -1;
    }

    buf = skipline(buf, buflen);
    if (buf == nullptr) {
        debug("no line ending, line is incomplete");
        return -1;
    }

    // S: 後 7 個整數依次為
    // [control_group] [control_index] [negative_scale] [positive_scale] [offset] [min_output] [max_output]
    // 比輸出定義多了前兩個,用來標明組號與組內序號
    control_group = u[0];
    control_index = u[1];
    scaler.negative_scale = s[0] / 10000.0f;
    scaler.positive_scale = s[1] / 10000.0f;
    scaler.offset         = s[2] / 10000.0f;
    scaler.min_output     = s[3] / 10000.0f;
    scaler.max_output     = s[4] / 10000.0f;

    return 0;
}

整體脈絡整理

把上面的碎片拼起來,mixer 的載入流程是:

  1. load_mixer_file() 讀檔並過濾掉非 X: 格式的雜訊行。
  2. 透過 ioctl(MIXERIOCLOADBUF) 把乾淨的緩衝區交給裝置。
  3. MixerGroup::load_from_buf() 逐行讀取,用首字元分派到對應的 Mixer 子類。
  4. 以 SimpleMixer 為例,from_text() 先讀 M: 取得輸入數量,配置記憶體,再依序解析一組 O:(輸出)與 N 組 S:(各輸入)。
  5. 所有參數除以 10000 還原成浮點,填入結構體,交給裝置使用。

理解這套「首字元分派 + 固定標籤行」的設計之後,要自訂混控行為時,就知道該從哪裡下手了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved