Application of MPU6050 in Ros

Source: Internet
Author: User
Tags sleep zip git clone
read MPU6050 data under Arduino


Reference: http://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/ hardware Arduino uno or Arduino mega2560 MPU6050 sensor (GY-521 or GY-9150) DuPont line several connections







Note: If you use Arduino mega2560, the SCL and SDA of mpu6050 are wired to the pin21 and pin20 pins of the Arduino mega2560. program



Download Http://diyhacking.com/projects/MPU6050.zip and http://diyhacking.com/projects/ I2cdev.zip, unzip it and put it in the libraries directory of the Arduino to restart the Arduino.



Open the program and upload it via file–> examples–> mpu6050–> examples–> mpu6050_dmp6.




Open serial monitoring, set baud rate 115200, see "Send any character to begin DMP programming and Demo:" When you enter any one character, and then send. You can see the values of yaw, pitch, and roll, as shown in the following figure.




Note: Data for the first 10 seconds is inaccurate.



If you want to correct the offset of the mpu6050, refer to:
http://www.i2cdevlib.com/forums/topic/96-arduino-sketch-to-automatically-calculate-mpu6050-offsets/
The script calculates the following 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 is close to 0 0 16384 0 0 0


Bring the offset into mpu6050 to replace the following value:

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

The following results are replaced:




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
application in Ros



Reference: Https://github.com/fsteinhardt/mpu6050_serial_to_imu



In Arduino, create a new project, copy the contents of file–> examples–> mpu6050–> examples–> mpu6050_dmp6, and then comment out "#define Output_ Readable_yawpitchroll ", and the" #define Output_teapot "before the line" # "removed, upload code.



In the Ros working directory, download and compile the code, where Imu_tools is to add rviz_imu_plugin, so that it can be visualized in 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


By running the following command, you can convert the value of mpu6050 to the/IMU message of Ros and see the change in mpu6050 direction through Rviz.

read the angle of the IMU


Convert/imu to Yaw angle, print out, you can change the angle arbitrarily. The code is as follows:




#! /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()
Calculation Drift


MPU6050 change over time, there is drift: even if the position is not changed, the yaw of reading changes continuously with the angle. If the following script detects the rate of change in drift, it changes every 10 seconds.




#! /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()

The results obtained are as follows and can be averaged 10 times.





[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
Correction Error



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


How to calibrate –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 do using the Energia code, which is used to display the raw values of the sensor mentioned I N
Chapter 6, working with robotic sensors. This example was already available in Energia examples. You'll 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 a 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 are coming from
The Launchpad. Leave the breakout for 5 to ten 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 to the offsets that can is used for future purposes.Todo


Raspberry Pi under mpu9150
http://blog.csdn.net/crazyquhezheng/article/details/44463651


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.