Pixhawk GPS coordinates arrive wrong on a Raspberry Pi
If Pixhawk GPS coordinates look wrong on a Raspberry Pi, the data isn't corrupt: latitude is multiplied by 10 million and altitude is in millimeters. The right conversion and two traps.
You connected the Pi to the Pixhawk, and you're reading the position and sending it to the ground station. On screen, the latitude shows as 371591001, the altitude as 10001 and the battery as 12600. The marker on the map either doesn't appear at all or lands on the other side of the world.
The cable is fine, and so is the link. The numbers are correct; only their units haven't been converted.
Why the numbers come in so large
The Pixhawk doesn't send the position to the ground station as a decimal number. It multiplies the latitude by 10 million to make it an integer, and converts the altitude to millimeters and the heading to hundredths of a degree. That's how centimeter precision fits into a small packet.
Mission Planner knows these multipliers and shows degrees and meters on screen. Your code on the Pi doesn't. If you dump the message into JSON as is and send it, your ground station sees the raw number.
With the drone holding at 10 meters relative to home, here are the values coming from the autopilot and how to read them:
| Message and field | Received value | Unit | Value to read |
|---|---|---|---|
| GLOBAL_POSITION_INT lat | 371591001 | degrees × 10 million | 37,1591001° |
| GLOBAL_POSITION_INT lon | 387968998 | degrees × 10 million | 38,7968998° |
| GLOBAL_POSITION_INT relative_alt | 10001 | millimeters | 10.0 m, relative to home |
| GLOBAL_POSITION_INT alt | 510080 | millimeters | 510.08 m, above sea level |
| GLOBAL_POSITION_INT hdg | 21 | degrees × 100 | 0,21° |
| SYS_STATUS voltage_battery | 12600 | millivolts | 12.6 V |
| SYS_STATUS current_battery | 2810 | amperes × 100 | 28.1 A |
| VFR_HUD alt | 510.08 | meters | 510.08 m, above sea level |
The VFR_HUD altitude on the last line already comes in meters, but relative to sea level, not to home. If you see a number over 500 on the Pi while the drone is at 10 meters, you're reading the wrong field. For altitude relative to home, use relative_alt.
Example: the target comes from the ground station to the Pi
Imagine that in a TEKNOFEST mission you pick the target by clicking on the map at the ground station. The map gives you degrees, like 37.1590, and the Pi passes this on to the autopilot. This time the direction is reversed: you need to multiply the degrees by 10 million and convert the result to an integer.
If you forget to multiply, Python throws an error, because the latitude field in the packet expects an integer. If you multiply but skip converting with int(), you get the same error: 37.1590 * 1e7 is still a decimal number.
Position can also shift when you divide
Even if you convert in the right direction, two traps remain. If you divide with // in Python, the fraction is dropped and 371591001 // 10000000 gives 37. If you store the latitude in a 32-bit float variable at the ground station, the number gets rounded.
The figure shows the cost of both on the same latitude. Look at the red bars: in float32 the position jumps in 42-centimeter steps, while integer division moves the drone 17.7 kilometers away.
Latitude storage format and loss
The integer from the Pixhawk has its own step of 1.1 centimeters. Python's regular float goes far below that. If you divide with / and store the result as is, you lose nothing from the position.
Solution
Convert the units on the Pi and send ready-to-use values to the ground station. The code below reads the position and the battery, divides each field by its own multiplier and prints a single line of JSON.
On the Pi, the connection address is set to /dev/serial0. If you use another port, pass it as an argument when you run the code: python3 pi_konum_json.py /dev/ttyAMA0
import sys
import json
from pymavlink import mavutil
BAGLANTI = sys.argv[1] if len(sys.argv) > 1 else "/dev/serial0"
m = mavutil.mavlink_connection(BAGLANTI, baud=57600)
m.wait_heartbeat()
m.mav.request_data_stream_send(m.target_system, m.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 4, 1)
def paket(gp, ss):
return {
"enlem": gp.lat / 1e7, # degE7 -> degrees
"longitude": gp.lon / 1e7, # degE7 -> degrees
"irtifa_m": gp.relative_alt / 1000, # mm -> m, eve gore
"yon_derece": gp.hdg / 100, # cdeg -> degrees
"voltaj_v": ss.voltage_battery / 1000, # mV -> V
"akim_a": None if ss.current_battery == -1 else round(ss.current_battery / 100, 2), # cA -> A
}
gp = m.recv_match(type="GLOBAL_POSITION_INT", blocking=True)
ss = m.recv_match(type="SYS_STATUS", blocking=True)
print("raw:", gp.lat, gp.lon, gp.relative_alt, gp.hdg, ss.voltage_battery, ss.current_battery)
print(json.dumps(paket(gp, ss)))If you run the code while the drone holds at 10 meters, you'll see this. Your position and voltage will be different, but the format is the same:
raw: 371591001 387968998 9999 21 12600 2808
{"enlem": 37.1591001, "boylam": 38.7968998, "irtifa_m": 9.999, "yon_derece": 0.21, "voltaj_v": 12.6, "akim_a": 28.08}If akim_a comes in as null, your power module doesn't measure current. The autopilot sends -1 for current it doesn't measure, and the code leaves this value empty.
Error Messages
TypeError: Object of type MAVLink_global_position_int_message is not JSON serializable
You passed the message object straight to json.dumps. Converting it to a dictionary with msg.to_dict() makes the error go away, but raw numbers still go to the ground station. Convert the fields one by one and build a new dictionary.
struct.error: required argument is not an integer
You sent the target coordinate to the autopilot in degrees, or without multiplying it and converting it to an integer. If you send int(round(enlem * 1e7)), the packet goes through.
struct.error: 'i' format requires -2147483648 <= number <= 2147483647
You multiplied a value that was already multiplied by 10 million a second time. If you're sending back the lat field you read from the autopilot, send it without multiplying.
Learn the link between the Pi and the ground station
We explain step by step how to build the packet going from the Pi to the ground station, which field the autopilot writes the battery to and how, and why the target you send can be falsely confirmed by an old position packet, in the Drone Communication Technologies course.
Custom Turkish Ground Control Station Library: a MAVLink GCS library written in C#, with Turkish comments. You build your own ground station and connect it to the vehicle over a serial port, TCP or UDP.
Was this article helpful?
Learn this topic from start to finish
Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student