Question: Re - write this node to do more than just go straight, it should head toward the detected person without hitting any obstacles that might
Rewrite this node to do more than just go straight, it should head toward the detected person without hitting any obstacles that might get in its way.
Here is what I have
usrbinenv python
import rospy
from geometrymsgsmsg import Twist
from sensormsgsmsg import LaserScan
class RobotController:
def initself:
# Initialize the ROS node
rospy.initnoderobotcontroller', anonymousTrue
self.velocitypublisher rospy.Publishercmdvel', Twist, queuesize
self.lasersubscriber rospy.Subscriberscan LaserScan, self.lasercallback
self.movecmd Twist
self.obstacledetected False
self.rate rospy.Rate
def lasercallbackself data:
validranges r for r in data.ranges if r floatInf and r floatNaN
if validranges:
mid lenvalidranges
frontrange validrangesmid : mid
if frontrange and minfrontrange:
self.obstacledetected True
else:
self.obstacledetected False
else:
self.obstacledetected False
def moveforwardself:
while not rospy.isshutdown:
if self.obstacledetected:
# Stop the robot
self.movecmdlinear.x
self.movecmdangular.z
rospy.loginfoObstacle detected! Stopping the robot."
else:
# Move forward
self.movecmdlinear.x
self.movecmdangular.z
rospy.loginfoMoving forward."
self.velocitypublisher.publishselfmovecmd
self.rate.sleep
if namemain:
try:
controller RobotController
controller.moveforward
except rospy.ROSInterruptException:
pass
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
