(Jetson Project#12)Integrating MPU6050 IMU with Robot's TF Tree

mseokq23·2024년 11월 26일
0
post-thumbnail

6.1. Creating a TF Broadcaster Node

We'll create a TF broadcaster to link the IMU frame (imu_link) to the robot's base frame (base_link).

  1. Navigate to the mpu6050_driver Package's Scripts Directory:
cd ~/catkin_ws/src/mpu6050_driver/scripts
  • But in last post, i have made this script directory with "mpu6050_publisher.py". so, i skip this.
  1. Create the tf_broadcaster.py Script:
nano tf_broadcaster.py

this Script is same above. just edit code.

  1. Add the Following Content:
#!/usr/bin/env python3
import rospy
import tf
from geometry_msgs.msg import TransformStamped

class TFBroadcasterNode:
    def __init__(self):
        rospy.init_node('imu_tf_broadcaster', anonymous=True)
        self.br = tf.TransformBroadcaster()
        self.rate = rospy.Rate(10)  # 10 Hz

        # Define static transformation parameters
        self.translation = (0.0, 0.0, 0.0)  # Adjust based on sensor placement
        self.rotation = (0.0, 0.0, 0.0, 1.0)  # Quaternion (x, y, z, w)

    def broadcast(self):
        while not rospy.is_shutdown():
            self.br.sendTransform(
                self.translation,
                self.rotation,
                rospy.Time.now(),
                "imu_link",
                "base_link"
            )
            self.rate.sleep()

if __name__ == '__main__':
    try:
        node = TFBroadcasterNode()
        node.broadcast()
    except rospy.ROSInterruptException:
        pass

Explanation of the Script:
translation and rotation: Define the spatial relationship between base_link and imu_link. Adjust these values based on how the MPU6050 is mounted on robot.

  1. Make the Script Executable:
chmod +x tf_broadcaster.py
  1. Update CMakeLists.txt for the New Script
  • Edit CMakeLists.txt:
nano ~/catkin_ws/src/mpu6050_driver/CMakeLists.txt
  • Add the Following Lines:
catkin_install_python(PROGRAMS scripts/tf_broadcaster.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

  1. Create a Launch File for the TF Broadcaster
  • Navigate to the Launch Directory:
mkdir -p ~/catkin_ws/src/mpu6050_driver/launch
cd ~/catkin_ws/src/mpu6050_driver/launch

this directory is already made. So, if you same, don't have to.

  • Create the tf_broadcaster.launch File:
nano tf_broadcaster.launch

Add the Following Content:

<launch>
  <node name="imu_tf_broadcaster" pkg="mpu6050_driver" type="tf_broadcaster.py" output="screen"/>
</launch>

  1. Build the ROS Package:
cd ~/catkin_ws
catkin_make
source devel/setup.bash
  1. Launch the TF Broadcaster Node:
roslaunch mpu6050_driver tf_broadcaster.launch

  • Verify(1) - TF Frames in RViz:

    In RViz:
    Add the TF display if not already present.
    Ensure that the transform from base_link to imu_link is correctly represented.

    Replace with an actual image or diagram for clarity.

  • Verify(2) - Check node in rqt

rosrun rosrpt ~~~~ (i don't remember.)

0개의 댓글