TA的每日心情 | 开心 2015-6-18 12:27 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
看大家都更新了这么多教程,小的是在有愧啊,毕设也赶得紧,只能挤时间来搞curie nano了。
好了,废话不多说,我们看一下如何用curie nano来得到当前姿态。
首先我们在安装了curie的库之后会有一些简单的工程例子给我们,如下:
我们进入 curieIMU 里面,可以看到很多有关加速度计和陀螺仪的例程,我们就用这些例程来进行修改然后得到我们想要的姿态信息。
首先,我们需要安装Madgwick这个库,直接在arduino开发环境里面添加,具体怎么操作就不写了。
融合代码如下:- #include <CurieIMU.h>
- #include <MadgwickAHRS.h>
- Madgwick filter;
- unsigned long microsPerReading, microsPrevious;
- float accelScale, gyroScale;
- void setup() {
- Serial.begin(9600);
- // start the IMU and filter
- CurieIMU.begin();
-
- // Set data rate
-
- filter.begin(25);
- // Set the accelerometer range to 2G
- CurieIMU.setAccelerometerRange(2);
- // Set the gyroscope range to 250 degrees/second
- CurieIMU.setGyroRange(250);
- // initialize variables to pace updates to correct rate
- microsPerReading = 1000000 / 25;
- microsPrevious = micros();
- }
- void loop() {
- int aix, aiy, aiz;
- int gix, giy, giz;
- float ax, ay, az;
- float gx, gy, gz;
- float mx=0, my=0, mz=0;
- float roll, pitch, heading;
- unsigned long microsNow;
- // check if it's time to read data and update the filter
- microsNow = micros();
- if (microsNow - microsPrevious >= microsPerReading) {
- // read raw data from CurieIMU
- CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
- //这个函数是读取原始值
- // convert from raw data to gravity and degrees/second units
- ax = convertRawAcceleration(aix);
- ay = convertRawAcceleration(aiy);
- az = convertRawAcceleration(aiz);
- gx = convertRawGyro(gix);
- gy = convertRawGyro(giy);
- gz = convertRawGyro(giz);
- // update the filter, which computes orientation
- filter.update(gx, gy, gz, ax, ay, az, mx, my ,mz);
- //这个函数可以进去到Madgwick里面看
- // print the heading, pitch and roll
- roll = filter.getRoll();
- pitch = filter.getPitch();
- heading = filter.getYaw();
- Serial.print("Orientation: ");
- Serial.print(heading);
- Serial.print(" ");
- Serial.print(pitch);
- Serial.print(" ");
- Serial.println(roll);
- // increment previous time, so we keep proper pace
- microsPrevious = microsPrevious + microsPerReading;
- }
- }
- float convertRawAcceleration(int aRaw) {
- // since we are using 2G range
- // -2g maps to a raw value of -32768
- // +2g maps to a raw value of 32767
-
- float a = (aRaw * 2.0) / 32768.0;
- return a;
- }
- float convertRawGyro(int gRaw) {
- // since we are using 250 degrees/seconds range
- // -250 maps to a raw value of -32768
- // +250 maps to a raw value of 32767
-
- float g = (gRaw * 250.0) / 32768.0;
- return g;
- }
复制代码
然后就可以通过串口助手来观察板子当前所处的状态:
但是通过这个算法得到的yaw值会漂,所以希望得到更精确的值,在板子上面有另一个模块hmc5883l电子罗盘,我们可以使用电子罗盘来校准yaw的值。
我目前为止没有什么进展,因为姿态融合这一块本来就比较难,相关资料也比较少,也希望有同方向一起研究的人。
后面更新就如何在在上述代码下启用hmc5883l,并通过这个电子罗盘来校准我们得到的姿态值。
|
|