# Needed some explanation for Action Server Source code

**URL:** <https://get-help.theconstruct.ai/t/needed-some-explanation-for-action-server-source-code/2804>\
**Category:** ROS Basics In 5 Days (Python)\
**Created:** [March 28, 2020, 10:47pm UTC](https://get-help.theconstruct.ai/t/needed-some-explanation-for-action-server-source-code/2804 "2020-03-28T22:47:45Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 30, 2020, 9:10am UTC](https://get-help.theconstruct.ai/t/needed-some-explanation-for-action-server-source-code/2804/2 "2020-03-30T09:10:28Z")

</div>

> [@kashish.dhal](#):
>
> _client.send\_goal(goal, feedback\_cb=feedback\_callback)_  
> _state\_result = client.get\_state()_  
> _rate = rospy.Rate(1)_
> 
> This means that we send the goal to the action server which will run at 1 Hz and give us update at 1 Hz

Not exactly. We are **not** setting the “rate” of the action server here. Here’s what happens in those three lines, explained in comments:

```auto
# send the goal to the action server and specify the callback function 
# that the action server will call from time to time to give feedback
client.send_goal(goal, feedback_cb=feedback_callback) 

# get the state of the action (is it done?)
state_result = client.get_state()

# create a Rate object to be used later on. We have not used it at his point
rate = rospy.Rate(1) 

```

> [@kashish.dhal](#):
>
> Afterwards, we implement:  
> _i=0_  
> _while not i == 3:_  
> _takeoff.publish(takeoff\_msg)_  
> _rospy.loginfo(‘Taking off…’)_  
> _time.sleep(1)_  
> _i += 1_
> 
> From this I understand that takeoff msg will be published and then sleep function is called with an argument of 1 sec (meaning you will not get back to this loop in next 1 second) .

What this code block does simply is **publish the takeoff command three times, once per second**. When you use `time.sleep(x)`, the code pauses for `x` seconds. Each time we publish, we are also increasing `i` by 1, so that the loop can exit after we have published the third time. **Execution of the loop has nothing to do with the ROS\_MASTER, it’s just pure Python.**

> [@kashish.dhal](#):
>
> Now here I see we have used rate.sleep instead of time.sleep and I am not sure why?

`rospy.Rate.sleep()` works differently:

- `time.sleep(x)` - pause the Python code for `x` seconds. Can be used anywhere in the program - inside or outside a loop.
- rospy.Rate.sleep() - _Only used within a loop_. Tries to ensure that the loop runs at the defined rate per second. In this case, it will try to make the loop run 1 time per second. Read more about rospy.Rate here: [Proper use of rospy.Rate or ros::Rate](https://get-help.robotigniteacademy.com/t/proper-use-of-rospy-rate-or-ros-rate/59)

> [@kashish.dhal](#):
>
> I see here that for time.sleep call in while loop, Taking off… is executed only once between each action pair call namely 0, 1 and 1,2 and 2,3 (see above). However, if we look at the output after 3 seconds between 3rd and 4th action, publishing is done three times. Also, another thing that I noted was in later action calls, publishing was only executed once between the consecutive action calls. Lastly, between 9th and 10th publishing was done twice.

The messages `[Feedback] image n.x received` appearing between the `Taking off...` messages are coming from the callback function `feedback_callback`, _which is running in a sepate thread_ from the main program (so nothing in the main program affects it. It’s independent.).

What you call “action server calls” are actually feedback messages from the action server, coming from the callback function `feedback_callback`. The action server was only called once in the line

```auto
client.send_goal(goal, feedback_cb=feedback_callback)

```

* * *

In conclusion, I think you have so many questions because you’re probably new to both Python and ROS. Don’t worry, you will pick both up with more practise. Cheers!

---

_[View the full topic](https://get-help.theconstruct.ai/t/needed-some-explanation-for-action-server-source-code/2804)._
