
3edbe50c34812a3f369dd45a1a23468d.ppt
- Количество слайдов: 126
Target. Link 2. 0 www. hirain. com 2004© ——目标代码生成 具 北 京 九 州 恒 润 科 技 有 限 公 司 1
d. SPACE公司 恒 润 科 技 有 限 公 司 n 成立于1988年 Paderborn n 在Detroit、Paris、Cambridge设立分 Paderborn 公司 n 在Stuttgart 和 Munich设立办事处 Holzgerlingen/ Stuttgart Pfaffenhofen/ Munich www. hirain. com 2004© n 总部位于 n 全球总计超过400名员 n 快速持续增长 2
d. SPACE公司 d. SPACE Ltd. Cambridge, United Kingdom Netherlands: Czech Republic and Slovak Republic: Sweden: d. SPACE Sarl Paris, France Poland: d. SPACE Gmb. H Paderborn, Germany d. SPACE Inc. Detroit, USA 恒 润 科 技 有 限 公 司 Japan: Korea: China: India: Taiwan: Australia: § § 在 12个国家设立代理商 恒润科技公司为中国独家代理 50%的产品销往德国以外 目前全球总计超过10, 000套产品系统 www. hirain. com 2004© Israel: 3
1. Target. Link介绍 n n n www. hirain. com 2004© n 背景 Target. Link简介 Target. Link组成 安装 恒 润 科 技 有 限 公 司 4
传统开发方法 功能开发者 算法知识 规范 产品代码 大量的调试时间 #include
代码生成方法 软件专家 代码实现知识 功能开发者 算法知识 Target. Link • ANSI-C • 语言特性 • 汇编语言 • 处理器特性 … 如何优化代码! www. hirain. com 2004© 代码实现知识: 恒 润 科 技 有 限 公 司 代码生成器 代码实现知识 6
什么是Target. Link? 前端 - MATLAB, Simulink, Stateflow - ASAM MCD 2 文件生成器 - 文档生成器 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 代码生成器 - 模型分析 - 模块间优化 - 产品级代码 后端 后端 后端 ANSI-C 处理器定制C & 汇编 OSEK/VDX Partner 7
Target. Link模块 Target. Link 模块 汽车 定制模块 OSEK/VDX 模块 Hitachi SH-2/ SH-2 E Infineon C 16 x Infineon Tricore Motorola 683 xx Mitsubishi M 32 R Motorola MPC 5 xx Motorola HCS 12/ HC 12 目标 仿真模块 Hitachi Motorola TI SH-2/ Infineon Motorola Mitsubishi Motorola HCS 12/ Tricore TMS 470 683 xx M 32 R SH-2 E, C 16 x MPC 5 xx HC 12 H 8 S 基本组成 ANSI C coder Data. Dictionary Target. Link Blockset Documentgenerator www. hirain. com 2004© 目标 优化模块 恒 润 科 技 有 限 公 司 Worst-case autoscaling Target. Link Base Suite License 8
Target. Link 安装要求 硬件 n n n 软件 § 系统: MS Windows 98, Me, NT 4. 0 SP 6 or 2000 SP 1, XP § Math. Works: MATLAB R 12. 1, R 13. 01, R 13 SP 1 § Mex编译器: Microsoft Visual C++ 6. 0/7. 0 Watcom Version 10. 6/11. 0 LCC as shipped with MATLAB § 目标编译器 (只用于目标仿真) 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 最小系统: Pentium II 500 MHz (or equivalent), 256 MB 内存, 340 MB 硬盘空间 推荐: Pentium IV 1. 6 GHz (or equivalent), 512 MB 内存, 340 MB 硬盘空间 目标仿真: 仿真板, COM口 不需要RTW 和 Stateflow Coder! 9
Target. Link的安装 Installation Manager § 用于管理d. SPACE 软件 § 安装和维护多个安装版本 § 在安装版本之间进行切换 www. hirain. com 2004© 可以在同一PC上安装多个版本的Target. Link! 恒 润 科 技 有 限 公 司 10
2. 代码优化原则 n n www. hirain. com 2004© n 代码要求 模块间优化 代码优化模板 恒 润 科 技 有 限 公 司 11
代码要求 1. 速度和存储器占有效率比手写代码低过20%是不可接受的。 2. 生成代码具有可读性并且易于检查。 3. 生成的代码是效率和可读性的折中。 No ’trick’ programming 。 4. 当要求满足时,可以生成ANSI C代码以及选择使用非ANSI C或汇编代码。 www. hirain. com 2004© Target. Link 恒 润 科 技 有 限 公 司 12
模块间优化 Stateflow和Simulink模块 通用的内部表现形式 的混合模型 恒 润 科 技 有 限 公 司 § § 无缝的整合Stateflow和Simulink 将Simulink函数嵌入Stateflow,反之亦然 将模块代码和少量C代码线间结合 消除中间变量 www. hirain. com 2004© 优点: 13
模块间优化的例子 RCP 代码 bool 1 = (In >= 10); bool 2 = (In <= -10); bool 3 = bool 1 || bool 2; if (bool 3) out = tmp 1; else out = tmp 2; Target. Link 代码 - 没有不必要的代码 - 不浪费执行时间 - 没有临时变量 www. hirain. com 2004© tmp 1 = table_lookup(Table, In); tmp 2 = In; 恒 润 科 技 有 限 公 司 if ((In >= UT) || (In <= LT)) out = table_lookup(Table, In); else out = In; 14
不同的建模方案 Target. Link 代码: if ((in >= UT) || (in <= LT)) out = table_lookup(Table, in); else out = in; 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 不同的建模方案生成相同的代码 15
模块优化 问题: § 同一中算法存在多种不同实现方式 § 不同的编译器/处理器之间的最优实现方式不同 恒 润 科 技 有 限 公 司 解决方法: C 16 x CPU 32 SH 2 . . . www. hirain. com 2004© § 代码模板库 § 对于每个编译器/处理器都有最佳代码模板 § 可以选择: • ANSI C • 汇编语言和定制C编译器 例子: + + 16 bit Saturated Addition 16
例子: Saturated Addition Solution 1: ANSI C, 真实性检测(plausibility check) Int 16 x 1, x 2, y; /* declaration */ y = x 1 + x 2; /* addition */ (x 1 >= 0) && (x 2 >= 0) && (y < 0) ) /* saturation */ = 32767; if ( (x 1 < 0) && (x 2 < 0) && (y >= 0) ) = -32768; Solution 2: ANSI C, 增加字宽(increase of word width) Int 16 x 1, x 2, y; Int 32 dummy; /* declaration */ dummy = (Int 32)x 1 + (Int 32)x 2; /* addition */ if (dummy > 32767) y = 32767; else if (dummy < -32768) y = -32768; else y = (Int 16)dummy; /* saturation */ www. hirain. com 2004© if ( y else y 恒 润 科 技 有 限 公 司 17
例子:Saturated Addition Solution 3: Assembly code, 溢出标志检测(overflow flag check) register Int 16 tmp_s 1 = s 1; register Int 16 tmp_s 2 = s 2; /* declaration */ r = asm(Int 16, " move. w ‘tmp_s 1‘, D 0 ", " add. w ‘tmp_s 2‘, D 0 ", " bvc *+16 ", " tst. w D 0 ", " blt *+8 ", " move. w #-32768, D 0 ", " move. w #-"#csatval"-1, D 0 ", " bra *+6 ", " move. w #32767, D 0 "); " move. w #"#csatval", D 0 "); ; addition ; saturation 恒 润 科 技 有 限 公 司 www. hirain. com 2004© Macro: AC__I 16 ADDI 16_SAT(x 1, x 2, y); Solution 4: Language extensions, saturation type yy == (_sat Int 16)x 1 ++ (_sat Int 16)x 2; (_sat Int 16)x 1 (_sat Int 16)x 2; /* addition */ 18
Saturated Addition的对比 对比表格 Method Solution 1 Solution 2 Solution 3 Solution 4 执行时间 代码长度 2. 2 µs 34 Bytes 2. 3 µs 28 Bytes 1. 8 µs 20 Bytes n/a Infineon C 167 执行时间 1. 8 µs 4. 0 µs 1. 0 µs n/a 代码长度 执行时间 36 Bytes 1. 2 µs 60 Bytes 1. 0 µs 20 Bytes n/a <0. 05 µs 代码长度 48 Bytes n/a 10 Bytes Infineon Tri. Core n/a Clock: 20. 0 MHz, 0 waitstates 选择最佳代码模板 Solution 1: ANSI C, plausibility check Solution 2: ANSI C, increase of word width Solution 3: Assembly code, overflow flag check Solution 4: Language extension, saturation type www. hirain. com 2004© Motorola M 68332 恒 润 科 技 有 限 公 司 19
3. Target. Link 作流程 n www. hirain. com 2004© n 仿真模式 典型的 作流程 恒 润 科 技 有 限 公 司 20
Target. Link 作流程 Target. Link 模块 Simulink 模块 ECU 恒 润 科 技 有 限 公 司 自动转换 代码 主机 浮点仿真 (MIL) 算法设计 § 执行有效性检测 § 定标 (自动或手动) § 溢出检测 § 参考跟踪 主机 产品代码仿真(SIL) 定点的影响 § 量化误差 § 饱和与溢出 § 输入实现选项 目标 产品代码仿真 (PIL) www. hirain. com 2004© 生成 代码确认 § 压缩执行时间 § 测量堆栈大小 § 测量RAM / ROM § 最终确认 21
典型 作流程 项目开始阶段 从Simulink模型转换 成Target. Link 从头开始创建模型 产品代码主机仿真 产品代码目标仿真 确定位精度 定点影响和C代码校验 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 浮点仿真 确定控制策略 编译器和处理器校验, 压缩代码 22
典型 作流程 项目集成阶段 变量类分配 函数分割和函数重用 ASAP-2 文件 生成器 § § § § 变量类Variable classes 函数类Function classes 用户类型User types 符合代码标准 模型转换Model conversion 特殊模型和用户代码库 ASAP-2 和文档文件模板 One-time settings 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 整合手写代码,继承代码,I/O程序和 RTOS 整体项目结构 (Data Dictionary) 文档 23
4. Target. Link模块 n n Target. Link仿真模块 Target. Link功能模块 模型转换 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 24
Target. Link模块 使用命令tllib打开 模块库 用于离散时间控制 器的所有典型模块 www. hirain. com 2004© 插入Stateflow Chart! 恒 润 科 技 有 限 公 司 25
直接支持的Simulink模块 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 这些模块不需要转换 26
专用模块 n 一些专用模块位于 l. Extras u u Flip-Flops双稳态多谐触发器 Stateflow变量的Logging • 采样模块 - 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 位操作模块 数学模块 非线性模块 辅助模块用于 - 调用 具 - 定制文档 27
Target. Link功能模块 Target. Link Main Dialog: 进行全 局设置以及 具 Target. Link 功能模块 在不同仿真模式之间 进行快速切换 Addfile block: include 或 link 用户源文件 Function block: 将 子系统生成独立的C函数 www. hirain. com 2004© Target. Link subsystem: 放置生成代码的子系统 恒 润 科 技 有 限 公 司 28
模型转换和模块库转换 每个模型包括一 个Target. Link Main Dialog www. hirain. com 2004© 步骤: Target. Link Main Dialog -> Tools -> Conversion 恒 润 科 技 有 限 公 司 对模型转换的要求: § 只有包含在Target. Link blockset内或者在libmaps中定义的模块 29
模型转换和模块库转换 n 选择需要转换的子系统 l选择需要的操作 n 选择要转换的参考库 l创建一个TL库和libmap 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 30
Step 1: 创建Target. Link模型 1. Load block diagram ‘throttle_control_step 1’. 2. Insert Target. Link Main Dialog. 3. Start conversion tool. Main Dialog/Tools/Simulink ---> Target. Link 4. . then highlight PI_Controller and click “Select current subsystem”. 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 31
5. 数据记录 恒 润 科 技 有 限 公 司 n 概述 操作 www. hirain. com 2004© n 32
Data Logging Services体系 Target. Link 模型 Property Manager logging properties TLDS 结果 Data Server Data Dictionary Manager Signal Management www. hirain. com 2004© 仿真 恒 润 科 技 有 限 公 司 MATLAB workspace *. mat 文件 visualization 33
数据记录 Global logging options (in Target. Link Main Dialog) 不对单独模块进行操作 1. Log according to local settings 2. Log all blocks 3. Log min/max values only www. hirain. com 2004© 4. Log nothing Block-specific logging options (在每个模块内) 1. Log each sample step 2. Log min/max values only 恒 润 科 技 有 限 公 司 Inf 3. Log nothing Index of vector element Max. number of logged samples 34
数据记录对代码的影响 Data logging enabled § 在代码中插入宏(LOG_VAR, LOG_VEC, LOG_BIT) § 记录的变量无法被代码优化器消除 Data logging disabled 只留下应用程序代码 www. hirain. com 2004© - 代码更多 - 堆栈更大more stack size - 执行时间更多 恒 润 科 技 有 限 公 司 - 严格精简的结果 - 产品级的代码 35
6. 定标 n n n www. hirain. com 2004© n 定标选项 默认值 浮点仿真 使用仿真结果进行自动定标 恒 润 科 技 有 限 公 司 36
整型变量的定标 原则: 基于模块定标 速度/存储器/精度的关系: high 执行速度 low power-of-two scaling with offset arbitrary scaling with offset f = 2 n i + offs f=Si f = S i + offs low 定标对话框: 精度 high www. hirain. com 2004© power-of-two scaling 恒 润 科 技 有 限 公 司 37
定标默认值 变量名: $S_$B 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 变量类: default 数据类型: Int 16 LSB 值: 2^0 Offset: None 信号宽度(signal width): 1 38
浮点仿真(MIL) 恒 润 科 技 有 限 公 司 隐藏定标限 www. hirain. com 2004© 浮点信号 39
在MIL中的溢出警告 Plot Overview Window: Message Browser: Overflow W 02201: Positive overflow 恒 润 科 技 有 限 公 司 模型: 命令行: www. hirain. com 2004© Block Dialog: 40
使用仿真结果自动定标 Headrooms 自动定标选项: 最高和最低的定标空间 Bit或者%的形式 § Autoscaling enabled for this block § Autoscaling disabled for this block 选中表示定标信息 被用于周围模块的 定标 模块对话框: Start autoscaling tool Main Dialog Tools: autoscaling 如何 作? n n n 根据浮点仿真中发现的最大和最 小值来计算LSB和Offset 基于给出的字宽度 考虑指定范围作为安全系数 www. hirain. com 2004© 防止自动定标改变 offset和数据类型 恒 润 科 技 有 限 公 司 自动定标取决于正确完整的仿真 41
使用仿真结果自动定标 使用仿真的范围进行模型定标 1. 2. 3. 4. 5. 开始MIL仿真并log所有模块以自动定标 打开Autoscaling Tool并切换到Scaling 选择要定标的子系统 选择定标模式: “Use simulation ranges”. 指定定标选项 6. 7. 定标Outputs, States, Parameters? 开始autoscaling. TL是否要使用netlist. • 没有netlist • • 带有netlist • 8. 每个模块将会使用它的输出范围来 定标 周围模块的定标和模块输出范围将 会被考虑(继承定标) www. hirain. com 2004© - 恒 润 科 技 有 限 公 司 如果模块定标完成并要固定下来,在模 块对话框中选中“Scaling reviewed” 42
浮点仿真 恒 润 科 技 有 限 公 司 定标限值 www. hirain. com 2004© 浮点信号 43
自动定标的结果 定标结果 仿真数据范围 www. hirain. com 2004© 结果范围 恒 润 科 技 有 限 公 司 44
Step 2: 模型定标 1. Call start in the MATLAB workspace and select step 2. Select all Target. Link In. Ports, Out. Ports, ‘error‘, ‘control_I‘ and the table ‘u_throttle 2%’ (table is located in the linearization subsystem) for data logging. Block Dialog/Logging & Autoscaling 3. Start ‘MIL Simulation’ and inspect the results. 4. Manually scale the blocks ‘error‘, ‘control_I‘ and the table ‘u_throttle 2%’ (table is located in the linearization subsystem, select 16 Bit datatypes for the table) and disable autoscaling for these blocks. Block Dialog/Output 5. Select all blocks for data logging in the Main Dialog. (preparation for autoscaling) www. hirain. com 2004© Click 'show or hide scaling range', if necessary 恒 润 科 技 有 限 公 司 Main Dialog/Code Generation/Simulation Frame Options 6. Start ‘MIL simulation’ and the autoscaling. Main Dialog/Tools/Start autoscaling tool 45
7. 软件在回路仿真(SIL) n n 产品代码生成过程 精度校验 溢出检测 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 46
Host 仿真 Build Host 1. 生成产品代码 2. 使用MEX编译器进行编译 3. 将MEX文件嵌入在S-function 仿真框架中 www. hirain. com 2004© Start Simulation: 恒 润 科 技 有 限 公 司 47
精度检查 检查范围: 5· 10 -3 - 10· 10 -3 细节查看画出偏差图 } www. hirain. com 2004© 缩放 恒 润 科 技 有 限 公 司 定点和浮点信号一 起画出 48
Step 3 a: 校验定标 1. Call start in the MATLAB workspace and select step 3 2. Generate code for “Host Simulation”. Main Dialog/Code Generation/Simulation Mode Main Dialog/Code Generation/Build Host 3. Start “Host Simulation”. 4. Inspect the results. Do floating-point and fixed-point simulation results match? www. hirain. com 2004© Main Dialog/Simulation 恒 润 科 技 有 限 公 司 49
Step 3 b: 检验定标 1. Inspect the code. Main Dialog/Code Generation/Code Editor 2. Mark “Clean Code”. Main Dialog/Code Generation/Clean code 3. Generate code. 4. Inspect the code. Main Dialog/Code Generation/Code Editor How does 'not instrumented' code compare to 'instrumented' code? www. hirain. com 2004© Main Dialog/Code Generation/Generate Code 恒 润 科 技 有 限 公 司 50
8. 处理器在回路仿真(PIL) n 生成过程 代码压缩 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 51
目标代码仿真 生成目标代码 1. 2. 3. 5. 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 4. 生成产品代码 将产品代码嵌入在目标仿真 框架中 使用目标编译器编译连接 下载到EVB 生成并嵌入主机S函数仿真 框架 52
通过PIL进行代码压缩 S-function 恒 润 科 技 有 限 公 司 system stack user tack RS-232 C www. hirain. com 2004© exec time 53
Step 4: 确认并压缩目标代码 1. Generate code for “Target Simulation”. Main Dialog/Code Generation/Simulation Mode Main Dialog/Code Generation/Build Target 2. Start “Target Simulation” and inspect execution time and stack consumption results. Main Dialog/Code Generation/Code generation options 4. Inspect code size information. Main Dialog/Code Generation/Codesize www. hirain. com 2004© 3. Check “clean code” and start “Target Simulation” again. Inspect the results. 恒 润 科 技 有 限 公 司 54
9. Target. Link API n n 定义 命令 属性管理器 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 55
Target. Link API n 定义: 通过M文件命令访问Target. Link n Target. Link API 命令可以: l l l 使 具自动执行成为可能 - 代码自动生成 - 自动测试 - 模型自动修正 - 链接外部 具 www. hirain. com 2004© l 查找Target. Link模块特定属性值 访问和修改模块属性 访问和修改Stateflow属性 调用Targe. Link 具,例如模块转换,自动定标,产品代码生成, 仿真,等 恒 润 科 技 有 限 公 司 56
访问属性 tl_find(
访问程序 调用Target. Link程序 具自动化 Hook Functions 嵌入自己的命令以在代码生成过程的特定位置执行 使代码生成适应用户指定的 具链 恒 润 科 技 有 限 公 司 www. hirain. com 2004© tl_generate_code tl_compile_target tl_build_target tl_download tl_set_sim_mode tl_compile_host. . . etc. - 代码生成后处理 - 自动代码压缩和统计分析 -. . . etc. 58
属性管理器GUI 模块 浏览器 恒 润 科 技 有 限 公 司 模型 浏览器 过滤器 属性 选择器 上下文 www. hirain. com 2004© 模块 菜单 属性 编辑器 59
Step 5: 执行8 bit转换 1. Mark blocks 3. Mark type 4. Click 'Set' www. hirain. com 2004© 2. Select property 恒 润 科 技 有 限 公 司 任务: 所有整型从16 bit转换成 8 bit. 60
Step 5: 执行8 bit转换 5. Unmark “Clean Code” and select “Log according block data”. Main Dialog/Code Generation/Clean code Main Dialog/Code Generation/Simulation frame options 6. Start “Floating-Point Simulation” and start autoscaling. Main Dialog/Tools/Autoscaling Main Dialog/Simulation Do floating-point and fixed-point simulation results match? www. hirain. com 2004© 7. Start “Host Simulation” and inspect the results. 恒 润 科 技 有 限 公 司 61
10. 定义变量类和用户类型 n n n 类 名称 地址 用户类型 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 62
variable classes 恒 润 科 技 有 限 公 司 variable classes的属性: GLOBAL STATIC_GLOBAL CAL DISP FCN_RETURN others. . . - 本地或全局(local or global) - 静态或外部(static or extern) - 只读或读写(readonly or readwrite) - 易失性(volatile) - 常数(const) -. . . - 全局变量 - 静态全局变量 - 可标定变量 - 可测量变量 - 函数输出 www. hirain. com 2004© 用户定义 variable classes: 63
定义variable classes n 如果default class, 则代码生成器 l l l n 不生成模块输出,状态或者参数的变量 将不同模块整合到一个C代码中 消除定标代码 恒 润 科 技 有 限 公 司 定义variable classes 在Data Dictionary Manager (Pool - Variable. Classes) 1. 使用上下文菜单 Variable. Classes – Create Variable Class 或者拷贝一个存在的 class. 2. 指定class name 3. 指定class properties. www. hirain. com 2004© 创建class: 64
本地变量 DD的结构 local 恒 润 科 技 有 限 公 司 Int 8 off /* static local variables */ #define STATIC_LOCAL static. . . Void func(. . . ) { /* STATIC_LOCAL: static local variables */ STATIC_LOCAL Int 8 P_Sa 1_Kp; . . . } www. hirain. com 2004© 结果 65
全局变量 结构 恒 润 科 技 有 限 公 司 global Int 8 /* static global variables */ #define STATIC_GLOBAL static. . . /* STATIC_GLOBAL: static global variables */ STATIC_GLOBAL Int 8 P_Sa 1_Kp; . . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 66
常数临时变量 结构 恒 润 科 技 有 限 公 司 on on Int 8 /* static global variables */ #define STATIC_GLOBAL static const volatile. . . /* STATIC_GLOBAL: static global variables */ STATIC_GLOBAL Int 8 P_Sa 1_Kp; . . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 67
使用类的名称(Class Names) 结构 恒 润 科 技 有 限 公 司 结果 off . . . /* STATIC_GLOBAL: static global variables */ static const volatile Int 8 P_Sa 1_Kp; . . . Void func(. . . ) {. . . } www. hirain. com 2004© Int 8 68
初始化 结构 STATIC_GLOBAL on Int 8 结果 /* static global variables */ #define STATIC_GLOBAL static const volatile. . . /* STATIC_GLOBAL: static global variables */ STATIC_GLOBAL Int 8 P_Sa 1_Kp = 0 x 28 /* 5. */; . . . Void func(. . . ) {. . . } www. hirain. com 2004© on 恒 润 科 技 有 限 公 司 69
可标定变量 结构 恒 润 科 技 有 限 公 司 Int 8 /begin CHARACTERISTIC P_Sa 1_Kp "proportional gain factor" VALUE 0 x 424 EA TL_VAR_S 8 0. 0 CONV. P_Sa 1_Kp -10 10 /end CHARACTERISTIC readonly MEASUREMENT /* /* /* 结果 variable name */ description */ type */ address: P_Sa 1_Kp */ ref. to record layout */ max diff */ ref. to compu method */ lower limit */ upper limit */ www. hirain. com 2004© readwrite CHARACTERISTIC 70
选择要标定的变量 CAL or DISP class 恒 润 科 技 有 限 公 司 www. hirain. com 2004© /* calibratable parameters */ #define STATIC_CAL static const volatile. . . /* CAL: calibratable parameters */ STATIC_CAL Int 8 P_Sa 1_Kp = 0 x 28 /* 5. */; 标定系统的信息 名字,基本类型,定标,上下限,等. ASAP 2 File 71
ASAP 2文件生成器 Target. Link 模型 External ASAP 2 files ASAP 2 Stylesheet 输入到 生成 /begin MODULE ASAP 2 Generator /begin CHARACTERISTIC generates. . . /begin MEASUREMENT. . . /begin RECORD_LAYOUT. . . Code Files *. c Calibration System *. a 2 l Make Tool *. a 2 l www. hirain. com 2004© Target. Link ASAP 2 File 输入到 恒 润 科 技 有 限 公 司 ASAP 2 Project File *. a 2 l /include module 72
独立的定义文件 结构 恒 润 科 技 有 限 公 司 Int 8 written to: PI_Controller_cal. c /* static global variables */ #define STATIC_GLOBAL static const volatile. . . /* STATIC_GLOBAL: static global variables */ STATIC_GLOBAL Int 8 P_Sa 1_Kp = 0 x 28 /* 5. */; . . . 结果 www. hirain. com 2004© “$N_Cal” “” 73
链接器部分 结构 恒 润 科 技 有 限 公 司 Int 8 § § § § Pre. Declaration. Block. Statement Post. Declaration. Block. Statement Pre. Declartion. Statement Post. Declaration. Statement Pre. Definition. Block. Statement Post. Definition. Block. Statement Pre. Definition. Statement Post. Definition. Statement /* calibratable parameters */ #define CAL static const volatile. . . /* CAL: calibratable parameters */ #pragma class NB=FLASH_WORD CAL Int 8 P_Sa 1_Kp = 0 x 28 /* 5. */; . . . #pragma default_attributes. . . www. hirain. com 2004© 结果 74
可合并的变量 结构 恒 润 科 技 有 限 公 司 RPM_Li m 结果 /* merged calibratable parameters */ #define MERGEABLE_CAL const volatile. . . /* MERGEABLE_CAL: merged calibratable parameters */ MERGEABLE_CAL Int 8 RPM_Lim = 0 x 28 /* 5. */; . . . Void func(Int 8 in, *Int 8 out) { *out = (in * RPM_Lim) + RPM_Lim; } www. hirain. com 2004© Mergeable 75
Macro 结构 恒 润 科 技 有 限 公 司 on off /* MACRO: preprocessor macros */ /* Description: proportional gain factor */ #define P_Sa 1_Kp ((Int 8)40 /* 5. */). . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 76
External Macro 结构 恒 润 科 技 有 限 公 司 /* Parameter access structures */ #define O 2_thr para_struct[eco_mode]. O 2_thr #define Rpm_thr para_struct[eco_mode]. Rpm_thr. . . 不同代码 www. hirain. com 2004© 结果 /* Register access macros */ #define GET_ADC 1 read_SFR(adc 1) #define GET_ADC 2 read_SFR(adc 2). . . 77
变量名及地址 变量名 $S_$B - 子系统ID 和模块名 pid_$B - 附加字符 my_name - 固定名字 www. hirain. com 2004© 存储器地址 恒 润 科 技 有 限 公 司 0 x 4 A 3 F - 固定地址 &var - 参考其它变量 &s. var - 结构元素 78
Variable Name Macro Specification 恒 润 科 技 有 限 公 司 Int 8 /* CAL: calibratable parameters */ #define CAL const volatile. . . /* calibratable parameters */ CAL Int 8 P_Sa 1_Kp_gain; . . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 79
Name Macro 在代码生成过程中,macro被它所代表的字符串取代。 “$S_$B”是大部分变量默认的名字 - Simulink子系统ID - 如果没有名字不清,则为空字符串,否则取类似名字 - 模块名 - 信号线名 - 模块名 - Target. Link子系统ID - Target. Link根系统名 - 模块参数作为字符串 - 基本类型名 - Data Dictionary中的变量名 - state chart ID - MATLAB表达式的结果 www. hirain. com 2004© 一些macros: $S $R $B $L $M $I $N $P $T $D $C $E(expr) 恒 润 科 技 有 限 公 司 80
Hex地址分配 Specification 恒 润 科 技 有 限 公 司 . . . /* variable ADC 1_in mapped to address 0 x 4 A 3 F */ #define ADC 1_in (*((volatile UInt 16 *) 0 x 4 A 3 F)); . . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 81
用户定义的数据类型 n 基本数据类型与定标参数整合在一起 n 如果将同一定标分配给同一种信号,可以使用该类型 n 可在每个模块对话框选择 n 定义用户类型 www. hirain. com 2004© 在Data Dictionary中 恒 润 科 技 有 限 公 司 82
选择用户定义的数据类型 Specification 恒 润 科 技 有 限 公 司 filt_eng_speed 代码生成器使用定标 参数 #ifndef _Engine_RPM_TYPE_ #define _Engine_RPM_TYPE_ typedef UInt 16 Engine_speed; #endif. . . /* global variables */ GLOBAL Engine_speed filt_eng_speed; . . . Void func(. . . ) {. . . } www. hirain. com 2004© 结果 83
Step 6: Using User-Defined Data Types 1. Start “Floating-Point Simulation”. 2. Open the Data Dictionary Manager. 4. Set all output datatypes to 'Vel' with the Property Manager. Main Dialog/Property Manager 5. Look at the output page of one of the blocks in your model which the new data type is selected. www. hirain. com 2004© 3. Create the new user-defined data type 'Vel' and a scaling in the Data Dictionary. type. Name = 'Vel'; base. Type = 'Int 16'; min = Na. N; lsb = 2^-7; max = Na. N; offset = 0; unit = ''; 恒 润 科 技 有 限 公 司 6. Start “Floating-Point Simulation” and inspect the results. What is the main difference between the scaling methods? 84
11. Look-Up Table n n 概述 Table 选项 搜索算法 优化 外部代码 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 85
Look-Up Tables 恒 润 科 技 有 限 公 司 § 1 D/2 D, Direct Look-up Table § 等距/不等距 § 许多选项用于平衡代码大小, 速度和精度 § 搜索结果重用 § 容易整合集成 www. hirain. com 2004© 提供了什么? 86
Look-Up Tables选项 轴向量 插值-外推 用于提高表格采 样点之间的精度 搜索算法 用于不等距(nonequidistant)表格 www. hirain. com 2004© 边界点 加入到代码中 以减少代码大 小并加速表格 搜索 恒 润 科 技 有 限 公 司 87
表格操作选择 n 等距表格(Equidistant Table) l l l § 不等距表格(Non-equidistant Table) • 横坐标和纵坐标都被存储 需要更多的存储器 • 需要搜索算法 更多执行时间 • 对于低斜率和大斜率的表格推荐使用 恒 润 科 技 有 限 公 司 www. hirain. com 2004© l 横坐标值等距,由步长宽度、第一和最后的坐标点定义 减少存储器的消耗 纵坐标值被存储着 没有搜索算法,横坐标输入是计算得到的 较少执行时间 对于斜率平滑的表格推荐使用 88
搜索算法 n 线性搜索,从低端开始 l l l n 线性搜索,从高端开始 l l 本地搜索 l l l n 从高端开始,线性的通过横坐标入口 代码小,算法慢 推荐用于小表格, ~10 to 20 入口, 例如 RPM tables 搜索从前一个找到的输出值的索引开始,用当前值的线性搜索的方向继续进行 代码大,算法较快 当表格输入在两个采样步长之间较为接近时,推荐使用 二进位搜索 l l l 恒 润 科 技 有 限 公 司 www. hirain. com 2004© l n 从低端开始,线性的通过横坐标入口 代码小,算法慢 推荐用于小表格, ~10 to 20 入口 将搜索区域分为两半,在输入值的方向连续搜索 代码大,算法快 推荐用于表格大的情况 当不知道选择何种方式时:使用profiling! 89
边界点和外推法 如果输入值超过横坐标 根据边界间距的梯度进行外推得到输出 边界点 www. hirain. com 2004© 边界点作为表格入口插入在横坐标的执行范围边界中 使用边界点: - 不需要额外的代码以检测输入是否超出范围 - 不需要额外的代码以进行外推法的溢出保护 恒 润 科 技 有 限 公 司 90
表格偏差图(Table Deviation Plot) 精度检测 § 减去指定的执行表格 § 显示结果 从Table Plot窗口取得偏差图 (deviation plot) www. hirain. com 2004© 按下: 恒 润 科 技 有 限 公 司 91
Look-Up Table的优化 指定目标的优化 Motorola M 683 xx & HC 12: TBL命令用于等距表格和整型 n Hitachi SH 2 & Infineon Tri. Core: 带有优化传递(optimized pipeline behavior) 的搜索算法 n Floating-Point Processors: 特定的程序用于执行整型的表格数据,以及浮点型 的输入输出。 n 先决条件: § 相同输入 § 相同的横坐标向量 § 相同的搜索选项 www. hirain. com 2004© 搜索结果的重用 (TL 2. 0不支 持) 恒 润 科 技 有 限 公 司 只有一个搜索被执行 92
自定义Look-Up Table 函数 /* define my legacy lookup table structure */ /* declare my legacy lookup table structure */ ENGINE_TABLE_TYPE Eng. Tab. A_struct {. . . }; MY_MAP 2 D_TYPE my_map 2 d_struct = = {. . . }; . . . Look-up 数据预处理 /* call my legacy table Sa 1_Look_Up_Table_2 D_ = yes no no 选择替代? Eng. Tab. A. m yes 使用TL的标准 look-up函数 从描述文件开始 执行 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 自定义lookup函数描述 文件是否存 在 lookup function */ Eng. Tab. A_func(Eng. Tab. A_struct, my_map 2 d_func(my_map_struct, Sa 1_speed, Sa 1_throttle); 生成自定义look-up函数 93
12. 函数分割 恒 润 科 技 有 限 公 司 n n n 概述 函数类 函数接口 函数重用 www. hirain. com 2004© n 94
函数分割 默认操作 一致的代码结构 n 子系统对代码没有影响 效率高的代码 n 函数模块 例外: 触发和使能子系统 - 作为平衡代码大小和执行速度之间的线间操作 - 原子子系统标志(atomic subsystem flag)可见 原子子系统(Atomic subsystem) - 作为代码模块嵌入 - 在子系统边界没有模块间优化 www. hirain. com 2004© 为子系统生成C函数 控制代码结构 n 恒 润 科 技 有 限 公 司 95
函数模块 n n n 通知代码生成器将子系统作为函数来执行 如果子系统成为模块库的一部分,则使用In. Port/Out. Port模块 函数重用的必要条件 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 96
函数类 (预定义) § GLOBAL_FCN: 可以用于其他模块 § STATIC_FCN: 其他模块看不见 § INLINE_FCN: (目标指定选项) - 插入线间(inline)关键字 - 通知编译器线间扩展优先 比上一类减少函数调用,更多的代码 www. hirain. com 2004© § EXTERN_FCN: - 只用于仿真的子系统 - 只产生一个函数调用(function call) - 用户提供的函数代码 导入继承代码、驱动等 恒 润 科 技 有 限 公 司 § INTERRUPT_FCN: (目标指定选项) - 产生用于中断函数的代码 97
函数接口通过变量类(Variable Classes)来定义: In. Port Int 16 in, out; void controller(void) { out =. . . in; } Variable class with FCN_ARG/_REF_ARG scope 典型的Variable Classes: § GLOBAL § FCN_ARG § FCN_REF_ARG § FCN_RETURN or void controller(Int 16 in, Int 16 *out) { out =. . . in; } Variable class with fcn_return scope or 恒 润 科 技 有 限 公 司 www. hirain. com 2004© Out. Port Variable class default Int 16 in; Int 16 controller(void) {. . . return(out); } 98
函数重用 在函数模块中进行切换 Multiple instances of library block functionname 恒 润 科 技 有 限 公 司 functionname • Instance-specific variables (e. g. states) are passed as a structure • Instance-specific parameters (e. g. gain values) are passed as a structure, or can be specified as - FCN_PARAM - FCN_REF_PARAM • Reused functions may be nested n 函数重用,需要: 1. 2. 3. www. hirain. com 2004© Capable of re-entrants: 将带有函数模块的子系统放入模块库中 指定唯一的函数名 将子系统从库拖入模型中 99
Step 7: 分割模型 1. Call start in the MATLAB workspace and select step 7. 2. Start “Floating-Point Simulation”. 3. Linearize the plant signal by specifying the inverse “Look-Up Table” in the subsystem “Linearization”. - Add a Table Block of the Target. Link Library to the linearization subsystem - Specify block dialog/y-axis values & block dialog/x-axis values Scale the look-Up table. - Select 16 bit datatypes for the table variables (x-, y-axis, output) - Block Dialog/Scale Table 5. Specify the subsystem “Linearization” as a function with the name '$S_$B‘. -Insert the 'Function' block from TLLIB into the subsystem 6. Start “Floating-Point Simulation”. 7. Start “Host Simulation”. www. hirain. com 2004© 4. 恒 润 科 技 有 限 公 司 Do the plots match? 8. Inspect the code. Do you find the function? 100
13. 文档生成器 恒 润 科 技 有 限 公 司 n 概念 范例 www. hirain. com 2004© n 101
自动生成文档 定义: n 为Target. Link模型自动生成 HTML (PDF, RTF) –文档 n 包括的信息有 - n 用户定制 - - 包括或者不包括文档选项 configuration:
Documentation Macros 恒 润 科 技 有 限 公 司 - 在m文件中进行指定 uk: English de: German • 超链接 - 插入到任何描述编辑器中 - 链接将在生成的文档中显示 - $file: featurespec. doc $http: www. dspace. de $ftp: ftp. dspace. de etc. • 外部文本 - 能够插入到模块注释文字中 文本将显示在生成的文档中 - www. hirain. com 2004© • 语言 $insert: shortspec. doc - 103
HTML文档文件 范例 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 104
Step 8: 生成文档 1. Start a MIL and a SIL simulation and log some blockoutputs 2. Generate documentation. Main Dialog/Tools/Generate documentation 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 105
14. 用户代码 恒 润 科 技 有 限 公 司 n n 概念 执行过程 接口 www. hirain. com 2004© n 106
整合用户代码 使用用户代码的原因 n n 需要做的是: • 用C写用户代码 (Tab: Code & Logging) • 在对话框中指定模型接口 (Tab: Interface) 代码生成器所做的是: • 生成变量声明和定标代码 • 将用户代码拷贝并粘贴到生成的代码中 恒 润 科 技 有 限 公 司 www. hirain. com 2004© n 使用测试过的集成代码 执行附加功能 l定时器 l计数器 l等等 将不支持的模块转换为 Target. Link l. S-functions l指定的Simulink模块库 方法 107
指定用户代码 1. 将用户代码插入模型 中 恒 润 科 技 有 限 公 司 2. 输入用户代码的文件名 www. hirain. com 2004© 3. 选择“Use production code for floating-point simulation“ (选项). 4. 编写用户代码 5. (使用 customcode_tmpl. c). /* fxp_update_begin */ if (reset) counter = 0; else if (enable) counter++; /* fxp_update_end */ 用户代码编程和S函数编程类似 108
指定用户代码 5. 插入所有变量 7. Build S-function - 用于仿真 - 更新模块图标 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 6. 输入每个变量 - 定标参数 - 变量类 - (初始) 值 (选项) - 限值(选项) - 说明 109
用户代码段 flp_ : 用于浮点仿真的代码 fxp_ : 用于定点仿真和代码生成的代码 : 代码声明 /* fxp_init_begin */ _end : 初始化代码 /* fxp_output_begin */ 码 _end : 模型输出准备代 /* fxp_update_begin */ 码 _end : 更新状态变量代 /* fxp_restart_begin */ _end : /* fxp_header_begin */ _end : 重启代码 恒 润 科 技 有 限 公 司 www. hirain. com 2004© /* fxp_decl_begin */ _end Header代码 /* fxp_terminate_begin */: 终止代码 _end 110
用户代码关键字 恒 润 科 技 有 限 公 司 /* fxp_output_begin(top, common) */ Adc. Start(); /* fxp_output_end */ Result Void func(. . . ) { /* declarations of local variables */ Int 18 Sa 1_Kp; . . . www. hirain. com 2004© Custom Code /* first line of function body */ Adc. Start(); . . . } 111
Exercise: 复位计数器 1. Switch to directory tl-trainingtl-basiccustom_code call start and select step 1 2. Include the Custom Code block into the subsystem. Custom Code Block from TLLIB 3. Enter the filename ‘counter_code. c’ into the block's dialog. Block Dialog/Code & Logging/Filename Check "Use production code for floating-point simulation“. 5. Enter code into the template, that implements a resetable counter. - inputs: reset, enable - outputs: y - states: counter Block Dialog/Code & Logging/Edit Custom Code 6. Enter the same variable names into the block dialog. Block Dialog/Interface/Inputs. . . Outputs. . . States 7. www. hirain. com 2004© 4. 恒 润 科 技 有 限 公 司 Build S-function. Block Dialog/Code & Logging/Build S-function 8. Connect the second input to the Custom Code block. 9. Run Simulation. 112
15. Stateflow 恒 润 科 技 有 限 公 司 n n 综合定义 数据类型&定标 变量类 子系统 设计导向 www. hirain. com 2004© n 113
Stateflow integration 定义: n n 恒 润 科 技 有 限 公 司 www. hirain. com 2004© n 整合到模型和代码生成器 中 不需要转换 用属性管理器(Property Manager)控制Stateflow 变量 114
Stateflow的数据类型和定标 Stateflow和Target. Link 数据类型之间的关系 恒 润 科 技 有 限 公 司 www. hirain. com 2004© n ‘any’ = 通过属性管理器Property Manager自 由选择变量属性 115
Stateflow Charts的函数 n 通过属性管理器Property Manager进行函数说明 选择一个chart模块 n Chart 属性 - 函数类 - 函数名 - 代码文件名 恒 润 科 技 有 限 公 司 www. hirain. com 2004© 2. 选择属性 1. 选择 Stateflow chart 3. 修改属性并点 击 'Set' 116
16. 使用Libmaps进行模型转换 n 模型转换 模块库转换 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 117
使用Libmaps进行模型转换 n Libmaps 可以用于: l取代不支持的Simulink模块 (通常由 Target. Link模块or Target. Link子系统) l取代S-function模块 (通常由用户代码模块Custom Code blocks) Libmap是用户定义的结构,以扩展嵌入转换过程(the built-in conversion process) n Libmaps由M文件定义, e. g. tl_get_libmap. m n Libmaps描述了一个模块被另一模块所取代,并且它们之间的 参数如何映射 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 118
Example: 使用Libmaps 进行模型转换 用用户代码模块取 代 Backlash block n 对 masked subsystems也起作用 n 高级选项: l回调函数 恒 润 科 技 有 限 公 司 www. hirain. com 2004© libmap(i). masktype = 'Backlash'; libmap(i). slblock = 'built-in/Backlash'; libmap(i). tlblock = 'tllib_ext/Backlash'; libmap(i). map. SL 2 TL = {. . . 'param(1). value' 'Backlash. Width'}; libmap(i). map. TL 2 SL = {. . . 'Backlash. Width' 'param(1). value'}; libmap(i). func. SL 2 TL = ''; libmap(i). func. TL 2 SL = ''; libmap(i). filter = ''; l. Filter 非独立参数转换 119
17. 整合Target. Link代码和外部代码 n 外部代码 RTOS内核 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 120
外部代码整合1 1. Addfile block n n 外部文件被包括入代码中(通过 #include-statement) 或 外部文件被包括到makefile中 2. Custom Code block n 直接将C代码插入到生成代码中 用于: l 算法 l 调用外部代码 3. Extern macros n n n 选择EXTERN_MACRO用于常量和变量 常量或变量将被作为宏 假设宏在一个外部文件中定义 (使用 Addfile block) www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 121
外部代码整合2 4. 从Simulink调用用户手写函数 n 生成外部函数调用 为了调用: n n n 5. 从Stateflow调用用户手写函数 n 生成外部函数调用 为了调用: n n www. hirain. com 2004© n 将Function block 插入子系统 输入名字function name 选择函数类EXTERN_FCN 插入包括函数定义的Addfile block 恒 润 科 技 有 限 公 司 在作用语言中写function call 在M文件中指定 function prototype (使用 sf_
外部代码整合3 6. 从外部 Stateflow chart调用 n 从外部函数产生函数调用 为了调用: 在 state chart: 包括 ‘Output to Simulink’ 事件 选择‘Function Call’ 触发 n 在子系统subsystem: 包括触发模块(Trigger block) 选择 'function-call' trigger n 连接源和目标 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 函数接口: 用于校正仿真: 选择global 函数接口 123
外部代码整合4 7. 从外部Simulink子系统 n 从外部函数产生函数调用 为了调用: 在调用子函数中: 包括函数调用生成器模块(Function-Call Generator block) n 在被调用子函数中: 包括触发模块(Trigger block) 选择 ‘function-call’触发 n 连接源和目标 www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 函数接口: 用于校正仿真: 选择global 函数接口 124
项目整合 Target. Link 模型 文件导出 具 Tools / Copy Files Destination Directory 模型C文件和H文件, make 文 件段, 添加文件 • 输入到 Files 继承代码 Files _Include _Lib _Tools _Doc _Lib 目标代码库 • _Tools ASAP 2 make file, 文件列表 文件到处 C: ECUTorque_Control • Make Tool or Compiler IDE • _Doc HTML 文档文件 www. hirain. com 2004© 系统H文件 Target. Link 生成 _Include 恒 润 科 技 有 限 公 司 选择 “Clean code” 以用于产 品代码生成! 125
联系我们 n 公司总部 l 地址:北京市朝阳区安翔北里甲 11号北京创业大厦B座 8层 l 邮编: 100101 l 电话: 010 -64840606 l 传真: 010 -64848259 上海办事处 l 地址:上海市徐汇区漕宝路 70号光大会展中心C座 2204室 l 邮编: 200235 l 电话: 021 -64325413, 64325415, 64325416 l 传真: 021 -64325144 n www. hirain. com 2004© n 恒 润 科 技 有 限 公 司 成都办事处 l 地址:成都市八宝街 90号九龙商务楼A 301 l 邮编: 610031 l 电话: 028 -86255196 l 传真: 028 -86278053 126