Executing python scripts using the Stretch_body library

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?)

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.

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?


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:

$ stretch_respeaker_test.py

You can see the source code for this script here. Look at the play_audio function to see how it’s done.

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?

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

$ alsamixer

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

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.