# Obtaining BlueROV2 IMU Readings Via Pymavlink

**URL:** <https://discuss.bluerobotics.com/t/obtaining-bluerov2-imu-readings-via-pymavlink/10039>\
**Category:** Modifications\
**Tags:** pymavlink\
**Created:** [June 30, 2021, 11:08pm UTC](https://discuss.bluerobotics.com/t/obtaining-bluerov2-imu-readings-via-pymavlink/10039 "2021-06-30T23:08:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CBW3750](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CBW3750](https://discuss.bluerobotics.com/u/CBW3750)\
**Post date:** [June 30, 2021, 11:08pm UTC](https://discuss.bluerobotics.com/t/obtaining-bluerov2-imu-readings-via-pymavlink/10039/1 "2021-06-30T23:08:05Z")

</div>

Hi all,

I’m interested in reading data from the BlueROV2 IMU magnetometer utilizing Pymavlink, so I can get the compass reading. I’ve referenced the following hyperlinks: [Pymavlink · GitBook](http://www.ardusub.com/developers/pymavlink.html), [Full Parameter List · GitBook](https://www.ardusub.com/developers/full-parameter-list.html), [Using Pymavlink Libraries (mavgen) | MAVLink Guide](https://mavlink.io/en/mavgen_python/), [MAVLINK Common Message Set (common.xml) | MAVLink Guide](https://mavlink.io/en/messages/common.html#REQUEST_DATA_STREAM), [MAVLINK Common Message Set (common.xml) | MAVLink Guide](https://mavlink.io/en/messages/common.html#MAV_CMD_GET_MESSAGE_INTERVAL) and

##### [MAVLINK Common Message Set (common.xml) | MAVLink Guide](https://mavlink.io/en/messages/common.html#RAW_IMU). Below is my python script:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/4/4dbfe74ae38ccc0d9ce4bba0b573aa86b2aaf819.png)

I know that I have to request the IMU readings by requesting a data stream. When I utilize the message ID “SCALED\_IMU” or “RAW\_IMU” I get the following errors:

_“Traceback (most recent call last):_

_File “Read\_BlueROV2\_Magnetometer.py”, line 23, in_

_mavutil.mavlink.SCALED\_IMU, 0, 1)_

_AttributeError: ‘module’ object has no attribute ‘SCALED\_IMU’”_

_“Traceback (most recent call last):_

_File “Read\_BlueROV2\_Magnetometer.py”, line 23, in_

_mavutil.mavlink.RAW\_IMU, 0, 1)_

_AttributeError: ‘module’ object has no attribute ‘RAW\_IMU’”_

I’d appreciate any ideas to help in solving how to obtain the IMU readings from the Autopilot.

---

<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 1, 2021, 3:01am UTC](https://discuss.bluerobotics.com/t/obtaining-bluerov2-imu-readings-via-pymavlink/10039/2 "2021-07-01T03:01:25Z")

</div>

Hi @CBW3750,

I just tried this and the new broadcast already sends both the `SCALED_IMU2` and `RAW_IMU` messages by default, so you should be able to just do

```python
from pymavlink import mavutil

master = mavutil.mavlink_connection('udpin:0.0.0.0:513')
master.wait_heartbeat()

while True:
    msg = master.recv_match(type='SCALED_IMU2', blocking=True).to_dict()
    ... # do something with 'msg'

```

without needing to request a new data stream.

Note that your `try-except` block likely won’t work because `wait_heartbeat` doesn’t raise any exceptions (as far as I’m aware). If there’s something already using the UDP port you’ll get an `OSError` when you create the connection (before `wait_heartbeat` can be called), and if there’s nothing sending data to the port then `wait_heartbeat` by default will just wait forever. If you’re wanting to check for the second case (no data) then I’d suggest you instead do something like

```auto
timeout = 5 # maximum seconds to wait for a message
if not master.wait_heartbeat(timeout=timeout):
    raise TimeoutError(f"No heartbeat within {timeout=} seconds!")

```

With the \>1Hz requirement for heartbeats, the timeout could in theory just be 1, but a larger number may be relevant if you want to leave time for the other device(s) to start up.

For reference, the issue you were having is a naming error. I wrote up a quick filtered list comprehension in the interactive python shell to find out what the actual name is, which you may find a useful process if you have a similar issue in future (where you know a module but not the variable/function/class name you’re after):

```auto
>>> from pymavlink import mavutil
>>> [p for p in dir(mavutil.mavlink) if '_IMU' in p]
['MAVLINK_MSG_ID_HIGHRES_IMU', 'MAVLINK_MSG_ID_RAW_IMU', 'MAVLINK_MSG_ID_SCALED_IMU', 'MAVLINK_MSG_ID_SCALED_IMU2', 'MAVLINK_MSG_ID_SCALED_IMU3', 'MAV_COMP_ID_IMU', 'MAV_COMP_ID_IMU_2', 'MAV_COMP_ID_IMU_3']
>>> mavutil.mavlink.MAVLINK_MSG_ID_RAW_IMU
27

```

This also matches the naming convention in our [request message interval pymavlink example](https://www.ardusub.com/developers/pymavlink.html#request-message-interval). I understand those examples are a bit difficult to navigate at the moment, so am currently involved in reordering them, and adding a hyperlinked table of contents and some extra examples that we’ve been meaning to add for a while. That should hopefully be sorted out by next week, and be a bit easier to see and get to the range of examples 🙂

---

<div class="post-metadata">

**Author:** ![CBW3750](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CBW3750](https://discuss.bluerobotics.com/u/CBW3750)\
**Post date:** [July 1, 2021, 4:49pm UTC](https://discuss.bluerobotics.com/t/obtaining-bluerov2-imu-readings-via-pymavlink/10039/3 "2021-07-01T16:49:00Z")

</div>

Thank you very much! That post was very useful. It helped me in learning that you can get the compass reading by using the MAVLink message “VFR\_HUD” and the field “heading”.  
Below is my code:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/7/7e428dfd9def88a5b8f43a128f9b2be7c0481811.png)
