Read GPS and IMU data form PixHawk to raspberry pi

Hello, I am trying to communicate to pixhawk via raspberry pi 2 over MAVLink. I can send commands but i can not read the real time sensor data from the pixhawk. Is it possible to do that??
Because I want the NED position co-ordinates provided by the IMU and GPS. Can anyone help me in this?

I know it can be done in dronekit by overriding the .on_message method

#Create a message listener using the decorator.
def listener(self, name, message):
print message

More info here

The two messages would then be for example GLOBAL_POSITION_INT and HIGHRES_IMU

To listen to more multiple messages I think you need to use the .add_message_listener function. so your code would look something like:


def receivedPos(self, name, posMsg):
#Do something with the message

def receivedImu(self, name, imuMsg):
#Do something with the message

1 Like

I have the same issue. Have you figured it out??

Hello Sameena,
It is easy to use the command “add_message _listener”.

def any_func_name(self, name, msg):
print msg
vehicle.add_message_listener(‘GLOBAL_POSITION_INT’, any_func_name)

Hi Ronnie,
I didn’t get you. After doing the mavlink mavproxy setup and getting this

what should be done?
I’m stuck at this. Would be grateful if you could help

Thanks & Regards
Sameena Shaikh

December 12

Hi Ronnie,
I didn’t get you. After doing the mavlink mavproxy setup and getting this

what should be done?
I’m stuck at this. Would be grateful if you could help

Thanks & Regards
Sameena Shaikh

Visit Topic

Hello Sameena,
You have successfully made the mavlink connection.
Now you have to write a code in C or python to access the data that you
Follow the python syntax that i send in my last email.
Address the serial port as you did in the command line.

Follow the steps in the above link to install dronekit python.

Take the help from the example code to write your own. If you do not want
to work with Dronekit SITL, then wisely remove the sitl parts from the


To unsubscribe from these emails, click here

Hi Ronnie,
Thanks for the help.
Yesi will write a python code for it.
But how can i get real time data like pitch n roll through commands. I frst need to do that and then next go to writing a python script

Hello Sameena,
Message number 30 gives you the roll, pitch and yaw information.

Hi Ronnie,
I tried running that code from the start guide hello drone and got this error. Trying to figure out this error

pi@raspberrypi:~ $ python
Start simulator (SITL)
Starting copter simulator (SITL)
SITL already Downloaded and Extracted.
Ready to boot.
Failed to run (/home/pi/.dronekit/sitl/copter-3.3/apm)
Traceback (most recent call last):
File “”, line 3, in
sitl = dronekit_sitl.start_default()
File “/home/pi/.local/lib/python2.7/site-packages/dronekit_sitl/”, line 454, in start_default
sitl.launch(sitl_args, await_ready=True, restart=True)
File “/home/pi/.local/lib/python2.7/site-packages/dronekit_sitl/”, line 253, in launch
caps = ArdupilotCapabilities(self.path)
File “/home/pi/.local/lib/python2.7/site-packages/dronekit_sitl/”, line 165, in init
self.has_defaults_path = “–defaults path” in helptext
UnboundLocalError: local variable ‘helptext’ referenced before assignment

and the meassage or command for getting the imu data should be given how. Like i used param set arming_check 0. It was successful. bt when i use get_imu or attitude i get unknown command

There might be some issue with your sitl setup.
Mavlink messages directly feeding into cmd line is something i never tried
for it doesnt serve my purpose.
However the mavlink messages works fine with python and C.

Did you use it on assh client server or just with a screen on raspberry pi. On some blog i read sitl wont work for arm processors…

I can receive the messages with python and C. But it can only get three types(though I have set it to get all the information),

I need the IMU message, GPS, how can I get them.

Hi, I meet the same issue with you! Have you get the method?

Hi! I’m trying to get these data, I’m using MAVlink protocol to connect Rapsberry pi 3 B with pixhawk. How you got it?

I found a way to do this when connected to Software In The Loop (SITL). Not yet tested on Rpi but should work if you can establish connection.

Basically you start request_data_stream_send from pymavlink. I set to 1 hertz (so sends every one second) but is only read every 3 seconds. The read_loop combines all the messages to a single dictionary, to store as JSON, SQLite or whatever.

There might be a better way to do this, but I am only a couple days in to programmatically controlling ardupilot.

See code here:

I would be quite interested in any progress on this.

G’day Jack,

I have moved on from this code. I found that there are advantages in using mavproxy and writing a module to interact programmatically with the rover. I found myself always going to mavproxy source to see how it grabbed the Mavlink data so I just decided to see if I could use it.

There are a bunch of modules for MavProxy. I been experimenting with modifing the restserver (and console) module so I could receive all the console data and also POST mavproxy commands. I found that the mavproxy console values and text and mpstatus json dump gives a fair portion of what is needed and being able to POST mavproxy commands rather than mavlink simplifies things too. On my local copy I have a mission endpoint that accepts mission points via post and creates a text file to send to rover.

This way any software you run on the pi can interact with mavproxy as a bridge to rover with just http.

Being a below average self taught programmer, I find mavproxy code a bit confusing but once I understand a piece I often get why they did something in such a way. It is designed for modules which helps a lot.

I’ll share my code but most of it is just a hack on mavproxy to get proof on concept with bugs probably. All only tested in simulator.

I am currently rethinking all this actually and not actively working on this method. Learning lots experimenting with the simulator and have appreciated all the efforts on the Ardupilot project.

I am still on the mavlink learning curve and have the simple goal of getting RTK/GPS and clock data from APM to RPi.

Good luck with your rover.

Thanks - before I started hacking at MavProxy I was able to get that data to my Pi with direct with pymavlink. The most straight forward examples were on the ArduSub documentation:

Also you might want to search forum for ‘Apsync’ and ‘Pi Connect’ for software and hardware that is more complete solution.