Skip to content

Commit 5a81653

Browse files
committed
English language only
1 parent dab9101 commit 5a81653

File tree

14 files changed

+348
-2301
lines changed

14 files changed

+348
-2301
lines changed

.claude/settings.local.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
"Bash(timeout 30 pio test -e native --filter test_pid_rate)",
2020
"Bash(timeout 30 pio test -e native --filter test_mixer_quadx -vv)",
2121
"Bash(find:*)",
22-
"Bash(ln:*)"
22+
"Bash(ln:*)",
23+
"Bash(timeout 30 pio test -e native)",
24+
"Bash(timeout 15 pio test -e native --filter test_safety_arming -vv)",
25+
"Bash(timeout 30 pio test -e native --filter test_safety_arming)"
2326
],
2427
"deny": [],
2528
"ask": []

drone_refactoring.md

Lines changed: 128 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,136 @@
1-
# Refaktoryzacja Firmware Drona - Od Arduino Sketch do Modularnej Architektury
1+
# Drone Firmware Refactoring - From Arduino Sketch to Modular Architecture
22

3-
## 🎯 Cel Projektu
3+
## 🎯 Project Goal
44

5-
Przekształcenie monolitycznego sketch'a Arduino (437 linii w jednym pliku) w profesjonalną, modularną architekturę oprogramowania dla mikro-quadcopter'a opartego na **XIAO ESP32-S3**.
5+
Transforming a monolithic Arduino sketch (437 lines in a single file) into professional, modular firmware architecture for a micro-quadcopter based on **XIAO ESP32-S3**.
66

77
---
88

9-
## 📄 Kod Wyjściowy - Oryginalny Arduino Sketch
9+
## 📄 Original Source Code - Arduino Sketch
1010

11-
### Charakterystyka oryginalnego kodu:
12-
- **Plik**: `rx_arduino.ino` (437 linii)
13-
- **Architektura**: Monolityczna - wszystko w jednym pliku
14-
- **Styl**: Typowy Arduino sketch z globalnymi zmiennymi
15-
- **Język**: Mieszanka polskiego i angielskiego w komentarzach
11+
### Original code characteristics:
12+
- **File**: `rx_arduino.ino` (437 lines)
13+
- **Architecture**: Monolithic - everything in one file
14+
- **Style**: Typical Arduino sketch with global variables
15+
- **Language**: Mix of Polish and English in comments
1616

17-
### Funkcjonalności implementowane w oryginalnym kodzie:
17+
### Functionalities implemented in original code:
1818

19-
#### 🛩️ **System Sterowania**
20-
- **Tryby kontroli**:
21-
- ANGLE mode dla roll/pitch (kaskadowy PID: kątprędkość kątowa)
22-
- RATE mode dla yaw (bezpośredni PID prędkości kątowej)
23-
- **Filtry**: Komplementarny filtr do estymacji kątów (98% gyro + 2% accel)
24-
- **PID z dodatkami**:
25-
- Warunkowa integracja dla osi YAW (anti-windup)
26-
- Ograniczenia wyjścia i członu całkującego
19+
#### 🛩️ **Control System**
20+
- **Control modes**:
21+
- ANGLE mode for roll/pitch (cascaded PID: angleangular rate)
22+
- RATE mode for yaw (direct angular rate PID)
23+
- **Filters**: Complementary filter for angle estimation (98% gyro + 2% accel)
24+
- **PID with additions**:
25+
- Conditional integration for YAW axis (anti-windup)
26+
- Output and integral term limiting
2727
- Throttle-gated stabilization
2828

29-
#### 🔗 **Komunikacja**
30-
- **ESP-NOW**: Odbiornik radiowy (receive-only, kanał 1)
31-
- **Walidacja pakietów**: Magic byte + wersja + CRC-16/X.25
32-
- **Struktura pakietu**: Throttle (0-1000), Roll/Pitch/Yaw (±1000), flagi ARM
29+
#### 🔗 **Communication**
30+
- **ESP-NOW**: Radio receiver (receive-only, channel 1)
31+
- **Packet validation**: Magic byte + version + CRC-16/X.25
32+
- **Packet structure**: Throttle (0-1000), Roll/Pitch/Yaw (±1000), ARM flags
3333

34-
#### ⚙️ **Silniki i Hardware**
35-
- **4x silniki bezszczotkowe**: Sterowanie PWM przez LEDC (12kHz, 10-bit)
34+
#### ⚙️ **Motors and Hardware**
35+
- **4x brushless motors**: PWM control via LEDC (12kHz, 10-bit)
3636
- **X-frame mixer**: M1=CW, M2=CCW, M3=CW, M4=CCW
37-
- **Funkcje zaawansowane**:
38-
- Slew limiting (ograniczenie szybkości zmian PWM)
39-
- Idle floor (minimalna wartość PWM gdy uzbrojony)
40-
- Anti-clip recenter (zachowanie proporcji sterowania)
41-
- Motor scaling (kompensacja asymetrii CW/CCW)
42-
43-
#### 🧭 **Sensory**
44-
- **MPU6050**: Żyroskop ±500dps, akcelerometr ±2g, DLPF ~42Hz
45-
- **Kalibracja**: 500-próbkowa kalibracja bias'u żyroskopu przy starcie
46-
- **Mapowanie osi**: Konfigurowalne makra dla orientacji IMU
47-
48-
#### 🛡️ **Bezpieczeństwo**
49-
- **Failsafe**: 200ms timeout → rozbrojon + stop silników
50-
- **Arming logic**: Soft ramp przez 0.5s, wymaga link + ARM flag
51-
- **PID reset**: Przy rozbrojeniu reset wszystkich całek PID
37+
- **Advanced features**:
38+
- Slew limiting (PWM rate of change limiting)
39+
- Idle floor (minimum PWM value when armed)
40+
- Anti-clip recenter (preserving control proportions)
41+
- Motor scaling (CW/CCW asymmetry compensation)
42+
43+
#### 🧭 **Sensors**
44+
- **MPU6050**: Gyroscope ±500dps, accelerometer ±2g, DLPF ~42Hz
45+
- **Calibration**: 500-sample gyroscope bias calibration at startup
46+
- **Axis mapping**: Configurable macros for IMU orientation
47+
48+
#### 🛡️ **Safety**
49+
- **Failsafe**: 200ms timeout → disarm + motor stop
50+
- **Arming logic**: Soft ramp over 0.5s, requires link + ARM flag
51+
- **PID reset**: On disarm, reset all PID integrals
5252

5353
---
5454

55-
## 🏗️ Rezultat Refaktoryzacji - Modularna Architektura
55+
## 🏗️ Refactoring Result - Modular Architecture
5656

57-
### Struktura Projektu
57+
### Project Structure
5858
```
59-
drone-firmware/ # 🏠 Root projektu
60-
├── platformio.ini # ⚙️ Konfiguracja PlatformIO
59+
drone-firmware/ # 🏠 Project root
60+
├── platformio.ini # ⚙️ PlatformIO configuration
6161
├── .gitignore # 🚫 Git exclusions
62-
├── rx_arduino.ino # 📄 Oryginalny kod (referencja)
63-
└── firmware/ # 💾 Nowy modularny firmware
64-
├── include/ # 📂 Pliki nagłówkowe (13 plików)
65-
│ ├── config.h # 🔧 Wszystkie stałe i konfiguracja
66-
│ ├── types.h # 📋 Struktury danych i typy
67-
│ ├── state.h # 🔄 Globalna pamięć volatile
68-
│ ├── rc_packet.h # 📡 Format pakietu RC + CRC
69-
│ ├── radio.h # 📻 Interface ESP-NOW
70-
│ ├── imu.h # 🧭 Interface MPU6050
71-
│ ├── attitude.h # 📐 Filtr komplementarny
72-
│ ├── pid.h # 🎛️ Kontrolery PID
62+
├── rx_arduino.ino # 📄 Original code (reference)
63+
└── firmware/ # 💾 New modular firmware
64+
├── include/ # 📂 Header files (13 files)
65+
│ ├── config.h # 🔧 All constants and configuration
66+
│ ├── types.h # 📋 Data structures and types
67+
│ ├── state.h # 🔄 Global volatile memory
68+
│ ├── rc_packet.h # 📡 RC packet format + CRC
69+
│ ├── radio.h # 📻 ESP-NOW interface
70+
│ ├── imu.h # 🧭 MPU6050 interface
71+
│ ├── attitude.h # 📐 Complementary filter
72+
│ ├── pid.h # 🎛️ PID controllers
7373
│ ├── mixer.h # 🌀 X-frame mixer + anti-clip
7474
│ ├── motors.h # ⚡ LEDC PWM + slew limiting
75-
│ ├── safety.h # 🛡️ Arming i failsafe
76-
│ ├── filters.h # 📊 LPF1 i funkcje pomocnicze
77-
│ └── utils.h # 🔧 Timing i matematyka
78-
└── src/ # 💻 Implementacje (10 plików)
79-
├── main.cpp # 🚀 Główna pętla aplikacji
80-
├── rc_packet.cpp # 📡 Walidacja pakietów + CRC
75+
│ ├── safety.h # 🛡️ Arming and failsafe
76+
│ ├── filters.h # 📊 LPF1 and helper functions
77+
│ └── utils.h # 🔧 Timing and math
78+
└── src/ # 💻 Implementations (10 files)
79+
├── main.cpp # 🚀 Main application loop
80+
├── rc_packet.cpp # 📡 Packet validation + CRC
8181
├── radio.cpp # 📻 ESP-NOW implementation
82-
├── imu.cpp # 🧭 Driver MPU6050
83-
├── attitude.cpp # 📐 Estymacja kątów
84-
├── mixer.cpp # 🌀 Miksowanie silników
85-
├── motors.cpp # ⚡ Kontrola wyjść PWM
86-
├── safety.cpp # 🛡️ Logika bezpieczeństwa
87-
├── utils.cpp # 🔧 Funkcje pomocnicze
88-
└── state.cpp # 🔄 Definicje globalnych zmiennych
82+
├── imu.cpp # 🧭 MPU6050 driver
83+
├── attitude.cpp # 📐 Angle estimation
84+
├── mixer.cpp # 🌀 Motor mixing
85+
├── motors.cpp # ⚡ PWM output control
86+
├── safety.cpp # 🛡️ Safety logic
87+
├── utils.cpp # 🔧 Helper functions
88+
└── state.cpp # 🔄 Global variable definitions
8989
```
9090

9191
---
9292

93-
## 🔄 Kluczowe Usprawnienia
93+
## 🔄 Key Improvements
9494

95-
### 1. **Separacja Odpowiedzialności**
95+
### 1. **Separation of Concerns**
9696
```cpp
97-
// PRZED: Wszystko w jednym pliku
97+
// BEFORE: Everything in one file
9898
void loop() {
99-
// 200+ linii mixed funkcjonalności
99+
// 200+ lines of mixed functionality
100100
}
101101

102-
// PO: Czyste interfejsy modułów
102+
// AFTER: Clean module interfaces
103103
safety_update_arming(linkAlive, armFlag, armState, dt);
104104
imu_read(imuData);
105105
attitude_update(rollRate, pitchRate, ax, ay, az, dt, angles);
106106
mixer_full(base, outRoll, outPitch, outYaw, motorScale, mixOut);
107107
```
108108
109-
### 2. **Centralna Konfiguracja**
109+
### 2. **Centralized Configuration**
110110
```cpp
111-
// config.h - Wszystkie parametry w jednym miejscu
111+
// config.h - All parameters in one place
112112
#define ANG_MAX_DEG 30.0f
113113
#define PID_ANGLE_ROLL_KP 3.0f
114114
#define DUTY_MIN_IDLE 80
115115
#define LINK_TIMEOUT_MS 200
116116
```
117117

118-
### 3. **Bezpieczeństwo Typów**
118+
### 3. **Type Safety**
119119
```cpp
120-
// Struktury zamiast surowych zmiennych
120+
// Structures instead of raw variables
121121
struct ImuData {
122-
float gx_dps, gy_dps, gz_dps; // Jasne jednostki
122+
float gx_dps, gy_dps, gz_dps; // Clear units
123123
float ax_g, ay_g, az_g;
124124
};
125125

126126
struct MixOut {
127-
float m1, m2, m3, m4; // Enkapsulacja wyjść
127+
float m1, m2, m3, m4; // Output encapsulation
128128
};
129129
```
130130
131-
### 4. **Obsługa Błędów**
131+
### 4. **Error Handling**
132132
```cpp
133-
// Sprawdzanie inicjalizacji modułów
133+
// Module initialization checking
134134
if (!imu_begin()) {
135135
Serial.println("IMU initialization failed!");
136136
}
@@ -141,7 +141,7 @@ if (!radio_begin()) {
141141

142142
---
143143

144-
## ⚙️ Konfiguracja Buildu
144+
## ⚙️ Build Configuration
145145

146146
### PlatformIO Setup
147147
```ini
@@ -150,87 +150,87 @@ platform = espressif32@^6.4.0
150150
board = seeed_xiao_esp32s3
151151
framework = arduino
152152

153-
lib_deps =
153+
lib_deps =
154154
adafruit/Adafruit MPU6050@^2.2.4
155155
adafruit/Adafruit Unified Sensor@^1.1.14
156156

157-
build_flags =
157+
build_flags =
158158
-std=gnu++17
159159
-O2
160160
-DARDUINO_XIAO_ESP32S3
161161
```
162162

163-
### Komendy Buildu
163+
### Build Commands
164164
```bash
165-
pio run # Kompilacja
166-
pio run -t upload # Upload na ESP32
165+
pio run # Compile
166+
pio run -t upload # Upload to ESP32
167167
pio device monitor # Serial monitor
168168
```
169169

170170
---
171171

172-
## 🎯 Zachowane Funkcjonalności
172+
## 🎯 Preserved Functionalities
173173

174-
### **100% Zachowanie Lotu**
175-
- **Identyczne znaki mixera**: M1=+yaw, M2=-yaw, M3=+yaw, M4=-yaw
176-
- **Te same parametry PID**: Kp, Ki, Kd bez zmian
177-
- **Timing bez zmian**: ARM ramp, throttle gating, slew rates
178-
- **Oryginalny format pakietu**: Kompatybilność z nadajnikiem
174+
### **100% Flight Preservation**
175+
- **Identical mixer signs**: M1=+yaw, M2=-yaw, M3=+yaw, M4=-yaw
176+
- **Same PID parameters**: Kp, Ki, Kd unchanged
177+
- **Timing unchanged**: ARM ramp, throttle gating, slew rates
178+
- **Original packet format**: Compatibility with transmitter
179179

180-
### **Wszystkie Algorytmy**
181-
- **Komplementarny filtr**: CF_ALPHA = 0.98
182-
- **Warunkowa integracja**: YAW PID z anti-windup
183-
- **Anti-clip mixer**: Zachowanie proporcji sterowania
184-
- **PITCH↔ROLL swap**: Kompatybilność z existującym tuningiem
180+
### **All Algorithms**
181+
- **Complementary filter**: CF_ALPHA = 0.98
182+
- **Conditional integration**: YAW PID with anti-windup
183+
- **Anti-clip mixer**: Preserving control proportions
184+
- **PITCH↔ROLL swap**: Compatibility with existing tuning
185185

186186
### **Hardware Interface**
187-
- **Pinout identyczny**: GPIO 7,4,3,1 dla silników
187+
- **Identical pinout**: GPIO 7,4,3,1 for motors
188188
- **PWM parameters**: 12kHz, 10-bit, LOW_SPEED mode
189-
- **I2C konfiguracja**: SDA=GPIO6, SCL=GPIO5, 400kHz
190-
- **ESP-NOW settings**: Kanał 1, receive-only
189+
- **I2C configuration**: SDA=GPIO6, SCL=GPIO5, 400kHz
190+
- **ESP-NOW settings**: Channel 1, receive-only
191191

192192
---
193193

194-
## 📊 Statystyki Refaktoryzacji
194+
## 📊 Refactoring Statistics
195195

196-
| Aspekt | Przed | Po | Poprawa |
197-
|--------|-------|----|---------|
198-
| **Pliki** | 1 plik (437 linii) | 23 pliki (organizacja modularna) | 📈 Struktura |
199-
| **Funkcje** | ~20 funkcji globalnych | 50+ funkcji w namespace'ach | 📈 Organizacja |
200-
| **Konfiguracja** | Rozproszone #define | Centralne config.h | 📈 Łatwość tuning |
201-
| **Testowanie** | Monolityczne | Moduły testowalne osobno | 📈 QA |
202-
| **Rozszerzalność** | Trudne dodawanie funkcji | Czyste interfejsy | 📈 Development |
203-
| **Bezpieczeństwo** | Surowe typy | Enkapsulowane struktury | 📈 Type Safety |
196+
| Aspect | Before | After | Improvement |
197+
|--------|-------|----|---------|
198+
| **Files** | 1 file (437 lines) | 23 files (modular organization) | 📈 Structure |
199+
| **Functions** | ~20 global functions | 50+ functions in namespaces | 📈 Organization |
200+
| **Configuration** | Scattered #define | Centralized config.h | 📈 Tuning ease |
201+
| **Testing** | Monolithic | Independently testable modules | 📈 QA |
202+
| **Extensibility** | Hard to add features | Clean interfaces | 📈 Development |
203+
| **Safety** | Raw types | Encapsulated structures | 📈 Type Safety |
204204

205205
---
206206

207-
## 🚀 Korzyści dla Rozwoju
207+
## 🚀 Development Benefits
208208

209-
### 1. **Łatwość Modyfikacji**
210-
- Zmiany PID → tylko `config.h`
211-
- Nowy sensor → nowy moduł w `include/` + `src/`
212-
- Debug → jasne interfejsy modułów
209+
### 1. **Ease of Modification**
210+
- PID changes → only `config.h`
211+
- New sensor → new module in `include/` + `src/`
212+
- Debug → clear module interfaces
213213

214-
### 2. **Współpraca Zespołowa**
215-
- Różne osoby mogą pracować nad różnymi modułami
216-
- Jasne API między modułami
217-
- Git-friendly (małe, logiczne commity)
214+
### 2. **Team Collaboration**
215+
- Different people can work on different modules
216+
- Clear API between modules
217+
- Git-friendly (small, logical commits)
218218

219-
### 3. **Jakość Kodu**
220-
- Kompilacja z `-std=gnu++17`
221-
- Type safety i const correctness
219+
### 3. **Code Quality**
220+
- Compilation with `-std=gnu++17`
221+
- Type safety and const correctness
222222
- Separation of concerns
223223

224-
### 4. **Przyszłe Rozszerzenia**
225-
- **Telemetria**: Gotowe miejsce w architekturze
226-
- **VBAT monitoring**: Stub już przygotowany
227-
- **Multiple flight modes**: Łatwe do dodania
224+
### 4. **Future Extensions**
225+
- **Telemetry**: Ready place in architecture
226+
- **VBAT monitoring**: Stub already prepared
227+
- **Multiple flight modes**: Easy to add
228228
- **Parameter tuning**: Runtime PID adjustment
229229

230230
---
231231

232-
## 🎖️ Rezultat
232+
## 🎖️ Result
233233

234-
**Transformacja z hobby Arduino sketch do professional-grade embedded firmware** zachowując 100% funkcjonalności lotu przy dramatycznym zwiększeniu maintainability, testability i extensibility.
234+
**Transformation from hobby Arduino sketch to professional-grade embedded firmware** while preserving 100% flight functionality with dramatic increase in maintainability, testability, and extensibility.
235235

236-
Projekt gotowy do kontynuacji rozwoju, team collaboration i production deployment! 🚁✨
236+
Project ready for continued development, team collaboration, and production deployment! 🚁✨

0 commit comments

Comments
 (0)