1回答

0收藏

[原创] 【Curie Nano试用】六轴姿态融合

DFROBOT DFROBOT 5562 人阅读 | 1 人回复 | 2017-05-16

看大家都更新了这么多教程,小的是在有愧啊,毕设也赶得紧,只能挤时间来搞curie nano了。

好了,废话不多说,我们看一下如何用curie nano来得到当前姿态。

首先我们在安装了curie的库之后会有一些简单的工程例子给我们,如下:
我们进入 curieIMU 里面,可以看到很多有关加速度计和陀螺仪的例程,我们就用这些例程来进行修改然后得到我们想要的姿态信息。
首先,我们需要安装Madgwick这个库,直接在arduino开发环境里面添加,具体怎么操作就不写了。


融合代码如下:
  1. #include <CurieIMU.h>
  2. #include <MadgwickAHRS.h>

  3. Madgwick filter;
  4. unsigned long microsPerReading, microsPrevious;
  5. float accelScale, gyroScale;

  6. void setup() {
  7.   Serial.begin(9600);

  8.   // start the IMU and filter
  9.   CurieIMU.begin();

  10. // Set data rate
  11.   
  12.   filter.begin(25);

  13.   // Set the accelerometer range to 2G
  14.   CurieIMU.setAccelerometerRange(2);
  15.   // Set the gyroscope range to 250 degrees/second
  16.   CurieIMU.setGyroRange(250);

  17.   // initialize variables to pace updates to correct rate
  18.   microsPerReading = 1000000 / 25;
  19.   microsPrevious = micros();
  20. }

  21. void loop() {
  22.   int aix, aiy, aiz;
  23.   int gix, giy, giz;
  24.   float ax, ay, az;
  25.   float gx, gy, gz;
  26.   float mx=0, my=0, mz=0;
  27.   float roll, pitch, heading;
  28.   unsigned long microsNow;

  29.   // check if it's time to read data and update the filter
  30.   microsNow = micros();
  31.   if (microsNow - microsPrevious >= microsPerReading) {

  32.     // read raw data from CurieIMU
  33.     CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
  34. //这个函数是读取原始值

  35.     // convert from raw data to gravity and degrees/second units
  36.     ax = convertRawAcceleration(aix);
  37.     ay = convertRawAcceleration(aiy);
  38.     az = convertRawAcceleration(aiz);
  39.     gx = convertRawGyro(gix);
  40.     gy = convertRawGyro(giy);
  41.     gz = convertRawGyro(giz);

  42.     // update the filter, which computes orientation
  43.     filter.update(gx, gy, gz, ax, ay, az, mx, my ,mz);
  44. //这个函数可以进去到Madgwick里面看

  45.     // print the heading, pitch and roll
  46.     roll = filter.getRoll();
  47.     pitch = filter.getPitch();
  48.     heading = filter.getYaw();
  49.     Serial.print("Orientation: ");
  50.     Serial.print(heading);
  51.     Serial.print(" ");
  52.     Serial.print(pitch);
  53.     Serial.print(" ");
  54.     Serial.println(roll);

  55.     // increment previous time, so we keep proper pace
  56.     microsPrevious = microsPrevious + microsPerReading;
  57.   }
  58. }

  59. float convertRawAcceleration(int aRaw) {
  60.   // since we are using 2G range
  61.   // -2g maps to a raw value of -32768
  62.   // +2g maps to a raw value of 32767

  63.   float a = (aRaw * 2.0) / 32768.0;
  64.   return a;
  65. }

  66. float convertRawGyro(int gRaw) {
  67.   // since we are using 250 degrees/seconds range
  68.   // -250 maps to a raw value of -32768
  69.   // +250 maps to a raw value of 32767

  70.   float g = (gRaw * 250.0) / 32768.0;
  71.   return g;
  72. }
复制代码

然后就可以通过串口助手来观察板子当前所处的状态



但是通过这个算法得到的yaw值会漂,所以希望得到更精确的值,在板子上面有另一个模块hmc5883l电子罗盘,我们可以使用电子罗盘来校准yaw的值。
我目前为止没有什么进展,因为姿态融合这一块本来就比较难,相关资料也比较少,也希望有同方向一起研究的人。


后面更新就如何在在上述代码下启用hmc5883l,并通过这个电子罗盘来校准我们得到的姿态值。

分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

feixiang20

发表于 2018-8-17 21:18:35 | 只看该作者

多找点资料学呗
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条