I am using ROS melodic on ubuntu 18.04, turtlebot 2.

I tried to create a simple ROS program, that tries to make the bot follow a specified path. My gazebo world looks like this:

enter image description here

The task is to try to make the bot follow the white path. The approach is to use the bot's camera to keep clicking photos using of its environment,and save it as "image.jpg", at a particular rate. This job is done by a program known as take_photo_mod.py. Now, there is another program "goforward_mod.py" that does two things: Processes image.jpg using openCV functions , and finds out the angle which the bot should move with the vertical to be aligned with the path. It's angular velocity is controlled by this angle. If the angle (error) is zero, then the bot simply moves forward at a velocity 0.2 units (and zero angular velocity) If there's some error, then the angular velocity is set using that error, and linear velocity is set to zero.

The openCV aspect is working flawlessly and I have tested it numerous times. However, whenever I run the entire program (including the goforward aspect) through the terminal, the program immediately ends, and gives no error. I tried to remove the openCV aspect from the code, and made the "error" variable, a constant, and I set the angular velocity by simply using a P-controller. This code should simply make the turtlebot rotate around the z axis but again, the script simply terminates without showing any errors.

#!/usr/bin/env python

import rospy
import cv2
import numpy as np
import math as m
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist

class GoForward():

    def _init_(self):
        rospy.init_node('GoForward', anonymous=False)
        rospy.loginfo("To stop TurtleBot CTRL + C")
        self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
        r = rospy.Rate(10)
        move_cmd = Twist()
        while not rospy.is_shutdown():
            move_cmd.linear.x = vx
            move_cmd.angular.z = -w  #-sign because we are controlling angular velocity in -z direction (clockwise),if the error is positive, and vice versa

    def shutdown(self):
        rospy.loginfo("Stop TurtleBot")


if __name__ == '__main__':
        rospy.loginfo("GoForward node terminated.")

Neither "To stop TurtleBot CTRL + C", nor "GoForward node terminated." is printed on the terminal. The terminal simply displays the value of error , and then the program finishes. It appears as if GoForward() is not executing at all.....what can be the causes for this?

  • $\begingroup$ How are you launching this node? Are you running the python script or do you have a launch file? When you try running, please run a rosnode list and see what's going on. roswtf might also help. $\endgroup$ Sep 10 '21 at 18:38
  • $\begingroup$ running the python script. $\endgroup$
    – satan 29
    Sep 10 '21 at 18:40
  • $\begingroup$ So I assume you are running rosrun <package> <node>.py. Does the rosnode list return anything? $\endgroup$ Sep 10 '21 at 19:21
  • $\begingroup$ I simply ran it using "python goforward_mod.py" $\endgroup$
    – satan 29
    Sep 10 '21 at 19:48
  • $\begingroup$ Please use ros commands. rosrun <package_name> <script>.py. I hope your python script is part of a package and is executable? $\endgroup$ Sep 10 '21 at 22:30

I took at look at your example and you're correctly noticing that it is not executing. The problem is that python expects the constructor will invoke __init__ while you are defining _init_ Note the different number of underscores. Thus non of your setup code is being executed. I fixed that and saw more of the outputs that you expected since the system was then successfully started.

More details on __init__

PS Thanks for the compact code example. I was able to reproduce your problem quickly to help you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.