# simple ROS script terminating without displaying errors

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:

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")
rospy.on_shutdown(self.shutdown)
self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
r = rospy.Rate(10)
move_cmd = Twist()
w=0
vx=0
while not rospy.is_shutdown():
#error=extract(frame)
error=-0.47

if(error==0):
vx=0.2
w=0
else:
vx=0
kp=0.15
w=kp*error

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
self.cmd_vel.publish(move_cmd)
r.sleep()

def shutdown(self):
self.cmd_vel.publish(Twist())
rospy.sleep(1)

print(error)

if __name__ == '__main__':
try:
GoForward()
except:


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?

• 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. Sep 10, 2021 at 18:38
• running the python script. Sep 10, 2021 at 18:40
• So I assume you are running rosrun <package> <node>.py. Does the rosnode list return anything? Sep 10, 2021 at 19:21
• I simply ran it using "python goforward_mod.py" Sep 10, 2021 at 19:48
• Please use ros commands. rosrun <package_name> <script>.py. I hope your python script is part of a package and is executable? Sep 10, 2021 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__