# Gazebo unresponsive to service call

**URL:** <https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458>\
**Category:** ROS Beginners Exam\
**Created:** [April 15, 2021, 7:43pm UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458 "2021-04-15T19:43:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![torpanvil](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/t/eada6e/32.png) [@torpanvil](https://get-help.theconstruct.ai/u/torpanvil)\
**Post date:** [April 15, 2021, 7:43pm UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/1 "2021-04-15T19:43:27Z")

</div>

Hello,

I’m having an issue with task 2. I’ve got the drone moving, landing and taking off, and launch file works fine too. However, when calling the service /my\_service through the command line, the method take\_off() is called, but gazebo does not react. Is this an error in gazebo, or my coding?

Thank you.

forward\_motion.py

```
#! /usr/bin/env python

import rospy

import time

from geometry_msgs.msg import Twist

from std_msgs.msg import Empty

class RobotControl(object):

    def __init__ (self):

        # creates publishers

        self.pub_cmd_vel = rospy.Publisher("/cmd_vel", Twist, queue_size=10)

        self.pub_take_off = rospy.Publisher("/drone/takeoff", Empty, queue_size=10)

        self.pub_land = rospy.Publisher("/drone/land", Empty, queue_size=10)

        # creates messages to publish

        self.move = Twist()

        self.takeoff = Empty()

        self.land = Empty()

        self.rate = rospy.Rate(10)

    def take_off(self):

        rospy.loginfo('Taking off...')

        self.pub_take_off.publish(self.takeoff)

        time.sleep(5)

        rospy.loginfo("The quadcopter finished taking off")

    def move_forward(self):

        rospy.loginfo("Moving forward...")

        self.move.linear.x = 1

        self.move.angular.z = 0

        self.pub_cmd_vel.publish(self.move)

        time.sleep(5)

        rospy.loginfo("The quadcopter finished moving forward")

    def stopping(self):

        rospy.loginfo('Stopping...')

        self.move.linear.x = 0

        self.move.angular.z = 0

        self.pub_cmd_vel.publish(self.move)

        time.sleep(5)

        rospy.loginfo("The quadcopter finished stopping")

    def landing(self):

        rospy.loginfo('Landing...')

        self.pub_land.publish(self.land)

        time.sleep(5)

        rospy.loginfo("The quadcopter finished landing")

if __name__ == ' __main__':

    name_of_node = "forward_motion"

    rospy.init_node(name_of_node)

    rospy.loginfo("Node initiated: %s", name_of_node)

    rc = RobotControl()

    time.sleep(2)

    rate = rospy.Rate(0.5)

    

    ctrl_c = False

    def shutdownhook():

        global ctrl_c

        rospy.loginfo("Shutdown time!")

        ctrl_c = True

    rospy.on_shutdown(shutdownhook)

    

    while not ctrl_c: # while not true

        rospy.loginfo("Main loop...")

        rc.take_off()

        rc.move_forward()

        rc.stopping()

        rc.landing()

        ctrl_c = True

```

motion\_service.py

```
#! /usr/bin/env python

import rospy

from std_srvs.srv import EmptyResponse, Empty as EmptyServiceMsg

from std_msgs.msg import Empty

from forward_motion import RobotControl

def my_callback(request):

    rospy.loginfo("The Service to start the drone has been called")

    rc = RobotControl()

    rc.take_off()

    rospy.loginfo("Finished service")

    response = EmptyResponse()

    return response

rospy.init_node('service_move_drone') 

my_service = rospy.Service('/my_service', EmptyServiceMsg , my_callback)

rospy.loginfo("Service /my_service Ready")

rospy.spin() # mantain the service open.
```

---

<div class="post-metadata">

**Author:** ![u1802520](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/u1802520/32/6826_2.png) [@u1802520](https://get-help.theconstruct.ai/u/u1802520)\
**Post date:** [April 15, 2021, 9:24pm UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/2 "2021-04-15T21:24:06Z")

</div>

Try looping the command:

> for i in range(10):  
> self.pub\_take\_off.publish(self.takeoff)

publishing in topics sometimes fails the first time you publish.

---

<div class="post-metadata">

**Author:** ![torpanvil](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/t/eada6e/32.png) [@torpanvil](https://get-help.theconstruct.ai/u/torpanvil)\
**Post date:** [April 16, 2021, 8:25am UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/3 "2021-04-16T08:25:15Z")

</div>

Great. Thanks! It worked.

Say you have several topics in a program, how would one go about writing a function/method to publish once into all topics, without actually causing the robot to act on it? Would it make sense to put such a function as the first call in the program once everything is set up, or should it be called EVERY time a publish is wanted?

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [April 16, 2021, 9:02am UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/4 "2021-04-16T09:02:10Z")

</div>

A post was split to a new topic: [How to get all topics ready for publishing](https://get-help.theconstruct.ai/t/how-to-get-all-topics-ready-for-publishing/9463)

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [April 16, 2021, 8:52am UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/5 "2021-04-16T08:52:43Z")

</div>

@torpanvil.  
Bravo!!!  
This is a great way to do it, and I’m going to make this a wiki for everyone!

---

<div class="post-metadata">

**Author:** ![torpanvil](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/t/eada6e/32.png) [@torpanvil](https://get-help.theconstruct.ai/u/torpanvil)\
**Post date:** [April 16, 2021, 9:00am UTC](https://get-help.theconstruct.ai/t/gazebo-unresponsive-to-service-call/9458/6 "2021-04-16T09:00:01Z")

</div>

Oh great! I must be getting better at this! 😅
