My robot is supposed to be moving forward at 0.2, then slow down at 0.1 when closer to wall, then stop completely when very close to wall. But it did not stop, instead it bang onto the wall with full speed. Why? Any wrong with my code or other possible hidden issues? I have the same code as the video tutorial by The Construct which was also illustrating the same section of the practice. I have seen several people encounter the same issue as mine, they managed to solve, some said its the front laser readings were not reading, but did not provide actual solution. Can anyone help? Much appreciation!
wall_following.py:
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from rclpy.qos import ReliabilityPolicy, QoSProfile
class WallFollower(Node):
def init(self):
super().init(‘wall_follower_node’)
self.publisher_ = self.create_publisher(Twist, ‘cmd_vel’, 10)
self.subscriber = self.create_subscription(LaserScan, ‘/scan’, self.move_turtlebot, QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT))
self.subscriber
self.timer_period = 0.5
self.laser_forward = 0
self.cmd = Twist()
self.timer = self.create_timer(self.timer_period, self.motion)
def move_turtlebot(self,msg):
self.laser_forward = msg.ranges[359]
def motion(self):
self.get_logger().info('I recieve "%s"' % str (self.laser_forward))
if self.laser_forward > 0.6:
self.cmd.linear.x = 0.2
self.cmd.angular.z = 0.0
elif self.laser_forward < 0.6 and self.laser_forward > 0.3:
self.cmd.linear.x = 0.1
self.cmd.angular.z = 0.0
else:
self.cmd.linear.x = 0.0
self.cmd.angular.z = 0.0
self.publisher_.publish(self.cmd)
def main(args=None):
rclpy.init(args=args)
wall_follower = WallFollower()
rclpy.spin(wall_follower)
wall_follower.destroy_node()
rclpy.shutdown()
if name == ‘main’:
main()