Struggling with ROS2 Basics Python Real Robot Project Section 1 Topics

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()

Hi @dansyw91, welcome to the community!

To improve the readability of your post, please wrap your code in three ``` symbols like this:

this is an example

Now, to your question:

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 don’t know why, but it sounds like you are having trouble detecting the wall.

The first thing to do when this happens is debugging. Adding loging to your program is very helpful for this. I see you are already doing that at the beginning of motion(), you should add more and more to try and see what is happening.

Fore example, you set laser_forward = msg.ranges[359]:

  • Are you sure that corresponds to the front of the robot? How did you determine that (It’s different for the simulation and the real robot!)
  • Have you considered using more than one value? that way, you add robustness to your code. If one of those values is faulty, then the robot still knows what to do.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.