# Controlling PWM Values with Python

**URL:** <https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529>\
**Category:** Blue Robotics Software\
**Created:** [July 15, 2022, 10:44pm UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529 "2022-07-15T22:44:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cagan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@cagan](https://discuss.bluerobotics.com/u/cagan)\
**Post date:** [July 15, 2022, 10:44pm UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/1 "2022-07-15T22:44:01Z")

</div>

Hi, I am controlling my ROV with a Python script for an autonomous mission. But when I tried to send manual control it fails and it only goes in one dimension. I used this page as reference:https://www.ardusub.com/developers/pymavlink.html#send-manual-control  
What can be the problem with this?

---

<div class="post-metadata">

**Author:** ![cagan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@cagan](https://discuss.bluerobotics.com/u/cagan)\
**Post date:** [July 16, 2022, 9:22am UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/2 "2022-07-16T09:22:21Z")

</div>

Also, when I tried to set target depth/attitude as referenced here: https://www.ardusub.com/developers/pymavlink.html#set-target-depthattitude  
I am facing with this error:  
DEPTH\_HOLD\_MODE = master.mode\_mapping()[DEPTH\_HOLD]  
TypeError: ‘NoneType’ object is not subscriptable

---

<div class="post-metadata">

**Author:** ![cagan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@cagan](https://discuss.bluerobotics.com/u/cagan)\
**Post date:** [July 16, 2022, 2:52pm UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/3 "2022-07-16T14:52:13Z")

</div>

The motors, which are working, are connected to single ESCs. Others are connected to a fourth motor controller.

---

<div class="post-metadata">

**Author:** ![EliotBR](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/eliotbr/32/6937_2.png) [@EliotBR](https://discuss.bluerobotics.com/u/EliotBR)\
**Post date:** [July 27, 2022, 1:23pm UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/4 "2022-07-27T13:23:28Z")

</div>

Hi @cagan, sorry for the delay on responding to this.

> [@cagan](#):
>
> The motors, which are working, are connected to single ESCs. Others are connected to a fourth motor controller.

Are you saying you have multiple motors connected to one ESC? From the FAQs on our T200 product page:

> [@T200 - FAQs - Can I connect more than one thruster to an ESC?](#):
>
> No, one ESC is needed for each thruster. In order to properly drive the motor, the ESC needs to accurately sense the position of the rotor as it is spinning, this is not possible with more than one motor.

> [@cagan](#):
>
> when I tried to send manual control it fails and it only goes in one dimension.

Are you able to control each motor individually from the [Motors Setup page](https://www.ardusub.com/reference/ardusub/motors-setup-page.html) in QGroundControl?

> [@cagan](#):
>
> when I tried to set target depth/attitude as referenced here: [Pymavlink · GitBook](https://www.ardusub.com/developers/pymavlink.html#set-target-depthattitude)  
> I am facing with this error:  
> DEPTH\_HOLD\_MODE = master.mode\_mapping()[DEPTH\_HOLD]  
> TypeError: ‘NoneType’ object is not subscriptable

I’m not sure what’s going on there - I’ll need to look more into it.

---

<div class="post-metadata">

**Author:** ![EliotBR](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/eliotbr/32/6937_2.png) [@EliotBR](https://discuss.bluerobotics.com/u/EliotBR)\
**Post date:** [July 28, 2022, 8:19am UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/5 "2022-07-28T08:19:43Z")

</div>

> [@EliotBR](#):
>
> I’m not sure what’s going on there - I’ll need to look more into it.

Following up on this, because BlueOS has additional MAVLink components for the camera manager and some other systems, there are multiple heartbeats. Accordingly the first heartbeat received through the MAVLink connection is not necessarily a heartbeat from the vehicle, so it’s necessary to wait until a correct one has been found. That can be done with something like the following:

```python
from pymavlink import mavutil

... # create the connection

# wait for vehicle heartbeat before sending commands
ensure_autopilot_heartbeat = (
    f'HEARTBEAT.get_srcSystem() == {master.target_system} and '
    f'HEARTBEAT.get_srcComponent() == {mavutil.mavlink.MAV_COMP_ID_AUTOPILOT1}'
)
master.recv_match(type='HEARTBEAT', condition=ensure_autopilot_heartbeat, blocking=True)

... # rest of the program

```

---

<div class="post-metadata">

**Author:** ![Eliseo](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eliseo](https://discuss.bluerobotics.com/u/Eliseo)\
**Post date:** [November 25, 2024, 10:28pm UTC](https://discuss.bluerobotics.com/t/controlling-pwm-values-with-python/12529/6 "2024-11-25T22:28:22Z")

</div>

Hi Eliot, just for curiosity,

Could you please help me with this?

I’m using Pymavlink to move servos, however my python script get stuck on master.wait\_hearbeat()  
I’m using the normal configuration of the tutorials for initialization of the UDPIN, my question is, do I need to connect any other Bluerobotics peripherals like camera, current sensor or something to made it work?

I’m implementing in a raspberry 4 like you sell and the navigator with external source.

Thanks in advance!
