【Follow me第二季第4期】任务2: IMU传感器
本帖最后由 zsy-s 于 2024-11-29 14:54 编辑<p># 介绍</p>
<p>LSM6DSOXTR是一款高性能的6轴惯性测量单元(IMU),由STMicroelectronics(意法半导体)制造。集成了3轴数字加速度计和3轴数字陀螺仪,采用系统级封装技术,具有出色的性能和低功耗特性。提供SPI、I²C和MIPI I3C串行接口,支持主处理器数据同步。是一款功能强大、性能卓越的6轴IMU。</p>
<p>封装度深得好处,显而易见,开发毫无费力,手册都不用看得。</p>
<p># 代码</p>
<p>imu模块有依赖库可使用。</p>
<p>在包管理器上,搜索LSM6DSOX。</p>
<p> </p>
<div style="text-align: left;"></div>
<div style="text-align: center;"> </div>
<div style="text-align: center;"> </div>
<p>添加 #include <Arduino_LSM6DSOX.h> 应用就可以愉快开发了。</p>
<p>如下是依赖库得接口说明</p>
<div></div>
<p>初始化imu模块</p>
<pre>
<code class="language-cpp">if(IMU.begin() == 0){
Serial.println("IMU.begin fail \n\r ");
}</code></pre>
<p>读取 加速计和陀螺仪数据。</p>
<pre>
<code class="language-cpp">float x,y,z;
float gx, gy, gz;
// the loop function runs over and over again forever
void loop() {
digitalWrite(LEDB, HIGH);// turn the LED on (HIGH is the voltage level)
digitalWrite(LEDR, LOW); // turn the LED off by making the voltage LOW
digitalWrite(LEDG, LOW); // turn the LED off by making the voltage LOW
delay(200); // wait for a second
digitalWrite(LEDB, LOW); // turn the LED off by making the voltage LOW
digitalWrite(LEDR, HIGH); // turn the LED off by making the voltage LOW
digitalWrite(LEDG, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(LEDB, LOW); // turn the LED off by making the voltage LOW
digitalWrite(LEDR, LOW); // turn the LED off by making the voltage LOW
digitalWrite(LEDG, HIGH); // turn the LED off by making the voltage LOW
delay(200); // wait for a second
Serial.println("RP2040 hello \n\r ");
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x,y,z);
Serial.print("Acceleration:");
Serial.print(x);
Serial.print('\t');
Serial.print(y);
Serial.print('\t');
Serial.println(z);
}
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(gx, gy, gz);
Serial.print("Gyroscope:");
Serial.print(gx);
Serial.print('\t');
Serial.print(gy);
Serial.print('\t');
Serial.println(gz);
}
if (IMU.temperatureAvailable())
{
int temperature_deg = 0;
IMU.readTemperature(temperature_deg);
Serial.print("LSM6DSOX Temperature = ");
Serial.print(temperature_deg);
Serial.println(" °C");
}
</code></pre>
<p>效果如下,温度挺准,准确得多算了十度。</p>
<div style="text-align: left;"></div>
<div style="text-align: left;"> </div>
<p># 知识学习</p>
<p>从手册查看芯片坐标轴,如下</p>
<p>从上端看,x是航向,y是偏角,z是俯仰。</p>
<div style="text-align: left;"></div>
<div style="text-align: left;">静止不动得情况,有一个恒定得地心引力,1g,作用到芯片上。</div>
<div style="text-align: left;">加速度值就能看出来,平放时候,z轴加速度 为1,反过来放 -1.</div>
<div style="text-align: left;">90度侧放,y轴的值分别 1 -1.</div>
<div style="text-align: left;">90度竖放,x轴值分别 1 -1.</div>
<div style="text-align: left;">陀螺仪的数值分别来自于航向/翻滚/俯仰方向的角速度变化值,可以分别尝试绕航向角转,翻滚角转,俯仰角转,能明显看出来对应的数值变化。</div>
<p># 疑难问题</p>
<p>无</p>
<p># 结论</p>
<p>只有一个赞,arduino的库写的正好。之前看过c写的imu代码,一大片的初始化代码,很复杂,看很久也迷糊。</p>
<p>还是用轮子写,写的开心</p>
<p>arduino库采样率能达到多少?</p>
<p>IMU.readTemperatureFloat(temperature);</p>
<p>温度可以用float试试</p>
<div style="position: fixed; opacity: 1; height: 300px; width: 100%; font-size: 25px; text-align: center; bottom: 200px; left: 0px; display: none; flex-direction: column; justify-content: center; z-index: 1661789940; top: calc(50% + 0px);"> </div>
页:
[1]