MPU6050在ROS中应用

2023-05-16

arduino下读取MPU6050数据

参考:http://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/

硬件

  • arduino uno或arduino mega2560
  • mpu6050传感器(GY-521或GY-9150)
  • 杜邦线若干

连线

这里写图片描述

注意:如果使用arduino mega2560, mpu6050的SCL和SDA连线到arduino mega2560的pin21和pin20引脚。

程序

下载 http://diyhacking.com/projects/MPU6050.zip 和 http://diyhacking.com/projects/I2Cdev.zip,解压后放到Arduino的libraries目录下,重启Arduino.

通过 File –> Examples –> MPU6050 –> Examples –> MPU6050_DMP6 打开程序并上传。
这里写图片描述

打开串口监控,设置波特率115200, 看到 “Send any character to begin DMP programming and demo: “ 时输入任意一个字符,然后发送。就能看到yaw, pitch 和roll的值,如下图所示。
这里写图片描述

注意: 最初10秒的数据是不准确的。

如果要修正mpu6050的偏移,请参考:
http://www.i2cdevlib.com/forums/topic/96-arduino-sketch-to-automatically-calculate-mpu6050-offsets/
该脚本计算出的偏移如下:

Sensor readings with offsets:   -3  -3  16375   -1  1   -1
Your offsets:   -2341   -1472    1260   181 -22 7

Data is printed as: acelX acelY acelZ giroX giroY giroZ
Check that your sensor readings are close to 0 0 16384 0 0 0

把该偏移带入mpu6050替换下面的值:

// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

替换后结果如下:


// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(181);
mpu.setYGyroOffset(-22);
mpu.setZGyroOffset(7);
mpu.setZAccelOffset(1260); // 1688 factory default for my test chip

在ROS中应用

参考:https://github.com/fsteinhardt/mpu6050_serial_to_imu

在arduino中,新创建一个项目,复制File –> Examples –> MPU6050 –> Examples –> MPU6050_DMP6的内容,然后,注释掉”#define OUTPUT_READABLE_YAWPITCHROLL”,并把“#define OUTPUT_TEAPOT”的行前的“#”去掉,上传代码。

在ROS的工作目录下,下载和编译代码,其中imu_tools是为了添加rviz_imu_plugin,以便于在rviz中可视化。

cd ~catkin_ws/src/
git clone https://github.com/fsteinhardt/mpu6050_serial_to_imu
git clone https://github.com/ccny-ros-pkg/imu_tools

sudo apt-get install ros-indigo-serial

cd ~catkin_ws/
catkin_make --pkg  mpu6050_serial_to_imu imu_tools
source ~/.bashrc

运行如下命令,就可以把mpu6050的值转换为ROS的/imu 消息,同时通过rviz看到mpu6050朝向的变化。

roslaunch mpu6050_serial_to_imu demo.launch 

读取IMU的角度

把/imu转化为yaw的角度,打印出来,就可以随意变更角度。 代码如下:

#! /usr/bin/python

import PyKDL
import rospy
from sensor_msgs.msg import Imu
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Twist
from math import *
import threading
import os
import subprocess
import yaml


def quat_to_angle(quat):
  rot = PyKDL.Rotation.Quaternion(quat.x, quat.y, quat.z, quat.w)
  return rot.GetRPY()[2]
def normalize_angle(angle):
  res = angle
  while res > pi:
    res -= 2.0*pi
  while res < -pi:
    res += 2.0*pi
  return res


class CalibrateRobot:
  def __init__(self):
    self.lock = threading.Lock()
    self.sub_imu = rospy.Subscriber('imu', Imu, self.imu_cb)
    self.last_imu_angle = 0
    self.imu_angle = 0
    while not rospy.is_shutdown():
      rospy.sleep(0.1)
      rospy.loginfo("imu_angle:"+str(self.imu_angle*180/3.1415926))

  def imu_cb(self, msg):
    with self.lock:
      angle = quat_to_angle(msg.orientation)
      self.imu_angle = angle
      self.imu_time = msg.header.stamp

def main():
  rospy.init_node('scan_to_angle')
  CalibrateRobot()


if __name__ == '__main__':
  main()

计算漂移

MPU6050随时间变化,是存在漂移的:即使位置不变,读取的yaw在随角度不停地变化。 如果下面脚本测出漂移的变化率,每10秒改变一次。

#! /usr/bin/python

import PyKDL
import rospy
from sensor_msgs.msg import Imu
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Twist
from math import *
import threading
import os
import subprocess
import yaml


def quat_to_angle(quat):
  rot = PyKDL.Rotation.Quaternion(quat.x, quat.y, quat.z, quat.w)
  return rot.GetRPY()[2]
def normalize_angle(angle):
  res = angle
  while res > pi:
    res -= 2.0*pi
  while res < -pi:
    res += 2.0*pi
  return res


class CalibrateRobot:
  def __init__(self):
    self.lock = threading.Lock()
    self.sub_imu = rospy.Subscriber('imu', Imu, self.imu_cb)
    self.last_imu_angle = 0
    self.imu_angle = 0
    while not rospy.is_shutdown():
      self.last_imu_angle = self.imu_angle
      rospy.sleep(10)
      rospy.loginfo("imu_drift:"+str((self.imu_angle-self.last_imu_angle)*180/3.1415926))


  def imu_cb(self, msg):
    with self.lock:
      angle = quat_to_angle(msg.orientation)
      self.imu_angle = angle
      self.imu_time = msg.header.stamp

def main():
  rospy.init_node('scan_to_angle')
  CalibrateRobot()


if __name__ == '__main__':
  main()

获取的结果如下, 可以取10次取平均数。

[INFO] [WallTime: 1463810774.132861] imu_drift:0.0549391295367
[INFO] [WallTime: 1463810784.137872] imu_drift:0.0553483626062
[INFO] [WallTime: 1463810794.148451] imu_drift:0.0763623061112
[INFO] [WallTime: 1463810804.158945] imu_drift:0.0360694372181
[INFO] [WallTime: 1463810814.169398] imu_drift:0.0550469979142
[INFO] [WallTime: 1463810824.179115] imu_drift:0.0455562992295
[INFO] [WallTime: 1463810834.189545] imu_drift:0.0409165927309
[INFO] [WallTime: 1463810844.190111] imu_drift:0.0600102750276
[INFO] [WallTime: 1463810854.200587] imu_drift:0.0612612484692
[INFO] [WallTime: 1463810864.206757] imu_drift:0.0342962911961
[INFO] [WallTime: 1463810874.209526] imu_drift:0.0712592774024
[INFO] [WallTime: 1463810884.213722] imu_drift:0.0702398006237

校正误差

imu_delta = 2*pi + normalize_angle(imu_end_angle - imu_start_angle) - imu_drift*(imu_end_time - imu_start_time).to_sec()
rospy.loginfo('Imu error: %f percent'%(100.0*((imu_delta/scan_delta)-1.0)))
imu_result = imu_delta/scan_delta

如何标定–turtlebot_calibration

  • http://wiki.ros.org/turtlebot_calibration
  • http://wiki.ros.org/turtlebot_calibration/Tutorials/Calibrate%20Odometry%20and%20Gyro
  • https://github.com/turtlebot/turtlebot_apps/tree/indigo/turtlebot_calibration
  • http://blog.csdn.net/u013453604/article/details/49358721

TODO

  • Calibrating the MPU 6050

MPU 6050 calibration can be done using the Energia code, which is used to display the raw values of the sensor mentioned in
Chapter 6, Working with Robotic Sensors . This example is already available in Energia examples. You will get this code by
navigating to File/Examples/MPU 6050/Examples/MPU 6050_raw . Load this sketch into Energia and follow the procedure given
here:
1. Place the MPU 6050 breakout board on a flat surface. We can use an inclinometer to check the inclination of the surface.
2. Modify the current program by setting the offset to zero. We can set the offset of three axes values of gyroscope to 0
using the following function:
(“setXGyroOffset , setYGyroOffset , setZGyroOffset” =0)
3. Upload the raw code to Launchpad and take the Energia serial monitor to confirm whether the serial data is coming from
the Launchpad. Leave the breakout for 5 to 10 minutes to allow the temperature to stabilize and read the gyro values and
note down the values.
4. Set the offset values to the readings noted and upload the code again to the Launchpad.
5. Repeat this procedure until we get a reading of 0 from each gyro axis.
6. After achieving the goal, we finally get the offsets that can be used for future purposes.


Todo

raspberry pi下mpu9150
http://blog.csdn.net/crazyquhezheng/article/details/44463651

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MPU6050在ROS中应用 的相关文章

随机推荐

  • 布谷鸟算法(cuckoo_search)可能会遇到的问题

    1 布谷鸟算法是先进行在pa的判断 xff0c 然后决定是进行Levy飞行还是做出相应毁坏蛋的行为 xff0c 再所有的粒子一起输出 2 布谷鸟算法里面的gamma函数在有的地方可能不能使用 xff0c 比如S Function xff0c
  • protues仿真常见问题解决方案

    目录 1 如何确定自己的仿真版本 2 仿真软件破解 3 仿真工程格式 4 未加载HEX文件 5 缺失元件模型 6 低版本无法打开高版本问题 7 仿真结果异常问题 8 仿真图打开失败 9 仿真运行过程中闪退 10 缺失 SDF文件 仿真出问题
  • 第20章 USART—串口通讯—零死角玩转STM32-F429系列

    第20章 USART 串口通讯 全套 200 集视频教程和 1000 页 PDF 教程请到秉火论坛下载 xff1a www firebbs cn 野火视频教程优酷观看网址 xff1a http i youku com firege 本章参考
  • UC/OS-II 源码下载【资源】

    目录 1 前序 2 官网下载源代码 3 STM32F1示例代码 1 前序 你是不是也在学习ucos相关知识 xff0c 想要去网上下载源码 xff0c 但是又不知道去哪里下载 xff0c 网上冲浪大半天也没找到源码 xff0c 结果键盘无故
  • 文件系统编译进内核

    1 利用busybox制作根文件目录 需要有init 和 linuxrc 2 makemenuconfig 在general setup 中设置 initial ram filesystem and RAM disk 后指定文件系统源文件路
  • 零基础制作平衡小车【连载】13---平衡小车代码讲解(附源码)

    前言 今天聊一聊代码 xff0c 只有直立功能的代码 代码总体思路 给定一个目标值 xff0c 单片机通过IIC和mpu6050通信 xff0c 得知数据后 xff0c 根据角度环计算出一个PWM值给电机驱动器 xff0c 从而控制单机转动
  • NRF51822---开发板介绍(连载1)

    无线遥控器项目先放一放吧 xff0c 要学习的东西太多了 xff0c 一开始以为很简单 xff0c 结果坑越挖越深 还保持采用NRF51822的方案做手柄 xff0c 在做之前先把51822这个骨头啃下来 我又重新开了一个专题 xff0c
  • nrf51822+rfx2401c系统板(开源原理图、PCB)

    快过年了 xff0c 没啥东西可送的 xff0c 就把刚做的射频板子开源了吧 原理图 3D图 板子还有点毛病 xff0c 不过问题不大 把PCB隔断 xff0c 飞根线就行了 xff0c 等你拿到手就能看到了 下图中画黑色圈的两个过孔把信号
  • 无名小哥对无名飞控中的问题回答总结

    文章目录 IIR二阶数字低通陀螺仪数据噪声传感器矫正姿态解算惯性导航控制滤波性能系统响应巴特沃斯滤波器的截止频率如何取累计漂移姿态估计精度误差大控制周期时间保证传感器数据不丢帧 IIR二阶数字低通 对于IIR而言 xff0c 阶次越高 xf
  • Vue反向代理服务器

    在项目中创建 vue config js 文件 module exports 61 devServer proxy 39 api 39 target 39 http localhost 3000 39 pathRewrite 39 api
  • putty连接centos7 Network error:connection timed out

    首先说明一下 xff0c centos7系统下的开机服务不再在 etc inittab的管理之下了 xff0c centos7使用systemd模块管理系统的服务 xff0c 所有的服务文件都改为 etc systemd system的 s
  • ESP32_使用天猫精灵利用巴法云控灯例程测试

    1 说明 虽然官方例程写的是ESP8266 xff0c 但是改一下就能直接用到ESP32的 xff0c 参考这个链接 xff1a esp8266接入天猫精灵教程 xff0c 附开源app控制 巴法开放论坛 xff0c 整个过程非常非常简单
  • 附录A 进阶游戏编程书籍总结与推荐

    章节导读 很多游戏编程书籍都有一个通病 xff0c 一本书写完 xff0c 读者看完之后 xff0c 不知道下一步该如何前进 这里的附录就是让大家看完这本书后 xff0c 知道自己接下来可以学习什么 xff0c 如何进一步提升自己 为学习思
  • 最受欢迎的菜品

    7 2 最受欢迎的菜品 20分 某自助餐厅要求餐厅的客人在就餐后进行投票 xff0c 选出一款最喜爱的菜品 xff0c 每日营业结束后进行投票统计 xff0c 选出投票数最多的菜品为最受欢迎的菜品 请编写一个程序帮助餐厅快速完成这个统计工作
  • 利用grafana&prometheus 快速配置k8s监控面板 & 主机监控面板

    系列文章目录 1 使用helm快速安装 grafana amp prometheus 2 利用grafana amp prometheus 快速配置 k8s amp 主机监控 3 grafana amp prometheus 快速配置报警规
  • 使用idea创建一个简单的servlet项目

    在创建一个简易的项目之前首先需要在idea配置好tomcat的环境 xff08 1 xff09 点击add configuration xff08 2 xff09 点击 xff0b 号 xff08 3 xff09 选择好你的tomcat版本
  • 为什么我们需要uCos?带你透彻理解RTOS

    与uCos见面还是大学的时候 xff0c 老师让我为毕业设计选一个课题 xff0c 要求有关嵌入式实时操作系统 xff0c 于是开始在网上搜索 xff0c 顺理成章的就发现了uCos xff0c 于是开始了uCos之路 xff0c 但后来由
  • 多旋翼PID控制器笔记

    多旋翼PID控制器笔记 高度控制垂向速度环加速度指令到油门指令的映射垂向加速度环 水平控制水平速度环水平加速度到姿态的映射姿态环欧拉运动学方程角速度环欧拉动力学方程 高度控制 垂向速度环 垂向速度指令在NED坐标系下给出 xff0c 采用P
  • WGS-84坐标系

    WGS 84坐标系 WGS84坐标系基本参数地理坐标曲率半径WGS 84与NED坐标系的转换 WGS84坐标系 WGS 84坐标系 xff08 World Geodetic System一1984 Coordinate System xff
  • MPU6050在ROS中应用

    arduino下读取MPU6050数据 参考 xff1a http diyhacking com arduino mpu 6050 imu sensor tutorial 硬件 arduino uno或arduino mega2560mpu