Get pisition of pitch, roll and yaw for python

hello

I have the rasberry pi 3 and a navio+.
i would like to know if there is some code that get pisition of pitch, roll and yaw for python

thanks a lot for your help

Look at mavlink to get messages

thanks for responding

we are dont using mavlink.
we are programming in python the raspberry pi 3 and also have a navio plus, but the library AHRS is in the C++ and we are needing this library in python but we have dont can find.

we also find the command import subprocess but with this method the code in c++ only run just once.
i would know if there is some method of program run together and that the code en c++ sends data to code in python

thanks for your attention

Hey Cristian, I also had the same problem of finding an AHRS code in python so I put one together based on this journal paper:

"Gain-Scheduled Complementary Filter Design for a MEMS Based Attitude and Heading Reference System"

The python code is on my github here:

To use it, you will have to use the MPU IMU code from EMLID. The basic flow is:

  • Call EMLID’s ‘getMotion9()’
  • Subtract any gyro offsets
  • from the AHRS library, call ‘attitude3( … )’
  • Repeat

The one caveat for this AHRS code is that it can not handle angles past 90degs in roll or pitch. Since it does not use quaternions, the code breaks down past 90degs, however, there is a reset function that can be called in the event that you do to get the filter back on track. I haven’t had any problems with this since I don’t do any inverted flight with my aircraft. Hope this helps!

Lars

thanks for your response!!!

dude, i did your code,but i am new programing in python and i called the code Complementary_Filter2 from the same folder that principal code.

from Complementary_Filter2 import comp_filt
posicion = comp_filt()
posicion.init()

after this i initialized the imu with the codes of emlied, this part is excellent and i haven’t any problem with it.

later

while 1:
m9a, m9g, m9m = imu.getMotion9()
salida=posicion.attitude3(float(m9a[0]),float(m9a[1]),float(m9a[2]),float(m9g[0]),float(m9g[1]),float(m9g[2]),float(m9m[0]),float(m9m[1]),float(m9m[2]))
pitch=salida.pitch_d
print(pitch)

this is an approximate code

the proble is the follow

Connection established: True
Traceback (most recent call last):
File “principal.py”, line 251, in
pitch=salida.pitch_d
AttributeError: ‘NoneType’ object has no attribute ‘pitch_d’

do you know? that i am doing wrong

thanks

hello Lars

i was doing tests and your code worked but i see the data and the data not change, it always has the same data

i would like know. what are there than change and your code ?

thanks for yout help!!

You should use ROS on the RPI, and just subscribe to the IMU messages from your Python script.

Besides having to deal with gimbal lock, writing your own filter (!) won’t match what the aircraft is actually using for control or benefit from calibrations applied.

1 Like

Cristian, the only difference that I’m seeing between my code and yours is that instead of

salida = posicion.attitude3(…)
pitch=salida.pitch_d

I have

posicion.attitude3(…)
pitch=posicion.pitch_d

Give that a try. Also one other thing to note, if you want the yaw angle to be correct you will have to do a magnetometer calibration. The hard iron offsets go into the initialization.

Srim, I know I could use ROS or ArduPilot to achieve these things but I enjoy writing my own code (and filters) and learn a lot more by doing so then using ‘canned’ code. I’m assuming when you say that it won’t match what the aircraft is doing this assumes that it is being used with ArduPilot. I don’t use Ardupilot, instead I use a custom autopilot I wrote for flight dynamics analysis so the complimentary filter works quiet well.