deckle studioOpen in the editor

Micro quadcopter with remote control (ESP32-C3, SX1280)

X-frame drone and a handheld remote: fly it with the sticks over a 2.4 GHz LoRa link.

  • ESP32-C3
  • MPU-6050
  • SX1280
  • Coreless motors
  • Joysticks
Open in the simulatorNo account needed: press Simulate and it runs.

Two emulated chips and the flight physics: on most computers this runs slower than real time.

How it works

Two boards in one project. The quadcopter: an X-shaped board with a 1S LiPo, 3.3 V LDO, ESP32-C3 with a Zephyr flight controller (source firmware/esp32c3/quad_rc), MPU-6050 gyro/accelerometer, an SX1280 2.4 GHz receiver, four AO3400A low-side drivers and four coreless motors. The remote: ESP32-C3 (firmware/esp32c3/rc_remote), SX1280, two thumb joysticks (left: throttle and yaw, right: pitch and roll), an arm switch and a status LED. The remote sends the sticks 250 times a second (LoRa SF5, 812 kHz, packets with sequence number and CRC, every 8th slot answered with telemetry); the quad flies what the sticks say. Arm: switch on with the throttle at the bottom. Without a valid packet for 0.5 s the quad levels out and lowers its throttle to zero (failsafe). The packets go through the simulated air: carry the remote far away in the World and the link breaks; the remote LED blinks fast when the quad no longer answers. In the World view, drag the sticks of the remote with the mouse, or map a game controller. The quad is the flying body: its mass and inertia come from the board and its parts.

Wiring

Each line is one connection (net) and the pins it joins.

NetPins
GNDBT1 −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
3V3C2 1C3 1C4 1C9 1R1 1R2 1R11 1R12 1U1 3V3U1 ENU2 VDDU2 VLOGICU3 OUTU4 VBATU4 VBAT_IO
VBATBT1 +C1 1D1 KD2 KD3 KD4 KM1 +M2 +M3 +M4 +U3 INU3 EN
CPOUTC6 1U2 CPOUT
D1D1 AM1 −Q1 D
D2D2 AM2 −Q2 D
D3D3 AM3 −Q3 D
D4D4 AM4 −Q4 D
G1Q1 GR3 2R7 1
G2Q2 GR4 2R8 1
G3Q3 GR5 2R9 1
G4Q4 GR6 2R10 1
PWM1R3 1U1 GPIO4
PWM2R4 1U1 GPIO5
PWM3R5 1U1 GPIO6
PWM4R6 1U1 GPIO7
R_3V3C102 1C103 1C106 1JS101 VCCJS102 VCCR101 1R102 1R103 1U101 3V3U101 ENU102 VBATU102 VBAT_IOU103 OUT
R_ARMR101 2SW101 1U101 GPIO4
R_BUSYU101 GPIO18U102 BUSY
R_DIO1U101 GPIO19U102 DIO1
R_LEDD101 KU101 GPIO8
R_LEDAD101 AR102 2
R_MISOU101 GPIO5U102 MISO
R_MOSIU101 GPIO7U102 MOSI
R_NSSR103 2U101 GPIO10U102 NSS
R_PITJS102 VRYU101 GPIO2
R_RF_ANTC108 1J101 RFL102 2
R_RF_DCCL101 1U102 DCC_SW
R_RF_RFIOC107 1L102 1U102 RFIO
R_RF_VREGC104 1L101 2U102 VDD_INU102 DCC_FB
R_RF_VRPAC105 1U102 VR_PA
R_RF_XTAU102 XTAY101 1
R_RF_XTBU102 XTBY101 2
R_ROLJS102 VRXU101 GPIO3
R_SCKU101 GPIO6U102 SCK
R_THRJS101 VRYU101 GPIO0
R_VBATBT101 +C101 1U103 INU103 EN
R_YAWJS101 VRXU101 GPIO1
REGOUTC5 1U2 REGOUT
RF_ANTC11 1J1 RFL2 2

14 more connections in the editor.

D101
LED

Parts

Firmware

src/main.c · 247 lines · the first 120 here, all of it in the 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));
}
Open in the simulator

More examples: Drones and radio