[{"data":1,"prerenderedAt":462},["ShallowReactive",2],{"example-page-quadcopter":3},{"id":4,"info":5,"parts":6,"wired":47,"refs":48,"nets":49,"moreNets":455,"code":456},"quadcopter","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\u002Fesp32c3\u002Fquad_rc), MPU-6050 gyro\u002Faccelerometer, an SX1280 2.4 GHz receiver, four AO3400A low-side drivers and four coreless motors. The remote: ESP32-C3 (firmware\u002Fesp32c3\u002Frc_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.",[7,10,13,16,19,21,23,25,28,30,32,34,36,38,40,43,45],[8,9],"capacitor",19,[11,12],"resistor",15,[14,15],"mounting_hole",8,[17,18],"coreless_7x16",4,[20,18],"diode",[22,18],"inductor",[24,18],"nmos",[26,27],"crystal",2,[29,27],"esp32c3",[31,27],"joystick",[33,27],"ldo",[35,27],"liion_cell",[37,27],"sx1280",[39,27],"ufl_antenna",[41,42],"led",1,[44,42],"mpu6050_qfn",[46,42],"toggle_spdt",false,{"BT1":35,"BT101":35,"C1":8,"C2":8,"C3":8,"C4":8,"C5":8,"C6":8,"C7":8,"C8":8,"C9":8,"C10":8,"C11":8,"C101":8,"C102":8,"C103":8,"C104":8,"C105":8,"C106":8,"C107":8,"C108":8,"J1":39,"J101":39,"JS101":31,"JS102":31,"Q1":24,"Q2":24,"Q3":24,"Q4":24,"R7":11,"R8":11,"R9":11,"R10":11,"SW101":46,"U1":29,"U2":44,"U3":33,"U4":37,"U101":29,"U102":37,"U103":33,"Y1":26,"Y101":26,"R1":11,"R2":11,"R11":11,"R12":11,"D1":20,"D2":20,"D3":20,"D4":20,"M1":17,"M2":17,"M3":17,"M4":17,"R3":11,"R4":11,"R5":11,"R6":11,"R101":11,"R102":11,"R103":11,"D101":41,"L102":22,"L101":22,"L2":22},[50,149,178,203,208,215,220,225,230,238,245,252,259,265,271,277,283,303,309,316,323,330,335,341,347,355,362,370,377,384,393,399,405,411,418,424,430,437,443,448],{"name":51,"pins":52},"GND",[53,56,58,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,101,103,105,108,110,112,114,116,118,120,122,125,127,129,131,133,135,137,139,141,143,145,147],[54,55],"BT1","−",[57,55],"BT101",[59,60],"C1","2",[62,60],"C2",[64,60],"C3",[66,60],"C4",[68,60],"C5",[70,60],"C6",[72,60],"C7",[74,60],"C8",[76,60],"C9",[78,60],"C10",[80,60],"C11",[82,60],"C101",[84,60],"C102",[86,60],"C103",[88,60],"C104",[90,60],"C105",[92,60],"C106",[94,60],"C107",[96,60],"C108",[98,51],"J1",[100,51],"J101",[102,51],"JS101",[104,51],"JS102",[106,107],"Q1","S",[109,107],"Q2",[111,107],"Q3",[113,107],"Q4",[115,60],"R7",[117,60],"R8",[119,60],"R9",[121,60],"R10",[123,124],"SW101","COM",[126,51],"U1",[128,51],"U2",[128,130],"AD0",[128,132],"CLKIN",[128,134],"FSYNC",[136,51],"U3",[138,51],"U4",[140,51],"U101",[142,51],"U102",[144,51],"U103",[146,51],"Y1",[148,51],"Y101",{"name":150,"pins":151},"3V3",[152,154,155,156,157,159,161,163,165,166,168,170,172,174,176],[62,153],"1",[64,153],[66,153],[76,153],[158,153],"R1",[160,153],"R2",[162,153],"R11",[164,153],"R12",[126,150],[126,167],"EN",[128,169],"VDD",[128,171],"VLOGIC",[136,173],"OUT",[138,175],"VBAT",[138,177],"VBAT_IO",{"name":175,"pins":179},[180,182,183,186,188,190,192,194,196,198,200,202],[54,181],"+",[59,153],[184,185],"D1","K",[187,185],"D2",[189,185],"D3",[191,185],"D4",[193,181],"M1",[195,181],"M2",[197,181],"M3",[199,181],"M4",[136,201],"IN",[136,167],{"name":204,"pins":205},"CPOUT",[206,207],[70,153],[128,204],{"name":184,"pins":209},[210,212,213],[184,211],"A",[193,55],[106,214],"D",{"name":187,"pins":216},[217,218,219],[187,211],[195,55],[109,214],{"name":189,"pins":221},[222,223,224],[189,211],[197,55],[111,214],{"name":191,"pins":226},[227,228,229],[191,211],[199,55],[113,214],{"name":231,"pins":232},"G1",[233,235,237],[106,234],"G",[236,60],"R3",[115,153],{"name":239,"pins":240},"G2",[241,242,244],[109,234],[243,60],"R4",[117,153],{"name":246,"pins":247},"G3",[248,249,251],[111,234],[250,60],"R5",[119,153],{"name":253,"pins":254},"G4",[255,256,258],[113,234],[257,60],"R6",[121,153],{"name":260,"pins":261},"PWM1",[262,263],[236,153],[126,264],"GPIO4",{"name":266,"pins":267},"PWM2",[268,269],[243,153],[126,270],"GPIO5",{"name":272,"pins":273},"PWM3",[274,275],[250,153],[126,276],"GPIO6",{"name":278,"pins":279},"PWM4",[280,281],[257,153],[126,282],"GPIO7",{"name":284,"pins":285},"R_3V3",[286,287,288,289,291,292,294,296,298,299,300,301,302],[84,153],[86,153],[92,153],[102,290],"VCC",[104,290],[293,153],"R101",[295,153],"R102",[297,153],"R103",[140,150],[140,167],[142,175],[142,177],[144,173],{"name":304,"pins":305},"R_ARM",[306,307,308],[293,60],[123,153],[140,264],{"name":310,"pins":311},"R_BUSY",[312,314],[140,313],"GPIO18",[142,315],"BUSY",{"name":317,"pins":318},"R_DIO1",[319,321],[140,320],"GPIO19",[142,322],"DIO1",{"name":324,"pins":325},"R_LED",[326,328],[327,185],"D101",[140,329],"GPIO8",{"name":331,"pins":332},"R_LEDA",[333,334],[327,211],[295,60],{"name":336,"pins":337},"R_MISO",[338,339],[140,270],[142,340],"MISO",{"name":342,"pins":343},"R_MOSI",[344,345],[140,282],[142,346],"MOSI",{"name":348,"pins":349},"R_NSS",[350,351,353],[297,60],[140,352],"GPIO10",[142,354],"NSS",{"name":356,"pins":357},"R_PIT",[358,360],[104,359],"VRY",[140,361],"GPIO2",{"name":363,"pins":364},"R_RF_ANT",[365,366,368],[96,153],[100,367],"RF",[369,60],"L102",{"name":371,"pins":372},"R_RF_DCC",[373,375],[374,153],"L101",[142,376],"DCC_SW",{"name":378,"pins":379},"R_RF_RFIO",[380,381,382],[94,153],[369,153],[142,383],"RFIO",{"name":385,"pins":386},"R_RF_VREG",[387,388,389,391],[88,153],[374,60],[142,390],"VDD_IN",[142,392],"DCC_FB",{"name":394,"pins":395},"R_RF_VRPA",[396,397],[90,153],[142,398],"VR_PA",{"name":400,"pins":401},"R_RF_XTA",[402,404],[142,403],"XTA",[148,153],{"name":406,"pins":407},"R_RF_XTB",[408,410],[142,409],"XTB",[148,60],{"name":412,"pins":413},"R_ROL",[414,416],[104,415],"VRX",[140,417],"GPIO3",{"name":419,"pins":420},"R_SCK",[421,422],[140,276],[142,423],"SCK",{"name":425,"pins":426},"R_THR",[427,428],[102,359],[140,429],"GPIO0",{"name":431,"pins":432},"R_VBAT",[433,434,435,436],[57,181],[82,153],[144,201],[144,167],{"name":438,"pins":439},"R_YAW",[440,441],[102,415],[140,442],"GPIO1",{"name":444,"pins":445},"REGOUT",[446,447],[68,153],[128,444],{"name":449,"pins":450},"RF_ANT",[451,452,453],[80,153],[98,367],[454,60],"L2",14,{"file":457,"lang":458,"text":459,"lines":460,"more":461},"src\u002Fmain.c","c","\u002F* Micro quadcopter flight controller with an RC receiver (ESP32-C3 + SX1280, Zephyr).\n *\n * The SX1280 listens for the remote's packets (rc_link.h: four channels, arm flag, sequence,\n * CRC, 250 Hz); a radio thread takes each packet on DIO1, checks it, keeps the channels, the\n * RSSI \u002F SNR of the packet status and the link quality (share of the last 100 packets that\n * arrived), and answers a telemetry request at once. The control loop (250 Hz, as\n * firmware\u002Fesp32c3\u002Fquad) flies what the sticks say:\n *   throttle stick -> collective thrust 0..100 % PWM (no fixed hover value: the pilot hovers)\n *   pitch \u002F roll sticks -> attitude set point ±25°, yaw stick -> yaw rate ±120 °\u002Fs\n * Arming: the arm switch on and the throttle stick at the bottom (\u003C 5 %); the switch off\n * disarms. Failsafe: no valid packet for 500 ms -> level, the throttle falls from where it was to\n * zero within 1 s, then disarmed; flying again needs the switch and low throttle, as at the start.\n *\n * Pins: MPU-6050 on I2C0 (SDA GPIO1, SCL GPIO3), motors GPIO4..7 (LEDC PWM 1 kHz), SX1280 on\n * GP-SPI2 (SCLK GPIO0, MOSI GPIO18, MISO GPIO19, NSS GPIO10), BUSY GPIO9, DIO1 GPIO8.\n *\n * Axes as on the MPU-6050 and the frame: x forward, y left, z up. Roll > 0: right side down;\n * pitch > 0: nose down; yaw > 0: counter-clockwise seen from above. Motors (Crazyflie order):\n * M1 front-right CW, M2 back-right CCW, M3 back-left CW, M4 front-left CCW.\n *\u002F\n#include \u003Czephyr\u002Fkernel.h>\n#include \u003Czephyr\u002Fdevice.h>\n#include \u003Czephyr\u002Fdrivers\u002Fi2c.h>\n#include \u003Czephyr\u002Fdrivers\u002Fpwm.h>\n#include \u003Czephyr\u002Fspinlock.h>\n#include \u003Cmath.h>\n#include \u003Cstdio.h>\n\n#define SX_BUSY_PIN 9\n#define SX_DIO1_PIN 8\n#include \"sx1280.h\"\n#include \"rc_link.h\"\n\n#define MPU 0x68\n#define LOOP_US 4000            \u002F* 250 Hz *\u002F\n#define DEG (180.0f \u002F 3.14159265f)\n#define FAILSAFE_MS 500\n\nstatic const struct pwm_dt_spec motor[4] = {\n\tPWM_DT_SPEC_GET(DT_NODELABEL(m1)), PWM_DT_SPEC_GET(DT_NODELABEL(m2)),\n\tPWM_DT_SPEC_GET(DT_NODELABEL(m3)), PWM_DT_SPEC_GET(DT_NODELABEL(m4)),\n};\nstatic const struct device *i2c = DEVICE_DT_GET(DT_NODELABEL(i2c0));\n\n\u002F* gains (per degree \u002F degree per second; motor command 0..1), as firmware\u002Fesp32c3\u002Fquad *\u002F\n#define ANGLE_P   6.0f\n#define RATE_P    0.0012f\n#define RATE_I    0.0010f\n#define RATE_D    0.00002f\n#define YAW_P     0.0020f\n#define MAX_ANGLE 25.0f\n#define MAX_YAW   120.0f\n\nenum { DISARMED, ARMED, FAILSAFE };\nstatic const char *const STATE[] = { \"disarmed\", \"armed\", \"failsafe\" };\n\n\u002F* ---- what the radio thread received (guarded by lock) ---------------------------------------- *\u002F\nstatic struct k_spinlock lock;\nstatic struct {\n\tuint16_t ch[4];\n\tuint8_t flags;\n\tint64_t at;          \u002F* ms of the last valid packet, 0 = none yet *\u002F\n\tint rssi, snr, lq;\n\tuint32_t ok, bad;\n} rx;\nstatic volatile int state = DISARMED;\n\nstatic void radio_thread(void)\n{\n\tuint8_t hist[100] = { 0 };   \u002F* per sequence slot: 1 = arrived *\u002F\n\tint hn = 0, last_seq = -1;\n\tsx_rx(0xFFFF);\n\tfor (;;) {\n\t\tuint16_t f = sx_wait_irq(K_MSEC(100));\n\t\tif (!f) continue;\n\t\tif (f & SX_IRQ_TX_DONE) { sx_rx(0xFFFF); continue; }   \u002F* telemetry sent: listen again *\u002F\n\t\tif (!(f & SX_IRQ_RX_DONE)) { sx_rx(0xFFFF); continue; }\n\t\tuint8_t p[RC_LEN];\n\t\tint rssi, snr;\n\t\tsx_read_buf(p, RC_LEN);\n\t\tsx_packet_status(&rssi, &snr);\n\t\tif ((f & SX_IRQ_CRC_ERROR) || !rc_valid(p)) {\n\t\t\tk_spinlock_key_t k = k_spin_lock(&lock); rx.bad++; k_spin_unlock(&lock, k);\n\t\t\tcontinue;\n\t\t}\n\t\t\u002F* link quality: the sequence numbers missed since the last packet count as lost (the remote\n\t\t * counts only packets it sends: its telemetry slots leave no gap) *\u002F\n\t\tint seq = p[0];\n\t\tint gap = last_seq \u003C 0 ? 1 : (uint8_t)(seq - last_seq);\n\t\tif (gap > 100) gap = 100;\n\t\tfor (int i = 1; i \u003C gap; i++) { hist[hn] = 0; hn = (hn + 1) % 100; }\n\t\thist[hn] = 1; hn = (hn + 1) % 100;\n\t\tlast_seq = seq;\n\t\tint lq = 0;\n\t\tfor (int i = 0; i \u003C 100; i++) lq += hist[i];\n\n\t\tk_spinlock_key_t k = k_spin_lock(&lock);\n\t\tfor (int i = 0; i \u003C 4; i++) rx.ch[i] = p[2 + 2 * i] | p[3 + 2 * i] \u003C\u003C 8;\n\t\trx.flags = p[1];\n\t\trx.at = k_uptime_get();\n\t\trx.rssi = rssi; rx.snr = snr; rx.lq = lq; rx.ok++;\n\t\tk_spin_unlock(&lock, k);\n\n\t\tif (p[1] & RC_FLAG_TLM) {\n\t\t\tuint8_t t[RC_LEN] = { (uint8_t)seq, (uint8_t)(0x80 | state), (uint8_t)(-rssi), (uint8_t)snr, (uint8_t)lq };\n\t\t\trc_seal(t);\n\t\t\tsx_write_buf(t, RC_LEN);\n\t\t\tsx_tx(0);\n\t\t}\n\t}\n}\nK_THREAD_STACK_DEFINE(radio_stack, 2048);\nstatic struct k_thread radio;\n\nstatic void set_motor(int k, float u)\n{\n\tif (u \u003C 0.0f) u = 0.0f;\n\tif (u > 1.0f) u = 1.0f;\n\tpwm_set_pulse_dt(&motor[k], (uint32_t)(u * motor[k].period));\n}",247,true,1796304409661]