(Jetson Project#11)Integrate with Existing Nodes

mseokq23·2024년 11월 24일
post-thumbnail

5.2. Integrate with Existing Nodes

Ensure that all necessary nodes are running simultaneously:

  • Terminal 1: Launch roscore (if not already running)
roscore
  • Terminal 2: Launch RPLIDAR Node
roslaunch rplidar_ros rplidar_a1.launch
  • Terminal 3: Launch IMU Node
roslaunch mpu6050_driver mpu6050.launch
  • Terminal 4: Launch Motor Control Node
roslaunch motor_control motor_control.launch
  • Terminal 5: Launch Obstacle Avoidance Node
roslaunch motor_control obstacle_avoidance.launch
  • Terminal 6: Launch RViz
rviz
  • Set Fixed Frame to laser.
  • Add Displays:
    • Map
    • RobotModel
    • LaserScan (/scan)
    • IMU (/imu/data_raw)
    • TF

But, errored like above....
So, again trouble Shooting..

  • Correcting the obstacle_avoidance_node.py Script
#!/usr/bin/env python3
import rospy
from sensor_msgs.msg import LaserScan
from std_msgs.msg import String
import math

class ObstacleAvoidanceNode:
    def __init__(self):
        rospy.init_node('obstacle_avoidance_node', anonymous=True)
        
        # Parameters
        self.obstacle_distance = rospy.get_param('~obstacle_distance', 0.5)  # meters
        self.scan_topic = rospy.get_param('~scan_topic', '/scan')
        self.command_topic = rospy.get_param('~command_topic', '/motor_command')
        
        # Publisher to motor_command
        self.motor_pub = rospy.Publisher(self.command_topic, String, queue_size=10)
        
        # Subscriber to LaserScan
        rospy.Subscriber(self.scan_topic, LaserScan, self.scan_callback)
        
        # State
        self.obstacle_detected = False

    def scan_callback(self, scan_data):
        # Define the front sector (e.g., +/-30 degrees)
        front_angle_min = -math.radians(30)
        front_angle_max = math.radians(30)

        # Calculate indices corresponding to the front sector
        index_min = int((front_angle_min - scan_data.angle_min) / scan_data.angle_increment)
        index_max = int((front_angle_max - scan_data.angle_min) / scan_data.angle_increment)

        # Ensure indices are within the range of scan data
        index_min = max(0, index_min)
        index_max = min(len(scan_data.ranges) - 1, index_max)

        # Extract relevant range data
        front_ranges = scan_data.ranges[index_min:index_max+1]

        # Filter out invalid readings (NaN or Infinity)
        valid_ranges = [
            r for r in front_ranges
            if not math.isnan(r) and not math.isinf(r) and r > 0.0
        ]

        if not valid_ranges:
            min_distance = float('inf')
        else:
            min_distance = min(valid_ranges)

        rospy.loginfo(f"Minimum distance in front: {min_distance:.2f} meters")

        if min_distance < self.obstacle_distance:
            if not self.obstacle_detected:
                rospy.logwarn("Obstacle detected! Stopping.")
                self.motor_pub.publish('S')
                self.obstacle_detected = True
        else:
            if self.obstacle_detected:
                rospy.loginfo("Path clear. Moving forward.")
                self.motor_pub.publish('F')
                self.obstacle_detected = False

    def run(self):
        rospy.spin()

if __name__ == '__main__':
    try:
        node = ObstacleAvoidanceNode()
        node.run()
    except rospy.ROSInterruptException:
        pass
  • Rebuild the ROS Workspace:
cd ~/catkin_ws
catkin_make
source devel/setup.bash
  • Relaunch the Obstacle Avoidance Node:

roslaunch motor_control obstacle_avoidance.launch

if you want to see the graph node, command this.

 rosrun rqt_graph rqt_graph

additionally, roscore isn't necessary. cuz we already launch other roslaunch files.

so, you just launch four terminal. +) rviz

0개의 댓글