134 lines
5.3 KiB
Python
134 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""拆分 底盘/云台 的 robot_param.h:
|
|
- 公共参数 -> Simba-OmniInfantry/common/robot_param_common.h
|
|
- 底盘专属 -> 底盘/Application/robot_param.h (include common)
|
|
- 云台专属 -> 云台/Application/robot_param.h (include common)
|
|
|
|
按"参数归属"分类(不是按文件里是否出现):
|
|
COMMON : 两块板都会编译/使用的公共项(CToC ID、掉线时间、IMU 温控、蜂鸣器调试)
|
|
CHASSIS: MECANUM_* / CHASSIS_* / POWER_LIMIT_* (底盘专属;CHASSIS_SPIN_FEEDFORWARD_K 实际用于云台,归 GIMBAL)
|
|
GIMBAL : 其余(GIMBAL_* / PITCH_* / YAW_* / PC_MOUSE_* / DEBUG_SHOOT_* / 台架拨弹等)
|
|
|
|
原文件是 GBK 编码,输出保持 GBK,避免 µVision 显示/编译问题。
|
|
"""
|
|
import os
|
|
import re
|
|
import shutil
|
|
import tempfile
|
|
|
|
BASE = r'E:\ELCT\Simba-OmniInfantry'
|
|
CHASSIS = os.path.join(BASE, '底盘', 'Application', 'robot_param.h')
|
|
GIMBAL = os.path.join(BASE, '云台', 'Application', 'robot_param.h')
|
|
COMMON = os.path.join(BASE, 'common', 'robot_param_common.h')
|
|
os.makedirs(os.path.dirname(COMMON), exist_ok=True)
|
|
|
|
COMMON_NAMES = {
|
|
'CTOC_MASTER_ID1', 'CTOC_SLAVE_ID1', 'CTOC_SLAVE_ID2', 'CTOC_SLAVE_ID3', 'CTOC_SLAVE_ID4',
|
|
'CTOC_OFFLINE_TIME_MS', 'DEVICE_OFFLINE_TIME_MS',
|
|
'IMU_TEMPERATURE_TARGET', 'IMU_TEMPERATURE_LIMIT',
|
|
'IMU_TEMP_KP', 'IMU_TEMP_KI', 'IMU_TEMP_KD',
|
|
'DEBUG_BUZZER_MELODY', 'BUZZER_MELODY_SPEED_DIV',
|
|
}
|
|
|
|
|
|
def is_chassis_name(n):
|
|
if n == 'CHASSIS_SPIN_FEEDFORWARD_K':
|
|
return False # 名字带 CHASSIS,实际用于云台自旋前馈
|
|
return n.startswith(('MECANUM_', 'CHASSIS_', 'POWER_LIMIT_'))
|
|
|
|
|
|
def split_lines(path):
|
|
"""返回行列表,以及每个 #define 行对应的宏名(按行号索引)。"""
|
|
lines = open(path, encoding='gbk', errors='replace').read().splitlines(keepends=True)
|
|
names = {}
|
|
for i, ln in enumerate(lines):
|
|
m = re.match(r'\s*#\s*define\s+(\w+)\b', ln)
|
|
if m:
|
|
names[i] = m.group(1)
|
|
return lines, names
|
|
|
|
|
|
def filter_file(path, keep):
|
|
lines, names = split_lines(path)
|
|
out = []
|
|
removed = []
|
|
in_guard = True
|
|
for i, ln in enumerate(lines):
|
|
n = names.get(i)
|
|
if n is None:
|
|
out.append(ln)
|
|
continue
|
|
if n in keep:
|
|
out.append(ln)
|
|
else:
|
|
removed.append(n)
|
|
return out, removed
|
|
|
|
|
|
ch_lines, ch_names = split_lines(CHASSIS)
|
|
gy_lines, gy_names = split_lines(GIMBAL)
|
|
all_names = set(ch_names.values()) | set(gy_names.values())
|
|
|
|
ch_keep = {n for n in all_names if (n in COMMON_NAMES) or (n in ch_names.values() and not is_chassis_name(n) is False)}
|
|
# 底盘保留:公共 + 底盘专属
|
|
ch_keep = {n for n in ch_names.values() if n in COMMON_NAMES or is_chassis_name(n)}
|
|
gy_keep = {n for n in gy_names.values() if n in COMMON_NAMES or not is_chassis_name(n)}
|
|
|
|
# 1) 公共头:从云台文件里按原顺序抽取公共 define 行
|
|
common_lines = []
|
|
for ln, n in zip(gy_lines, [gy_names.get(i) for i in range(len(gy_lines))]):
|
|
if n in COMMON_NAMES:
|
|
common_lines.append(' ' + ln.replace('\n', '') + '\n')
|
|
|
|
common_text = '''/* =============================================================================
|
|
* robot_param_common.h - 底盘/云台/module_test 共用参数(单一来源)
|
|
*
|
|
* 本文件由 common 统一维护,底盘和云台各自的 robot_param.h 通过
|
|
* #include "../../common/robot_param_common.h"
|
|
* 引用,不再各存一份。
|
|
*
|
|
* 内容:板间通讯 CToC 帧 ID、掉线判定时间、IMU 温控、蜂鸣器/调试开关。
|
|
* 底盘专属参数见 底盘/Application/robot_param.h;
|
|
* 云台专属参数见 云台/Application/robot_param.h。
|
|
* ========================================================================== */
|
|
#ifndef ROBOT_PARAM_COMMON_H
|
|
#define ROBOT_PARAM_COMMON_H
|
|
|
|
''' + ''.join(common_lines) + '''
|
|
#endif /* ROBOT_PARAM_COMMON_H */
|
|
'''
|
|
open(COMMON, 'w', encoding='gbk', errors='replace').write(common_text)
|
|
print('common written:', COMMON, 'macros:', len(common_lines))
|
|
|
|
|
|
def rewrite(path, keep, guard, label):
|
|
lines, names = split_lines(path)
|
|
out = []
|
|
for i, ln in enumerate(lines):
|
|
n = names.get(i)
|
|
if n is None:
|
|
out.append(ln)
|
|
elif n in keep:
|
|
out.append(ln)
|
|
text = ''.join(out)
|
|
# 替换 include guard 并插入 common include
|
|
text = text.replace('#ifndef ROBOT_PARAM_H', '#ifndef %s' % guard, 1)
|
|
text = text.replace('#define ROBOT_PARAM_H', '#define %s' % guard, 1)
|
|
text = text.replace('#define %s\n' % guard,
|
|
'#define %s\n\n#include "../../common/robot_param_common.h"\n' % guard, 1)
|
|
open(path, 'w', encoding='gbk', errors='replace').write(text)
|
|
print(label, 'kept macros:', len([n for n in names.values() if n in keep]))
|
|
|
|
|
|
rewrite(CHASSIS, ch_keep, 'ROBOT_PARAM_CHASSIS_H', 'chassis:')
|
|
rewrite(GIMBAL, gy_keep, 'ROBOT_PARAM_GIMBAL_H', 'gimbal:')
|
|
|
|
# 简单验证:交集不应包含跨板参数
|
|
for p, keep, label in [(CHASSIS, ch_keep, 'chassis'), (GIMBAL, gy_keep, 'gimbal')]:
|
|
lines, names = split_lines(p)
|
|
got = set(names.values())
|
|
print(label, 'macros now:', len(got))
|
|
extra_c = sorted(n for n in got if is_chassis_name(n) and label == 'gimbal')
|
|
extra_g = sorted(n for n in got if (not is_chassis_name(n)) and n not in COMMON_NAMES and label == 'chassis')
|
|
print(' cross-board leftovers:', extra_c + extra_g)
|