Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4476

Camera board • Correct Timing for GS Dual Camera Capture with External Trigger on Raspberry Pi 5

$
0
0
Hello everyone,

I am working on a project using Raspberry Pi 5 to capture images of products on a production line. The setup involves two GS cameras (IMX296 modules) connected to the Pi. A hardware sensor detects the presence of a product and sends a signal to the Raspberry Pi. The Pi then toggles the external trigger (XTR) pins on the cameras to capture images.

The Setup:
1.Cameras: Two IMX296 cameras (currently testing with one camera).
2.Driver: Using the Raspberry Pi libcamera with the PISP pipeline.
3.External Trigger: Controlled via GPIO17 (XTR pin).
4.Sensor Input: Connected to GPIO27.
5.Software: Custom program written in C that:
•Waits for a signal (falling edge on GPIO27) from the hardware sensor.
•Toggles the XTR pin (GPIO17) to low for 10ms.
•Simultaneously invokes libcamera-still to capture an image.

The Problem:

I am experiencing difficulties with the proper timing for capturing images. Here’s the issue:
1.When running my program, I often encounter the following error:

Code:

[2:52:49.641967455] [5555]  INFO Camera camera.cpp:1197 configuring streams: (0) 728x544-YUV420 (1) 1456x1088-BGGR_PISP_COMP1[2:52:49.642048159] [5558]  INFO RPI pisp.cpp:1450 Sensor: /base/axi/pcie@120000/rp1/i2c@88000/imx296@1a - Selected sensor format: 1456x1088-SBGGR10_1X10 - Selected CFE format: 1456x1088-PC1B[2:52:58.674775179] [5558]  WARN V4L2 v4l2_videodevice.cpp:2095 /dev/video4[16:cap]: Dequeue timer of 9000000.00us has expired![2:52:58.674826254] [5558] ERROR RPI pipeline_base.cpp:1364 Camera frontend has timed out![2:52:58.674834050] [5558] ERROR RPI pipeline_base.cpp:1365 Please check that your camera sensor connector is attached securely.[2:52:58.674841698] [5558] ERROR RPI pipeline_base.cpp:1366 Alternatively, try another cable and/or sensor.
2.If I manually generate the trigger signal independently and run the libcamera-still command (e.g., libcamera-still -o test.jpg), the image is captured successfully. This indicates that the issue lies in the timing or sequence of toggling the XTR pin and invoking libcamera.

Current Workflow:
1.The sensor sends a signal to GPIO27.
2.My program detects this signal and:
•Waits for a specified delay (e.g., 200ms).
•Toggles GPIO17 (XTR) to low for 10ms.
•Simultaneously runs:

Code:

libcamera-still -o photo.jpg --flush --shutter 1000 --nopreview --camera 0 --timeout 500 --immediate --mode 1456:1088:12:P --viewfinder-mode 1456:1088:12:P
Planned Dual Camera Setup:

Once I resolve the issue with a single camera, I aim to control two cameras simultaneously to capture images of the same product from different angles. Each camera will be connected to a separate XTR pin, triggered at the same time.

Questions:
1.Correct Timing for Triggered Capture:
•Should the XTR pin be toggled before or after invoking libcamera?
•Does libcamera-still need to be running and streaming before the trigger signal is sent?
2.Dual Camera Synchronization:
•Is there a recommended workflow to synchronize two cameras using libcamera?
•Can both cameras be triggered simultaneously, or should the captures be staggered?
3.Alternative Methods for Faster Image Capture:
•libcamera-still works but feels quite slow for a real-time production line application.
•Are there alternative tools, libraries, or methods to capture images faster while maintaining support for external triggers?
4.Initial State of XTR:
•What should be the default state of the XTR pin before triggering (high or low)?

Additional Notes:
•In attachment there is my C program
•I am using the following command for capture:

Code:

libcamera-still -o photo.jpg --flush --shutter 1000 --nopreview --camera 0 --timeout 500 --immediate --mode 1456:1088:12:P --viewfinder-mode 1456:1088:12:P

Any insights, sample code, or recommendations for proper timing, faster capture methods, and synchronization would be greatly appreciated. I’d also love to hear from anyone who has experience with dual-camera setups on Raspberry Pi.

My C program

Code:

#include <gpiod.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>#define GPIO_CHIP "/dev/gpiochip0"#define GPIO_INPUT 27  // Input pin#define GPIO_OUTPUT 17 // Output pinvoid delay_ns(long ns) {    struct timespec ts;    ts.tv_sec = 0;    ts.tv_nsec = ns;    nanosleep(&ts, NULL);}int main(int argc, char *argv[]) {    if (argc != 3) {        fprintf(stderr, "Usage: %s <wait time in ms> <pulse duration in ns>\n", argv[0]);        return 1;    }    int waitMs = atoi(argv[1]);       // Wait time in ms    long pulseNs = atol(argv[2]);    // Pulse duration in ns    int fileIndex = 1;               // Counter for image numbering    struct gpiod_chip *chip;    struct gpiod_line *input_line, *output_line;    int event;    // Opening the GPIO chip    chip = gpiod_chip_open(GPIO_CHIP);    if (!chip) {        perror("Failed to open GPIO chip");        return 1;    }    // Configuring the input pin    input_line = gpiod_chip_get_line(chip, GPIO_INPUT);    if (!input_line) {        perror("Failed to get input GPIO line");        gpiod_chip_close(chip);        return 1;    }    if (gpiod_line_request_falling_edge_events(input_line, "gpio_program") < 0) {        perror("Failed to configure input GPIO");        gpiod_chip_close(chip);        return 1;    }    // Configuring the output pin    output_line = gpiod_chip_get_line(chip, GPIO_OUTPUT);    if (!output_line) {        perror("Failed to get output GPIO line");        gpiod_chip_close(chip);        return 1;    }    if (gpiod_line_request_output(output_line, "gpio_program", 1) < 0) {        perror("Failed to configure output GPIO");        gpiod_chip_close(chip);        return 1;    }    printf("Program started. Waiting for a falling edge on GPIO27...\n");    while (1) {        event = gpiod_line_event_wait(input_line, NULL);        if (event == 1) {            struct gpiod_line_event levent;            gpiod_line_event_read(input_line, &levent);            printf("Falling edge detected. Waiting %d ms...\n", waitMs);            usleep(waitMs * 1000); // Waiting in ms            // Driving GPIO17 to 0            gpiod_line_set_value(output_line, 0);            delay_ns(pulseNs); // Driving for the duration in ns            gpiod_line_set_value(output_line, 1);            // Taking images with unique file names            char leftFilename[50];            snprintf(leftFilename, sizeof(leftFilename), "left_%d.jpg", fileIndex);            printf("Capturing image: %s\n", leftFilename);            char leftCommand[200];            snprintf(leftCommand, sizeof(leftCommand),                      "libcamera-still -o %s --flush --shutter 2000 --nopreview --camera 0 --timeout 500 --immediate --mode 1456:1088:12:P --viewfinder-mode 1456:1088:12:P",                     leftFilename);            system(leftCommand);            printf("Image saved as %s. Waiting for the next falling edge.\n", leftFilename);            // Incrementing the image number            fileIndex++;        }    }    gpiod_chip_close(chip);    return 0;}

Statistics: Posted by Rafal-And-Tech — Wed Jan 01, 2025 8:34 pm — Replies 0 — Views 17



Viewing all articles
Browse latest Browse all 4476

Trending Articles