esp32-drone-marin-ev2-jlassi-python — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by iyadh-jlassi01.

Components

Sketch code

# ═══════════════════════════════════════════════════════════════
# DRONE — MicroPython ESP32
# EV2 Jlassi Mohamed Iyadh
# 3 modes : IDLE (LED bleue) | MANUAL (LED verte) | EMERGENCY (LED rouge)
# MPU-6050 : SDA=GPIO21  SCL=GPIO22
# Bouton START : GPIO14   Bouton EMERGENCY : GPIO13
# ═══════════════════════════════════════════════════════════════

import machine, time, math, json, sys

# ── Délai boot : laisse le moniteur série s'ouvrir ────────────
time.sleep_ms(300)

# ═══════════════════════════════════════════════════════════════
# BROCHES
# ═══════════════════════════════════════════════════════════════
led_idle      = machine.Pin(26, machine.Pin.OUT)   # BLEU
led_manual    = machine.Pin(27, machine.Pin.OUT)   # VERT
led_emergency = machine.Pin(32, machine.Pin.OUT)   # ROUGE

btn_start = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
btn_emerg = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)

try:
    pwm_servo = machine.PWM(machine.Pin(25), freq=50)
    PWM_OK = True
except:
    PWM_OK = False

try:
    i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21), freq=400000)
except Exception as e:
    print("[WARN] I2C:" + str(e))
    i2c = None

# ═══════════════════════════════════════════════════════════════
# VARIABLES MESURES
# ═══════════════════════════════════════════════════════════════
nb_cycles = 0; nb_cycles_conformes = 0
jitter_max = 0; jitter_min = 99999; jitter_somme = 0.0; jitter_pid = 0
t_pid_prec = 0; duree_pid_us = 0
delai_reponse = 0; delai_securite = 0
nb_lectures_imu = 0; nb_erreurs_imu = 0
taux_cpu = 0.0; nb_boucles = 0

# ═══════════════════════════════════════════════════════════════
# ÉTAT DRONE
# ═══════════════════════════════════════════════════════════════
g_mode        = "IDLE"
g_roll        = 0.0; g_pitch = 0.0; g_yaw = 0.0
g_battery     = 100; g_motor = 0; g_heading = 0
g_servo_angle = 90.0; imu_ok = False
Kp=1.5; Ki=0.01; Kd=0.5
pid_integral=0.0; pid_err_prev=0.0
bs_prev=1; be_prev=1
t_bs=time.ticks_ms(); t_be=time.ticks_ms()
_sim_t=0.0; _serial_buf=""

# ═══════════════════════════════════════════════════════════════
# SERVO
# ═══════════════════════════════════════════════════════════════
def servo_ecrire(angle):
    if not PWM_OK: return
    try:
        duty = int((500 + (angle/180.0)*2000) / 20000.0 * 1023)
        pwm_servo.duty(duty)
    except: pass

# ═══════════════════════════════════════════════════════════════
# LEDs — 3 modes uniquement
# ═══════════════════════════════════════════════════════════════
def update_led():
    led_idle.value(0); led_manual.value(0); led_emergency.value(0)
    if   g_mode == "IDLE":      led_idle.value(1)       # Bleu
    elif g_mode == "MANUAL":    led_manual.value(1)     # Vert
    elif g_mode == "EMERGENCY": led_emergency.value(1)  # Rouge

# ═══════════════════════════════════════════════════════════════
# IMU MPU-6050
# ═══════════════════════════════════════════════════════════════
def init_imu():
    global imu_ok
    if i2c is None:
        p

More projects by iyadh-jlassi01