当前位置:   article > 正文

利用IMU数据来计算位移_imu加速度计求位移

imu加速度计求位移

目标:

利用IMU测得的加速度信息来计算位移,很简单假设是匀加速运动或是匀速运动都可以,主要是看问题的背景来具体去确定运动模型,这里我就取个简单的匀加速运动模型。

X= v_{0} \cdot t+ \frac{1}{2} \cdot a \cdot t^{2}


学习过程:

1、了解IMU的相关原理知识

2、阅读产品自带的SDK包

3、自己添加并修改相关代码


学习内容:

这里主要介绍我是如何添改的代码。因为想要直接获取位移需要时间和加速度,这里的出速度我设为0,其加速度的获取可以阅读产品自带的功能包中的源码,我在编写过程中主要是在时间的获取上出现了一点问题,需要获得IMU开启时的时间戳以及当前帧的时间戳。

在运行产品自带的源码时,可以看见每一帧都对应着一个 seq 和 stamp ,所以只要把 seq 为1的那一帧对应的时间戳提取出来作为起始时间即可,如图所示,同样的方法去提取当前帧的时间戳。


学习产出:

  1. #include <ahrs_driver.h>
  2. #include <Eigen/Eigen>
  3. using namespace std;
  4. void my_handle_imu_data(const sensor_msgs::Imu::ConstPtr& imu_data)
  5. {
  6. cout<<"accelaration X is :"<<imu_data->linear_acceleration.x<<endl;
  7. cout<<"accelaration Y is :"<<imu_data->linear_acceleration.y<<endl;
  8. static int start_time;
  9. int now_time;
  10. if(imu_data->header.seq == 50)
  11. {
  12. start_time = int(imu_data->header.stamp.toSec());
  13. cout<<"when the seq is "<<imu_data->header.seq<<" "<<" the stamp is "<<start_time<<endl;
  14. }
  15. cout<<"the start time is :"<<start_time<<endl;
  16. now_time = int(imu_data->header.stamp.toSec());
  17. if(start_time == now_time)
  18. {
  19. cout<<"the shift on X is :"<< 0 <<endl;
  20. cout<<"the shift on Y is :"<< 0 <<endl;
  21. }
  22. else if(now_time > start_time)
  23. {
  24. double X_Shift,Y_Shift;
  25. int Dealt_Time;
  26. Dealt_Time = now_time - start_time;
  27. X_Shift = 0.5 * imu_data->linear_acceleration.x * Dealt_Time * Dealt_Time;
  28. Y_Shift = 0.5 * imu_data->linear_acceleration.y * Dealt_Time * Dealt_Time;
  29. cout<<"the shift on X is :"<<X_Shift<<endl;
  30. cout<<"the shift on Y is :"<<Y_Shift<<endl;
  31. cout<<"now time is :"<<now_time<<endl;
  32. cout<<"start_time is :"<<start_time<<endl;
  33. cout<<imu_data->header.seq<<endl;
  34. //cout<<imu_data->header.stamp<<endl;
  35. cout<<"*********************************************"<<endl;
  36. }
  37. }
  38. int main(int argc, char *argv[])
  39. {
  40. ros::init(argc,argv,"cal_shift");
  41. ros::NodeHandle nh;
  42. ros::Subscriber sub_imu_data_ = nh.subscribe<sensor_msgs::Imu>("/mobile_base/sensors/imu_data",10,my_handle_imu_data);
  43. ros::spin();
  44. return 0;
  45. }

代码很短,原理也很简单,主要是需要注意一下 start_time 变量前的 static 关键字,它可以保证该变量在程序结束时,再释放变量。同学可以试一下删除 static 之后变量的值是什么。

然后就是ros相关的知识,想要学习的话可以在这里学习:保姆级教程

【奥特学园】ROS机器人入门课程《ROS理论与实践》零基础教程_哔哩哔哩_bilibili

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/891643
推荐阅读
相关标签
  

闽ICP备14008679号