Ver Fonte

Merge pull request #4127 from jomjol/PowerResetCamera_fix

Power reset camera fix
SybexXx há 1 mês atrás
pai
commit
38d94475e1

+ 23 - 23
.github/workflows/build.yaml

@@ -19,7 +19,7 @@ jobs:
       with:
         concurrent_skipping: same_content_newer
 
-    - uses: actions/checkout@v4
+    - uses: actions/checkout@v6
       with:
         submodules: recursive
 
@@ -29,28 +29,28 @@ jobs:
         echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
 
     - name: Update PIP cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: ~/.cache/pip
         key: pip-${{ github.run_id }}
         restore-keys: pip # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update PlatformIO cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: ~/.platformio
         key: platformio-${{ github.run_id }}
         restore-keys: platformio # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update Build cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: ./code/.pio/
         key: build-${{ github.run_id }}
         restore-keys: build # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update generated-files cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: |
           ./code/.pio/build/esp32cam/firmware.bin
@@ -62,7 +62,7 @@ jobs:
         restore-keys: generated-files # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache          
 
     - name: Set up Python
-      uses: actions/setup-python@v5
+      uses: actions/setup-python@v6
       with:
         python-version: '3.10'
 
@@ -122,10 +122,10 @@ jobs:
     needs: build
 
     steps:
-    - uses: actions/checkout@v4
+    - uses: actions/checkout@v6
 
     - name: Update generated-files cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: |
           ./code/.pio/build/esp32cam/firmware.bin
@@ -137,7 +137,7 @@ jobs:
         restore-keys: generated-files # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update update cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: update
         key: update-${{ github.run_id }}
@@ -169,7 +169,7 @@ jobs:
         cp ./sd-card/config/*.tflite ./update/config/ 2>/dev/null || true
 
     - name: Upload update as update.zip artifact (Firmware + Web UI + CNN)
-      uses: actions/upload-artifact@v4
+      uses: actions/upload-artifact@v6
       with:
         name: "AI-on-the-edge-device__update__${{ steps.vars.outputs.branch }}_(${{ steps.vars.outputs.sha_short }})"
         path: ./update/*
@@ -189,10 +189,10 @@ jobs:
     needs: build
 
     steps:
-    - uses: actions/checkout@v4
+    - uses: actions/checkout@v6
 
     - name: Update generated-files cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: |
           ./code/.pio/build/esp32cam/firmware.bin
@@ -204,7 +204,7 @@ jobs:
         restore-keys: generated-files # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update remote_setup cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: remote_setup
         key: remote_setup-${{ github.run_id }}
@@ -234,7 +234,7 @@ jobs:
         cp ./sd-card/config/* ./remote_setup/config/ 2>/dev/null || true
 
     - name: Upload remote_setup as remote_setup.zip artifact (Firmware + Web UI + Config)
-      uses: actions/upload-artifact@v4
+      uses: actions/upload-artifact@v6
       with:
         name: "AI-on-the-edge-device__remote-setup__${{ steps.vars.outputs.branch }}_(${{ steps.vars.outputs.sha_short }})"
         path: ./remote_setup/*
@@ -249,10 +249,10 @@ jobs:
     needs: build
 
     steps:
-    - uses: actions/checkout@v4
+    - uses: actions/checkout@v6
 
     - name: Update generated-files cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: |
           ./code/.pio/build/esp32cam/firmware.bin
@@ -264,7 +264,7 @@ jobs:
         restore-keys: generated-files # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update manual_setup cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: manual_setup
         key: manual_setup-${{ github.run_id }}
@@ -296,7 +296,7 @@ jobs:
         cd ./manual_setup
 
     - name: Upload manual_setup.zip artifact (Firmware + Bootloader + Partitions + Web UI)
-      uses: actions/upload-artifact@v4
+      uses: actions/upload-artifact@v6
       with:
         name: "AI-on-the-edge-device__manual-setup__${{ steps.vars.outputs.branch }}_(${{ steps.vars.outputs.sha_short }})"
         path: ./manual_setup
@@ -317,24 +317,24 @@ jobs:
       id-token: write
 
     steps:
-    - uses: actions/checkout@v4
+    - uses: actions/checkout@v6
 
     - name: Update update cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: update
         key: update-${{ github.run_id }}
         restore-keys: update # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update remote_setup cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: remote_setup
         key: remote_setup-${{ github.run_id }}
         restore-keys: remote_setup # This matches above key as it is only used as a prefix. it the restores the nearest cache, see https://github.com/restore-keys:/blob/main/tips-and-workarounds.md#update-a-cache
 
     - name: Update manual_setup cache on every commit
-      uses: actions/cache@v4
+      uses: actions/cache@v5
       with:
         path: manual_setup
         key: manual_setup-${{ github.run_id }}
@@ -425,7 +425,7 @@ jobs:
 
     steps:
     - name: Checkout
-      uses: actions/checkout@v4
+      uses: actions/checkout@v6
 
     - name: Get version of last release
       id: last_release

+ 8 - 11
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -26,6 +26,7 @@
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 
+#include "server_camera.h"
 #include "esp_camera.h"
 
 #include "driver/ledc.h"
@@ -115,6 +116,10 @@ CCamera::CCamera(void)
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "CreateClassCamera");
 #endif
+
+	CCstatus.ImageQuality = camera_config.jpeg_quality;
+    CCstatus.ImageFrameSize = camera_config.frame_size;
+	SetLEDIntensity(LedIntensity);
     CCstatus.WaitBeforePicture = 2;
 
     ledc_init();
@@ -124,19 +129,12 @@ esp_err_t CCamera::InitCam(void)
 {
     ESP_LOGD(TAG, "Init Camera");
 
-    TickType_t cam_xDelay = 100 / portTICK_PERIOD_MS;
-
-    CCstatus.ImageQuality = camera_config.jpeg_quality;
-    CCstatus.ImageFrameSize = camera_config.frame_size;
-
     // De-init in case it was already initialized
-    esp_camera_deinit();
-    vTaskDelay(cam_xDelay);
-
+    PowerResetCamera();
+    vTaskDelay(pdMS_TO_TICKS(200));
+	
     // initialize the camera
     esp_err_t err = esp_camera_init(&camera_config);
-    vTaskDelay(cam_xDelay);
-
     if (err != ESP_OK)
     {
         ESP_LOGE(TAG, "Camera Init Failed");
@@ -147,7 +145,6 @@ esp_err_t CCamera::InitCam(void)
 
     // Get a reference to the sensor
     sensor_t *s = esp_camera_sensor_get();
-
     if (s != NULL)
     {
         CCstatus.CamSensor_id = s->id.PID;

+ 26 - 18
code/components/jomjol_controlcamera/server_camera.cpp

@@ -18,24 +18,32 @@ static const char *TAG = "server_cam";
 
 void PowerResetCamera()
 {
-#if CAM_PIN_PWDN == GPIO_NUM_NC // Use reset only if pin is available
-    LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No power down pin availbale to reset camera");
-#else
-    ESP_LOGD(TAG, "Resetting camera by power down line");
-    gpio_config_t conf;
-    conf.intr_type = GPIO_INTR_DISABLE;
-    conf.pin_bit_mask = 1LL << CAM_PIN_PWDN;
-    conf.mode = GPIO_MODE_OUTPUT;
-    conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
-    conf.pull_up_en = GPIO_PULLUP_DISABLE;
-    gpio_config(&conf);
-
-    // carefull, logic is inverted compared to reset pin
-    gpio_set_level(CAM_PIN_PWDN, 1);
-    vTaskDelay(1000 / portTICK_PERIOD_MS);
-    gpio_set_level(CAM_PIN_PWDN, 0);
-    vTaskDelay(1000 / portTICK_PERIOD_MS);
-#endif
+    // Use reset only if pin is available
+    if (CAM_PIN_PWDN == GPIO_NUM_NC)
+    {
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No power down pin availbale to reset camera");
+    }
+    else
+    {
+        ESP_LOGD(TAG, "Resetting camera by power down line");
+        gpio_config_t conf = {
+            .pin_bit_mask = 1LL << CAM_PIN_PWDN,
+            .mode = GPIO_MODE_OUTPUT,
+            .pull_up_en = GPIO_PULLUP_DISABLE,
+            .pull_down_en = GPIO_PULLDOWN_DISABLE,
+            .intr_type = GPIO_INTR_DISABLE,
+        };
+        gpio_config(&conf);
+
+        // carefull, logic is inverted compared to reset pin
+        ESP_LOGD(TAG, "Camera power down");
+        gpio_set_level(CAM_PIN_PWDN, 1);
+        vTaskDelay(pdMS_TO_TICKS(1000));
+        
+        ESP_LOGD(TAG, "Camera power up");
+        gpio_set_level(CAM_PIN_PWDN, 0);
+        vTaskDelay(pdMS_TO_TICKS(1000));
+    }
 }
 
 esp_err_t handler_lightOn(httpd_req_t *req)

+ 37 - 44
code/main/main.cpp

@@ -323,59 +323,52 @@ extern "C" void app_main(void)
                 else { // PSRAM OK
                     // Init camera
                     // ********************************************
-                    PowerResetCamera();
-                    esp_err_t camStatus = Camera.InitCam();
-                    Camera.LightOnOff(false);
+                    esp_err_t camStatus = ESP_OK;
+                    Camera.LightOnOff(true);
 
-                    xDelay = 2000 / portTICK_PERIOD_MS;
-                    ESP_LOGD(TAG, "After camera initialization: sleep for: %ldms", (long) xDelay * CONFIG_FREERTOS_HZ/portTICK_PERIOD_MS);
-                    vTaskDelay( xDelay );
-
-                    // Check camera init
-                    // ********************************************
-                    if (camStatus != ESP_OK) { // Camera init failed, retry to init
-                        char camStatusHex[33];
-                        sprintf(camStatusHex,"0x%02x", camStatus);
-                        LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Camera init failed (" + std::string(camStatusHex) + "), retrying...");
-
-                        PowerResetCamera();
+                    for (int i = 0; i < 3; i++)
+                    {
                         camStatus = Camera.InitCam();
-                        Camera.LightOnOff(false);
-
-                        xDelay = 2000 / portTICK_PERIOD_MS;
-                        ESP_LOGD(TAG, "After camera initialization: sleep for: %ldms", (long) xDelay * CONFIG_FREERTOS_HZ/portTICK_PERIOD_MS);
-                        vTaskDelay( xDelay ); 
-
-                        if (camStatus != ESP_OK) { // Camera init failed again
-                            sprintf(camStatusHex,"0x%02x", camStatus);
-                            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera init failed (" + std::string(camStatusHex) +
-                                                                    ")! Check camera module and/or proper electrical connection");
-                            setSystemStatusFlag(SYSTEM_STATUS_CAM_BAD);
-                            Camera.LightOnOff(false);   // make sure flashlight is off
-                            StatusLED(CAM_INIT, 1, true);
+                        if (camStatus == ESP_OK)
+                        {
+                            break;
                         }
+                        // De-init in case it was already initialized
+                        esp_camera_deinit();
+                        vTaskDelay(pdMS_TO_TICKS(200));
                     }
 
-                    if (camStatus == ESP_OK) { // ESP_OK -> Camera init OK --> continue to perform camera framebuffer check
-                        // Camera framebuffer check
-                        // ********************************************
-                        if (!Camera.testCamera()) {
-                            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera framebuffer check failed");
-                            // Easiest would be to simply restart here and try again,
-                            // how ever there seem to be systems where it fails at startup but still work correctly later.
-                            // Therefore we treat it still as successed! */
-                            setSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD);
-                            StatusLED(CAM_INIT, 2, false);
-                        }
-                        Camera.LightOnOff(false);   // make sure flashlight is off before start of flow
-
+                    if (camStatus != ESP_OK)
+                    {
+                        // Camera init failed, retry to init
+                        char camStatusHex[33];
+                        sprintf(camStatusHex, "0x%02x", camStatus);
+                        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera init failed (" + std::string(camStatusHex) + ")! Check camera module and/or proper electrical connection");
+						setSystemStatusFlag(SYSTEM_STATUS_CAM_BAD);
+                        StatusLED(CAM_INIT, 1, true);
+                    }
+                    // Camera framebuffer check
+                    // ********************************************
+                    else if (!Camera.testCamera())
+                    {
+                        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera framebuffer check failed");
+                        // Easiest would be to simply restart here and try again,
+                        // how ever there seem to be systems where it fails at startup but still work correctly later.
+                        // Therefore we treat it still as successed! */
+                        setSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD);
+                        StatusLED(CAM_INIT, 2, false);
+                    }
+                    else
+					{
                         // Print camera infos
                         // ********************************************
                         char caminfo[50];
-                        sensor_t * s = esp_camera_sensor_get();
-                        sprintf(caminfo, "PID: 0x%02x, VER: 0x%02x, MIDL: 0x%02x, MIDH: 0x%02x", s->id.PID, s->id.VER, s->id.MIDH, s->id.MIDL);
+                        sensor_t *sensor = esp_camera_sensor_get();
+                        sprintf(caminfo, "PID: 0x%02x, VER: 0x%02x, MIDL: 0x%02x, MIDH: 0x%02x", sensor->id.PID, sensor->id.VER, sensor->id.MIDH, sensor->id.MIDL);
                         LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Camera info: " + std::string(caminfo));
-                    }
+					}
+
+					Camera.LightOnOff(false); // make sure flashlight is off before start of flow
                 }
             }
         }