satan 29
satan 29

Reputation: 267

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:

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")
        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()
        turn_cmd= Twist()
        w=0
        vx=0
        
        while not rospy.is_shutdown():
            #frame=cv2.imread("image.jpg",1)
            #error=extract(frame)
            error=-0.47
            
            if(error==0):
                vx=0.2
                w=0
                move_cmd.linear.x = vx
                move_cmd.angular.z = -w
                self.cmd_vel.publish(move_cmd)
                                       
            else:
                vx=0
                kp=0.15
                w=kp*error
                turn_cmd.linear.x=vx
                turn_cmd.angular.z = -w
                self.cmd_vel.publish(turn_cmd)

                
      
            r.sleep()
        


    def shutdown(self):
        rospy.loginfo("Stop TurtleBot")
        self.cmd_vel.publish(Twist())
        rospy.sleep(1)

if __name__ == '__main__':
    try:
        GoForward()
    except:
        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 the error variable , and then the program finishes. It appears as if GoForward() is not executing at all.....what can be the causes for this?

Upvotes: 1

Views: 521

Answers (1)

BTables
BTables

Reputation: 4843

A couple of things here. While it isn't really a fatal problem with python ROS nodes, you should probably be launching it via rosrun or roslaunch.

That being said, your error is actually python syntax. You missed a set of _ in _init_(). Change it to def __init__(self):

Upvotes: 1

Related Questions