Stuck Within If statement

I’m trying to complete quiz 3.7 on topic 3 Ros2 Humble (python). I am attempting to apply the code used in Exercise 3.1. I am able to get the robot to move to where there is a gap but then I am stuck inside of an If statement reading the laser distance to be infinity. Below is the if statement i believe I’m stuck in.

def laser_callback(self, msg):
# Save the frontal laser scan info at 90° directly to left
self.laser_forward = msg.ranges[90]

def motion(self):
    # print the data
    self.get_logger().info('I receive: "%s"' % str(self.laser_forward))
    # Logic of move
    if self.laser_forward == 'inf':
        self.cmd.linear.x = 0.0
        self.cmd.angular.z = 0.0
    elif self.laser_forward < 4 and self.laser_forward >= 3:
        self.cmd.linear.x = 0.5
        self.cmd.angular.z = 0.0
        self.cmd.linear.x = 0.0
        self.cmd.angular.z = 0.0

    # Publishing the cmd_vel values to a Topic

Here, you are trying to compare a float with a string. It should be:

if self.laser_forward == float('inf'):

See how can I represent an infinite number in Python.

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