How to get altitude via mavlink?

Hey guys! I am new at mavlink. I need a python code that can tell me what is the depth of the robot or is camera recording or something like that.
import serial
import time
from pymavlink import mavutil
ArduinoSerial = serial.Serial(‘com3’, 9600)

autopilot = mavutil.mavlink_connection(‘udpin:’)
while True:
if temp>43.33:
else :

I will light a led with my code. Like if it is recording the light will led. (That code for temp info.) Can you help me guys ?


You can use lua scripting in ardupilot, and save yourself the trouble of connecting an arduino.

But how can I get the data from pixhawk ? That is my problem.

Read the lua scripting documentation and examples.



Section of the forum to talk about lua:

This is how i made a lua script that reads channels and changes flight mode:

function update()
pwm9 = rc:get_pwm(9)
pwm10 = rc:get_pwm(10)
pwm12 = rc:get_pwm(12)
pwm13 = rc:get_pwm(13)
if pwm9 > 1700 then
elseif pwm10 > 1700 then
elseif pwm12 > 1700 then
elseif pwm13 > 1700 then

return update, 100
return update, 500

Some answers, at times, are worse that not answering at all.

EDIT: My criticism of Corrado_Steri was probably too harsh - the use-case is different than I presumed.

You should never need 4 discrete channels for 4 discrete flight modes, but if that was your desired end state, the following parameters would entirely negate the need for your script:


Have a look here at some of the ways to configure one or two channels to control up to six flight modes.

@Ugur_Demirezen, Lua scripting is probably best. The following snippet will store your vehicle’s altitude in a variable twice per second. You can then use it as you see fit to control LEDs, provide user warnings, etc.

local altitude
function update()
    if not ahrs:healthy() then return update, 500
    local posit = ahrs:get_position()
    altitude = posit:alt()
    return update, 500
return update()

Review the documentation linked above to see how you might use a relay pin to control a single LED or perhaps explore the Neopixel/RGB LED options.

Thank you for your replies guys. I will try that code with ArduSub. Is it possible with that ?

I don’t see why it won’t work with ArduSub.

Where is the LED located? Is it physically attached to the vehicle? Or are you trying to provide user-level warnings at a remote control station (GCS)?

Yes I am trying to provide user-level warnings at out control station. But I am new at this so I don’t know how to use this code at my script.

Ok, I still think Lua is a good answer for you, but let’s use custom GCS text messages instead of an LED to alert the user. This modification to my previous code snippet will alert the user every half second when the altitude is less than 10m (1000cm).

function update()
    if not ahrs:healthy() then return update, 500
    local posit = ahrs:get_position()
    if posit:alt() < 1000 then
        gcs:send_text(4, 'WARNING: Altitude below 10m!')
    return update, 500
return update()

I have 4 buttons (not toggle buttons) on the transmitter (MK15) that i want to be associated each to a flight mode. Each button can be associated to a different channel, but only 1 button per channel.
Don’t see where is the logic flawed. Basicly i wanted the buttons to act like a joystick button.
If you have a better code than mine i am all ears.

@Corrado_Steri, try setting the parameters I showed (and remove the script). It does make more sense that they are momentary buttons, but the script is probably unnecessary.

How can i implement this script to mine ? just add it like that ?

If you do it with options it goes back to what is set by modes channel when button is released, with lua it sticks to the selected mode, like it was done with mavlink.

No. Not at all.

This is a completely different language (Lua) and it is interpreted onboard by your flight controller. Your script wouldn’t be used at all in this case, and instead, the messages would be passed directly to GCS software like Mission Planner. Please read the documentation linked above.

@Corrado_Steri, I thought the mode change would be persistent until another mode command was sent. My mistake. I understand the intent of your script now. It seemed like a very strange implementation at first.

If there’s a way to “latch” the PWM output upon button pushes, you could avoid the script. OpenTX allows for that sort of thing. Not sure what you’re using, and that takes the discussion way off topic, anyway.

Using MK15, i wrote it in the previous message. A bit off topic but still good infos.

To be honest i tried options and tought it would have been persistent until next mode command but it didn’t. Maybe there was something wrong in my configuration but i really couldn’t have it working the way i wanted it.

1 Like

@Corrado_Steri, in the interest of providing another Lua example, and knowing now that your channels are mapped to momentary switches, I would implement the script this way:


local FREQUENCY            = 100
local COPTER_MODE_AUTO     =   3
local COPTER_MODE_LOITER   =   5
local COPTER_MODE_RTL      =   6

local ch_auto    = rc:find_channel_for_option(300)
local ch_loiter  = rc:find_channel_for_option(301)
local ch_rtl     = rc:find_channel_for_option(302)
local ch_althold = rc:find_channel_for_option(303)

function update()

    if ch_auto:rc_chan:get_aux_switch_pos() >= 1 then
        return update, FREQUENCY

    if ch_loiter:rc_chan:get_aux_switch_pos() >= 1 then
        return update, FREQUENCY

    if ch_rtl:rc_chan:get_aux_switch_pos() >= 1 then
        return update, FREQUENCY

    if ch_althold:rc_chan:get_aux_switch_pos() >= 1 then
        return update, FREQUENCY

    return update, FREQUENCY

return update, 500

Will try it but mine is half the size and way easier :slight_smile: