> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dwe.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Frame-Synchronizing Multiple Cameras

> A guide on how to synchronize stellarHD cameras.

<Icon icon="triangle-person-digging" color="#FFC107" iconType="regular" size={32} /> ***This documentation is still being improved and under construction!***

<Note>
  Both **electrical** and **software** synchronization are required. See below for detailed instructions.
</Note>

# Electrical Wiring Guide

<Tabs>
  <Tab title="Leader-Follower Mode">
    ## Overview

    This mode is designed for configurations **not using an External Clock Signal**.

    You will utilize one camera in `leader` mode (outputting a synchronization signal) and all remaining cameras in `follower` mode.

    <Card title="Leader/Follower Firmware Guide" icon="link" href="/stellarHD/guides/stellarHD-firmware">
      Click here to see our guide on updating your camera between Leader and Follower modes.
    </Card>

    <Frame>
      <img className="block dark:hidden" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-light.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=4c5823a5d745629d5a62fb7dd84a1a49" width="800" height="300" data-path="stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-light.svg" />

      <img className="hidden dark:block" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-dark.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=1ae7234422717f420009665eb9043f22" width="800" height="300" data-path="stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-dark.svg" />
    </Frame>

    ***

    ## Wiring

    <Frame>
      <img width="800px" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-Wiring-Diagram.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=5b0ab2de70b284309a3f13901f0a77b9" data-path="stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-Wiring-Diagram.svg" />
    </Frame>

    ### Example

    <Frame caption="An example of wires from two cameras connected for frame-synchronization.">
      <img width="400px" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-Wiring-Example.jpg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=8fedebc8290229dd132d773e49a63a49" data-path="stellarHD/guides/images/syncing-multiple-cameras/Leader-Follower-Wiring-Example.jpg" />
    </Frame>
  </Tab>

  <Tab title="Follower-Only Mode (External Clock)">
    ## Overview

    This mode is designed for configurations **using an External Clock Signal** to output the synchronization signal for all cameras.

    In this mode, all cameras must be set to `follower` mode.

    <Card title="Leader/Follower Firmware Guide" icon="link" href="/stellarHD/guides/stellarHD-firmware">
      Click here to see our guide on updating your camera between Leader and Follower modes.
    </Card>

    <Frame>
      <img className="block dark:hidden" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Follower-light.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=362833b4adcee1048a3833f297c67770" width="800" height="300" data-path="stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Follower-light.svg" />

      <img className="hidden dark:block" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Follower-dark.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=fd356395a3435524d7647e4928f2126c" width="800" height="300" data-path="stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Follower-dark.svg" />
    </Frame>

    ***

    ## Wiring

    Check your specific device's product page for external frame-syncing specifications.

    <Frame>
      <img width="800px" src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Wiring-Diagram.svg?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=b49517610c550da641202bb88f6441ab" data-path="stellarHD/guides/images/syncing-multiple-cameras/External-Signal-Wiring-Diagram.svg" />
    </Frame>

    ***

    ## External Clock Signal Generation

    ### Arduino Integration

    <Note>
      The **Arduino Uno (ATmega328P)** is currently the only *confirmed* working board for this configuration.
    </Note>

    <Steps>
      <Step title="Download the PWM Library">
        Download the source code for the PWM library here: [ArduinoPWM v1.0.0](https://github.com/DeepWaterExploration/ArduinoPWM/archive/refs/tags/v1.0.0.zip)
      </Step>

      <Step title="Add the .ZIP Library">
        Import the downloaded `.ZIP` library into your Arduino IDE.

        <Frame>
          <img src="https://mintcdn.com/deepwaterexplorationinc/PotJvdv7IDe7YzbV/stellarHD/guides/images/syncing-multiple-cameras/ArduinoPWM-Library.png?fit=max&auto=format&n=PotJvdv7IDe7YzbV&q=85&s=ae6bb3c799a2b3d3dba92d8696fa9dce" width="1210" height="933" data-path="stellarHD/guides/images/syncing-multiple-cameras/ArduinoPWM-Library.png" />
        </Frame>
      </Step>

      <Step title="Upload the Sketch">
        Upload one of the two sketches below to your Arduino.

        * **Simple Control:** The quickest way to get started, but requires reflashing the firmware to adjust your FPS.
        * **Serial Control:** Allows you to adjust frequency and duty cycle in real-time without reflashing (settings will reset if the device loses power).

        <Tabs>
          <Tab title="Simple Control">
            ```cpp theme={null}
            #include <PWM.h>

            // Pin configuration
            int pin = 9;       // Pin
            int value = 127;   // Initial value for the PWM duty cycle

            // SET FREQUENCY HERE
            float frequency = 60; // Initial frequency in Hz

            void setup()
            {
                // Initialize all timers except for timer0 to save timekeeping tasks
                InitTimersSafe();

                // Set the initial PWM frequency and duty cycle
                if (SetPinFrequencySafe(pin, frequency))
                {
                    pwmWrite(pin, value);
                    Serial.println("PWM frequency and initial duty cycle set.");
                }
                else
                {
                    Serial.println("Failed to set frequency.");
                }
            }
            ```
          </Tab>

          <Tab title="Serial Commands">
            ```cpp theme={null}
            #include <PWM.h>

            // Pin configuration
            int pin = 9;          // Pin
            int value = 127;      // Initial value for the PWM duty cycle
            float frequency = 60; // Initial frequency in Hz

            void setup()
            {
                Serial.begin(9600);

                // Initialize all timers except for timer0 to save timekeeping tasks
                InitTimersSafe();

                // Set the initial PWM frequency and duty cycle
                if (SetPinFrequencySafe(pin, frequency))
                {
                    pwmWrite(pin, value);
                    Serial.println("PWM frequency and initial duty cycle set.");
                }
                else
                {
                    Serial.println("Failed to set frequency.");
                }
            }

            void loop()
            {
                if (Serial.available())
                {
                    // Read frequency and duty cycle values over Serial
                    String inputString = Serial.readStringUntil('\n'); 
                    inputString.trim();                                
                    int freqDutySplitIndex = inputString.indexOf(',');

                    if (freqDutySplitIndex != -1)
                    {
                        String freqStr = inputString.substring(0, freqDutySplitIndex);  
                        String dutyStr = inputString.substring(freqDutySplitIndex + 1); 

                        // Parse floats
                        float newFrequency = freqStr.toFloat();
                        int newDutyCycle = dutyStr.toInt();

                        // Validate frequency range
                        if (newFrequency >= 1 && newFrequency <= 2000000)
                        {
                            // Set the PWM frequency and apply duty cycle
                            if (SetPinFrequencySafe(pin, newFrequency))
                            {
                                value = map(constrain(newDutyCycle, 0, 100), 0, 100, 0, 255);
                                pwmWrite(pin, value);
                                Serial.print("Set frequency to: ");
                                Serial.print(newFrequency);
                                Serial.print(" Hz, duty cycle to: ");
                                Serial.print(newDutyCycle);
                                Serial.println("%");
                            }
                            else
                            {
                                Serial.println("Failed to set new frequency.");
                            }
                        }
                        else
                        {
                            Serial.println("Invalid frequency value.");
                        }
                    }
                    else
                    {
                        Serial.println("Invalid input format. Use frequency,dutycycle (e.g., 60,50).");
                    }
                }
            }
            ```

            This code allows you to modify the PWM signal on the fly. Open your serial monitor at `9600` baud and send commands using the format `{FREQUENCY},{DUTY_CYCLE}` (e.g., `60,50` for 60Hz at a 50% duty cycle).
          </Tab>
        </Tabs>
      </Step>
    </Steps>
  </Tab>
</Tabs>

***

## Synchronized Video Python Library (Linux)

In addition to our upcoming SDK, we currently provide a simple Python process to grab synchronized frames from your camera setups. *(Note: This may be phased out in favor of the fully featured SDK in the future.)*

<Card title="Synchronized Video Python Library" icon="github" href="https://github.com/DeepWaterExploration/synchronized_camera_python">
  Follow our GitHub guide to install and use our custom Linux, python library.
</Card>

***

## Synchronized Stitched Video

This will go over using GStreamer to synchronize and stitch two video feeds.
Then, creating a virtual device to use in other programs.

<Tabs>
  <Tab title="Linux">
    <Steps>
      <Step title="Install Required Packages">
        Install the necessary dependencies using the following command:

        ```bash theme={null}
        sudo apt install -y libx264-dev libjpeg-dev \
        libglib2.0-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
        gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
        gstreamer1.0-plugins-bad gstreamer1.0-libav libgstreamer-plugins-bad1.0-dev \
        gstreamer1.0-plugins-ugly gstreamer1.0-gl \
        v4l-utils
        ```
      </Step>

      <Step title="List Current Camera Devices">
        Run the following command to find your connected cameras:

        ```bash theme={null}
        v4l2-ctl --list-devices
        ```

        **Example output:**

        ```text theme={null}
        stellarHD Leader: stellarHD Lea (usb-0000:00:14.0-8.3):
            /dev/video0
            /dev/video1
            /dev/media0

        stellarHD Follower: stellarHD F (usb-0000:00:14.0-8.4):
            /dev/video2
            /dev/video3
            /dev/media1
        ```
      </Step>

      <Step title="Create a Virtual Device">
        Identify the device IDs of the two cameras you wish to use, then select a device ID number that is **not** currently listed.

        In this example, we will use `9` (as it is unused above) to create a virtual device named `stellarHD_stitched`. Update the `9` in the command below to match your chosen unused ID:

        ```bash theme={null}
        sudo modprobe v4l2loopback video_nr=9 \
        card_label=stellarHD_stitched exclusive_caps=1
        ```
      </Step>

      <Step title="Launch the GStreamer Pipeline">
        Launch the pipeline to create the stitched video and output the stream to your new virtual device:

        ```bash theme={null}
        gst-launch-1.0 -v \
        compositor name=mix \
            sink_0::xpos=0    sink_0::ypos=0   sink_0::alpha=1 \
            sink_1::xpos=1600 sink_1::ypos=0   sink_1::alpha=1 \
        ! jpegenc ! jpegdec ! videoconvert ! v4l2sink device=/dev/video9 \
        v4l2src device=/dev/video0 ! image/jpeg,width=1600,framerate=60/1 ! jpegdec ! videorate ! mix.sink_0 \
        v4l2src device=/dev/video2 ! image/jpeg,width=1600,framerate=60/1 ! jpegdec ! videorate ! mix.sink_1
        ```

        <Note>
          You must keep this command running while using the synchronized, stitched videos. You can modify this command to add more cameras, change resolutions/framerates, or alter the layout.
        </Note>
      </Step>

      <Step title="Use the Virtual Device in OpenCV">
        <Card title="OpenCV Starter Code" icon="github" href="https://github.com/DeepwaterExploration/opencv-guides">
          Follow our sample code to use `/dev/video9` in OpenCV.
        </Card>
      </Step>

      <Step title="Remove the Loopback Device">
        To safely remove the loopback device, you can either restart your computer or run the following sequence of commands:

        **1. Identify programs currently using the loopback device:**

        ```bash theme={null}
        sudo lsof /dev/video*
        ```

        **2. Kill the processes using their PID (Process ID) numbers:**

        ```bash theme={null}
        sudo kill <INSERT_PID_NUMBER>
        ```

        **3. Remove the loopback device:**

        ```bash theme={null}
        sudo modprobe -r v4l2loopback
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>
