
本帖最后由 点点&木木 于 2019-4-12 23:57 编辑 7 T/ R7 t! Z: ^2 _* c 5 |0 j% c z( X# d1 U$ ^0 w; d 硬件组件 STM32 Nucleo-F746ZG × 1 博世BMI088 × 13 跳线(通用) × 1 手动工具和制造机器 烙铁(通用) 焊料空气流动站 信号分析仪 1 g5 h' _5 K3 g6 \介绍 在这个项目中,我描述了我在Ada中的小型奥德赛,用于嵌入式ARM从头开始创建冗余IMU(使用多个6DoF陀螺仪/加速传感器)和软件环境来控制它。 我还将向您展示我用来达到目标的工具和方法(调试,分析器......)来估算一组6DoF IMU的音高和滚动: file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png
![]() % z) a* |/ Y) L2 t* c 局限性: 在本教程中,我没有描述控制火箭副翼或如何减少偏航旋转的PID过程,而只描述如何获取数据并估算滚转和俯仰。工作正在进行中,将在单独的教程中进行描述。 # P% ?4 P9 b9 F; v; X2 @动机: 对于正在进行的业余火箭项目,我们需要一个能够容忍各种加速度(和陀螺仪)的IMU,而无需在飞行范围内重新配置。此外,我们希望有一个多IMU板用于可靠性和过滤目的。 出于这个原因,我设计了一个冗余的BMI088(13x IMU),试图在高振动环境中获得最佳结果,一个业余火箭和高精度无人机。 在我们的使用案例中,它将被放置在如下的火箭中,连接到托管微控制器的板,该微控制器能够控制和处理来自传感器的数据。 ![]() " k2 Q( Q* J$ Y6 H 使用AdaCore框架和工具链将使我们能够从他们提供的强大编码环境中受益(在安全/可靠性导向的行业中得到高度认可,如航空航天和运输)。 我注意到在代码实现过程中,Ada代码,特别是在AdaCore GitHubrepo中编辑的代码很容易阅读。对于像我这样习惯于C /C ++和java环境的人来说,使用Ada并不是特别难。 2 z5 c& F7 H, H& S8 Z# }语境: SuperIMU已被开发用于CNRS PlaneteScience的业余火箭队。它将有助于在高速和变化的振动环境中计算精确俯仰/滚转,以在飞行期间使用副翼稳定火箭。 / ?9 v& u" z$ A b! a ![]() 火箭的第一个目标 SuperIMU必须嵌入芯径小于90mm中。 . j, r3 [# o+ u5 x![]() 4 Y+ K0 t g j 第1步:系统描述 SuperIMU具有13倍冗余6DoF IMU,可以直径50mm。它将使用SPI接口与微控制器通信,以获取原始数据(加速度和陀螺仪数据)。 ![]() 板载配置 我们使用简单的架构来可视化和测试SuperIMU。 微控制器使用SPI端口从SuperIMU过滤原始数据(加速度,角度速率),并估算角度。出于测试目的,它使用UART端口发送角度值。 ) W! E8 q8 R& ?6 I) P3 r' F1 X![]() 测试configuartion 6 f M' y, c/ Z& C" n$ ~1 hUART端口连接到计算机,以串行到USB模块可视化数据。 该项目的下一步是为SuperIMU创建一个特定的主机板。主机板将是圆形的,并嵌入STM32F746微控制器。 IMU按以下方式分布,呈星形: ![]() 角度分布遵循45°模式: ![]() , r7 o! _- S. U5 {4 E& N 选择了先前的模式以最大化表面上IMU的存在。根据在德克萨斯大学(RedundantIMU Configurations:paper)进行的几何研究,其中测试了不同的几何配置: 6 [! C$ B) `' S# A# } 传感器的3D空间中的几何分布比计划中的分布更好的结果(我们的情况) 分布对加速度没有影响(aDOP Accelaration Dilution of Precision) 分布对旋转精度有影响(wDOP角精度稀释) 我们添加的IMU越多,我们的精确度就越高 IMU的方向对结果没有显着影响 ![]() 我们记住这个研究,为了达到像配置这样的立方体,我们可以将两个SuperIMU并行叠加。目前我们继续使用UnitCircle配置,尝试从中获取最大值。 7 ^ z ]. w2 G; `1 }' j![]() 灵敏度范围配置:火箭模式 在业余火箭中,在飞行的最初几秒内,加速度可以达到8到9克。我们在滑槽开口和落地时观察到了一阵骚动。 这是一个典型的飞行日志(加速度/速度)的业余火箭与三个阶段(推进,上升无推进,滑槽开放,潜水与滑槽,地面接触): ![]() 在短途飞行期间,典型的加速曲线如下: 发射<7秒:加速度> 6g 在降落伞开启前<11s,无推进上升:<3g 用降落伞潜水<50s:<3 ![]() 在飞行期间,感应范围需要容忍低和高,以无缝地进行精确控制。IMU的范围将在发布前配置。目前我将尝试四种不同的配置,当然这些配置不是确定的,它们需要通过实验进行调整: ![]() IMU分布,范围和名称 在飞行期间,将根据配置的范围忽略传感器原始值: ![]() 过滤过程 在开始使用它们的俯仰/滚转估计之前,根据它们的范围过滤来自IMU的采集数据(加速度,角速度)。 范围过滤器: 过滤器获取原始数据,并根据每个IMU的配置范围,仅输出低于阈值的值:配置范围的95%。 范围滤波器还根据配置的范围提供每个测量数据的权重。权重用于取消高范围值的影响,它由平均过滤器使用。 权重具有以下值:0,1 / 2,1 / 4,1 / 8。 例如,如果IMU的范围是3G并且测量的加速度是2.9G,则范围滤波器输出来自该IMU的值为0的权重。 ![]() 例如,如果测量的加速度<1G,则最准确的IMU是配置有3G范围的IMU。3G IMU的重量为1,6G IMU的重量为1/2,12G IMU的重量为1/4,24G IMU的重量为1/8。 权重根据下表计算: ![]() 例外,当测量数据> 24%> 95%时,它不会为零加权。它会避免产生价值。 范围滤波器的输出值是每个IMU的四倍(加速度,加速度,陀螺仪,陀螺仪等)。 6 ]# Z' G, `; D9 E* s 平均过滤器: 该过滤器根据每个重量计算平均值。当权重为空时,不考虑测量值。 平均滤波器输出滤波数据,该数据是3轴的耦合(accel_filtered-xyz,gyro_filtered-xyz)。 滚动/间距估算器: 然后使用经过滤的数据来估计滚转角和俯仰角。使用众所周知的互补滤波器来估计飞行角度。 ![]() TimeSampling 目标st具有估计角度@ 1ms的采样率。这意味着我们需要测量所有13xIMU,在1ms的时间范围内进行滤波和补充估计。 第2步:使用硬件 IMU传感器BMI088 使用的传感器是BMI088,它专为无人机和机器人设计: 它可以承受24g的加速度。 SPI运行速度最高为10Mhz。 分辨率:0.09mg,0.004°/ s 低TCO为0.2 mg / K,低光谱噪声仅为230 pg / sqrt(Hz)最大值 输出数据速率:1.6kHz - 每个加速度为0.6ms - 陀螺仪最大为2000Hz - 每个0.5ms Nucleo-F746ZG的描述: 核板最有趣的是它们嵌入了ST Link V2-1编程接口以允许调试(例如使用OpenOCD或St-Util)。 它运行@ 216Mhz,非常适合需要快速采集和计算以获得最佳采样时间的用例。 您只需使用micro usb电缆将电路板连接到计算机。对于我的情况,我使用Ubuntu 18.x,自动检测到电路板,无需安装任何驱动程序。 ![]() Nucleo主板的另一个有趣之处在于它们与ArduinoUno标头保持兼容。在下面以绿色(D0 ... D13)呈现。 ![]() 资料来源:https://os.mbed.com/platforms/ST-Nucleo-F746ZG/ . V/ Z8 ^# @1 f3 V4 m k 由于我们需要在读取BMI088传感器(13x IMU)时将延迟降至最大,因此我们使用SPI端口获取Accel和Gyro数据(在这种情况下,I2C不适用)。 0 ?! R0 D) l+ O4 q, s& n 对于代码,我们将使用STM32F746的SPI1: SCK:PA_5 MISO:PA_6 MOSI:PA_7 Super IMU的描述: SuperIMU基于BMI088,它使用SPI接口配置和获取IMU的数据。从数据表中,用户电路如下: ![]() 在SuperIMU的情况下,没有使用中断,它按顺序获取数据。ACCEL和GYRO的SDO在电路板中输出相同的输出。 我必须单独选择每个Gyro和Accel。 ![]() AdaIMU BMI088 Circle(SuperIMU)的原理图 SuperIMU的设计可以使用单个SPI获取数据(下一个版本将使用多个SPI),它具有13x BMI088: ![]() 这是它的样子。我需要焊接接头以连接杜邦线: ![]() 在后面有引脚的描述,SPI(SDO / SCK / SDI)以及用于选择陀螺仪和引脚的引脚: ![]() s! G# E. R( n3 v8 S ![]() K2 L9 S/ }0 k, N9 L: L9 Z 8 X3 z! ^0 t: d7 q0 E, ~![]() 完成焊接后的样子如下: ![]() La pieuvre: ![]() - e" X K0 k3 o- H" C) r: v ![]() SuperIMU使用3v3电源和SPI端口1连接到Nucleo板。 ![]() % V+ d6 ?; S- ~; c7 @" H- C GPIO引脚用于选择Gyro,13x IMU的Accel将按如下方式完成: ![]() 用于选择IMU的陀螺仪/加速度计的GPIO引脚的定义: -- IMU select IMU1_SPI1_SELC : GPIO_Point renames PC8; IMU1_SPI1_SELG : GPIO_Point renames PC9; IMU2_SPI1_SELC : GPIO_Point renames PC10; IMU2_SPI1_SELG : GPIO_Point renames PC11; IMU3_SPI1_SELC : GPIO_Point renames PC12; IMU3_SPI1_SELG : GPIO_Point renames PD2; IMU4_SPI1_SELC : GPIO_Point renames PG2; IMU4_SPI1_SELG : GPIO_Point renames PG3; IMU5_SPI1_SELC : GPIO_Point renames PD7; IMU5_SPI1_SELG : GPIO_Point renames PD6; IMU6_SPI1_SELC : GPIO_Point renames PD5; IMU6_SPI1_SELG : GPIO_Point renames PD4; IMU7_SPI1_SELC : GPIO_Point renames PD3; IMU7_SPI1_SELG : GPIO_Point renames PE2; IMU8_SPI1_SELC : GPIO_Point renames PE4; IMU8_SPI1_SELG : GPIO_Point renames PE5; IMU9_SPI1_SELC : GPIO_Point renames PE6; IMU9_SPI1_SELG : GPIO_Point renames PE3; IMU10_SPI1_SELC : GPIO_Point renames PF8; IMU10_SPI1_SELG : GPIO_Point renames PF7; IMU11_SPI1_SELC : GPIO_Point renames PF9; IMU11_SPI1_SELG : GPIO_Point renames PG1; IMU12_SPI1_SELC : GPIO_Point renames PA3; IMU12_SPI1_SELG : GPIO_Point renames PC0; IMU13_SPI1_SELC : GPIO_Point renames PC3; IMU13_SPI1_SELG : GPIO_Point renames PF3; IMU_SPI1_SEL_Points : constant STM32.GPIO.GPIO_Points := (IMU1_SPI1_SELC, IMU1_SPI1_SELG, IMU2_SPI1_SELC, IMU2_SPI1_SELG, IMU3_SPI1_SELC, IMU3_SPI1_SELG, IMU4_SPI1_SELC, IMU4_SPI1_SELG, IMU5_SPI1_SELC, IMU5_SPI1_SELG, IMU6_SPI1_SELC, IMU6_SPI1_SELG, IMU7_SPI1_SELC, IMU7_SPI1_SELG, IMU8_SPI1_SELC, IMU8_SPI1_SELG, IMU9_SPI1_SELC, IMU9_SPI1_SELG, IMU10_SPI1_SELC, IMU10_SPI1_SELG, IMU11_SPI1_SELC, IMU11_SPI1_SELG, IMU12_SPI1_SELC, IMU12_SPI1_SELG, IMU13_SPI1_SELC, IMU13_SPI1_SELG); IMU_SPI1_Accel_SEL_Points : array (1 .. 13) of access GPIO_Point'Class := ( IMU1_SPI1_SELC'Access, IMU2_SPI1_SELC'Access, IMU3_SPI1_SELC'Access, IMU4_SPI1_SELC'Access, IMU5_SPI1_SELC'Access, IMU6_SPI1_SELC'Access, IMU7_SPI1_SELC'Access, IMU8_SPI1_SELC'Access, IMU9_SPI1_SELC'Access, IMU10_SPI1_SELC'Access, IMU11_SPI1_SELC'Access, IMU12_SPI1_SELC'Access, IMU13_SPI1_SELC'Access ); IMU_SPI1_Gyro_SEL_Points : array (1 .. 13) of access GPIO_Point'Class := ( IMU1_SPI1_SELG'Access, IMU2_SPI1_SELG'Access, IMU3_SPI1_SELG'Access, IMU4_SPI1_SELG'Access, IMU5_SPI1_SELG'Access, IMU6_SPI1_SELG'Access, IMU7_SPI1_SELG'Access, IMU8_SPI1_SELG'Access, IMU9_SPI1_SELG'Access, IMU10_SPI1_SELG'Access, IMU11_SPI1_SELG'Access, IMU12_SPI1_SELG'Access, IMU13_SPI1_SELG'Access ); - f- K' v" T w- ^# F. c! V第3步:软件设置/ IDE 使用Adacore GPS IDE 我从Adacore网站安装了GPS IDE:gnat-community-2018-20180528-x86_64-linux-bin,它有一个很酷的编辑器,包括debuging界面和Ada和SPARK的所有检查环境。 我从Adacore网站安装了Arm-ELF编译器:gnat-community-2018-20180524-arm-elf-linux64-bin,它为ARM(以及bb-runtimes)提供编译器和构建器工具链。 使用项目的代码 要使用该软件,您需要克隆此分支http://github.com/LaetitiaEl/Ada_Drivers_Library,此存储库基于Ada驱动程序库:AdaCore /Ada_Drivers_Library 5 {8 _" E, d: b 使用./GNAT/2018/bin/gps 打开项目文件:Ada_Drivers_Library / examples /STM32F746_Nucleo / SuperIMU.gpr 你编译/上传,这就是全部! 8 v. |/ {% M6 I& ]5 w% _ 该项目针对STM32F746,如果需要可以轻松移植到STM32F4(通过github或黑客联系我寻求帮助)。 第4步:首先进行眨眼测试! 在继续之前,我需要确保可以正确编程电路板。 " e- r3 n' i% _8 @3 F0 t; [3 _" I 我保留了现有文件:blinky_f7disco.gpr,并修改了以下路径。继续使用stm32f746_discovery_full.gpr没问题 blinky_f7disco使用以下主条目: Ada_Drivers_Library/examples/shared/hello_world_blinky/src/blinky.adbblinky的内容如下: with STM32.Board; use STM32.Board;with STM32.GPIO; use STM32.GPIO; with Ada.Real_Time; use Ada.Real_Time; procedure Blinky is Period : constant Time_Span := Milliseconds (200); -- arbitrary2 v/ s% L: Z h, G Next_Release : Time := Clock;0 t: ]4 }+ w1 x+ G begin9 s5 |5 h1 l- {) C! w, n) J \ STM32.Board.Initialize_LEDs;$ V, H# Z# G* i! C loop Toggle (All_LEDs); Next_Release := Next_Release + Period;# p7 ]+ M; |4 U2 L! H delay until Next_Release; end loop;1 e9 v2 o1 R, X: e8 a0 W end Blinky; - F4 [) i4 @! S% n! I4 b4 } 似乎这个代码对所有主板都是通用的。使用GPS IDE我遵循All_LED的定义,它定义于: Ada_Drivers_Library/boards/stm32_common/stm32f746disco/stm32-board.ads& e7 g9 h* Z% r" c 值All_LEDs是索引为1的集合,指向PI1: Green_LED : User_LED renames PI1;& V6 p1 a7 `0 Y( vLED1 : User_LED renames Green_LED; LCH_LED : User_LED renames Green_LED;+ H- a5 I p& k C, h6 m All_LEDs : GPIO_Points := (1 => Green_LED); 7 F s4 H5 c1 |% p' j6 j 2 K0 T! m* @- c) G+ @3 t 在Nucleo板上没有LED连接到PI1,找出我可以使用的输出,简单的方法是检查核板的原理图http://www.st.com/resource/en/sc ... leo_144pins_sch.zip ![]() 我把蓝色LED连接到PB7。然后代码更新为蓝色:Blue_LED : User_LED renames PB7;* D3 O+ `, {( c, x LED1 : User_LED renames Blue_LED; LCH_LED : User_LED renames Blue_LED;& w! g0 I0 M; {6 H' K All_LEDs : GPIO_Points := (1 => Blue_LED); " Z* ^" l3 b6 W * X$ n9 q) e: s% l! g" ~8 j" \ ) N5 [7 \9 K6 I2 b8 Z9 d1 Q0 K 然后使用上传按钮上传代码(重要的是您需要配置上传/调试,在我的情况下,我使用OpenOCD,如下所述) ![]() 代码编译得很好,正确上传并且蓝色LED闪烁: ![]() 让我们开始认真的工作。为BMI088制作SPI驱动程序。 第5步:为BMI088制作驱动程序 很酷的是,Ada_Drivers_Library已经在“components / src / motion”目录中提供了一些SPI IMU示例。 我只需要使用l3gd20来启动bmi088的驱动程序以获得灵感。 ![]() 要正确选择寄存器和协议特性,请使用以下资源: - BST-BMI088-DS001- officiel C驱动程序:有助于获得时间 有关信息,现有驱动程序的演示位于路径中:Ada_Drivers_Library/ arch / ARM / STM32 / driver_demos / 更新板定义:定义SPI1在STM32.Board包体中的文件stm32-board.ads中,我们添加: -- SPI1 / IMU BMI088 example --' Q1 C, S: ^2 ~3 b* s- n9 A ---------------------------------" `3 Z) \/ @" g5 c( _ -- SPI Port IMU_SPI : SPI_Port renames SPI_1; -- SPI IOs SPI1_SCK : GPIO_Point renames PA5; -- D13 in connector CN10" ]8 @6 w i% u @ SPI1_MISO : GPIO_Point renames PA6; -- D14 in connector CN107 Q3 A* ^ u/ U" D8 g SPI1_MOSI : GPIO_Point renames PA7; -- D15 in connector CN107 N2 B- v% u$ N5 k1 f* _. X# N7 D( _& } -- IMU select& _3 B; m2 d) n) r IMU_SEL1 : GPIO_Point renames PG9; -- D0 in connector CN10' r& p5 P9 p5 g" d -- To initialize the SPI1 port for the BMI088 procedure Initialize_SPI1_For_BMI088;6 @% b6 b4 Z; u3 k+ q ) x) @- ]5 c6 }% k) P* p5 n' ~ 在STM32F7中使用8位SPI的技巧 BIM088与8位通信非常重要。但是数据tx / rx DR寄存器是16位,默认情况下它发送16位。首先写入/读取:NOK ![]() 根据参考手册,诀窍是将寄存器DS(数据大小)配置为4位,并正确格式化DR寄存器中写入的数据。 ![]() 这就是为什么我更新了SPI(stm32-spi.adb)的默认驱动程序,如下面的写/读8位模式。 3 s" a& I& m9 \' ? r procedure Configure (This : in out SPI_Port; Conf : SPI_Configuration) is$ C9 z/ E4 t- d8 Bbegin ...; n4 D4 K* S: y8 D This.Periph.CR1.DFF := Conf.Data_Size = HAL.SPI.Data_Size_16b;# c! y0 t2 A1 n7 Y) Y2 s This.Periph.CR2.DS := STM32_SVD.SPI.Size_4Bit; -- add this line This.Periph.CR1.CPOL := Conf.Clock_Polarity = High; ... procedure Send_8bit_Mode% C6 S# H, V' _* V% z9 E7 m1 `8 \* \ (This : in out SPI_Port; Outgoing : HAL.SPI.SPI_Data_8b)5 n0 T4 ~# s* ^8 ^4 G+ v' e$ D is& h8 t G0 R1 t3 S, | Tx_Count : Natural := Outgoing'Length;' G9 l f/ w+ B& i: b+ c* L; W/ C Index : Natural := Outgoing'First;! y7 B8 v+ u5 ?8 r7 B7 ~' O5 j Tmp : UInt16; begin: k" w* n r2 e0 l) A I( {$ N W if Current_Mode (This) = Slave or else Tx_Count = 1 then0 L: ~0 t6 ~4 _: l: D! P. e Tmp := UInt16 ((Outgoing (Index) and 16#0F#))*256 + UInt16((Outgoing (Index) and 16#F0#))/16;8 l e! K' {. s! |+ P. ^ This.Periph.DR.DR := Tmp; Index := Index + 1;! }' B) | Z4 x) p3 m Tx_Count := Tx_Count - 1; end if;( f! K. C4 v, O% m. l( \ ...1 W/ x4 u, h) \& f- C. } procedure Receive_8bit_Mode (This : in out SPI_Port;: v1 |; G P6 s+ B" [' \, G* C Incoming : out HAL.SPI.SPI_Data_8b)( F. ~) i& M4 F. D' K. M+ ` is0 \ K" A( D9 V$ }% L& f2 F) \7 y3 g/ o5 n Generate_Clock : constant Boolean := Current_Mode (This) = Master;8 n$ C$ u- e' p$ U) s1 ^ Tmp : Uint16; begin for K of Incoming loop# l& e* W# L( w" A if Generate_Clock then This.Periph.DR.DR := 0;* r7 C. J/ @8 Y! L- z6 V end if;" ?' R9 L2 x4 K- c while Rx_Is_Empty (This) loop null;% {. m6 T& G1 [: {4 |( q end loop;: h; c4 r/ V" s' a Tmp := This.Periph.DR.DR;5 c4 n4 W; o( X, U2 X K := UInt8 (Tmp and 16#000F#)*16+ UInt8 ((Tmp and 16#0F00#)/256);& ^: N% n, M- Y# }; |" w! L$ ^ ...0 T2 r C7 h3 ]) M ; S5 O8 o9 z6 L) Q 0 N$ \/ D7 b2 z 6 V/ l: A+ {, h! L 这样我获得了更好的时钟,BMI088寄存器中的读/写在8位模式下工作正常: ![]() + I2 J, o7 @* @& w. Y6 H0 U% d 但还是不行: 因为我们需要在读取加速度计时(不是陀螺仪的情况)总是丢弃BMI088中的第一个接收字节。这就是为什么每次我进行读取时,我都必须先读取虚拟字节。通过此更新,我获得了更好的结果。 在BMI088.adb中: ---------- -- Read -- ---------- procedure Accel_Read (This : Six_Axis_Imu; Addr : Register; Data : out UInt8) is Status : SPI_Status; Tmp_Data : SPI_Data_8b (1 .. 1); begin SPI_Mode (This, Enabled => True); -- select the chip This.Port.Transmit (SPI_Data_8b'(1 => UInt8 (Addr) or ReadWrite_CMD), Status); if Status /= Ok then raise Program_Error; end if; This.Port.Receive (Tmp_Data, Status); -- dummy constraint BMI088 This.Port.Receive (Tmp_Data, Status); if Status /= Ok then raise Program_Error; end if; Data := Tmp_Data (Tmp_Data'First); SPI_Mode (This, Enabled => False); -- unselect the chip end Read; ---------------- -- Read_UInt8s -- ---------------- procedure Accel_Read_UInt8s (This : Six_Axis_Imu; Addr : Register; Buffer : out SPI_Data_8b; Count : Natural) is Index : Natural := Buffer'First; Status : SPI_Status; Tmp_Data : SPI_Data_8b (1 .. 1); begin SPI_Mode (This, Enabled => True); This.Port.Transmit (SPI_Data_8b'(1 => UInt8 (Addr) or ReadWrite_CMD), Status); if Status /= Ok then raise Program_Error; end if; This.Port.Receive (Tmp_Data, Status); -- dummy constraint BMI088 for K in 1 .. Count loop This.Port.Receive (Tmp_Data, Status); if Status /= Ok then raise Program_Error; end if; Buffer (Index) := Tmp_Data (Tmp_Data'First); Index := Index + 1; end loop; SPI_Mode (This, Enabled => False); end Read_UInt8s; $ E- e! A' o% q0 k+ t) ]9 B: ]4 `& i 最好从寄存器中读取加速数据。在下图中,我们仅表示SDO和时钟(SDI未表示,我只有2个探测......:'(): ![]() 对于陀螺仪数据,虚拟字节没有问题。 这就是为什么必须要有Accel_Read和Gyro_Read不同的程序。 初始化BMI088的步骤 首先,您需要通过调用以下过程正确初始化SPI以与BMI088通信: Config : GPIO_Port_Configuration; Config_SPI : SPI_Configuration;! ?. t( q$ \* ~' u3 K! E' e0 g begin -- Enable the clocks Enable_Clock (IMU_SPI);/ l" ^' l \" A9 n9 P6 \6 O+ f% @* n4 H Enable_Clock (SPI1_Points); Enable_Clock (IMU_SEL1);2 Z$ V: t# R8 P -- Configure the SPI IOs* u* b7 _2 N4 {/ }6 i Config := (Mode => Mode_AF, AF => GPIO_AF_SPI1_5,& `7 y) o; h; t( b8 c. C) l, h+ e7 _ AF_Speed => Speed_100MHz,3 m" G0 \1 M7 | AF_Output_Type => Push_Pull,# _5 ~5 D, y% Q5 E9 H ~3 O Resistors => Floating);! Q7 ] @* |9 _$ m- |$ p Configure_IO(SPI1_Points, Config); -- Configure the SPI select chip IOs! n5 L" ^# M( }% C. |, U Config := (Mode => Mode_Out,) W+ {8 F$ X3 A% d Speed => Speed_25MHz, Output_Type => Push_Pull, Resistors => Pull_Up); Configure_IO(IMU_SEL_Points, Config);( }2 H7 i/ I. @4 ^; S -- Configure the SPI1 port5 C# P/ k: \- |- d: y: L Config_SPI := (Direction => D2Lines_FullDuplex, Mode => Master, Data_Size => HAL.SPI.Data_Size_8b, Clock_Polarity => Low,. _7 E: N4 M, U% I; U7 ?, V6 w5 h+ K6 N Clock_Phase => P1Edge,+ A2 ^3 l2 h! T Slave_Management => Software_Managed, Baud_Rate_Prescaler => BRP_32 First_Bit => MSB, CRC_Poly => 7); Configure (IMU_SPI, Config_SPI); STM32.SPI.Enable (IMU_SPI);8 ]8 k% p; W( i; ] end Initialize_SPI1_For_BMI088; 然后按照以下步骤获取加速度计(寄存器和命令的值在数据表中定义,您可以在最后的驱动程序中找到它们): # Y- u" g/ L+ ?% b l4 V& K ---- check the device ID7 z u7 @& U/ J8 j- jBmi.Read(ACC_CHIP_ID_ADDR, Result);; A3 w( g x. C' \: F# X while Result /= ACC_CHIP_ID loop Bmi.Read(ACC_CHIP_ID_ADDR, Result);- N3 ?1 D% V. [4 t% {* s delay until Clock + Milliseconds (1);/ V& T0 ]& X0 {, L end loop; ---- reset the accel Bmi.Write(ACC_SOFT_RESET_ADDR,ACC_RESET_CMD); delay until Clock + Milliseconds (50); Turn_ON(IMU_SEL1); delay until Clock + Milliseconds (5); Turn_Off(IMU_SEL1); delay until Clock + Milliseconds (5);3 |0 @1 m( R' P# Q" O ---- enable the accel (rundandant to make sure it works)" u' `* Q% \1 X1 n Bmi.Write(ACC_PWR_CNTRL_ADDR,ACC_ENABLE_CMD); delay until Clock + Milliseconds (50); Bmi.Read(ACC_PWR_CNTRL_ADDR, Result); while Result /= ACC_ENABLE_CMD loop Bmi.Write(ACC_SOFT_RESET_ADDR,ACC_RESET_CMD); --delay until Clock + Milliseconds (1);& s( X" ?* Z5 F* q/ D5 W Bmi.Write(ACC_PWR_CNTRL_ADDR,ACC_ENABLE_CMD);' O! b6 u( n" {4 f+ N$ L --delay until Clock + Milliseconds (50);* p* M5 q! U, R+ a# e6 L Bmi.Read(ACC_PWR_CNTRL_ADDR, Result); delay until Clock + Microseconds (4); end loop;- ?/ F* V& y# u! G ---- enter active mode Bmi.Write(ACC_PWR_CONF_ADDR,ACC_ACTIVE_MODE_CMD); _( E" r& p1 S& E delay until Clock + Milliseconds (5); Bmi.Read(ACC_PWR_CONF_ADDR, Result);3 C: R: j- `' a5 T# u while Result /= ACC_ACTIVE_MODE_CMD loop Bmi.Write(ACC_PWR_CONF_ADDR,ACC_ACTIVE_MODE_CMD); delay until Clock + Milliseconds (5); Bmi.Read(ACC_PWR_CONF_ADDR, Result); end loop; ---- set range 24G+ `! C9 i* i& c' V Bmi.Write(ACC_RANGE_ADDR,RANGE_24G);# E/ w4 J- K9 L. | delay until Clock + Milliseconds (1); Bmi.Read(ACC_RANGE_ADDR, Result);4 @+ }* w- c/ Y7 V while Result /= RANGE_24G loop Bmi.Read(ACC_RANGE_ADDR, Result); end loop; ---- set default ODR Bmi.Write(ACC_ODR_ADDR,ODR_1600HZ_BW_280HZ);' s9 D' E; R/ d, T2 l1 H5 l delay until Clock + Milliseconds (1); Bmi.Read(ACC_ODR_ADDR, Result); while Result /= ODR_1600HZ_BW_280HZ loop( A9 o2 m1 u9 u Bmi.Read(ACC_ODR_ADDR, Result);* y$ ^; g# w7 j) }% J3 t end loop;% y x' y- D" m4 J( F6 s ---- check if there is an error: A$ Q3 S: A: F R0 q7 L: e3 ^ Bmi.Read(ACC_ERR_CODE_ADDR, Result); while Result /= 16#0# loop Bmi.Read(ACC_ERR_CODE_ADDR, Result);& G: p# U( F X% p3 D end loop;! c8 s* h; i, m B2 i2 f! ? ---- read the value loop9 p W- y( O; \3 l Bmi.Read_UInt8s(ACC_ACCEL_DATA_ADDR,Received,6); delay until Clock + Microseconds (100); end loop;( C C8 t( c" I) p# m$ A 按照上面的步骤,我使用来自BMI088的SPI获取值。 0 k3 G9 ]) k4 G: o( z! P: R. \9 m步骤6:过滤和估计描述 应用几何变换: SuperIMU中IMU的配置不一样,它们被旋转(45°和90°): ![]() 只有加速度X和Y受到旋转的影响。陀螺仪没有受到影响(转速是相对的)。 % u+ [3 y) ? l3 N+ Q. B根据每个IMU位置完成旋转变换。旋转按以下过程编码: -------------------- -- Accel rotation -- -------------------- Sqrt2 : constant Float := 1.41421356237; IMU_Alpha_Index : constant array (1 .. 3) of Integer := (1,3,7); IMU_Beta_Index : constant array (1 .. 2) of Integer := (5,11); IMU_Gamma_Index : constant array (1 .. 2) of Integer := (4,9); IMU_Delta_Index : constant array (1 .. 2) of Integer := (2,13); procedure ApplyIMURotation(i : Integer;xf : in out Float; yf : in out Float) is begin if i in IMU_Alpha_Index'range then null; -- we don't change elsif i in IMU_Beta_index'range then xf := -xf; yf := -yf; elsif i in IMU_Gamma_Index'range then xf := yf; yf := xf; elsif i in IMU_Delta_Index'range then xf := -yf; yf := -xf; elsif i = 8 then xf := Sqrt2*(-yf + xf); yf := Sqrt2*(yf + xf); elsif i = 6 then xf := Sqrt2*(-yf - xf); yf := Sqrt2*(-yf + xf); elsif i = 10 then xf := Sqrt2*(yf + xf); yf := Sqrt2*(yf - xf); elsif i = 12 then xf := Sqrt2*(yf - xf); yf := Sqrt2*(-yf - xf); end if; end; 3 w, z$ f) h0 u, B- d2 O9 c! e& v1 Y 使用范围: IMU的范围不同。每组IMU都有一个特定的范围: ![]() IMU分布,范围和名称 , z5 V# n- {& S$ t配置中使用的范围使用以下代码计算: g7 a# E2 Q* m; ^% H$ W+ f$ J --------------------------% \% R7 V1 R2 `-- Get the range to use --* | H) b! n8 B --------------------------; Z) K3 S0 ]# r' m" V) l; n Range_3G_Index : constant array (1 .. 5) of Integer := (1,3,4,5,2); Range_6G_Index : constant array (1 .. 4) of Integer := (8,6,12,10);- v" `+ f. }5 y/ { Range_12G_Index : constant array (1 .. 2) of Integer := (9,13);0 j4 f" G8 @% P Range_24G_Index : constant array (1 .. 2) of Integer := (7,11);4 Y: {( s$ X$ T l function GetAccelRangeConf(i : Integer) return Accel_Range is! q) F4 q% A& `! y; }; M begin if i in Range_3G_Index'Range then return ACCEL_RANGE_3G;/ P6 L6 h& X6 D elsif i in Range_6G_Index'Range then return ACCEL_RANGE_6G;7 u$ _' u" a* q elsif i in Range_12G_Index'Range then% y3 c, |2 ]2 d: j9 X return ACCEL_RANGE_12G; elsif i in Range_24G_Index'Range then return ACCEL_RANGE_24G;4 ~* h. X' N. b, f else- G4 o* K4 o5 i' O. W return ACCEL_RANGE_24G; [+ q; _6 u9 ^6 [! ` end if;# V$ t6 t4 E/ D0 _) D. `1 e' \" b end;8 l, P8 l. g' u& f! l Range_125DPS_Index : constant array (1 .. 5) of Integer := (1,3,4,5,2); Range_500DPS_Index : constant array (1 .. 4) of Integer := (8,6,12,10);& v* O9 D7 P/ ~6 J" k) `( {. P Range_1000DPS_Index : constant array (1 .. 2) of Integer := (9,13);% Y8 m5 V B8 X0 e1 J" k; B. n Range_2000DPS_Index : constant array (1 .. 2) of Integer := (7,11); function GetGyroRangeConf(i : Integer) return Gyro_Range is* I: S: ?/ E( a- B+ i begin if i in Range_125DPS_Index'Range then& T7 N: z% O$ {4 }1 h return GYRO_RANGE_125DPS; elsif i in Range_500DPS_Index'Range then return GYRO_RANGE_500DPS; elsif i in Range_1000DPS_Index'Range then return GYRO_RANGE_1000DPS;) Z( D/ E+ @( B elsif i in Range_2000DPS_Index'Range then return GYRO_RANGE_2000DPS;$ E& g5 t6 A1 g) `% C0 b else return GYRO_RANGE_500DPS; end if; 4 \3 ]3 T: I) c: C end; + q2 t; c6 Y; b. s$ T9 G 过滤代码: 根据每个IMU的配置范围对加速度和陀螺仪速率进行滤波。 权重使用以下代码计算: function GetAccelWeight(rng : Accel_Range; val : Sensor_Accel_Data) return Float is q) e2 u* y/ o) P weight : Float := 0.0;5 ~. o3 R4 m- T3 | refaccel : Float := 3.0;6 a( M% o* L9 i0 l2 }: _3 `3 j( u$ O refrange : Float := 3.0;9 }& @( t! J# L( } r, A# c q begin if ((abs(val(X))<3.0*0.95*G and abs(val(Y))<3.0*0.95*G and abs(val(Z))<3.0*0.95*G)) then4 c# D5 G* L( `+ A" @; { refaccel := 3.0; elsif ((abs(val(X))<6.0*0.95*G and abs(val(Y))<6.0*0.95*G and abs(val(Z))<6.0*0.95*G)) then7 k! j5 U/ x- O refaccel := 6.0;8 Y* J+ N; A- ^4 m8 T3 o elsif ((abs(val(X))<12.0*0.95*G and abs(val(Y))<12.0*0.95*G and abs(val(Z))<12.0*0.95*G)) then# f+ u& P) u4 g3 d refaccel := 12.0;" H$ l" q+ E# u4 W* f elsif ((abs(val(X))<24.0*0.95*G and abs(val(Y))<24.0*0.95*G and abs(val(Z))<24.0*0.95*G)) then refaccel := 24.0;( f6 Z) ]8 d; C* [ end if;' R% h) Y# B+ e0 K0 L% ]9 i) a if rng = ACCEL_RANGE_3G then5 D0 ~% D( T W3 O0 B5 q6 O refrange := 3.0;. ?% v \2 d" o4 l elsif rng = ACCEL_RANGE_6G then9 B8 Y$ z) y/ H: g! s refrange := 6.0;: {1 C; R N" x+ Z0 q elsif rng = ACCEL_RANGE_12G then% n$ L9 l8 I2 `$ b* V refrange := 12.0;* M1 g$ X0 q4 y elsif rng = ACCEL_RANGE_24G then1 H" {: K' k' U+ [1 r refrange := 24.0; end if; weight := refaccel / refrange;" W4 R2 u: {- X/ o if weight > 1.0 then6 v3 w. z, l! r return 0.0; end if; return refaccel/refrange;; `- k- g+ }) y end GetAccelWeight;3 K/ Z) j' L7 ^' |0 e1 ^3 ^! ? function GetGyroWeight(rng : Gyro_Range; val : Sensor_Gyro_Data) return Float is3 y5 f. Z j* j* t. g weight : Float := 0.0; refgyr : Float := 125.0;( H2 |. |2 l+ h# X* N* y) X# r* ? refrange : Float := 125.0;- a6 G$ B" s) ] P; y begin; X2 Z& U+ {; `, C! J if ((abs(val(X))<125.0*0.95 and abs(val(Y))<125.0*0.95 and abs(val(Z))<125.0*0.95)) then1 p$ k& r b, T. _ refgyr := 125.0; elsif ((abs(val(X))<250.0*0.95 and abs(val(Y))<250.0*0.95 and abs(val(Z))<250.0*0.95)) then0 |1 G' B; G3 ~1 y refgyr := 250.0;/ I- _' n+ s; ?$ j+ ]! J6 y9 n" v elsif ((abs(val(X))<500.0*0.95 and abs(val(Y))<500.0*0.95 and abs(val(Z))<500.0*0.95)) then refgyr := 500.0;8 e, Z+ F* |1 [$ } elsif ((abs(val(X))<1000.0*0.95 and abs(val(Y))<1000.0*0.95 and abs(val(Z))<1000.0*0.95)) then refgyr := 1000.0; elsif ((abs(val(X))<2000.0*0.95 and abs(val(Y))<2000.0*0.95 and abs(val(Z))<2000.0*0.95)) then refgyr := 2000.0; end if;6 \) i1 F" l- `. B z if rng = GYRO_RANGE_125DPS then refrange := 125.0;; {& ?% j D, K0 u) I; W, w9 y) E elsif rng = GYRO_RANGE_250DPS then refrange := 250.0; elsif rng = GYRO_RANGE_500DPS then refrange := 500.0; elsif rng = GYRO_RANGE_250DPS then3 n) E8 t: H* y; }+ Y: ^3 G refrange := 1000.0;- P: C' O0 ?- D# F elsif rng = GYRO_RANGE_125DPS then refrange := 2000.0; I, k+ w' T9 J end if; d2 q. x/ R" A" q% y8 _ weight := refgyr / refrange;" o% Z8 k) z% d: U5 h6 k5 E if weight > 1.0 then return 0.0; end if; return refgyr/refrange;8 e) A! P( h z' o1 M end GetGyroWeight; 8 R- e7 ^; e! O( ^( [ 使用权重,加速度值是所有值的加权平均值: procedure FilterSuperIMU(facc : out Sensor_Accel_Data;8 }1 [* _: x) w* zfgyr : out Sensor_Gyro_Data) is1 F' w* }$ N/ R0 \% {5 O: L& P) ]! J; N sumacc : Sensor_Accel_Data; -- cumulated accel value: \7 \9 `& |' Q! l sumgyr : Sensor_Gyro_Data; -- cumulated gyro value sumwacc : Float := 0.0; -- cumulated accel weight sumwgyr : Float := 0.0; -- cumulated accel weight) H7 y! j+ [1 W; ~ macc : Sensor_Accel_Data; -- measured accel value. |7 R5 s8 P: F" s/ u mgyr : Sensor_Gyro_Data; -- measured accel value mwacc : Float := 0.0; -- measured accel weight mwgyr : Float := 0.0; -- measured accel weight7 v2 z) c. d, L7 P! v) f begin, g; H( ^7 O6 l" E7 R1 I sumacc := NullAccelRate;$ F" n/ r4 W: g L$ p sumgyr := NullGyroRate; for i in Integer range 1 .. BMIs'Length loop 1 m2 d; j# K `1 ]8 | -- get accelerartion BMIs(i).ReadAccelRates(macc);* r3 j( Q9 M3 ]- q5 ^! |, s; r5 W" p -- get weight according to the range mwacc := GetAccelWeight(BMIs(i).GetCurrentAccelRange, macc);$ Y# ]+ j) r) H# R: Z- x, N [ -- apply 45° and inversion tranformation (geometry of the IMU) ApplyIMURotation(i,macc(X),macc(Y)); -- update the cumulated sum) U& P3 f8 s8 g+ R b" i+ D/ i sumacc(X) := sumacc(X) + macc(X)*mwacc; sumacc(Y) := sumacc(Y) + macc(Y)*mwacc;) i7 K; i0 D* ~5 y sumacc(Z) := sumacc(Z) + macc(Z)*mwacc;/ D. z+ g- r* i( Q sumwacc := sumwacc + mwacc; -- get the gyro rate, S; h* i9 j+ q" d& J; Z0 { r -- we don't rotate the gyro rate, no need7 P; }1 x ~) A% K) Z BMIs(i).ReadGyroRates(mgyr); -- compute the weight according to the range" h t7 J: h+ \9 A7 j$ | mwgyr := GetGyroWeight(BMIs(i).GetCurrentGyroRange, mgyr);% C. G* y+ t! M; ` -- update the cummulated weighted sum sumgyr(X) := sumgyr(X) + mgyr(X)*mwgyr;4 d0 |. W2 X# d; m3 Y j& s sumgyr(Y) := sumgyr(Y) + mgyr(Y)*mwgyr;4 w* r; `6 K5 F9 _- s& C sumgyr(Z) := sumgyr(Z) + mgyr(Z)*mwgyr; -- . f3 r! O5 c# u1 I sumwgyr := sumwgyr + mwgyr;' w3 O( C9 P$ M6 y: L end loop; facc(X) := sumacc(X) / sumwacc; facc(Y) := sumacc(Y) / sumwacc;; R8 k' w4 K6 _- t facc(Z) := sumacc(Z) / sumwacc; fgyr(X) := sumgyr(X) / sumwgyr;/ Y5 T7 V% x5 |& x8 c$ Q7 y5 h1 q7 \ fgyr(Y) := sumgyr(Y) / sumwgyr; fgyr(Z) := sumgyr(Z) / sumwgyr;, y) |# N* U" U+ {. y2 |; E end FilterSuperIMU; 0 n5 p, X0 G; M9 {% x1 p: u2 s P 估算代码: 我用一个简单的互补滤波器来估算音高和滚动。我们从IMU获得加速度和陀螺仪速率数据。 代码在这里: procedure ComplementaryFilter(accelData : Sensor_Accel_Data; gyroData : Sensor_Gyro_Data) is pitchAccel : Float := 0.0; -- pitch computed from the accel rollAccel : Float := 0.0; -- roll computed from the accel begin -- integrate the gyroscope data estimatedPitch := estimatedPitch + gyroData(X)*dt; estimatedRoll := estimatedRoll + gyroData(Y)*dt; -- compensate the drift using the acceleration pitchAccel := Ada.Numerics.Elementary_Functions.Arctan(accelData(Y),accelData(Z))*RadToDeg; estimatedPitch := estimatedPitch * 0.98 + pitchAccel * 0.02; rollAccel := Ada.Numerics.Elementary_Functions.Arctan(accelData(X),accelData(Z))*RadToDeg; estimatedRoll := estimatedRoll * 0.98 + rollAccel * 0.02; null; end ComplementaryFilter; 2 W. ^7 L$ h# E1 s 第7步:退役 使用OpenOCD 您也可以使用st-util,我已经在其他项目中完成了openocd的工作,这就是我使用它的原因。 重要(3小时获取时间评论):你需要在sudo模式下运行gnat gps,否则openocd无法访问usb端口。 安装OpenOCD 在ubuntu上使用OpenOCD的最佳方法是构建它。这样你就拥有了最新版本。我使用了gnu-mcu-eclipse/ openocd的最新版OpenOCD 。 要构建它很简单,只需确保安装了通用的C / C ++构建工具: 用于OpenOCD的配置: - 本地主机的端口为3333 - 我使用了来自openocd github 的stm32f 74discovery的配置文件。 ![]() 用于OpenOCD的配置 H K' ?9 _' r使用带有Analyzer2go的CYUSB3KIT-003对SPI进行分配 Analyzer2Go只是一个用于小预算的神奇工具。结合电路板CYUSB3KIT-003(45 $),您可以在200MHz采样率下使用功能强大的16x通道分析仪! 对于我的情况,与SPI / UART的交换低于10Mhz,并且读取Nucleo板的MCO(在100MHz下的PLL),这就足够了。 SPI测试 SPI的速度配置为STM32F746的6MHz(波特率DIV 32)。 我可以使用来自BMI088实例的SPI使用以下过程正确获取IMU运动数据: ( |8 ?2 K5 u3 I# W) d procedure ReadAccelRates (This : in out Six_Axis_Imu; Result : out Sensor_Accel_Data);- D' Y2 X- I5 C$ w' Rprocedure ReadGyroRates (This : in out Six_Axis_Imu; Result : out Sensor_Gyro_Data);2 ]& W% C* @* ^1 i 0 W& z' I( w' x 0 u1 H5 j! }- d' c. g 第8步:PLL时钟测试:+ Z) p$ c/ A1 F; p 9 X- n6 h, B7 |1 v/ i 在核板中,外部高速时钟(HSE)不是来自25MHz晶体(如Adacore内置STM32F746Disco),而是来自ST-Link芯片@ 8MHz。" e9 O3 q( v5 p% E 经过数小时的调试后,我最终修改了adl_config.ads和s-bbbopa.ads(作为workarround)文件以设置HSE时钟的正确值。我终于为Nucleo-F746创建了一个新的BSP,而不是因为这种差异而使用stm32f746disco。 现在时钟正常运行,测量显示时间: 在调试模式下读取加速值需要35us 。1 q: n+ o9 V. s& V 8 |+ c7 q9 B6 h9 K0 \3 C+ m; g 6 a9 J* u( H m$ p; _* F ![]() 在从IMU读取值结束时,我使用流动代码切换引脚PG1: -- ReadAccelRates --- b# \% v/ u- B! }0 M -------------------- procedure ReadAccelRates (This : in out Six_Axis_Imu; Result : out Sensor_Data) is Received : SPI_Data_8b (1 .. 6); -- 2 bytes per axis AccelRates : BMI088.IMU_Rates;-- todo create AccelRate type1 Y( |* ]% M% Z5 L( p begin PG1.Set;. M: L k1 g5 N* ^( W+ m This.AccelRead_UInt8s(ACC_ACCEL_DATA_ADDR,Received,6);( }( [8 B7 a7 U [( ] AccelRates.X := As_IMU_Rates_Pointer (Received (1)'Address).all; AccelRates.Y := As_IMU_Rates_Pointer (Received (3)'Address).all;( K0 A( a) I' ^+ T: v AccelRates.Z := As_IMU_Rates_Pointer (Received (5)'Address).all; Result(X) := Float (AccelRates.X) / 32768.0 * Accel_Range_MSS; Result(Y) := Float (AccelRates.Y) / 32768.0 * Accel_Range_MSS; Result(Z) := Float (AccelRates.Z) / 32768.0 * Accel_Range_MSS;0 a* T( j) H f i$ F3 K( f& T; s PG1.Clear;1 `5 N# p! R. L" R5 u end ReadAccelRates;% u3 u- ]% v" _6 ]9 G Reading在调试模式下,完整的陀螺仪值需要34us 。 ![]() 4 E0 ^: Q) u7 M9 f ------------------- -- ReadGyroRates --8 e) A6 j2 R/ K3 e7 w8 v ------------------- procedure ReadGyroRates (This : in out Six_Axis_Imu;5 ]1 |3 c7 Q9 C' o7 z Result : out Sensor_Data) is Received : SPI_Data_8b (1 .. 6); -- 2 bytes per axis0 |- G- I8 c& }- M$ } GyroRates : BMI088.IMU_Rates;-- todo create AccelRate type8 |" J! @0 K4 n H begin PG1.Set; This.AccelRead_UInt8s(ACC_ACCEL_DATA_ADDR,Received,6); GyroRates.X := As_IMU_Rates_Pointer (Received (1)'Address).all;, h8 P M5 O2 {! r6 C9 U6 K GyroRates.Y := As_IMU_Rates_Pointer (Received (3)'Address).all; GyroRates.Z := As_IMU_Rates_Pointer (Received (5)'Address).all; Result(X) := Float (GyroRates.X) / 32768.0 * Gyro_Range_Rads;* ^1 B" z& V5 Q1 J* } Result(Y) := Float (GyroRates.Y) / 32768.0 * Gyro_Range_Rads;' o( [% m5 i/ e& O7 @! a Result(Z) := Float (GyroRates.Z) / 32768.0 * Gyro_Range_Rads;% b' _! `& ~1 H1 p PG1.Clear; end ReadGyroRates; . h4 C5 P: J: s1 X) X* k' t Of 对于真实用例,我需要将编译模式设置为生产以进行正确的优化。 步骤9:使用UART绘制数据 为了可视化输出数据,我想使用UART在超级惊人的酷系列绘图仪中可视化它:https://github.com/CieNTi/serial_port_plotter ![]() 现在的问题是将Float转换为String ..以使用UART将其发送到Serial_Port_Plotter工具。 为此,我在这里使用神奇的单词'Img in Ada 将Float转换为String。然后我逐字节地将字符串发送到UART。 如果要发送一个值: - 发送“$” - 发送浮点值' P& S1 D b" R9 G - 发送“;” 如果你想发送多个值:# I( `$ X/ G. g; ] - 发送“$”开始& g7 k9 r9 x$ D) _7 _1 P - 发送浮点数值1 - 发送空格“” ... - 发送浮点值i 9 w/ m3 ?& f- A8 ]" @ - 发送空格“” ! E7 V$ _ [( d6 G; S4 N - 发送“;” 终止 6 L& Y$ k' @ X7 I7 F1 p 为了能够向绘图仪发送加速度,我使用了以下代码: 1 o% Z/ j$ b4 f w. f% s* A type FixedFloat is delta 0.00001 digits 18;---data_1B(0) := UInt8(36); -- '$'6 w: s! _7 x( p* }$ P' t UART_OUT.Transmit(data_1B,status); for ch of FixedFloat(AccelRates(X))'Img loop. F. V. d e5 h3 l$ e( F c := Character(ch);: b/ m7 \9 _7 C5 T. R data_1B(0) := Character'Pos(c);& I$ {: y+ P. p$ J4 ~; T UART_OUT.Transmit(data_1B,status);" l6 O8 K$ B W: S: [, Y/ R* n end loop; data_1B(0) := UInt8(59); -- ';'( `; V. d9 e B4 J9 p% m; B UART_OUT.Transmit(data_1B,status); * q& {5 G# w% x 结果非常令人满意,在下面的快照中我实时可视化Accelerometer(X)值: ![]() 创建驱动程序后,该数据通过UART端口正确发送: ![]() 为UART制作驱动程序 不幸的是,在STM32F7器件中还没有UART驱动器(仅适用于STM32F4)。 要为UART创建驱动程序,我从STM32 F4 的stm32-usarts.ads/adb开始。我修改了文件以符合STM32 F7 的SVD 。我还要修改STM32-Device.ads/adb以包含STM32F7的UART / USART定义。 它适用于传输9或8位大小的字节。我没有测试它的DMA风格(因为从未做过 ![]() 7 ]0 S+ u7 P$ ?6 d+ Y 代码广告: -------------------------------------------------------------------------------- --) T9 b3 ^; Y9 [ -- Copyright (C) 2015-2016, AdaCore --- L/ u6 ~2 }6 _& \! u/ m) e -- -- -- Redistribution and use in source and binary forms, with or without -- -- modification, are permitted provided that the following conditions are --% j" T0 J7 p" @0 ` -- met: --* n- f" ^$ G( ` -- 1. Redistributions of source code must retain the above copyright --9 C9 h# m$ [" ] -- notice, this list of conditions and the following disclaimer. -- -- 2. Redistributions in binary form must reproduce the above copyright -- -- notice, this list of conditions and the following disclaimer in -- -- the documentation and/or other materials provided with the -- -- distribution. -- -- 3. Neither the name of STMicroelectronics nor the names of its -- -- contributors may be used to endorse or promote products derived --, v6 A* d _4 Z. h- N+ L -- from this software without specific prior written permission. -- -- -- -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --6 R4 n+ {* c" S1 F -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --4 e% L, F* w3 @8 @& O2 [2 H -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --. K1 d7 r3 F0 r5 j# _# q0 y -- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --- P3 R% o) `* s' F9 T( [% V7 M -- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -- -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --1 ]1 Q v) Y: `5 Z* }$ b -- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -- -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -- -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -- -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -- -- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- -- -- -- --1 p; a# _( K4 N5 v4 [0 B. \ w8 O -- This file is based on: --2 u; N4 x) X$ p7 l; r4 P5 x -- --7 _& I. e/ R- x- {2 V$ g+ J -- @file stm32f4xx_hal_usart.h --- C( o& y% D. y8 O2 @7 t* r5 ^8 A -- @author MCD Application Team --( e9 I4 J, o2 n/ \ -- @version V1.1.0 -- -- @date 19-June-2014 -- -- @brief Header file of USARTS HAL module. --( M0 m i6 f; P# W. X. l, \ -- -- -- COPYRIGHT(c) 2014 STMicroelectronics --0 `! R6 {; h* P f8 B8 w( k ------------------------------------------------------------------------------ -- This file provides register definitions for the STM32F4 (ARM Cortex M4F)) e( R: ?! N+ s2 y! q -- USART from ST Microelectronics. -- Note that there are board implementation assumptions represented by the -- private function APB_Clock. with System; with HAL.UART; use HAL.UART;) x9 e3 j5 l4 S0 c1 w: u private with STM32_SVD.USART;9 m! {& e; k7 A* t: O9 | package STM32.USARTs is type Internal_USART is limited private; type USART (Periph : not null access Internal_USART) is limited new HAL.UART.UART_Port with private;9 G8 j9 T9 n/ k5 h4 `; k8 ` procedure Enable (This : in out USART) with Post => Enabled (This), Inline;8 x; C/ P# C; t9 w- O+ r procedure Disable (This : in out USART) with4 R' ?4 t$ S* i" z Post => not Enabled (This), Inline;# d. I N, X% m( t# U function Enabled (This : USART) return Boolean with Inline; procedure Receive (This : USART; Data : out UInt9) with Inline; -- reads Device.DR into Data; X9 I3 l) m; @9 N- { function Current_Input (This : USART) return UInt9 with Inline;" e% J/ ^5 M7 M$ W# y! t -- returns Device.DR procedure Transmit (This : in out USART; Data : UInt9) with Inline;8 e; F9 W% g. Y! g function Tx_Ready (This : USART) return Boolean with Inline; function Rx_Ready (This : USART) return Boolean with Inline;( ^" q: O$ Z e/ o type Stop_Bits is (Stopbits_1, Stopbits_2) with Size => 2;3 [' a' `! ^) u2 \, X+ x5 a for Stop_Bits use (Stopbits_1 => 0, Stopbits_2 => 2#10#); procedure Set_Stop_Bits (This : in out USART; To : Stop_Bits); type Word_Lengths is (Word_Length_8, Word_Length_9); procedure Set_Word_Length (This : in out USART; To : Word_Lengths);8 m3 K! S/ ~; M q' i& ^ type Parities is (No_Parity, Even_Parity, Odd_Parity);2 e) |5 n5 K4 K$ T. f procedure Set_Parity (This : in out USART; To : Parities); subtype Baud_Rates is UInt32; procedure Set_Baud_Rate (This : in out USART; To : Baud_Rates);' }6 a/ N6 E& b. x* X% _. o type Oversampling_Modes is (Oversampling_By_8, Oversampling_By_16); -- oversampling by 16 is the default) a+ j, F% d$ g5 D% A7 i' r procedure Set_Oversampling_Mode (This : in out USART;' I( }4 z' _. c6 ^: g0 q To : Oversampling_Modes);3 e# ~* W, K4 o8 S( u" S type UART_Modes is (Rx_Mode, Tx_Mode, Tx_Rx_Mode); procedure Set_Mode (This : in out USART; To : UART_Modes);* W7 |6 M3 _( E# ^ r5 {# Q. E7 p type Flow_Control is (No_Flow_Control, RTS_Flow_Control, CTS_Flow_Control,6 m% r7 F; e _0 U4 i RTS_CTS_Flow_Control); procedure Set_Flow_Control (This : in out USART; To : Flow_Control);8 M, `: Z1 c: R/ A u type USART_Interrupt is' r/ a; z3 o/ Z" S8 ~$ N( q (Parity_Error,& Q0 R) g% W7 n: v! J Transmit_Data_Register_Empty, Transmission_Complete, Received_Data_Not_Empty, Idle_Line_Detection,5 j" Y8 @% |& E0 [6 \ Line_Break_Detection,: L1 N4 u! l& W! \ Clear_To_Send,6 Z$ d# O. t8 Y7 W+ i4 t8 ^ Error);9 }! }! t6 Q% G; _9 N" y4 ^4 j$ V procedure Enable_Interrupts. [+ ?& L2 f' `2 K: r+ B (This : in out USART;9 ]; L6 t+ C' E+ k: N, { Source : USART_Interrupt)$ f6 S! D9 q9 N) n4 U1 p with4 V1 H# B& p P1 f T% v Post => Interrupt_Enabled (This, Source), Inline; procedure Disable_Interrupts (This : in out USART;' d5 F7 s) L% S Source : USART_Interrupt) with: `* Y, |2 W/ G* P- y( P+ T- S Post => not Interrupt_Enabled (This, Source),$ P7 |( C4 Y+ u; J4 r) } Inline; function Interrupt_Enabled (This : USART; Source : USART_Interrupt)/ C. ~# \, J0 }, s! B return Boolean* c+ Q4 D( U5 e8 n& l% h5 M/ G with Inline; type USART_Status_Flag is (Parity_Error_Indicated,, d( t* i! V, s% F' p) U4 H Framing_Error_Indicated, USART_Noise_Error_Indicated, Overrun_Error_Indicated, Idle_Line_Detection_Indicated, Read_Data_Register_Not_Empty," x( l8 b! e0 S& i1 X2 T Transmission_Complete_Indicated,8 T, X9 ?! P/ _" L8 p0 K Transmit_Data_Register_Empty, Line_Break_Detection_Indicated,5 L4 [: q- b5 X5 d Clear_To_Send_Indicated);1 z$ c; o* O+ }# C function Status (This : USART; Flag : USART_Status_Flag) return Boolean with Inline; procedure Clear_Status (This : in out USART; Flag : USART_Status_Flag)& M, H+ w/ N; O. J0 t" S+ o- |: C with Inline; procedure Enable_DMA_Transmit_Requests (This : in out USART) with Inline,/ T* F9 t+ g3 ?. e4 M3 j, m Post => DMA_Transmit_Requests_Enabled (This); procedure Disable_DMA_Transmit_Requests (This : in out USART) with7 u( V+ g3 C4 N) ~5 B Inline, Post => not DMA_Transmit_Requests_Enabled (This);- V9 o! `9 y( J4 i2 j" U function DMA_Transmit_Requests_Enabled (This : USART) return Boolean with Inline;- p1 v# W& P r. {0 A! b8 f8 }4 W procedure Enable_DMA_Receive_Requests (This : in out USART) with$ G5 E ^5 `- A, C Inline,6 @. r+ h+ y" @* A$ a3 ] B Post => DMA_Receive_Requests_Enabled (This);: _# G' ~9 a) \2 K1 B procedure Disable_DMA_Receive_Requests (This : in out USART) with o; x* M9 J" ~7 n9 B Inline,/ D4 M# u' f! _% ] Post => not DMA_Receive_Requests_Enabled (This);9 T+ i( q& }8 V1 z) n7 @. E function DMA_Receive_Requests_Enabled (This : USART) return Boolean with Inline; procedure Pause_DMA_Transmission (This : in out USART) renames Disable_DMA_Transmit_Requests; procedure Resume_DMA_Transmission (This : in out USART) with Inline, Post => DMA_Transmit_Requests_Enabled (This) and* H0 ^( A* u2 T Enabled (This); procedure Pause_DMA_Reception (This : in out USART) renames Disable_DMA_Receive_Requests; procedure Resume_DMA_Reception (This : in out USART) with Inline, Post => DMA_Receive_Requests_Enabled (This) and+ L% h$ d$ w" t0 F) s. d Enabled (This); function Data_Receive_Register_Address (This : USART) return System.Address with& T. @! b6 w0 P! L6 y" k Inline; function Data_Transmit_Register_Address (This : USART) return System.Address with+ V* ~% M7 D+ e Inline; h% M# o# x9 Z" v$ h/ _ -- Returns the address of the USART Data Register. This is exported- \& \4 ~/ P4 z2 y+ g6 |/ Y7 P -- STRICTLY for the sake of clients driving a USART via DMA. All other; _7 M$ h0 b+ [8 B -- clients of this package should use the procedural interfaces Transmit, ?) k* v8 {2 y; {" n/ @ -- and Receive instead of directly accessing the Data Register!2 i' V) Z7 z2 P7 f) s0 o -- Seriously, don't use this function otherwise.% A! @5 |" H; G, x S% {8 r& Z6 _ -----------------------------) x" t- [ Q( m6 p! d2 p% {6 d6 J -- HAL.UART implementation --9 T* F3 J. ~( t) T) U/ t+ p ----------------------------- overriding function Data_Size (This : USART) return HAL.UART.UART_Data_Size;4 g; l+ }" L, o y. O5 o3 B overriding procedure Transmit (This : in out USART; Data : UART_Data_8b;6 Z3 U* Y3 \. n5 k, J* A1 w9 v Status : out UART_Status; Timeout : Natural := 1000)/ w( w: `8 ?9 q% M5 ]% v with Pre'Class => Data_Size (This) = Data_Size_8b;6 C% J; z5 c& P6 p) Q! X: k0 s overriding, U) T6 w. g7 I9 `9 ]" i" c procedure Transmit (This : in out USART; Data : UART_Data_9b;0 U+ D* {1 }- P/ j Status : out UART_Status; Timeout : Natural := 1000)% O/ C0 j& F% o7 t with& ^$ h$ |$ _' {7 Z Pre'Class => Data_Size (This) = Data_Size_9b;5 C! k/ O* b/ `0 t- g overriding (This : in out USART;9 I" n7 p3 e; i0 W8 ^, k6 @. e4 C( \+ n Data : out UART_Data_8b; Status : out UART_Status;, Z6 t0 J' N, F Timeout : Natural := 1000) with Pre'Class => Data_Size (This) = Data_Size_8b; overriding! L4 m/ n" @; q. h0 i5 d& {- V( u9 R procedure Receive (This : in out USART; Data : out UART_Data_9b; Status : out UART_Status; Timeout : Natural := 1000) with Pre'Class => Data_Size (This) = Data_Size_9b;; i7 C6 [& s: b. z( o private function APB_Clock (This : USART) return UInt32 with Inline; -- Returns either APB1 or APB2 clock rate, in Hertz, depending on the$ Z ]# X# \ d: l -- USART. For the sake of not making this package board-specific, we assume -- that we are given a valid USART object at a valid address, AND that the -- USART devices really are configured such that only 1 and 6 are on APB2.6 E% }+ B5 V$ R: {6 L -- Therefore, if a board has additional USARTs beyond USART6, eg USART8 on -- the F429I Discovery board, they better conform to that assumption. -- See Note # 2 in each of Tables 139-141 of the RM on pages 970 - 972.5 l! Y, |6 k; n6 M- P* v; y type Internal_USART is new STM32_SVD.USART.USART_Peripheral;5 V2 Q( H9 [: w; ?+ l type USART (Periph : not null access Internal_USART) is limited new HAL.UART.UART_Port with null record; end STM32.USARTs; m" Q1 r8 n8 b* w 代码adb: -------------------------------------------------------------------------------- --3 [' h9 P) T$ Q) y1 S | -- Copyright (C) 2015-2017, AdaCore --$ F1 E* |7 F6 u7 s -- -- -- Redistribution and use in source and binary forms, with or without -- -- modification, are permitted provided that the following conditions are --4 z) [7 K" F! ^# m7 z/ d -- met: -- -- 1. Redistributions of source code must retain the above copyright --( }6 \2 j, T, L% c# D* ? t( Y -- notice, this list of conditions and the following disclaimer. --, A8 I# x+ I( ?# ?0 ?& x% Q -- 2. Redistributions in binary form must reproduce the above copyright --2 z1 c( ^) O k; g6 X9 w: M4 P -- notice, this list of conditions and the following disclaimer in --0 C+ U9 Q$ K9 f -- the documentation and/or other materials provided with the --3 A7 `5 U+ b' N1 j0 d+ x, R -- distribution. -- -- 3. Neither the name of STMicroelectronics nor the names of its --/ C) C5 O% ]8 P) b2 F -- contributors may be used to endorse or promote products derived -- -- from this software without specific prior written permission. -- -- --. Q$ a; M6 O9 U; ^. d1 q. C -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --; b9 J4 F, }1 B* m7 B -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --8 ~" T7 t& H D( \ -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -- -- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --) o: W# C; ~* F5 B, B( N -- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --- I/ H% U$ F5 g. u -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -- -- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -- -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --; C6 w; I$ a' u -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --' X/ y: J x1 G& w -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --# n( {: z: [; D1 } -- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- -- --7 {* E. I8 b9 K0 w -- --! S! \0 r+ y+ L+ O: M -- This file is based on: -- -- -- -- @file stm32f4xx_hal_usart.c --" l' w1 l# f( o# g. M$ q -- @author MCD Application Team --8 @5 L; }$ J+ t6 E' }) X -- @version V1.1.0 -- -- @date 19-June-2014 --9 |/ G1 z! S$ C -- @brief USARTS HAL module driver. -- -- --" U3 y! X! D" a6 H3 f+ Y3 O, y -- COPYRIGHT(c) 2014 STMicroelectronics --* f1 s9 R% ], ^ }+ G, u ------------------------------------------------------------------------------ with System; use System;; r$ `2 M! v' @/ |/ S5 F* d& X2 \ with STM32_SVD.USART; use STM32_SVD, STM32_SVD.USART; with STM32.Device; use STM32.Device;, Q+ ~8 M" w$ Q package body STM32.USARTs is3 F3 @/ _% N- v% m+ f" ?9 T --------------- -- APB_Clock -- ---------------' T2 @# Z: D, @' A6 c function APB_Clock (This : USART) return UInt32 is Clocks : constant RCC_System_Clocks := System_Clock_Frequencies;# M6 G3 Z$ |$ ~: n6 Q begin5 V9 K1 j* T7 |! w1 O# x7 o% m if This.Periph.all'Address = USART1_Base% ]2 m) w4 a M* J! O or This.Periph.all'Address = USART6_Base2 o6 r* r( l4 c( e then return Clocks.PCLK2; else return Clocks.PCLK1;& t8 m) C( R y7 s9 D( ^7 i end if;8 U: _& B6 n f end APB_Clock;- j4 K2 J$ R# Q/ e! r ------------ -- Enable --9 M2 a$ G" {: o7 j; ] ------------+ Q& E* b1 q3 m8 j procedure Enable (This : in out USART) is begin$ r% K" P, S* F w) l This.Periph.CR1.UE := True; end Enable;! K7 Z& m; x8 X5 X7 Y& r( @ -------------6 ?( Z6 T2 Y7 G" I! R: c -- Disable -- -------------6 x- Y1 d( [. P0 @) d- a I' @ procedure Disable (This : in out USART) is begin* m B2 j$ ]5 M6 ^2 e! o This.Periph.CR1.UE := False;. W( Z v- _1 Q1 Q& k end Disable;3 V: s: C% R+ G) T ------------- -- Enabled --* H+ k( \+ E! L+ Y: N6 d7 ] -------------9 g# T/ t( B' P) [9 ^) t3 t* {' D function Enabled (This : USART) return Boolean is (This.Periph.CR1.UE);, P" `3 r$ d# U/ d: u0 p# W3 T -------------------+ b9 V u7 H5 G5 |7 g -- Set_Stop_Bits --2 k- [: j9 i' ^( A: Q& S- r0 T -------------------9 ~; V/ c" o8 M& @, |4 `) j4 { procedure Set_Stop_Bits (This : in out USART; To : Stop_Bits) is begin This.Periph.CR2.STOP := Stop_Bits'Enum_Rep (To);* a0 ~7 y+ \$ K4 c' H: l end Set_Stop_Bits;4 W- n# w7 _) R4 X5 a- A ---------------------* q0 T/ B) t$ V -- Set_Word_Length -- ---------------------& t6 D4 Q2 V9 x% f- L procedure Set_Word_Length6 L: @8 @" B% G. A% p& L0 J/ [ (This : in out USART; To : Word_Lengths) is begin ]( z! M5 H' f7 e) m7 D" f/ T This.Periph.CR1.M0 := To = Word_Length_9; end Set_Word_Length; ---------------- -- Set_Parity -- ---------------- procedure Set_Parity (This : in out USART; To : Parities) is0 `5 b4 W3 d: [0 u' j) [ begin case To is- D# [- e C7 Z3 T when No_Parity => This.Periph.CR1.PCE := False; This.Periph.CR1.PS := False;" M) p$ v9 ~% s. I: b; @1 ` when Even_Parity =>$ Q& F: T# E! ~* s This.Periph.CR1.PCE := True; This.Periph.CR1.PS := False;2 d( l4 i8 h* ^ n0 k when Odd_Parity => This.Periph.CR1.PCE := True; This.Periph.CR1.PS := True; end case;% T+ q' ?- X* L; F; A, c end Set_Parity; ------------------- -- Set_Baud_Rate -- -------------------- L* b7 K2 f9 J0 p+ R4 o* w' Q procedure Set_Baud_Rate (This : in out USART; To : Baud_Rates)( j1 w, y+ X& X7 y' y) F is6 X/ b' ]& d t4 j( c% p Clock : constant UInt32 := APB_Clock (This);5 o, v% `2 n0 B0 O* p: J' U Over_By_8 : constant Boolean := This.Periph.CR1.OVER8; Int_Scale : constant UInt32 := (if Over_By_8 then 2 else 4); Int_Divider : constant UInt32 := (25 * Clock) / (Int_Scale * To);5 p' y9 E7 i, F& Y$ Q9 E Frac_Divider : constant UInt32 := Int_Divider rem 100; begin -- the integer part of the divi2 z2 s( e' p6 z& }/ w if Over_By_8 then: Z% k4 B- c2 f$ R This.Periph.BRR.DIV_Fraction := BRR_DIV_Fraction_Field (((Frac_Divider * 8) + 50) / 100 mod 8); else This.Periph.BRR.DIV_Fraction :=; F8 w8 Y6 c* ^6 E3 B BRR_DIV_Fraction_Field (((Frac_Divider * 16) + 50) / 100 mod 16); end if; This.Periph.BRR.DIV_Mantissa := BRR_DIV_Mantissa_Field (Int_Divider / 100); F! V4 w9 t8 L. R end Set_Baud_Rate; ---------------------------/ _5 i5 I2 g- w4 c4 d -- Set_Oversampling_Mode -- ---------------------------3 F* R( o5 K2 j/ d3 S5 } procedure Set_Oversampling_Mode# G. z4 V* S4 V (This : in out USART; To : Oversampling_Modes). I, N8 X6 ~; u) `6 S# J9 ]1 ` is begin, X' ~* ^- R- |( G5 E This.Periph.CR1.OVER8 := To = Oversampling_By_8;& n/ k0 ?4 Q6 F A: @ end Set_Oversampling_Mode;. H) u+ Y3 F* T -------------- -- Set_Mode -- --------------5 X3 @& |8 Z0 j# `' H7 T procedure Set_Mode (This : in out USART; To : UART_Modes) is begin This.Periph.CR1.RE := To /= Tx_Mode;% ?) E0 T6 y( w; }0 i This.Periph.CR1.TE := To /= Rx_Mode;1 `/ ~: g" r# S2 E end Set_Mode;$ s; J2 | k! K& H ----------------------! E2 n5 ^$ K. i2 y+ \: S5 ~7 r0 |2 w( n# g -- Set_Flow_Control --7 c* }5 L; C$ p4 U* H5 V ----------------------+ u. g3 K5 L! A( r procedure Set_Flow_Control (This : in out USART; To : Flow_Control) is; P8 {( v/ @! ?: T/ U/ Y" J begin" W' E2 ^5 \# q x0 T case To is5 c- `0 \( M* E7 k R0 G when No_Flow_Control => This.Periph.CR3.RTSE := False;1 ?: ]/ X4 I' x/ g3 w This.Periph.CR3.CTSE := False;/ a- T, F' h; b1 s when RTS_Flow_Control =>& m% m& m. Z) ^ This.Periph.CR3.RTSE := True; This.Periph.CR3.CTSE := False; when CTS_Flow_Control => This.Periph.CR3.RTSE := False; This.Periph.CR3.CTSE := True; when RTS_CTS_Flow_Control =>5 F: D k: ~4 r# o4 ` This.Periph.CR3.RTSE := True; This.Periph.CR3.CTSE := True;% z& w- e" |4 X' T" g3 X end case; end Set_Flow_Control;# B8 H0 z5 T) [ --------- -- Put -- --------- procedure Transmit (This : in out USART; Data : UInt9) is begin1 j1 A7 E* Y* } P0 `! i This.Periph.TDR.TDR := TDR_TDR_Field (Data); end Transmit;0 _9 p# ]1 x7 G# ~ ---------! D. S/ j+ G: N* W& A4 N -- Get -- --------- procedure Receive (This : USART; Data : out UInt9) is# ?& w9 w; l- I1 E! j6 h begin Data := Current_Input (This); end Receive;' ~" t! ^3 C) A% l- x& S8 k/ k -------------------$ I7 ]* y7 J3 x -- Current_Input -- ------------------- function Current_Input (This : USART) return UInt9 is (Uint9 (This.Periph.RDR.RDR)); --------------* v' w s5 {9 V -- Tx_Ready -- --------------+ _+ y& d/ U. k6 e: _3 r function Tx_Ready (This : USART) return Boolean is8 I: o6 d) B5 q; u begin return This.Periph.ISR.TXE; T/ S' N" M+ U0 V, v8 j end Tx_Ready; -------------- -- Rx_Ready -- -------------- function Rx_Ready (This : USART) return Boolean is r( g p* J P4 J! Y" { begin return This.Periph.ISR.RXNE; end Rx_Ready; ------------ -- Status -- ------------ function Status (This : USART; Flag : USART_Status_Flag) return Boolean is begin case Flag is5 H0 v7 V! R5 F" p: ^1 I when Parity_Error_Indicated =># g4 m+ Q, r* r4 E) C/ l8 t return This.Periph.ISR.PE; when Framing_Error_Indicated => return This.Periph.ISR.FE; when USART_Noise_Error_Indicated => return This.Periph.ISR.NF; when Overrun_Error_Indicated =>( K- K: }9 o' J |3 T, ` return This.Periph.ISR.ORE; when Idle_Line_Detection_Indicated => return This.Periph.ISR.IDLE;- E4 J: U4 R* ?4 y when Read_Data_Register_Not_Empty =># Y& W8 G6 q5 h return This.Periph.ISR.RXNE;4 a/ L+ z2 H! t- P7 s when Transmission_Complete_Indicated =>/ q( M4 F5 u) y8 O return This.Periph.ISR.TC; when Transmit_Data_Register_Empty =>% [' d# I. g2 f6 A* I+ a, Y r0 Y return This.Periph.ISR.TXE; when Line_Break_Detection_Indicated => return This.Periph.ISR.LBDF;2 u+ C2 `/ w& L0 I7 F! P when Clear_To_Send_Indicated =>' J( N+ I& h' B; ~9 m return This.Periph.ISR.CTS; end case; end Status;! A8 [6 V1 ^+ Q& d2 F1 c2 o ------------------! m. O+ T& P a- m -- Clear_Status -- ------------------ procedure Clear_Status (This : in out USART; Flag : USART_Status_Flag) is9 ?8 A \; Z' w8 I begin case Flag is! x0 p$ u7 M0 k1 e8 h) g- n" _4 I when Parity_Error_Indicated => This.Periph.ISR.PE := False; when Framing_Error_Indicated => This.Periph.ISR.FE := False;* G% h" s- Y1 _ v8 k( r when USART_Noise_Error_Indicated => This.Periph.ISR.NF := False;7 g8 g0 `5 o: m% b6 G9 E" J when Overrun_Error_Indicated => This.Periph.ISR.ORE := False;; M+ A& i, Z* B0 M U L, q when Idle_Line_Detection_Indicated =>& d8 J9 e" \! V This.Periph.ISR.IDLE := False; when Read_Data_Register_Not_Empty => This.Periph.ISR.RXNE := False; when Transmission_Complete_Indicated =>; Q! |' Y: f- y/ A This.Periph.ISR.TC := False; when Transmit_Data_Register_Empty =>. K5 M# S' N, ?! W6 r3 w This.Periph.ISR.TXE := False; when Line_Break_Detection_Indicated =># F8 h5 K& a2 J$ z This.Periph.ISR.LBDF := False; when Clear_To_Send_Indicated =>5 [0 b: z* }" J$ a# E This.Periph.ISR.CTS := False; end case; end Clear_Status; ----------------------- -- Enable_Interrupts --& O2 k* l! I, g) I2 D% I+ } -----------------------! K8 B, z! ], h+ _+ X5 P& u procedure Enable_Interrupts8 p9 A+ J R9 U+ q (This : in out USART;! B4 B8 U F( } Source : USART_Interrupt); t. y( t1 i4 B& ^, T: B; Y is begin. U1 u3 K5 n& k/ |/ R& ] case Source is, T; L) }# s9 v/ } when Parity_Error => This.Periph.CR1.PEIE := True;# C7 u' l# f& @0 M# U when Transmit_Data_Register_Empty => This.Periph.CR1.TXEIE := True;; e! q5 h8 ~2 i( ]$ `9 Y when Transmission_Complete =>- r1 u0 @% S d# C( Q/ C, h+ r This.Periph.CR1.TCIE := True;" W x2 f3 J M7 R) x3 Q9 w) X when Received_Data_Not_Empty => This.Periph.CR1.RXNEIE := True; when Idle_Line_Detection =>* t u' |7 O2 O" i This.Periph.CR1.IDLEIE := True;; v0 p) J2 e8 t6 X$ E/ f when Line_Break_Detection => This.Periph.CR2.LBDIE := True; when Clear_To_Send => This.Periph.CR3.CTSIE := True;" n/ F# s6 \7 k5 m. j8 p+ U when Error => This.Periph.CR3.EIE := True;# Q' X5 v3 B: w+ P end case;0 F: ?* C/ m3 r0 h2 e end Enable_Interrupts;$ Y" r# M* {$ P) K G ------------------------ -- Disable_Interrupts -- ------------------------ procedure Disable_Interrupts (This : in out USART; Source : USART_Interrupt)9 M+ j- V c7 O is: ~4 M/ q* v3 `" Q5 F6 z: ] Q) J" I begin case Source is when Parity_Error =>: ]# O+ @# m' x5 j This.Periph.CR1.PEIE := False;* |6 b$ E4 R4 d z( n. ? when Transmit_Data_Register_Empty =>. ~ b5 n4 f7 p8 e( R This.Periph.CR1.TXEIE := False;: d$ ], e8 O3 z! @3 G6 [: C8 ^8 V1 \ when Transmission_Complete => This.Periph.CR1.TCIE := False; when Received_Data_Not_Empty => This.Periph.CR1.RXNEIE := False;& d- @ o3 o- w; E9 L& e when Idle_Line_Detection => This.Periph.CR1.IDLEIE := False; when Line_Break_Detection => This.Periph.CR2.LBDIE := False;: R5 }% k* L5 }8 n when Clear_To_Send => This.Periph.CR3.CTSIE := False;* D8 E: `* |' C1 {# O when Error => This.Periph.CR3.EIE := False;/ |& r9 c( p7 H" w end case; end Disable_Interrupts;5 m- j5 z0 t1 \9 {% B7 o _ ----------------------- -- Interrupt_Enabled -- ----------------------- function Interrupt_Enabled (This : USART; Source : USART_Interrupt)0 c' `* r, j$ T: w/ w- b return Boolean; R- q+ c6 H, L# y' W is begin case Source is when Parity_Error =>* ]& a- t7 k5 V1 W0 P return This.Periph.CR1.PEIE; when Transmit_Data_Register_Empty =>( {, Y( j1 F6 O: b$ j/ T( x return This.Periph.CR1.TXEIE; when Transmission_Complete => return This.Periph.CR1.TCIE; when Received_Data_Not_Empty =>' B3 v1 {2 ~0 T3 t9 ? return This.Periph.CR1.RXNEIE; when Idle_Line_Detection =>* Y* h( {, E- G. Q return This.Periph.CR1.IDLEIE; when Line_Break_Detection => return This.Periph.CR2.LBDIE; n# w! ]5 G: d5 q when Clear_To_Send => return This.Periph.CR3.CTSIE;" W( Y1 F% S6 a7 u4 {" q when Error =>) C, x: ^7 W( H# C9 M; l# M return This.Periph.CR3.EIE;$ i& O" _% r5 g' H* Y! @, T' y end case; end Interrupt_Enabled;* D: L8 L# ]1 T% D2 h ----------------------------------( M& D r9 t% C8 g -- Enable_DMA_Transmit_Requests --8 J7 `. @- K, U' b1 e+ r ---------------------------------- procedure Enable_DMA_Transmit_Requests (This : in out USART) is begin This.Periph.CR3.DMAT := True; end Enable_DMA_Transmit_Requests; ---------------------------------# n8 G7 c1 q% H, e" \ -- Enable_DMA_Receive_Requests --' N4 M5 a5 J# X# r ---------------------------------# [ |& z. D' T: y4 f' W# e procedure Enable_DMA_Receive_Requests (This : in out USART) is7 J) H6 |4 W4 H0 H$ a begin This.Periph.CR3.DMAR := True; end Enable_DMA_Receive_Requests;; _% ~- B2 f4 E1 v6 L ----------------------------------- -- Disable_DMA_Transmit_Requests --4 \5 L( X* `2 E# p1 R; [- }- c! @7 U -----------------------------------* ^, u% ]7 g' Y, |, X0 L procedure Disable_DMA_Transmit_Requests (This : in out USART) is" U+ K0 O, l: F( X9 b# A* a9 `# h begin/ A+ l1 K5 A% c% \- w# y: |; I+ h This.Periph.CR3.DMAT := False; end Disable_DMA_Transmit_Requests; ----------------------------------" ?4 ?. `1 I" H4 L# Q -- Disable_DMA_Receive_Requests --. N) R4 v- z/ i# c0 L. [& @ ---------------------------------- procedure Disable_DMA_Receive_Requests (This : in out USART) is/ T9 C" k& i4 x5 p1 \ begin- t* V: W# V4 `1 ^4 S( m. f8 E This.Periph.CR3.DMAR := False; end Disable_DMA_Receive_Requests; ----------------------------------- -- DMA_Transmit_Requests_Enabled --4 Z2 G7 \- @0 q) o( i6 j: k4 P; b ----------------------------------- function DMA_Transmit_Requests_Enabled (This : USART) return Boolean is" a) l: T6 A& u0 U0 Z (This.Periph.CR3.DMAT); ----------------------------------6 G; j9 k4 u0 z; a -- DMA_Receive_Requests_Enabled --0 O7 R9 ~ N3 q% b4 b4 D ----------------------------------* Z- v: A; C: m9 L' O function DMA_Receive_Requests_Enabled (This : USART) return Boolean is3 N8 O' F3 k! ]+ W2 J. n t (This.Periph.CR3.DMAR);; U1 j* x2 C8 h -----------------------------0 r7 Y, |5 w" W0 O/ @! g: z! V -- Resume_DMA_Transmission -- ----------------------------- procedure Resume_DMA_Transmission (This : in out USART) is4 I% D( @& J2 w7 ] S3 m begin Enable_DMA_Transmit_Requests (This); if not Enabled (This) then: i/ ^9 x0 r5 X Enable (This);/ X9 V& W# @4 h; S& l S6 l end if; Z8 U5 ~9 e2 |; W9 `9 [+ O [ end Resume_DMA_Transmission;$ G: B2 V7 G( q% E. q' r; v l -------------------------- -- Resume_DMA_Reception --# C0 |' O" M4 o& B( K -------------------------- procedure Resume_DMA_Reception (This : in out USART) is begin Enable_DMA_Receive_Requests (This);$ V" [4 C* h- K. D7 a6 p) L, ?, b if not Enabled (This) then6 ]. G7 c) ] `: x: s: Q: n Enable (This); end if; end Resume_DMA_Reception;, d% e. R' L( _( k5 d& V5 E7 l! ]7 N ---------------------------, H) W2 m- Z# Y) I6 g- G5 | -- Data_Register_Address -- ---------------------------5 d# q/ Q' y/ X. `# c E" x function Data_Receive_Register_Address (This : USART) return System.Address is (This.Periph.RDR'Address); --------------------------- -- Data_Register_Address -- ---------------------------8 y+ z0 d$ w3 F( t function Data_Transmit_Register_Address (This : USART) return System.Address is, q* f+ U8 Q5 {+ i (This.Periph.TDR'Address);+ c% T/ B9 [; p! o$ ^0 L" | --------------- -- Data_Size --+ B3 z- v- H9 J7 M' H( Y ---------------( W* @ U* N, U. w overriding function Data_Size (This : USART) return HAL.UART.UART_Data_Size is begin3 @* @2 k; A y' x+ [( u- f2 m; b if This.Periph.CR1.M0 then$ B" U% ~$ _) X- l" h! B: u return Data_Size_9b; else( u# }! c% s( S3 `+ w* {! ^ return Data_Size_8b;1 `0 L' F: T+ r( X, u" V end if;/ v/ `6 N. h0 R7 `# N end Data_Size;" J7 ^+ [, ^ m* W: u -------------- -- Transmit --: Z" c/ @ u2 Q" e9 u5 z4 r! | --------------( X- [4 a9 s" s( D/ e. _: v overriding" c3 S$ o0 w7 E! P5 R7 \5 { procedure Transmit+ G: h' C' U P' t+ [9 Z" q' l (This : in out USART; Data : UART_Data_8b; Status : out UART_Status;% F G$ v* g' M: I0 R3 Q Timeout : Natural := 1000)/ S( ^( Y8 e: j' I is6 c+ t7 t* m6 K! C% G# z pragma Unreferenced (Status, Timeout); begin$ x! P" F) x2 n, F! i* F8 Y, j6 T: x for Elt of Data loop loop* M# v& o8 ]. M; S; \/ ]; Y4 @& z exit when This.Tx_Ready;9 D; L6 P: F T- o; _ end loop;2 |8 X4 c, j4 C# I- A5 `' R2 t9 E This.Transmit (UInt9 (Elt)); end loop;; d* r+ ~; i! | Status := Ok; end Transmit; -------------- -- Transmit --% p, B2 i8 ]4 p- b --------------* A$ N4 N' R: o8 }% d, Z1 T overriding$ q# ^& X& N5 i) I1 Z/ Z procedure Transmit1 R1 C g0 ^2 W7 f+ e' t* B1 E (This : in out USART;( `8 r# k% R* b Data : UART_Data_9b; Status : out UART_Status; Timeout : Natural := 1000) is" T6 K8 l: n* v2 V) k0 R pragma Unreferenced (Status, Timeout);2 @/ g7 y7 ^8 I& n, G begin for Elt of Data loop$ u- B! x- Z+ e7 ~* A: n G loop exit when This.Tx_Ready; end loop;, D7 f- F7 z0 G; c; B) W+ s a9 D This.Transmit (Elt); end loop; Status := Ok; end Transmit;& Q" p& Y: p: Q1 M- R' e -------------/ R( w- Z, }$ r/ `# v" g8 N -- Receive -- ------------- overriding procedure Receive* q" e3 v1 k! r* p5 J1 l+ W (This : in out USART; Data : out UART_Data_8b;$ F. u" T, ~- R3 j6 j* p Status : out UART_Status;2 o& l% ?; M Q: B( n Timeout : Natural := 1000)' k) O0 w4 d' R0 @+ g is$ E: c. Y( |& q. T+ w! A. ^: r, N pragma Unreferenced (Status, Timeout);! y; _* J: @0 i# t" o begin4 F5 f; h4 y/ C2 k' G' y4 S for Elt of Data loop' e" V0 C7 W# f1 f0 _) A+ v loop2 a4 o3 {% H1 B$ n2 i# J6 b7 F exit when This.Rx_Ready;' @ V& v3 l! w! F; i end loop; This.Receive (UInt9 (Elt)); end loop; Status := Ok;& [( C6 u+ _8 r! O4 ^ end Receive;/ g' H* N4 B' s$ [! F2 D7 C ------------- -- Receive -- ------------- overriding procedure Receive (This : in out USART;. y! U# y3 C$ O+ | Data : out UART_Data_9b;4 \* v& ^# D$ o8 `$ j) m- I Status : out UART_Status; Timeout : Natural := 1000)3 [' e- y+ \: p- } is- D$ c- {9 p9 r1 A5 F* l! [* \ pragma Unreferenced (Status, Timeout);, Q$ u$ N1 {% o6 J, t& g begin for Elt of Data loop5 H; S/ C- c3 v7 U loop) J1 m$ x9 s3 e6 _8 Y6 [ exit when This.Rx_Ready; end loop;% |: w6 K& q2 l/ \. l* Q This.Receive (Elt); C( C9 J$ N$ D: g end loop; Status := Ok;! Q0 o1 y0 y) i8 C3 n7 ]3 k end Receive;8 B2 Z! x0 K5 u6 q end STM32.USARTs; 8 Y5 q4 U) v* s( m" v- V% G 第10步:结果 过滤后的SuperIMU在振动时更稳定。来自振动的噪声由多量程配置值和冗余度补偿。 结果非常酷,在图片中,我在垂直握手时抓住了滚动,桌子上有一些小的震动(更多的测试会有严重的振动): ![]() 红色:单个IMU,黄色:过滤SuperIMU(滚动) 测量的加速度和陀螺仪速率的加权平均值有助于减少振动环境中的噪声(例如,握手)。然而,成本与获得性能的使用可能不值得结果 ![]() ![]() 红色:单个IMU,黄色:过滤的SuperIMU ' a7 ^; x5 m3 i最后,使用以下命令使用生产编译模式: sudo gprbuild --target=arm-eabi -d superimu.gpr -XLCH=led -XRTS_Profile=ravenscar-full -XLOADER=ROM -XADL_BUILD_CHECKS=Disabled -XADL_BUILD=Production superimu.adb. i0 G/ u$ S' y8 F9 Z5 v9 ? 我能够在1ms内达成执行: ![]() 结论 经滤波的SuperIMU允许相对于振动和高加速度计算显着的俯仰和行的稳定值。 使用STM32F746 @200MHz通过过滤13x IMU并在1毫秒内估计一段时间内的音调,取得了良好的效果。 似乎对于火箭的需求,配置12G范围的单个BMI088足以满足火箭需求,而不是为超精确IMU支付300美元,使用两个BMI088突破可能会很有趣,每个25美元。 另一方面,在这个项目中,我学会了如何为STM32制作驱动程序(更新SPI驱动程序,制作UART驱动程序)以及制作BMI088芯片驱动程序。在嵌入式领域中,进行预检查,检查后具有强类型定义的可能性是一个真正的好处。与以前使用的C / C ++相比,在Ada中我不能错过UInt4和UInt8 ![]() 这是一次有趣的体验,在成功完成项目的这一重大步骤后,我感兴趣的是为我的个人项目挖掘更多并为其他板和传感器制作其他Ada驱动程序。并且还使用Adacore框架继续开发火箭的自动驾驶仪。 8 D, Y# Y) z! s9 s! B# w " x6 l. c3 c! g$ B P! h8 e 0 ~9 n9 [1 M( t% w9 S |