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:192.168.2.1:14560’)
while True:
msg1=autopilot.recv_match(type=“SCALED_PRESSURE”,blocking=True)
temp=msg1.temperature/100
print(temp)
if temp>43.33:
ArduinoSerial.write(b’1’)
else :
ArduinoSerial.write(b’0’)
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 ?
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
end
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.
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)?
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!')
end
return update, 500
end
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.
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.
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.
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.
@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 COPTER_MODE_ALT_HOLD = 2
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
vehicle:set_mode(COPTER_MODE_AUTO)
return update, FREQUENCY
end
if ch_loiter:rc_chan:get_aux_switch_pos() >= 1 then
vehicle:set_mode(COPTER_MODE_LOITER)
return update, FREQUENCY
end
if ch_rtl:rc_chan:get_aux_switch_pos() >= 1 then
vehicle:set_mode(COPTER_MODE_RTL)
return update, FREQUENCY
end
if ch_althold:rc_chan:get_aux_switch_pos() >= 1 then
vehicle:set_mode(COPTER_MODE_ALT_HOLD)
return update, FREQUENCY
end
return update, FREQUENCY
end
return update, 500