Python playground
Run Python without installing anything or creating an account. Change the code and press "Run". Libraries included: numpy, matplotlib and pymavlink.
- Runs in a sealed areaThe code runs in an area isolated from the rest of the site. It can't reach your account, this page or the internet.
- First load takes a few secondsOn the first run, Python downloads into your browser; after that it's fast.
Encode and decode a MAVLink message
We turn the HEARTBEAT message the ground station sends every second into bytes and decode it back. Corrupt one byte and the CRC check rejects the message: that's exactly what the "bad packet" counter in your telemetry is.
# HEARTBEAT: yer istasyonunun (GCS) her saniye gönderdiği kalp atışı mesajı
from pymavlink.dialects.v20 import ardupilotmega as mavlink
class Tampon:
def __init__(self):
self.veri = b""
def write(self, b):
self.veri += b
t = Tampon()
gcs = mavlink.MAVLink(t, srcSystem=255, srcComponent=190)
gcs.heartbeat_send(mavlink.MAV_TYPE_GCS, mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
print("Baytlar :", t.veri.hex(" "))
print("Uzunluk :", len(t.veri), "bayt (başlık 10 + yük 9 + CRC 2)")
m = mavlink.MAVLink(None).parse_buffer(t.veri)[0]
print("Çözülen :", m.get_type(), "| sistem", m.get_srcSystem(), "| bileşen", m.get_srcComponent())
# Bir baytı bozun: CRC tutmaz, mesaj kabul edilmez
bozuk = bytearray(t.veri)
bozuk[12] ^= 0xFF
cozucu = mavlink.MAVLink(None)
try:
sonuc = cozucu.parse_buffer(bytes(bozuk))
print("Bozuk paket:", sonuc)
except mavlink.MAVError as e:
print("Bozuk paket reddedildi:", e)
The full topic: Drone Control with Python from Scratch Open in Colab
Altitude hold with PID
A virtual multicopter climbs to 10 meters and tries to stay there. Play with Kp, Ki and Kd and watch on the chart how overshoot, oscillation and settling time change.
import matplotlib.pyplot as plt
kp, ki, kd = 1.2, 0.15, 0.9 # bu üç değerle oynayın
hedef = 10.0 # metre
dt = 0.05 # saniye
irtifa = hiz = integral = 0.0
onceki_hata = hedef
zaman, iz = [], []
for adim in range(400):
hata = hedef - irtifa
integral += hata * dt
turev = (hata - onceki_hata) / dt
onceki_hata = hata
ivme = kp * hata + ki * integral + kd * turev
ivme = max(-5.0, min(5.0, ivme)) # motorların sınırı: 5 m/sn2 (ivme)
hiz = (hiz + ivme * dt) * 0.98 # hava direnci
irtifa += hiz * dt
zaman.append(adim * dt)
iz.append(irtifa)
print(f"Son irtifa: {irtifa:.2f} m | en yüksek: {max(iz):.2f} m | aşım: {max(iz) - hedef:.2f} m")
plt.figure(figsize=(7, 3.2))
plt.plot(zaman, iz, label="irtifa")
plt.axhline(hedef, ls="--", c="gray", label="hedef")
plt.xlabel("zaman (sn)"); plt.ylabel("irtifa (m)"); plt.legend(); plt.title("PID ile irtifa tutma")
plt.show()
The full topic: Basic Drone Course: From Sensors to Flight Open in Colab
Distance and bearing between two GPS points
During a mission, use the haversine formula to work out how far the next waypoint is and which way to turn. Swap in the coordinates of your own field.
from math import radians, degrees, sin, cos, atan2, sqrt
R = 6371000 # Dünya'nın ortalama yarıçapı (m)
def mesafe_yon(enlem1, boylam1, enlem2, boylam2):
f1, f2 = radians(enlem1), radians(enlem2)
df, dl = radians(enlem2 - enlem1), radians(boylam2 - boylam1)
a = sin(df / 2) ** 2 + cos(f1) * cos(f2) * sin(dl / 2) ** 2
mesafe = 2 * R * atan2(sqrt(a), sqrt(1 - a))
yon = degrees(atan2(sin(dl) * cos(f2), cos(f1) * sin(f2) - sin(f1) * cos(f2) * cos(dl))) % 360
return mesafe, yon
kalkis = (40.2307, 29.0099) # örnek: Bursa
waypoint = (40.2335, 29.0152)
d, y = mesafe_yon(*kalkis, *waypoint)
print(f"Mesafe: {d:.1f} m | yön: {y:.0f} derece")
print(f"10 m/sn hızla yaklaşık {d / 10:.0f} saniye")
The full topic: Mission Planner Course from Scratch Open in Colab