
Ensure that all necessary nodes are running simultaneously:
roscore
roslaunch rplidar_ros rplidar_a1.launch
roslaunch mpu6050_driver mpu6050.launch
roslaunch motor_control motor_control.launch
roslaunch motor_control obstacle_avoidance.launch
rviz

But, errored like above....
So, again trouble Shooting..
#!/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
cd ~/catkin_ws
catkin_make
source devel/setup.bash
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