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(): #frame=cv2.imread("image.jpg",1) #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): rospy.loginfo("Stop TurtleBot") self.cmd_vel.publish(Twist()) rospy.sleep(1) print(error) 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 error , and then the program finishes. It appears as if
GoForward() is not executing at all.....what can be the causes for this?