查看: 3927|回复: 1

【Curie Nano试用】六轴姿态融合

[复制链接]
  • TA的每日心情
    开心
    2015-6-18 12:27
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2017-5-16 16:39:27 | 显示全部楼层 |阅读模式
    分享到:
    看大家都更新了这么多教程,小的是在有愧啊,毕设也赶得紧,只能挤时间来搞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,并通过这个电子罗盘来校准我们得到的姿态值。

    回复

    使用道具 举报

  • TA的每日心情
    难过
    2021-2-27 22:16
  • 签到天数: 1568 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2018-8-17 21:18:35 | 显示全部楼层
    多找点资料学呗
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 13:28 , Processed in 0.130034 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.