# Executing python scripts using the Stretch\_body library

**URL:** <https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95>\
**Category:** Discuss\
**Created:** [October 9, 2020, 5:59pm UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95 "2020-10-09T17:59:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://forum.hello-robot.com/u/rishi)\
**Post date:** [October 9, 2020, 5:59pm UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/1 "2020-10-09T17:59:29Z")

</div>

I am trying to execute some scripts that will have a routine of movements for the different parts (DoFs) of Stretch, but with no success.

Is there a specific location that user defined scripts should be located? (perhaps somewhere in the catkin workspace?)

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [October 9, 2020, 6:27pm UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/2 "2020-10-09T18:27:32Z")

</div>

There’s no necessary location to save user scripts that are using the “stretch\_body” python library. The catkin workspace is used when you’re using ROS to move Stretch’s different joints. The usual workflow is to create a ROS package in `~/catkin_ws/src/`, with a node that sends trajectory requests to an action server defined in our “stretch\_core” package.

Personally, when working at the stretch\_body level, I fill a python file with the movement commands needed (e.g. `robot.lift.move_to(0.4)` and `robot.push_command()`), and call it from the terminal using `$ python test_script.py`. If you post an example of what isn’t working for you, I’d be happy to help figure it out.

---

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://forum.hello-robot.com/u/rishi)\
**Post date:** [October 10, 2020, 6:51pm UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/4 "2020-10-10T18:51:06Z")

</div>

Thank you for your response!

I got the Stretch\_body API to work.

Another question I had was, does the user have the ability to play audio over the inbuilt speakers on Stretch? If yes, what python library do you recommend?

Thanks,  
Rishi

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [October 11, 2020, 7:28am UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/5 "2020-10-11T07:28:43Z")

</div>

Hey @rishi, glad you got it working. Yes, you can use the PyAudio library to play audio out the built-in speakers. We’ve written a test script that uses PyAudio to record and play audio. Both the script and PyAudio come installed on Stretch, and you can invoke it using:

```bash
$ stretch_respeaker_test.py

```

You can see the source code for this script [here](https://github.com/hello-robot/stretch_body/blob/master/tools/bin/stretch_respeaker_test.py). Look at the [play\_audio function](https://github.com/hello-robot/stretch_body/blob/master/tools/bin/stretch_respeaker_test.py#L210-L224) to see how it’s done.

---

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://forum.hello-robot.com/u/rishi)\
**Post date:** [October 14, 2020, 8:20pm UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/6 "2020-10-14T20:20:09Z")

</div>

Hey Binit,

thank you so much!

I tried the test script for the audio and then played around with it to test some other wav files to see how loud the speakers onboard are. I felt that the volume was very low. I then tried to play the same file with pydub package, and it was loud but the audio distorted. Does this sound like normal operation or I might be doing something wrong.

Also, when trying to program a few movements, I noticed that we need to issue a `time.sleep` command between movements. Will this come in the way of having smooth movements or will the `time.sleep` argument have to be given a timing carefully tested according to the specific movements to make them smooth?

---

<div class="post-metadata">

**Author:** ![bshah](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/bshah/32/55_2.png) [@bshah](https://forum.hello-robot.com/u/bshah)\
**Post date:** [October 15, 2020, 4:03am UTC](https://forum.hello-robot.com/t/executing-python-scripts-using-the-stretch-body-library/95/7 "2020-10-15T04:03:53Z")

</div>

No problem @rishi! Would you try increasing the volume level using the following command:

```bash
$ alsamixer

```

Here’s how the volume settings on my robot are setup:

 ![Screen Shot 2020-10-14 at 11.18.44 PM](https://canada1.discourse-cdn.com/flex030/uploads/hello_robot2/original/1X/94f714fe3f717f59de87eb1c0a06137ba41e4f77.jpeg)

 ![Screen Shot 2020-10-14 at 11.20.36 PM](https://canada1.discourse-cdn.com/flex030/uploads/hello_robot2/original/1X/cdc78621744708c05ee96714f19763fc29956e7f.jpeg)

* * *

Regarding the movements, you won’t need to calculate a sleep time to accomplish a smooth motion. All joints on the robot execute asynchronously, which means that `robot.push_command()` or the servo joint commands don’t block the program. So you can queue up joint commands, call push\_command, and sleep for however long that part of the movement should last. Then, either you fire the next set of commands for the movement (which will be the new targets for the joints and the controllers will move the joint there smoothly), or the joints smoothly arrive to the commanded positions of the last set of commands.

A concrete example of what I’m talking about can be seen in the `stretch_xbox_controller_teleop.py` script. The user acts as the sleep function, intermittently sending joint targets via the xbox controller, which the low level controllers in Stretch smoothly achieve.

Also keep in mind that you can set the velocity and acceleration for each individual command.
