ROS Topic e Subscriber

Dear experts. I am having an issue to move the robot toward the wall and later deviate it with subsccriber and publisher framework. My code is below:

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class MoveRobot():

def __init__(self):
    self.pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
    self.sub = rospy.Subscriber('kobuki/laser/scan', LaserScan, self.callback)

def callback(self,msg):
    laser_current_data = msg
    laser_current = laser_current_data.ranges
    move_robot = Twist()
    print("Frontal distance %f" % laser_current[360])
    print("Right side distance %f" %laser_current[0])
    print("Left side distance %f" %laser_current[719])

def move(self):
    if laser_current[360] == 'inf':
        move_robot.linear.x = 0.5
    while laser_current[360] > 1:
        move_robot.linear.x = 0.5
    move_robot.linear.x = 0.2
    move_robot.angular.z = 0.5
    while laser_current[0] < 1:
        move_robot.linear.x = 0
        move_robot.angular.z = 0.5
    move.robot.linear.x = 0.5
    while laser_current[719] < 1:
        move_robot.linear.x = 0
        move_robot.angular.z = -0.5
    move.robot.linear.x = 0.5
    elif:
        move_robot.linear.x = 0.5
    while laser_current[360] > 1:
        move_robot.linear.x = 0.5
    move_robot.linear.x = 0.2
    move_robot.angular.z = 0.5
    while laser_current[0] < 1:
        move_robot.linear.x = 0
        move_robot.angular.z = 0.5
    move.robot.linear.x = 0.5
    while laser_current[719] < 1:
        move_robot.linear.x = 0
        move_robot.angular.z = -0.5
    move.robot.linear.x = 0.5

    self.pub.publish(move_robot)

if name == ā€˜mainā€™:
rospy.init_node(ā€˜Move_the_TurtleBot_nodeā€™)
Displacement = MoveRobot()
rospy.spin()

The first issue is that the robot read the laser data but does not move. It does not entry in the ā€œwhileā€ and ā€œifā€ loop. I donā€™t know why. Later I also have a problem in the ā€œelifā€ syntax, it returns an error. Could you help me showing what can be corrected in my code or sending the solution please?
Thanks in advance

Dear Marcus,
I see you are doing a lot of questions and struggling in some of the exercises. Let me tell you that you are doing great!! Really, keep pushing like this and you are going to master. You just need to clarify some concepts a little bit.

Related to your problem, the reason why you donā€™t see the robot moving is because nobody is calling the move(self) function of the MoveRobot class.

In the main you are creating an instance of MoveRobot, which you called Displacement. That instance is setting the callback for the laser, so every time that a new laser is published, the callback is activated and run.
However, nobody is activating the move function of Displacement. I hope you can see that point.

So, what you need to do is to add a small loop in the main program that will be calling periodically to the move function of the Displacement. For that you need to create a loop, and also a Rate to control the frequency that the Displacement.move() is called. Also you can get rid of the spin.

Let me know if that is clear for you. I would also recommend you to review the Python 3 for robotics course on the chapter about Python classes so you can better understand about instances and calling their functions.

Let me know if it is clear now or have still doubts. And please, do not despair! Keep pushing, you are doing great!

1 Like

Thanks @rtellez I really am practicing every day and on weekends to. I need to finish the begginer module fast and then later the autonomous driving module too in order to perform an autonomous navigation simulation to finish my master proposal with ROS. So I have not much time unfortunately. The other issue is that I had just one subject in my university in Brazil of programming 5 years ago, just C language. So I did not learn python or classes at all. I came back to this lesson and looked for other examples in internetā€¦So I could finish this exercise with successful thanks. My biggest difficult here is really the lackage of programming skills. But I really want to become better on it, so maybe I can advance faster and finish my project. How much time would you say is possible to finish the ROS begginer Module (including all URDF, TF, etc), the autonomous Driving module and maybe the perception module? I am already finishing the ROS Basics in five days (the third module of Ros for beginner course). Do you think improving my skills in the current module I am, I will walk faster in other ROS courses, such as those I have told you I want to make? And I am sorry for the lot of questions, but I just have you as professors to learn ROSā€¦ Thanks in advance. Best Regards