Mini-Quadrocopter mit Fernsteuerung (ESP32-C3, SX1280)
Drohne im X-Rahmen und eine Handfernsteuerung: mit den Sticks über eine 2,4-GHz-LoRa-Verbindung fliegen.
- ESP32-C3
- MPU-6050
- SX1280
- Coreless motors
- Joysticks
Zwei emulierte Chips und die Flugphysik: auf den meisten Rechnern läuft das langsamer als Echtzeit.
So funktioniert es
Zwei Platinen in einem Projekt. Der Quadrocopter: eine X-förmige Platine mit 1S-LiPo, 3,3-V-LDO, ESP32-C3 mit Zephyr-Flugregler (Quelle firmware/esp32c3/quad_rc), MPU-6050-Gyro/Beschleunigungssensor, einem SX1280-Empfänger für 2,4 GHz, vier AO3400A-Low-Side-Treibern und vier Glockenankermotoren. Die Fernsteuerung: ESP32-C3 (firmware/esp32c3/rc_remote), SX1280, zwei Daumen-Joysticks (links: Gas und Gieren, rechts: Nicken und Rollen), ein Scharf-Schalter und eine Status-LED. Die Fernsteuerung sendet die Sticks 250-mal pro Sekunde (LoRa SF5, 812 kHz, Pakete mit Folgenummer und CRC, jeder 8. Slot wird mit Telemetrie beantwortet); der Quadrocopter fliegt, was die Sticks sagen. Scharf schalten: Schalter ein, Gas ganz unten. Ohne gültiges Paket für 0,5 s geht er in die Waagerechte und nimmt das Gas auf null zurück (Failsafe). Die Pakete laufen durch die simulierte Luft: Trag die Fernsteuerung in der Welt weit weg, und die Verbindung reißt ab; die LED der Fernsteuerung blinkt schnell, wenn der Quadrocopter nicht mehr antwortet. Bewege in der Welt-Ansicht die Sticks mit der Maus oder belege einen Gamecontroller. Der Quadrocopter ist der fliegende Körper: Masse und Trägheit kommen aus der Platine und ihren Bauteilen.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | Pins |
|---|---|
| GND | BT1 −BT101 −C1 2C2 2C3 2C4 2C5 2C6 2C7 2C8 2C9 2C10 2C11 2C101 2C102 2C103 2C104 2C105 2C106 2C107 2C108 2J1 GNDJ101 GNDJS101 GNDJS102 GNDQ1 SQ2 SQ3 SQ4 SR7 2R8 2R9 2R10 2SW101 COMU1 GNDU2 GNDU2 AD0U2 CLKINU2 FSYNCU3 GNDU4 GNDU101 GNDU102 GNDU103 GNDY1 GNDY101 GND |
| 3V3 | C2 1C3 1C4 1C9 1R1 1R2 1R11 1R12 1U1 3V3U1 ENU2 VDDU2 VLOGICU3 OUTU4 VBATU4 VBAT_IO |
| VBAT | BT1 +C1 1D1 KD2 KD3 KD4 KM1 +M2 +M3 +M4 +U3 INU3 EN |
| CPOUT | C6 1U2 CPOUT |
| D1 | D1 AM1 −Q1 D |
| D2 | D2 AM2 −Q2 D |
| D3 | D3 AM3 −Q3 D |
| D4 | D4 AM4 −Q4 D |
| G1 | Q1 GR3 2R7 1 |
| G2 | Q2 GR4 2R8 1 |
| G3 | Q3 GR5 2R9 1 |
| G4 | Q4 GR6 2R10 1 |
| PWM1 | R3 1U1 GPIO4 |
| PWM2 | R4 1U1 GPIO5 |
| PWM3 | R5 1U1 GPIO6 |
| PWM4 | R6 1U1 GPIO7 |
| R_3V3 | C102 1C103 1C106 1JS101 VCCJS102 VCCR101 1R102 1R103 1U101 3V3U101 ENU102 VBATU102 VBAT_IOU103 OUT |
| R_ARM | R101 2SW101 1U101 GPIO4 |
| R_BUSY | U101 GPIO18U102 BUSY |
| R_DIO1 | U101 GPIO19U102 DIO1 |
| R_LED | D101 KU101 GPIO8 |
| R_LEDA | D101 AR102 2 |
| R_MISO | U101 GPIO5U102 MISO |
| R_MOSI | U101 GPIO7U102 MOSI |
| R_NSS | R103 2U101 GPIO10U102 NSS |
| R_PIT | JS102 VRYU101 GPIO2 |
| R_RF_ANT | C108 1J101 RFL102 2 |
| R_RF_DCC | L101 1U102 DCC_SW |
| R_RF_RFIO | C107 1L102 1U102 RFIO |
| R_RF_VREG | C104 1L101 2U102 VDD_INU102 DCC_FB |
| R_RF_VRPA | C105 1U102 VR_PA |
| R_RF_XTA | U102 XTAY101 1 |
| R_RF_XTB | U102 XTBY101 2 |
| R_ROL | JS102 VRXU101 GPIO3 |
| R_SCK | U101 GPIO6U102 SCK |
| R_THR | JS101 VRYU101 GPIO0 |
| R_VBAT | BT101 +C101 1U103 INU103 EN |
| R_YAW | JS101 VRXU101 GPIO1 |
| REGOUT | C5 1U2 REGOUT |
| RF_ANT | C11 1J1 RFL2 2 |
14 weitere Verbindungen im Editor.
- C10
- Kondensator
- C11
- Kondensator
- C101
- Kondensator
- C102
- Kondensator
- C103
- Kondensator
- C104
- Kondensator
- C105
- Kondensator
- C106
- Kondensator
- C107
- Kondensator
- C108
- Kondensator
- D1
- Diode
- D2
- Diode
- D3
- Diode
- D4
- Diode
- D101
- LED
- L2
- Spule
- L101
- Spule
- L102
- Spule
- Q1
- N-MOSFET
- Q2
- N-MOSFET
- Q3
- N-MOSFET
- Q4
- N-MOSFET
- R10
- Widerstand
- R11
- Widerstand
- R12
- Widerstand
- R101
- Widerstand
- R102
- Widerstand
- R103
- Widerstand
- Y1
- Quarz
- Y101
- Quarz
Bauteile
- Kondensator× 19
- Widerstand× 15
- Befestigungsbohrung× 8
- Glockenankermotor 7×16 mm + 45-mm-Propeller× 4
- Diode× 4
- Spule× 4
- N-MOSFET× 4
- Quarz× 2
- ESP32-C3 (RISC-V, Firmware)× 2
- Daumen-Joystick (2 Achsen + Druck)× 2
- LDO-Regler (TPS7A20)× 2
- Li-Ion-Zelle (geschützt)× 2
- SX1280 (2,4-GHz-LoRa-/FLRC-Transceiver)× 2
- Antennenbuchse U.FL (2,4 GHz)× 2
- LED
- MPU-6050 Chip (QFN-24, Beschleunigung + Gyro, I²C)
- Kippschalter, Umschalter (Subminiatur, THT)
Firmware
src/main.c · 247 Zeilen · die ersten 120 hier, alles im Editor
/* Micro quadcopter flight controller with an RC receiver (ESP32-C3 + SX1280, Zephyr).
*
* The SX1280 listens for the remote's packets (rc_link.h: four channels, arm flag, sequence,
* CRC, 250 Hz); a radio thread takes each packet on DIO1, checks it, keeps the channels, the
* RSSI / SNR of the packet status and the link quality (share of the last 100 packets that
* arrived), and answers a telemetry request at once. The control loop (250 Hz, as
* firmware/esp32c3/quad) flies what the sticks say:
* throttle stick -> collective thrust 0..100 % PWM (no fixed hover value: the pilot hovers)
* pitch / roll sticks -> attitude set point ±25°, yaw stick -> yaw rate ±120 °/s
* Arming: the arm switch on and the throttle stick at the bottom (< 5 %); the switch off
* disarms. Failsafe: no valid packet for 500 ms -> level, the throttle falls from where it was to
* zero within 1 s, then disarmed; flying again needs the switch and low throttle, as at the start.
*
* Pins: MPU-6050 on I2C0 (SDA GPIO1, SCL GPIO3), motors GPIO4..7 (LEDC PWM 1 kHz), SX1280 on
* GP-SPI2 (SCLK GPIO0, MOSI GPIO18, MISO GPIO19, NSS GPIO10), BUSY GPIO9, DIO1 GPIO8.
*
* Axes as on the MPU-6050 and the frame: x forward, y left, z up. Roll > 0: right side down;
* pitch > 0: nose down; yaw > 0: counter-clockwise seen from above. Motors (Crazyflie order):
* M1 front-right CW, M2 back-right CCW, M3 back-left CW, M4 front-left CCW.
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/spinlock.h>
#include <math.h>
#include <stdio.h>
#define SX_BUSY_PIN 9
#define SX_DIO1_PIN 8
#include "sx1280.h"
#include "rc_link.h"
#define MPU 0x68
#define LOOP_US 4000 /* 250 Hz */
#define DEG (180.0f / 3.14159265f)
#define FAILSAFE_MS 500
static const struct pwm_dt_spec motor[4] = {
PWM_DT_SPEC_GET(DT_NODELABEL(m1)), PWM_DT_SPEC_GET(DT_NODELABEL(m2)),
PWM_DT_SPEC_GET(DT_NODELABEL(m3)), PWM_DT_SPEC_GET(DT_NODELABEL(m4)),
};
static const struct device *i2c = DEVICE_DT_GET(DT_NODELABEL(i2c0));
/* gains (per degree / degree per second; motor command 0..1), as firmware/esp32c3/quad */
#define ANGLE_P 6.0f
#define RATE_P 0.0012f
#define RATE_I 0.0010f
#define RATE_D 0.00002f
#define YAW_P 0.0020f
#define MAX_ANGLE 25.0f
#define MAX_YAW 120.0f
enum { DISARMED, ARMED, FAILSAFE };
static const char *const STATE[] = { "disarmed", "armed", "failsafe" };
/* ---- what the radio thread received (guarded by lock) ---------------------------------------- */
static struct k_spinlock lock;
static struct {
uint16_t ch[4];
uint8_t flags;
int64_t at; /* ms of the last valid packet, 0 = none yet */
int rssi, snr, lq;
uint32_t ok, bad;
} rx;
static volatile int state = DISARMED;
static void radio_thread(void)
{
uint8_t hist[100] = { 0 }; /* per sequence slot: 1 = arrived */
int hn = 0, last_seq = -1;
sx_rx(0xFFFF);
for (;;) {
uint16_t f = sx_wait_irq(K_MSEC(100));
if (!f) continue;
if (f & SX_IRQ_TX_DONE) { sx_rx(0xFFFF); continue; } /* telemetry sent: listen again */
if (!(f & SX_IRQ_RX_DONE)) { sx_rx(0xFFFF); continue; }
uint8_t p[RC_LEN];
int rssi, snr;
sx_read_buf(p, RC_LEN);
sx_packet_status(&rssi, &snr);
if ((f & SX_IRQ_CRC_ERROR) || !rc_valid(p)) {
k_spinlock_key_t k = k_spin_lock(&lock); rx.bad++; k_spin_unlock(&lock, k);
continue;
}
/* link quality: the sequence numbers missed since the last packet count as lost (the remote
* counts only packets it sends: its telemetry slots leave no gap) */
int seq = p[0];
int gap = last_seq < 0 ? 1 : (uint8_t)(seq - last_seq);
if (gap > 100) gap = 100;
for (int i = 1; i < gap; i++) { hist[hn] = 0; hn = (hn + 1) % 100; }
hist[hn] = 1; hn = (hn + 1) % 100;
last_seq = seq;
int lq = 0;
for (int i = 0; i < 100; i++) lq += hist[i];
k_spinlock_key_t k = k_spin_lock(&lock);
for (int i = 0; i < 4; i++) rx.ch[i] = p[2 + 2 * i] | p[3 + 2 * i] << 8;
rx.flags = p[1];
rx.at = k_uptime_get();
rx.rssi = rssi; rx.snr = snr; rx.lq = lq; rx.ok++;
k_spin_unlock(&lock, k);
if (p[1] & RC_FLAG_TLM) {
uint8_t t[RC_LEN] = { (uint8_t)seq, (uint8_t)(0x80 | state), (uint8_t)(-rssi), (uint8_t)snr, (uint8_t)lq };
rc_seal(t);
sx_write_buf(t, RC_LEN);
sx_tx(0);
}
}
}
K_THREAD_STACK_DEFINE(radio_stack, 2048);
static struct k_thread radio;
static void set_motor(int k, float u)
{
if (u < 0.0f) u = 0.0f;
if (u > 1.0f) u = 1.0f;
pwm_set_pulse_dt(&motor[k], (uint32_t)(u * motor[k].period));
}Im Simulator öffnen