# Action Feedback is not publishing at 1 Hz

**URL:** <https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230>\
**Category:** ROS Basics In 5 Days (Python)\
**Tags:** rospy\
**Created:** [August 2, 2019, 3:28pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230 "2019-08-02T15:28:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrjbs87](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/nrjbs87/32/10_2.png) [@nrjbs87](https://get-help.theconstruct.ai/u/nrjbs87)\
**Post date:** [August 2, 2019, 3:28pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/1 "2019-08-02T15:28:30Z")

</div>

Hi,

I am trying to get my feedback to publish once every second but when I am using:

`rostopic echo /action_custom_msg_as/feedback`

I am only seeing a response when I pass the action a goal. I am expecting the code to continue publishing my goal as feedback. What am I missing?

```auto
#! /usr/bin/env python
import rospy
import actionlib
from actionlib.msg import TestAction, TestActionResult, TestActionFeedback
from geometry_msgs.msg import Twist
from std_msgs.msg import Empty
from actions_quiz.msg import CustomActionMsgAction, CustomActionMsgResult, CustomActionMsgFeedback
import time

class Move_Drone_Class(object):

  # create messages that are used to publish feedback/result
  _feedback = CustomActionMsgFeedback()
  _result = CustomActionMsgResult()

  def __init__ (self):
    # creates the action server
    self._as = actionlib.SimpleActionServer("/action_custom_msg_as", CustomActionMsgAction, self.goal_callback, False)
    self._as.start()
    self.rate = rospy.Rate(1)

  def goal_callback(self, goal):
    r = rospy.Rate(1)
    success = True

    if self._as.is_preempt_requested():
        rospy.loginfo('The goal has been cancelled/preempted')
        # the following line, sets the client in preempted state (goal cancelled)
        self._as.set_preempted()
        success = False

    # define the publishers we'll be using
    self.pub_takeoff = rospy.Publisher('/drone/takeoff', Empty, queue_size=1)
    self.takeoff_msg = Empty()
    self.pub_land = rospy.Publisher('/drone/land', Empty, queue_size=1)
    self.land_msg = Empty()

    command = goal.goal
    rospy.loginfo(command)

   if command == "TAKEOFF":
        rospy.loginfo('Drone is taking off')
        self.pub_takeoff.publish(Empty())
    elif command == "LAND":
        rospy.loginfo('Drone is landing')
        self.pub_land.publish(Empty())

    self._feedback.feedback = command
    self._as.publish_feedback(self._feedback)
    rospy.loginfo("we are publshing at 1 Hz")
    # the sequence is computed at 1 Hz frequency
    self.rate.sleep()

    if success:
        #self._result.result = None
        self._as.set_succeeded(self._result)

if __name__ == ' __main__':
  rospy.init_node('drone_command_via_string')
  Move_Drone_Class()
  rospy.spin()

```

---

<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:** [August 2, 2019, 4:54pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/2 "2019-08-02T16:54:33Z")

</div>

Hi @nrjbs87, the Rate object is useless outside a loop:

> **[Proper use of rospy.Rate or ros::Rate](https://get-help.theconstruct.ai/t/proper-use-of-rospy-rate-or-ros-rate/59)**
>
> Background I created this topic because I frequently come across the use of rospy.Rate (ROS Python) and ros::Rate (ROS C++) in places where they are not needed. By the way, what is this Rate thingy? According to ROS Wiki, rospy.Rate and ...

For things to work as expected,

- you need to publish the feedback in a loop.
- `self.rate.sleep()` will need to be in the loop as well.

Something like:

```auto
# be careful here: don't use a condition that will never be false
# otherwise your action will never finish!
while some_condition_is_true:  
    self._feedback.feedback = command
    self._as.publish_feedback(self._feedback)
    self.rate.sleep()

```

That said, I don’t think publishing the feedback every second will work out of the box though, because the whole action will be completed in probably less than a second. Of course, you can intentionally make the action last longer just to have some fun!

---

<div class="post-metadata">

**Author:** ![nrjbs87](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/nrjbs87/32/10_2.png) [@nrjbs87](https://get-help.theconstruct.ai/u/nrjbs87)\
**Post date:** [August 3, 2019, 6:48pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/3 "2019-08-03T18:48:34Z")

</div>

Hmm OK super helpful. Here is my concern: for the `actions_quiz` one of the conditions is

- As a feedback, it publishes once a second what action is taking place (taking off or landing).

I am wanting to make sure this condition holds true even if the action is only a 1 second interval. To your point about having it in a loop, I tried using the `get_state()` method demonstrated in the last section, but turns out that’s only for clients 😛

What kind of loop can we use with an `_as` to get a similar effect?

---

<div class="post-metadata">

**Author:** ![nrjbs87](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/nrjbs87/32/10_2.png) [@nrjbs87](https://get-help.theconstruct.ai/u/nrjbs87)\
**Post date:** [August 3, 2019, 7:05pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/4 "2019-08-03T19:05:26Z")

</div>

Also in that same vein for the requirement of:

- As a feedback, it publishes once a second what action is taking place (taking off or landing).

Is the grading program looking for a specific string for feedback? such as “TAKINGOFF” or “LANDING”?

---

<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:** [August 6, 2019, 6:48pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/5 "2019-08-06T18:48:16Z")

</div>

> [@nrjbs87](#):
>
> Here is my concern: for the `actions_quiz` one of the conditions is

I understand your concern. But don’t worry too much about the feedback condition; just ensure the TAKEOFF and LAND commands work and you send feedback at least once.

> [@nrjbs87](#):
>
> Is the grading program looking for a specific string for feedback? such as “TAKINGOFF” or “LANDING”?

Sending the goal as feedback, as you have already done, should be fine.

Should you run into any problems with the quiz evaluator because of following this advice, I’ve got your back 😉 !

---

<div class="post-metadata">

**Author:** ![nrjbs87](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/nrjbs87/32/10_2.png) [@nrjbs87](https://get-help.theconstruct.ai/u/nrjbs87)\
**Post date:** [August 6, 2019, 7:54pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/6 "2019-08-06T19:54:16Z")

</div>

Thanks so much for the help! So the quiz did have an error…but for a different reason that is out of my control.

I have experienced several instances where the simulation will not take my takeoff or land command until I have reset the simulation. This unfortunately happened during the scoring and took off points because of this. When tested multiple times before, the code worked. Code is below. Could you please advise?

```auto
#! /usr/bin/env python
import rospy
import actionlib
from actionlib.msg import TestAction, TestActionResult, TestActionFeedback
from geometry_msgs.msg import Twist
from std_msgs.msg import Empty
from actions_quiz.msg import CustomActionMsgAction, CustomActionMsgResult, CustomActionMsgFeedback
import time

class Drone_Custom_Action_Class(object):

  # create messages that are used to publish feedback/result
  _feedback = CustomActionMsgFeedback()
  _result = CustomActionMsgResult()

  def __init__ (self):
    # creates the action server
    self._as = actionlib.SimpleActionServer("/action_custom_msg_as", CustomActionMsgAction, self.goal_callback, False)
    self._as.start()
    self.rate = rospy.Rate(1)

  def goal_callback(self, goal):
    r = rospy.Rate(1)
    success = True

    if self._as.is_preempt_requested():
        rospy.loginfo('The goal has been cancelled/preempted')
        # the following line, sets the client in preempted state (goal cancelled)
        self._as.set_preempted()
        success = False

    # define the publishers we'll be using
    self.pub_takeoff = rospy.Publisher('/drone/takeoff', Empty, queue_size=1)
    self.takeoff_msg = Empty()
    self.pub_land = rospy.Publisher('/drone/land', Empty, queue_size=1)
    self.land_msg = Empty()

    command = goal.goal
    rospy.loginfo(command)

    if command == "TAKEOFF":

        rospy.loginfo('Drone is taking off')
        self.pub_takeoff.publish(Empty())
        self._feedback.feedback = command
        self._as.publish_feedback(self._feedback)
        self.rate.sleep()

    elif command == "LAND":

        rospy.loginfo('Drone is landing')
        self.pub_land.publish(Empty())
        self._feedback.feedback = command
        self._as.publish_feedback(self._feedback)
        self.rate.sleep()

    if success:
        self._as.set_succeeded(self._result)

if __name__ == ' __main__':
  rospy.init_node('drone_command_via_string')
  Drone_Custom_Action_Class()
  rospy.spin()

```

---

<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:** [August 6, 2019, 9:14pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/7 "2019-08-06T21:14:42Z")

</div>

Hi @nrjbs87,

Thanks for the feedback.

I’ve just reset your quiz score and you can try again!

I think the problem of the drone not “obeying” the commands sometimes might be happening because the associated publishers might not be ready to receive commands (no connection to the publisher) when the commands were issued.

Before submitting the quiz again, please check the following post to see how _waiting for a publisher connection_ is done. ( **PS:** the post is not specifically about this, but it contains the trick to do this. I will write a separate post for this later).

> **[How to stop your robot when ROS is shutting down](https://get-help.theconstruct.ai/t/how-to-stop-your-robot-when-ros-is-shutting-down/225)**
>
> You might already know that ROS topics operate in “obey the last command” mode when working with robots in Gazebo simulations. In other words, the robot continues to execute the last message published to a given topic, even after you stop...

Kindly let us know how it goes before submitting the quiz again.

---

<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:** [August 6, 2019, 9:37pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/8 "2019-08-06T21:37:41Z")

</div>

Further hints:

- A good place to publish your 1Hz feedback is within the while loop that waits for a connection to a publisher.
- Publish your feedback _before_ executing the TAKEOFF/LAND command…publish it both within and outside the loop (while waiting for a connection and after a connection is made, so that you will at least publish once if the while loop is skipped, i.e. a connection was already available).
- It seems you are not using `self.takeoff_msg` and `self.land_msg` anywhere; consider removing them.

---

<div class="post-metadata">

**Author:** ![nrjbs87](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/nrjbs87/32/10_2.png) [@nrjbs87](https://get-help.theconstruct.ai/u/nrjbs87)\
**Post date:** [August 7, 2019, 12:19pm UTC](https://get-help.theconstruct.ai/t/action-feedback-is-not-publishing-at-1-hz/230/9 "2019-08-07T12:19:21Z")

</div>

You my friend…are a great teacher :D.

Thank you, quiz resubmitted.  
Thanks!
