ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

resetting turtlesim programmatically in python

asked 2021-02-23 08:11:51 -0500

shiraz_baig gravatar image

I am writing a python code to control the turtlesim (turtlesim_node). I am using ubuntu 18.04 and ROS melodic. I am successfully moving the turtle as per my commands. But after some commands, I wish to reset the turtle. I have tried various options, but cannot find the right one.
Option 1:
from std_srvs.srv import Empty

rospy.wait_for_service('reset')
clear_bg = rospy.ServiceProxy('reset', Empty)
time.sleep(1)

Option 2:
Used this line instead:
clear_bg = rospy.ServiceProxy('/reset', Empty)

Option 3:
Used this line instead:
clear_bg = rospy.ServiceProxy('turtle1/reset', Empty)

The only option that works is:
import os
os.system('rosservice call /reset')

But Ido not like using system call. Is there any better way of resetting the turtle.

edit retag flag offensive close merge delete

Comments

Can you try to call your created Object:

resp1 = clear_bg()

Have a look again at the ROS Tutorials here.

turtleMaster20 gravatar image turtleMaster20  ( 2021-02-24 08:37:01 -0500 )edit

I did not create the object. The object is turtle1, created by command "rosrun turtlesim turtlesim_node" This is what I did after your suggestion.

def back_gr():
  rospy.wait_for_service('reset')
  resp1 = rospy.ServiceProxy('reset', Empty)

back_gr()

But neither it gives any error nor it resets the turtle.

shiraz_baig gravatar image shiraz_baig  ( 2021-02-24 23:17:09 -0500 )edit

Add resp1() at the end of def back_gr(). You are creating the Service Object but are yet to call it.

turtleMaster20 gravatar image turtleMaster20  ( 2021-02-25 00:51:40 -0500 )edit

---- here is my full and simplified code, plz tell me what I am doing wrong ---

pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size = 1)
rospy.init_node('turtle_controller', anonymous=True)
twist = Twist()

def back_gr():
   rospy.wait_for_service('reset')
   clear_bg = rospy.ServiceProxy('turtle1/reset', Empty)
   time.sleep(1)

while(True):
   atext = raw_input("next mesg: ")
   if atext[0]=='q': exit()
   if atext[0]=='c': #clear and reset system
      resp1 = back_gr()
      continue

   twist.linear.x = 1; twist.linear.y = 0; twist.linear.z = 0
   twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
   pub.publish(twist)

shiraz_baig gravatar image shiraz_baig  ( 2021-02-25 22:38:04 -0500 )edit

You have created a 'Service' but have not called it.

Between clear_bg = ... and time.sleep(1) add the code clear_bg() so the function back_gr() looks like this:

def back_gr():
 rospy.wait_for_service(`reset`)
 clear_bg = rospy.ServiceProxy('turtle1/reset', Empty)
 clear_bg()
 time.sleep(1)
turtleMaster20 gravatar image turtleMaster20  ( 2021-02-26 00:58:05 -0500 )edit

Thankyou very much. Very good suggestion. It worked very well.

First it did not work. And gave me the error that 'service [/turtle1/reset] unavailable'. I changed the service name from 'turtle1/reset' to 'reset'. Then it worked. I realised that 'reset' is a universal service and resets all turtles instead of just one turtle.

I also learnt, that in this statement clear_bg is not merely a variable. Instead it is a function call (service call) and has to be called to work. Thanks once again.

shiraz_baig gravatar image shiraz_baig  ( 2021-03-01 05:04:43 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2021-03-02 00:44:13 -0500

Call the created service by adding clear_bg()

edit flag offensive delete link more
0

answered 2021-03-01 05:12:29 -0500

shiraz_baig gravatar image

I was able to solve the problem as guided by turtlemaster20 in the comments. I wish to thank him. This is how it has to be done.
  clear_bg = rospy.ServiceProxy('reset', Empty)
  clear_bg()

edit flag offensive delete link more

Question Tools

Stats

Asked: 2021-02-23 08:11:51 -0500

Seen: 1,486 times

Last updated: Mar 02 '21