# Stretch\_body Python SDK and Python Multithreading

**URL:** <https://forum.hello-robot.com/t/stretch-body-python-sdk-and-python-multithreading/221>\
**Category:** Discuss\
**Created:** [June 23, 2021, 9:26pm UTC](https://forum.hello-robot.com/t/stretch-body-python-sdk-and-python-multithreading/221 "2021-06-23T21:26:19Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![aedsinger](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.hello-robot.com/aedsinger/32/6_2.png) [@aedsinger](https://forum.hello-robot.com/u/aedsinger)\
**Post date:** [November 9, 2021, 6:36pm UTC](https://forum.hello-robot.com/t/stretch-body-python-sdk-and-python-multithreading/221/10 "2021-11-09T18:36:24Z")

</div>

You might try using the `is_mg_moving` flag. This flag indicates if the underlying trapezoidal motion generator is commanding motion to the PID controller. When the trapezoidal motion generators arrives at its setpoint this flag will be zero.

This flag can be preferable as it isn’t dependent on the motor encoder which inherently has sensor noise. The PID controller tracks the commanded motion accurately so it serves as a reasonable approximation of the true physical motion. The exception being that an exception to the motion (say, a disturbance causes the wheel to stall) will not be caught.

The following diagram helps illustrate:

 ![Screenshot from 2021-11-09 10-33-20](https://canada1.discourse-cdn.com/flex030/uploads/hello_robot2/original/1X/414778fe67fdeb4955180d35e757399d7bdbc190.png)

Here’s a code snippet to test this use case:

```auto
#!/usr/bin/env python
import stretch_body.robot

def wait_until_not_moving(robot):
    import time
    # Note, this is necessary to give time for the trajectory to begin executing before polling for completion
    time.sleep(0.1)
    while robot.base.left_wheel.status['is_mg_moving'] or robot.base.right_wheel.status['is_mg_moving']:
        time.sleep(0.1)

robot = stretch_body.robot.Robot()
robot.startup()

robot.base.translate_by(.005)
robot.push_command()
wait_until_not_moving(robot)

robot.base.translate_by(-.005)
robot.push_command()
wait_until_not_moving(robot)

robot.stop()

```

This will cause the robot to move forward 5mm, then back 5mm.

---

_[View the full topic](https://forum.hello-robot.com/t/stretch-body-python-sdk-and-python-multithreading/221)._
