This shows you the differences between two versions of the page.
pm:prj2025:vradulescu:robert_ion.bolfa [2025/05/25 23:34] robert_ion.bolfa [Software Design] |
pm:prj2025:vradulescu:robert_ion.bolfa [2025/05/26 00:04] (current) robert_ion.bolfa [Jurnal] |
||
---|---|---|---|
Line 51: | Line 51: | ||
| Breadboard | 1 | Conexiuni | https://ardushop.ro/ | | | Breadboard | 1 | Conexiuni | https://ardushop.ro/ | | ||
| Fire | Multe | Conexiuni | https://ardushop.ro/ | | | Fire | Multe | Conexiuni | https://ardushop.ro/ | | ||
- | = Software Design = | ||
- | == Mediu de Dezvoltare == | ||
- | === Arduino IDE 2.3.2 === | + | ===== Software Design ===== |
- | * '''Compilator''': AVR-GCC 7.3.0 pentru microcontroller ATmega328P | + | |
- | * '''Target Platform''': Arduino UNO R3 (ATmega328P @ 16MHz) | + | |
- | * '''Upload Protocol''': AVR109 (USB-to-Serial via CH340/FTDI) | + | |
- | * '''Debugging''': Serial Monitor la 9600 baud rate | + | |
- | === Configurări Compilare === | + | NOTA: Descrierea codului aplicației (firmware): |
- | <pre> | + | * mediu de dezvoltare (if any) (e.g. AVR Studio, CodeVisionAVR) |
- | // Compilator flags optimizate pentru performanță | + | * librării și surse 3rd-party (e.g. Procyon AVRlib) |
- | #pragma GCC optimize("O2") // Optimizare nivel 2 | + | * algoritmi și structuri pe care plănuiți să le implementați |
- | #define F_CPU 16000000UL // Clock 16MHz | + | * (etapa 3) surse și funcții implementate |
- | </pre> | + | |
- | ---- | + | ================================================================================ |
- | == Librării şi Surse 3rd-Party == | + | MEDIU DE DEZVOLTARE |
- | === Core Arduino Libraries === | + | Arduino IDE 2.3.2 |
- | <code> | + | - Compilator: AVR-GCC 7.3.0 pentru microcontroller ATmega328P |
- | #include <Servo.h> // v1.2.1 - Control PWM servomotoare | + | - Target Platform: Arduino UNO R3 (ATmega328P @ 16MHz) |
- | #include <SoftwareSerial.h> // v1.0 - UART software pentru DFPlayer | + | - Upload Protocol: AVR109 (USB-to-Serial via CH340/FTDI) |
- | </code> | + | - Debugging: Serial Monitor la 9600 baud rate |
- | === External Libraries === | + | Configurări Compilare: |
- | <code> | + | #pragma GCC optimize("O2") // Optimizare nivel 2 |
- | #include <DFRobotDFPlayerMini.h> // v1.0.6 - Control DFPlayer Mini | + | #define F_CPU 16000000UL // Clock 16MHz |
- | </code> | + | |
- | * '''Sursă''': DFRobot Official Library (GitHub: 2.1M downloads) | + | |
- | * '''Funcționalitate''': Control complet player audio MP3 | + | |
- | * '''Protocoale''': Serial AT commands pentru control volum/track | + | |
- | === Hardware Abstraction Layer === | + | ================================================================================ |
- | <pre> | + | |
- | // Custom defines pentru abstractizare hardware | + | |
- | #define TRIG_PIN 2 | + | |
- | #define ECHO_PIN 3 | + | |
- | #define SERVO1_PIN 9 | + | |
- | #define SERVO2_PIN 10 | + | |
- | #define DFPLAYER_RX 7 | + | |
- | #define DFPLAYER_TX 8 | + | |
- | </pre> | + | |
- | ---- | + | LIBRĂRII ȘI SURSE 3RD-PARTY |
- | == Algoritmi şi Structuri Implementate == | + | Core Arduino Libraries: |
+ | #include <Servo.h> // v1.2.1 - Control PWM servomotoare | ||
+ | #include <SoftwareSerial.h> // v1.0 - UART software pentru DFPlayer | ||
+ | |||
+ | External Libraries: | ||
+ | #include <DFRobotDFPlayerMini.h> // v1.0.6 - Control DFPlayer Mini | ||
+ | |||
+ | - Sursă: DFRobot Official Library (GitHub: 2.1M downloads) | ||
+ | - Funcționalitate: Control complet player audio MP3 | ||
+ | - Protocoale: Serial AT commands pentru control volum/track | ||
+ | |||
+ | Hardware Abstraction Layer: | ||
+ | #define TRIG_PIN 2 | ||
+ | #define ECHO_PIN 3 | ||
+ | #define SERVO1_PIN 4 | ||
+ | #define SERVO2_PIN 5 | ||
+ | #define DFPLAYER_RX 6 | ||
+ | #define DFPLAYER_TX 7 | ||
+ | |||
+ | ================================================================================ | ||
+ | |||
+ | ALGORITMI ȘI STRUCTURI IMPLEMENTATE | ||
+ | |||
+ | 1. Algoritm Măsurare Distanță (Time-of-Flight) | ||
- | === 1. Algoritm Măsurare Distanță (Time-of-Flight) === | ||
- | <pre> | ||
Algorithm: measureDistance() | Algorithm: measureDistance() | ||
Input: None (GPIO control) | Input: None (GPIO control) | ||
Line 113: | Line 114: | ||
3. Apply formula: distance = (duration * 0.034) / 2 | 3. Apply formula: distance = (duration * 0.034) / 2 | ||
4. Validate result (timeout handling) | 4. Validate result (timeout handling) | ||
- | </pre> | ||
- | === 2. State Machine pentru Proximity Detection === | + | -------------------------------------------------------------------------------- |
- | <pre> | + | |
+ | 2. State Machine pentru Proximity Detection | ||
States: {IDLE, NEAR, FAR} | States: {IDLE, NEAR, FAR} | ||
- | Transitions: distance < 30cm → NEAR | + | Transitions: |
- | distance ≥ 30cm → FAR | + | distance < 30cm → NEAR |
+ | distance ≥ 30cm → FAR | ||
| | ||
State Actions: | State Actions: | ||
- | - NEAR: servo.write(90°) + play(001.mp3) | + | - NEAR: servo.write(90°) + play(001.mp3) |
- | - FAR: servo.write(270°) + play(002.mp3) | + | - FAR: servo.write(270°) + play(002.mp3) |
- | </pre> | + | |
+ | -------------------------------------------------------------------------------- | ||
+ | |||
+ | 3. Interrupt Service Routine (ISR) | ||
- | === 3. Interrupt Service Routine (ISR) === | ||
- | <pre> | ||
Algorithm: echoInterrupt() | Algorithm: echoInterrupt() | ||
Trigger: CHANGE on Pin 3 (INT1) | Trigger: CHANGE on Pin 3 (INT1) | ||
Features: | Features: | ||
- | - Debounce filtering (200ms window) | + | - Debounce filtering (200ms window) |
- | - Volatile flag setting | + | - Volatile flag setting |
- | - Non-blocking execution (<10µs) | + | - Non-blocking execution (<10µs) |
- | </pre> | + | |
+ | -------------------------------------------------------------------------------- | ||
+ | |||
+ | 4. PWM Control Algorithm | ||
- | === 4. PWM Control Algorithm === | ||
- | <pre> | ||
Function: moveServosTo(angle) | Function: moveServosTo(angle) | ||
Input: int angle [0-359°] | Input: int angle [0-359°] | ||
Process: | Process: | ||
- | - Convert angle to PWM duty cycle | + | - Convert angle to PWM duty cycle |
- | - Simultaneous dual servo control | + | - Simultaneous dual servo control |
- | - Non-blocking execution | + | - Non-blocking execution |
- | </pre> | + | |
- | ---- | + | ================================================================================ |
- | == Structuri de Date == | + | STRUCTURI DE DATE |
- | === Global Variables === | + | Global Variables: |
- | <pre> | + | // Volatile pentru ISR communication |
- | // Volatile pentru ISR communication | + | volatile bool proximityDetected = false; |
- | volatile bool proximityDetected = false; | + | volatile unsigned long lastInterruptTime = 0; |
- | volatile unsigned long lastInterruptTime = 0; | + | |
- | // State management | + | // State management |
- | float distance = 0; | + | float distance = 0; |
- | bool isClose = false; | + | bool isClose = false; |
- | bool lastState = false; | + | bool lastState = false; |
- | // Configuration constants | + | // Configuration constants |
- | const float THRESHOLD_DISTANCE = 30.0; // cm | + | const float THRESHOLD_DISTANCE = 30.0; // cm |
- | const unsigned long DEBOUNCE_TIME = 200; // ms | + | const unsigned long DEBOUNCE_TIME = 200; // ms |
- | </pre> | + | |
- | === Object Instances === | + | Object Instances: |
- | <pre> | + | Servo servo1, servo2; // PWM control objects |
- | Servo servo1, servo2; // PWM control objects | + | SoftwareSerial dfPlayerSerial(7, 8); // UART software instance |
- | SoftwareSerial dfPlayerSerial(7, 8); // UART software instance | + | DFRobotDFPlayerMini dfPlayer; // Audio player controller |
- | DFRobotDFPlayerMini dfPlayer; // Audio player controller | + | |
- | </pre> | + | |
- | ---- | + | ================================================================================ |
- | == Surse şi Funcţii Implementate (Etapa 3) == | + | SURSE ȘI FUNCȚII IMPLEMENTATE (ETAPA 3) |
- | === Core Functions === | + | CORE FUNCTIONS: |
- | ==== 1. Setup & Initialization ==== | + | 1. Setup & Initialization |
- | <pre> | + | void setup() |
- | void setup() | + | ├── Serial.begin(9600) // Debug interface |
- | ├── Serial.begin(9600) // Debug interface | + | ├── GPIO_Config() // Pin modes pentru TRIG/ECHO |
- | ├── GPIO_Config() // Pin modes pentru TRIG/ECHO | + | ├── PWM_Config() // Servo attachment |
- | ├── PWM_Config() // Servo attachment | + | ├── Interrupt_Config() // INT1 setup pe ECHO |
- | ├── Interrupt_Config() // INT1 setup pe ECHO | + | └── Audio_Init() // DFPlayer initialization |
- | └── Audio_Init() // DFPlayer initialization | + | |
- | </pre> | + | |
- | ==== 2. Main Control Loop ==== | + | 2. Main Control Loop |
- | <pre> | + | void loop() |
- | void loop() | + | ├── distance = measureDistance() // GPIO măsurare |
- | ├── distance = measureDistance() // GPIO măsurare | + | ├── stateCheck(distance) // State machine logic |
- | ├── stateCheck(distance) // State machine logic | + | ├── actionExecution() // PWM + Audio control |
- | ├── actionExecution() // PWM + Audio control | + | └── serialDebug() // Monitoring output |
- | └── serialDebug() // Monitoring output | + | |
- | </pre> | + | |
- | ==== 3. GPIO Control Functions ==== | + | 3. GPIO Control Functions |
- | <pre> | + | float measureDistance() { |
- | float measureDistance() { | + | // Time-of-flight calculation |
- | // Time-of-flight calculation | + | // Input: GPIO pulse generation |
- | // Input: GPIO pulse generation | + | // Output: Distance în cm |
- | // Output: Distance în cm | + | // Error handling: timeout protection |
- | // Error handling: timeout protection | + | } |
- | } | + | |
- | </pre> | + | |
- | ==== 4. PWM Control Functions ==== | + | 4. PWM Control Functions |
- | <pre> | + | void moveServosTo(int angle) { |
- | void moveServosTo(int angle) { | + | // Simultaneous dual servo control |
- | // Simultaneous dual servo control | + | // PWM signal generation @ 50Hz |
- | // PWM signal generation @ 50Hz | + | // Angle range: 0-359° (cu overflow handling) |
- | // Angle range: 0-359° (cu overflow handling) | + | } |
- | } | + | |
- | void activateProximityMode() { | + | void activateProximityMode() { |
- | // Composite action: PWM + Audio | + | // Composite action: PWM + Audio |
- | // Servo rotation: 0° → 90° | + | // Servo rotation: 0° → 90° |
- | // Audio trigger: 001.mp3 | + | // Audio trigger: 001.mp3 |
- | } | + | } |
- | void deactivateProximityMode() { | + | void deactivateProximityMode() { |
- | // Composite action: PWM + Audio | + | // Composite action: PWM + Audio |
- | // Servo rotation: 90° → 270° (-90°) | + | // Servo rotation: 90° → 270° (-90°) |
- | // Audio trigger: 002.mp3 | + | // Audio trigger: 002.mp3 |
- | } | + | } |
- | </pre> | + | |
- | ==== 5. Interrupt Handling ==== | + | 5. Interrupt Handling |
- | <pre> | + | void echoInterrupt() { |
- | void echoInterrupt() { | + | // ISR pentru ECHO pin change |
- | // ISR pentru ECHO pin change | + | // Debounce protection |
- | // Debounce protection | + | // Flag setting pentru main loop |
- | // Flag setting pentru main loop | + | // Execution time: <10µs |
- | // Execution time: <10µs | + | } |
- | } | + | |
- | </pre> | + | |
- | ==== 6. Audio Control Functions ==== | + | 6. Audio Control Functions |
- | <pre> | + | bool initializeDFPlayer() { |
- | bool initializeDFPlayer() { | + | // DFPlayer initialization sequence |
- | // DFPlayer initialization sequence | + | // Volume configuration (0-30) |
- | // Volume configuration (0-30) | + | // SD card validation |
- | // SD card validation | + | // Error handling pentru connection |
- | // Error handling pentru connection | + | } |
- | } | + | |
- | </pre> | + | |
- | ==== 7. Utility Functions ==== | + | 7. Utility Functions |
- | <pre> | + | void printSystemInfo() { |
- | void printSystemInfo() { | + | // Debug information output |
- | // Debug information output | + | // Pin configuration display |
- | // Pin configuration display | + | // System status monitoring |
- | // System status monitoring | + | } |
- | } | + | |
- | bool validateDistance(float dist) { | + | bool validateDistance(float dist) { |
- | // Input validation pentru măsurători | + | // Input validation pentru măsurători |
- | // Range checking: 2-400cm | + | // Range checking: 2-400cm |
- | // Invalid reading filtering | + | // Invalid reading filtering |
- | } | + | } |
- | </pre> | + | |
- | ---- | + | ================================================================================ |
- | == Memory & Performance Metrics == | + | MEMORY & PERFORMANCE METRICS |
- | === Resource Utilization === | + | Resource Utilization: |
- | <pre> | + | Flash Memory Usage: |
- | // Flash Memory Usage | + | Program Storage: ~18,432 bytes (56.9% of 32,256 bytes) |
- | Program Storage: ~18,432 bytes (56.9% of 32,256 bytes) | + | Dynamic Memory: ~1,024 bytes (50.0% of 2,048 bytes) |
- | Dynamic Memory: ~1,024 bytes (50.0% of 2,048 bytes) | + | |
- | // Execution Performance | + | Execution Performance: |
- | Setup Time: ~2 seconds (DFPlayer init) | + | Setup Time: ~2 seconds (DFPlayer init) |
- | Loop Cycle: ~100ms (măsurare + processing) | + | Loop Cycle: ~100ms (măsurare + processing) |
- | Interrupt Response: <50µs (hardware priority) | + | Interrupt Response: <50µs (hardware priority) |
- | </pre> | + | |
- | === Algorithm Complexity === | + | Algorithm Complexity: |
- | * '''measureDistance()''': O(1) - constant time | + | - measureDistance(): O(1) - constant time |
- | * '''State transitions''': O(1) - boolean comparison | + | - State transitions: O(1) - boolean comparison |
- | * '''PWM generation''': O(1) - hardware timer | + | - PWM generation: O(1) - hardware timer |
- | * '''ISR execution''': O(1) - direct flag setting | + | - ISR execution: O(1) - direct flag setting |
- | ---- | + | ================================================================================ |
- | == Software Architecture Pattern == | + | SOFTWARE ARCHITECTURE PATTERN |
- | === Event-Driven Architecture === | + | Event-Driven Architecture: |
- | <pre> | + | |
Hardware Events → ISR → Flag Setting → Main Loop Processing → Action Execution | Hardware Events → ISR → Flag Setting → Main Loop Processing → Action Execution | ||
↑ ↑ ↑ ↑ ↑ | ↑ ↑ ↑ ↑ ↑ | ||
GPIO/Timer Interrupt Volatile State Machine PWM/Serial | GPIO/Timer Interrupt Volatile State Machine PWM/Serial | ||
- | </pre> | ||
- | === Non-Blocking Design === | + | Non-Blocking Design: |
- | * '''Concurrent execution''': PWM, Audio, GPIO în paralel | + | - Concurrent execution: PWM, Audio, GPIO în paralel |
- | * '''Interrupt priority''': Hardware events cu prioritate maximă | + | - Interrupt priority: Hardware events cu prioritate maximă |
- | * '''Timer-based delays''': Evitarea blocking calls în loop principal | + | - Timer-based delays: Evitarea blocking calls în loop principal |
- | ---- | + | ================================================================================ |
- | == Error Handling & Recovery == | + | ERROR HANDLING & RECOVERY |
- | === Implemented Safeguards === | + | Implemented Safeguards: |
- | <pre> | + | // Timeout protection |
- | // Timeout protection | + | long duration = pulseIn(ECHO_PIN, HIGH, 30000); |
- | long duration = pulseIn(ECHO_PIN, HIGH, 30000); | + | if (duration == 0) return 0; // Invalid reading |
- | if (duration == 0) return 0; // Invalid reading | + | |
+ | // Debounce filtering | ||
+ | if (currentTime - lastInterruptTime < DEBOUNCE_TIME) return; | ||
+ | |||
+ | // DFPlayer connection validation | ||
+ | if (!dfPlayer.begin(dfPlayerSerial)) { | ||
+ | Serial.println("DFPlayer Error!"); | ||
+ | while(true) delay(0); // Safe halt | ||
+ | } | ||
+ | |||
+ | Graceful Degradation: | ||
+ | - Sensor failure: Continue operation cu last known state | ||
+ | - Audio failure: Maintain servo functionality | ||
+ | - Power fluctuations: Automatic recovery după reset | ||
+ | |||
+ | ================================================================================ | ||
+ | |||
+ | TABEL FUNCȚII IMPLEMENTATE | ||
+ | setup() -> Init; loop -> Main; measureDistande() -> GPIO; moveServosTo() -> PWM; echoInterrupt() -> ISR; activateProximityMode() -> Composite | ||
+ | deactivateProximityMode()-> Composite; initializeDfplayer() -> audio | ||
+ | | ||
- | // Debounce filtering | ||
- | if (currentTime - lastInterruptTime < DEBOUNCE_TIME) return; | ||
- | // DFPlayer connection validation | ||
- | if (!dfPlayer.begin(dfPlayerSerial)) { | ||
- | Serial.println("DFPlayer Error!"); | ||
- | while(true) delay(0); // Safe halt | ||
- | } | ||
- | </pre> | ||
- | === Graceful Degradation === | ||
- | * '''Sensor failure''': Continue operation cu last known state | ||
- | * '''Audio failure''': Maintain servo functionality | ||
- | * '''Power fluctuations''': Automatic recovery după reset | ||
- | ---- | ||
- | == Tabel Funcții Implementate == | ||
- | {| class="wikitable" | ||
- | |- | ||
- | ! Funcție !! Tip !! Complexitate !! Descriere | ||
- | |- | ||
- | | <code>setup()</code> || Initialization || O(1) || Configurare hardware și librării | ||
- | |- | ||
- | | <code>loop()</code> || Main Control || O(1) || Bucla principală non-blocking | ||
- | |- | ||
- | | <code>measureDistance()</code> || GPIO || O(1) || Măsurare ultrasonică Time-of-Flight | ||
- | |- | ||
- | | <code>moveServosTo()</code> || PWM || O(1) || Control sincronizat dual servo | ||
- | |- | ||
- | | <code>echoInterrupt()</code> || ISR || O(1) || Handler întrerupere cu debounce | ||
- | |- | ||
- | | <code>activateProximityMode()</code> || Composite || O(1) || PWM + Audio pentru apropriere | ||
- | |- | ||
- | | <code>deactivateProximityMode()</code> || Composite || O(1) || PWM + Audio pentru îndepărtare | ||
- | |- | ||
- | | <code>initializeDFPlayer()</code> || Audio || O(1) || Setup player MP3 cu validare | ||
- | |} | ||
===== Rezultate Obţinute ===== | ===== Rezultate Obţinute ===== | ||
<note tip> | <note tip> | ||
- | Care au fost rezultatele obţinute în urma realizării proiectului vostru. | + | In urma realizarii proiectului , in cele din urma dupa mult debug si multi nervi totul merge conform asteptarilor. Atunci cand ma apropii de senzor |
+ | , masca se deschide si un sunet canta inauntrul ei din difuzorul atasat la mini dfplayer. | ||
</note> | </note> | ||
===== Concluzii ===== | ===== Concluzii ===== | ||
+ | Am ramas cu un gust placut dupa acest proiect, m-a facut sa imi explorez iubita pasiune de a face lego la un alt nivel :D | ||
===== Download ===== | ===== Download ===== | ||
- | <note warning> | + | https://github.com/Robert326/IronMan_Mask |
- | O arhivă (sau mai multe dacă este cazul) cu fişierele obţinute în urma realizării proiectului: surse, scheme, etc. Un fişier README, un ChangeLog, un script de compilare şi copiere automată pe uC crează întotdeauna o impresie bună ;-). | + | |
- | Fişierele se încarcă pe wiki folosind facilitatea **Add Images or other files**. Namespace-ul în care se încarcă fişierele este de tipul **:pm:prj20??:c?** sau **:pm:prj20??:c?:nume_student** (dacă este cazul). **Exemplu:** Dumitru Alin, 331CC -> **:pm:prj2009:cc:dumitru_alin**. | ||
- | </note> | ||
- | ===== Jurnal ===== | ||
- | |||
- | <note tip> | ||
- | Puteți avea și o secțiune de jurnal în care să poată urmări asistentul de proiect progresul proiectului. | ||
- | </note> | ||
===== Bibliografie/Resurse ===== | ===== Bibliografie/Resurse ===== |