1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
| // --------------------------------------------------------
// *** Programme ALITA ***
// --------------------------------------------------------
// Historique ...
// 2025-12-11 - Rédaction structurée du programme
// détection de contours SOBEL sur image 80x60
// analyse sur 4 bloc centraux / 16 blocs
// référence calculée APRES appui bouton GO
// 2025-12-10 - redimensionnement image 80x60
// 2025-12-09 - blockSize 256->64 - seuilBlock 1000->300 - seuil 50->450
// résolution UXGA -> SVGA (800x600) 64 200 200
// 2025-12-08 - 1 seule rafale de 5 images - 175 ms -> cycle 1 seconde
// 2025-12-05 - Optimisation rafale
// 2025-12-04 - Codification des images par rafale
// 2025-12-02 - Reprise caméra seule, ajout sd_read_write : OK !!!
// 2025-12-01 - Suppression LED et LED couleur
// 2025-11-25 - Test de la librairie SD_READ_WRITE
// 2025-11-24 - Test des 9 pin GPIO (avec LED simple)
// 2025-11-23 - aide de FRED1599 : le programme USB fonctionne !
// 2025-11-22 - le programme 3 couleurs OK (pas de retour / moniteur série)
// 2025-10-24 - solution GitHub -> SIVAR2311 pour ESP32-S3 FH4R2 (non CAM)
// --------------------------------------------------------
// --------------------------------------------------------
// *** Programme ALITA ***
// ESP32-S3-n16r8 CAM Development Board
// Détection de mouvement par contours (Sobel) sur images 80x60
// Référence calculée APRES appui bouton GO
// --------------------------------------------------------
#include <Arduino.h>
#include "esp_camera.h"
#include "sd_read_write.h"
// ========== CONSTANTES GLOBALES ==========
// Entrées/Sorties
#define LED_SIMPLE 2 // LED on-board
#define BOUTON_GO 0 // Bouton on-board
#define INTERVALLE_PHOTO 175 // Temps entre images (ms)
#define NB_IMAGES_RAFALE 5 // Nombre d'images par rafale
#define DELai_DEMARRAGE 10 // Délai avant démarrage (s)
// Caméra
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 15
#define SIOD_GPIO_NUM 4
#define SIOC_GPIO_NUM 5
#define Y9_GPIO_NUM 16
#define Y8_GPIO_NUM 17
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 12
#define Y5_GPIO_NUM 10
#define Y4_GPIO_NUM 8
#define Y3_GPIO_NUM 9
#define Y2_GPIO_NUM 11
#define VSYNC_GPIO_NUM 6
#define HREF_GPIO_NUM 7
#define PCLK_GPIO_NUM 13
// Traitement d'image
#define SMALL_WIDTH 80 // Largeur image réduite
#define SMALL_HEIGHT 60 // Hauteur image réduite
#define ZONE_SIZE 10 // Taille des zones d'analyse
#define NUM_ZONES 16 // Nombre de zones (4x4)
#define ABSOLUTE_THRESHOLD 5000 // Seuil absolu pour détection
// ========== VARIABLES GLOBALES ==========
// Gestion des images
uint8_t *previousFrame = nullptr; // Image précédente (pleine résolution)
size_t previousFrameSize = 0;
const size_t MAX_FRAME_SIZE = 1600 * 1200 * 2; // Taille max image
// Buffers pour traitement 80x60
uint8_t referenceEdges[SMALL_WIDTH * SMALL_HEIGHT]; // Référence stable
uint8_t prevEdges[SMALL_WIDTH * SMALL_HEIGHT]; // Contours image précédente
uint8_t currentSmallImg[SMALL_WIDTH * SMALL_HEIGHT]; // Image réduite courante
uint8_t smoothedImg[SMALL_WIDTH * SMALL_HEIGHT]; // Image lissée
uint8_t laplacianImg[SMALL_WIDTH * SMALL_HEIGHT]; // Image après Laplacien
uint8_t currentEdges[SMALL_WIDTH * SMALL_HEIGHT]; // Contours courants
// Gestion rafale
int imageCount = 0;
int nbImage = NB_IMAGES_RAFALE;
char filename[64];
unsigned long debutPhoto, dureePhoto;
unsigned long debutRafale, dureeRafale;
// ========== ROUTINES ==========
void allume_LED_OK() { // Signal visuel OK
digitalWrite(LED_SIMPLE, HIGH);
delay(25);
digitalWrite(LED_SIMPLE, LOW);
}
void allume_LED() { // Allume LED
digitalWrite(LED_SIMPLE, HIGH);
}
void eteint_LED() { // Éteint LED
digitalWrite(LED_SIMPLE, LOW);
}
void allume_LED_NOK() { // Signal visuel ERREUR
digitalWrite(LED_SIMPLE, HIGH);
delay(50);
digitalWrite(LED_SIMPLE, LOW);
delay(100);
digitalWrite(LED_SIMPLE, HIGH);
delay(50);
digitalWrite(LED_SIMPLE, LOW);
}
// --------------------------------------------------------
void applySmoothing(uint8_t *src, uint8_t *dst) {
// Lissage 5x5 pour réduire le bruit JPEG
for (uint16_t y = 2; y < SMALL_HEIGHT - 2; y++) {
for (uint16_t x = 2; x < SMALL_WIDTH - 2; x++) {
int sum = 0;
for (int dy = -2; dy <= 2; dy++) {
for (int dx = -2; dx <= 2; dx++) {
sum += src[(y + dy) * SMALL_WIDTH + (x + dx)];
}
}
dst[y * SMALL_WIDTH + x] = sum / 25; // 5x5 = 25 pixels
}
}
}
// --------------------------------------------------------
void applyLaplacian(uint8_t *src, uint8_t *dst) {
// Filtre Laplacien pour renforcer les contours
for (uint16_t y = 1; y < SMALL_HEIGHT - 1; y++) {
for (uint16_t x = 1; x < SMALL_WIDTH - 1; x++) {
int laplacian = -4 * src[y*SMALL_WIDTH + x]
+ src[(y-1)*SMALL_WIDTH + x] + src[(y+1)*SMALL_WIDTH + x]
+ src[y*SMALL_WIDTH + (x-1)] + src[y*SMALL_WIDTH + (x+1)];
dst[y*SMALL_WIDTH + x] = min(255, abs(laplacian));
}
}
}
// --------------------------------------------------------
void applySobel(uint8_t *src, uint8_t *dst) {
// Détection contours avec binarisation
const uint8_t threshold = 30; // Seuil de binarisation
for (uint16_t y = 1; y < SMALL_HEIGHT - 1; y++) {
for (uint16_t x = 1; x < SMALL_WIDTH - 1; x++) {
int gx = -src[(y-1)*SMALL_WIDTH + (x-1)] - 2*src[(y-1)*SMALL_WIDTH + x]
- src[(y-1)*SMALL_WIDTH + (x+1)]
+ src[(y+1)*SMALL_WIDTH + (x-1)] + 2*src[(y+1)*SMALL_WIDTH + x]
+ src[(y+1)*SMALL_WIDTH + (x+1)];
int gy = -src[(y-1)*SMALL_WIDTH + (x-1)] - 2*src[y*SMALL_WIDTH + (x-1)]
- src[(y+1)*SMALL_WIDTH + (x-1)]
+ src[(y-1)*SMALL_WIDTH + (x+1)] + 2*src[y*SMALL_WIDTH + (x+1)]
+ src[(y+1)*SMALL_WIDTH + (x+1)];
int magnitude = (abs(gx) + abs(gy)) / 2;
dst[y*SMALL_WIDTH + x] = (magnitude > threshold) ? 255 : 0;
}
}
}
// --------------------------------------------------------
void resizeJpegApprox(uint8_t *srcBuf, size_t srcLen, uint8_t *dstBuf) {
// Redimensionnement par échantillonnage
size_t blockSize = srcLen / (SMALL_WIDTH * SMALL_HEIGHT);
for (size_t i = 0; i < SMALL_WIDTH * SMALL_HEIGHT; i++) {
size_t srcIndex = i * blockSize;
if (srcIndex < srcLen) {
dstBuf[i] = srcBuf[srcIndex];
}
}
}
// --------------------------------------------------------
bool detectMotion(camera_fb_t *currentFrame) {
// 1. Redimensionnement
resizeJpegApprox(currentFrame->buf, currentFrame->len, currentSmallImg);
// 2. Lissage agressif (5x5)
applySmoothing(currentSmallImg, smoothedImg);
// 3. Contours (Laplacien + Sobel)
applyLaplacian(smoothedImg, laplacianImg);
applySobel(laplacianImg, currentEdges);
// 4. Détection multi-zones (16 zones de 10x10)
int zoneDiffs[NUM_ZONES] = {0};
for (int z = 0; z < NUM_ZONES; z++) {
int startX = (z % 4) * ZONE_SIZE;
int startY = (z / 4) * ZONE_SIZE;
for (int y = 0; y < ZONE_SIZE; y++) {
for (int x = 0; x < ZONE_SIZE; x++) {
int i = (startY + y) * SMALL_WIDTH + (startX + x);
zoneDiffs[z] += abs(currentEdges[i] - prevEdges[i]);
}
}
}
// 5. Vérification des seuils absolus
bool movementDetected = false;
for (int z = 0; z < NUM_ZONES; z++) {
if (zoneDiffs[z] > ABSOLUTE_THRESHOLD) {
Serial.printf("Mouvement zone %d (diff=%d)\n", z, zoneDiffs[z]);
movementDetected = true;
}
}
// 6. Mise à jour conditionnelle de prevEdges
if (!movementDetected) {
memcpy(prevEdges, currentEdges, SMALL_WIDTH * SMALL_HEIGHT);
}
return movementDetected;
}
// ========== SETUP ==========
void setup() {
// Initialisation série et GPIO
Serial.begin(115200);
pinMode(LED_SIMPLE, OUTPUT);
pinMode(BOUTON_GO, INPUT_PULLUP);
// Initialisation SD
sdmmcInit();
// Allocation mémoire
previousFrame = (uint8_t *)malloc(MAX_FRAME_SIZE);
// Configuration caméra
camera_config_t config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_UXGA, // 1600x1200
.jpeg_quality = 60, // compression
.fb_count = 2,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
};
// Initialisation caméra
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
allume_LED_NOK();
Serial.printf("Erreur caméra: 0x%x\n", err);
while(1);
}
Serial.println("Caméra initialisée");
// Réglages caméra
sensor_t *s = esp_camera_sensor_get();
s->set_brightness(s, 0);
s->set_contrast(s, 0);
s->set_saturation(s, 0);
s->set_sharpness(s, 0);
s->set_denoise(s, 1);
}
// ========== LOOP ==========
void loop() {
// Attente bouton GO
Serial.println("Appuyez sur le bouton GO pour démarrer");
while (digitalRead(BOUTON_GO) == HIGH) delay(25);
delay(50); // Anti-rebond
while (digitalRead(BOUTON_GO) == LOW) delay(25);
// Délai de préparation (10s pour positionner la scène)
Serial.print("Préparez la scène. Départ dans ");
for (int i = 0; i < DELai_DEMARRAGE; i++) {
Serial.print(".");
allume_LED_OK();
delay(1000);
}
Serial.println(" GO!");
// ===== CALCUL DE LA RÉFÉRENCE APRES APPUI BOUTON =====
Serial.println("Calcul de la référence stable (5 images)...");
memset(referenceEdges, 0, SMALL_WIDTH * SMALL_HEIGHT); // Réinitialise
for (int i = 0; i < 5; i++) {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) continue;
resizeJpegApprox(fb->buf, fb->len, currentSmallImg);
applySmoothing(currentSmallImg, smoothedImg);
applyLaplacian(smoothedImg, laplacianImg);
applySobel(laplacianImg, currentEdges);
for (int j = 0; j < SMALL_WIDTH * SMALL_HEIGHT; j++) {
referenceEdges[j] += currentEdges[j];
}
esp_camera_fb_return(fb);
delay(100);
}
// Normalisation de la référence
for (int j = 0; j < SMALL_WIDTH * SMALL_HEIGHT; j++) {
referenceEdges[j] /= 5;
}
memcpy(prevEdges, referenceEdges, SMALL_WIDTH * SMALL_HEIGHT);
Serial.println("Référence calculée. Rafale en cours...");
// =======================================================
// Rafale de photos
debutRafale = millis();
allume_LED();
for (imageCount = 0; imageCount < nbImage; imageCount++) {
snprintf(filename, sizeof(filename), "/_image_%d.jpg", imageCount);
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
allume_LED_NOK();
Serial.println("Échec capture");
continue;
}
if (imageCount <= 1) { // Images 0-1 éliminées
esp_camera_fb_return(fb);
continue;
}
// Détection mouvement
bool mouvement = detectMotion(fb);
// Sauvegarde image
writejpg(SD_MMC, filename, fb->buf, fb->len);
if (imageCount == 2) { // Initialisation référence pleine résolution
memcpy(previousFrame, fb->buf, fb->len);
previousFrameSize = fb->len;
}
Serial.printf("Image %d: %s\n", imageCount,
mouvement ? "MOUVEMENT" : "FIXE");
esp_camera_fb_return(fb);
// Intervalle entre images
dureePhoto = millis();
if (millis() - debutPhoto < INTERVALLE_PHOTO) {
delay(INTERVALLE_PHOTO - (millis() - debutPhoto));
}
debutPhoto = millis();
}
eteint_LED();
dureeRafale = millis() - debutRafale;
Serial.printf("Rafale terminée en %lu ms\n", dureeRafale);
// Attente
Serial.print("Attente");
while(1) {
for (int i = 0; i < 10; i++) {
delay(1000);
Serial.print(".");
}
}
} |
Partager