PX4 固件中,mixer 是一個系統命令(位於 Firmware/src/systemcmds/mixer),負責將混控定義文件(.mix)解析並加載到具體輸出設備(如 UAVCAN ESC,設備節點 /dev/uavcan/esc)。設備驅動中的 MixerGroup 負責解析並執行混控邏輯。
| 類型 | 用途 |
|---|---|
NullMixer | 佔位未分組的輸出通道 |
SimpleMixer | 0~N 個輸入線性組合為 1 個輸出 |
MultirotorMixer | 將 ROLL/PITCH/YAW/THROTTLE 按預定義幾何矩陣映射為多輸出(多旋翼電調) |
mixer_load.c 中 load_mixer_file() 讀取 .mix 文件,過濾非法行(長度<2、首字符非大寫、第二字符非 ':'),將合法行存入緩衝區。ioctl(dev, MIXERIOCLOADBUF, buf) 將緩衝區傳給設備驅動。uavcan_main.cpp 的 ioctl() 處理 MIXERIOCLOADBUF,調用 MixerGroup::load_from_buf() 逐行解析。// 單通道縮放參數(對應 O: 後 5 個整數 / 10000.0f)
struct mixer_scaler_s {
float negative_scale; // 負向縮放
float positive_scale; // 正向縮放
float offset; // 偏移
float min_output; // 最小輸出
float max_output; // 最大輸出
};
// 輸入控制源(對應 S: 後 7 個整數)
struct mixer_control_s {
uint8_t control_group; // 輸入組號
uint8_t control_index; // 組內索引
struct mixer_scaler_s scaler; // 縮放參數
};
// SimpleMixer 定義
struct mixer_simple_s {
uint8_t control_count; // 輸入數量
struct mixer_scaler_s output_scaler; // 輸出縮放
struct mixer_control_s controls[0]; // 柔性數組,大小由 control_count 決定
};
MixerGroup::load_from_buf()遍歷緩衝區,根據行首字符分發:
Z → NullMixer::from_text()M → SimpleMixer::from_text()R → MultirotorMixer::from_text()解析成功則 add_mixer(m) 加入組,更新剩餘緩衝長度。
sscanf(buf, "M: %u%n", &inputs, &used); // M: 3
buf = skipline(buf, buflen); // 移至下一行
mixinfo = malloc(MIXER_SIMPLE_SIZE(inputs));
// MIXER_SIMPLE_SIZE = sizeof(mixer_simple_s) + inputs * sizeof(mixer_control_s)
int parse_output_scaler(const char* buf, unsigned& buflen, mixer_scaler_s& scaler) {
buf = findtag(buf, buflen, 'O'); // 找到 O: 行
int s[5];
sscanf(buf, "O: %d %d %d %d %d %n", &s[0], &s[1], &s[2], &s[3], &s[4], &n);
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;
}
for (unsigned i = 0; i < inputs; i++) {
parse_control_scaler(buf, buflen,
mixinfo->controls[i].scaler,
mixinfo->controls[i].control_group,
mixinfo->controls[i].control_index);
}
// S: group index neg_scale pos_scale offset min max
M: 2 # 2 個輸入
O: 10000 10000 0 -10000 10000 # 輸出縮放
S: 0 0 10000 10000 0 -10000 10000 # 輸入 0: 組0 索引0
S: 0 1 10000 10000 0 -10000 10000 # 輸入 1: 組0 索引1
Mixer 機制將控制分配邏輯從飛控核心剝離,通過文本文件配置,實現了靈活的輸出映射。理解其數據結構與解析流程,有助於開發自定義混控、調試輸出異常、移植新硬件平台。