# OAK USB deployment guide

OAK USB cameras use a USB-C cable for power and data. They support USB2 or USB3 (up to 10 Gbps) and connect to a host computer.

### Installing requirements

Follow instructions below to install dependencies:

#### macOS & Windows

No installation script is needed.

#### Linux

Execute the commands below to setup the udev rules on Linux systems:

```bash
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="03e7", MODE="0666"' | sudo tee /etc/udev/rules.d/80-movidius.rules
sudo udevadm control --reload-rules && sudo udevadm trigger
```

Please refer to [Installation documentation](https://docs.luxonis.com/software/depthai/manual-install.md#supported-platforms) if
any issues occur.

### Connecting the USB device

Use a USB3 USB-C cable for maximum bandwidth. The host can power the device, but an external supply is recommended. See Powering
OAK Devices [here](#OAK%2520USB%2520deployment%2520guide-Debugging).

USB3 cables are usually blue inside the USB-A connector. If it is not blue, it may be a USB2 charging cable.

> Connecting via a long (>2m) USB cable might cause issues with the connection. If you need to use a longer cable, make sure it's
an
> **active**
> USB3 cable.

### Initial Connection

DepthAI scans USB devices, uploads firmware/pipeline/assets, and starts the pipeline.

The fastest test is [OAK Viewer](https://docs.luxonis.com/software-v3/depthai/tools/oak-viewer.md). If it finds the device and
streams frames, the connection is working.

### Debugging

If you see RuntimeError: No available devices, DepthAI did not find a device. Confirm power and a USB3 connection, then use the
tabs below.

We recommend using the latest depthai version before any kind of troubleshooting.

#### lsusb

When executing lsusb | grep 03e7, you should see a similar line:

```bash
$ lsusb | grep 03e7
Bus 001 Device 120: ID 03e7:2485 Intel Movidius MyriadX
# Or, if the device is already booted, you should see:
# Bus 001 Device 001: ID 03e7:f63b Intel Myriad VPU [Movidius Neural Compute Stick]
```

If you use lsusb -v, it will show USB speed of 480 Mb/s when the device is waiting for connection. After connecting to the device,
depthai will upload the firmware, and the speed will change to 5 Gb/s (if USB3 is available).

If the bootloader is flashed, the device can enumerate as the bootloader first:

```bash
Bus 003 Device 038: ID 03e7:f63c Intel Luxonis Bootloader
```

FFC-style devices (OAK-FFC 4P/3P/...) can have the bootloader flashed, but it is not required. On a normal device without the
bootloader flashed, you should only see the device after an app boots (no bootloader entry).

When an app boots, it re-enumerates as:

```bash
Bus 002 Device 032: ID 03e7:f63b Intel Myriad VPU [Movidius Neural Compute Stick]
```

Only the booted device enumerates as USB3. If it stays at USB2 speed after the app starts, the most common cause is the host-side
USB port or cable. Try a different USB3 port, a shorter cable, or a powered USB3 hub.

When running dmesg -w and connecting your OAK camera to the host. You should see a similar output in the terminal:

```bash
/~$ dmesg -w

[223940.862544] usb 1-3.2: new high-speed USB device number 120 using xhci_hcd
[223940.963357] usb 1-3.2: New USB device found, idVendor=03e7, idProduct=2485, bcdDevice= 0.01
[223940.963364] usb 1-3.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[223940.963368] usb 1-3.2: Product: Movidius MyriadX
[223940.963371] usb 1-3.2: Manufacturer: Movidius Ltd.
[223940.963373] usb 1-3.2: SerialNumber: 03e72485
```

#### Powering OAK cameras

USB3 is capable of providing up to 900mA of current, while USB2 is capable of providing up to 500mA. OAK cameras draw between
500mA and 900mA, depending on the model and the current workload. Pro version of devices can draw up to 15W (at peaks), depending
on the usage of the IR projector and IR illumination LED. It also depends on the host computer. Some computers don't have a
powerful USB controllers (eg. SBCs like [RaspberryPi](https://docs.luxonis.com/hardware/platform/deploy/to-rpi.md), which has
eFuse at 1.2A). Host's USB controller will also affect how long of a USB cable you can use, as longer cables have higher
resistance and will drop the voltage more. You should externally power OAK cameras if:

 * You have additional USB devices connected to a RPi USB port that draw too much current. In total, RPi can supply 1.2A, so if
   all USB devices draw more than 1.2A, brownout will occur.
 * You have the Pro version of the OAK camera that has IR laser dot projector and IR illumination LED.

We always recommend externally powering your OAK camera, either via:

 * Barrel jack - on devices that have the barrel jack ([OAK-D](https://docs.luxonis.com/hardware/products/OAK-D.md), [OAK-FFC
   4P](https://docs.luxonis.com/hardware/products/OAK-FFC%25204P.md), [OAK-FFC
   3P](https://docs.luxonis.com/hardware/products/OAK-FFC%25203P.md))
 * Powered USB3 hub
 * With a [Y-adapter](https://docs.luxonis.com/hardware/products/Y-adapter.md)
 * For Pro devices, a 15W power adapter is needed with the Y-adapter to provide enough power.

The reason is that OAK have current spikes (especially when using Video Encoder and running AI inference) of up to 2W, which can
lead to brownout.

#### Linux udev rules

Linux requires a udev rule to allow the USB device to be accessed by non-root users. If permissions are not set, depthai will
throw an error like:

```bash
[warning] Insufficient permissions to communicate with X_LINK_BOOTED device having name "2.8". Make sure udev rules are set
```

The following rule has to be set to allow access to the USB device:

```bash
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="03e7", MODE="0666"' | sudo tee /etc/udev/rules.d/80-movidius.rules
sudo udevadm control --reload-rules && sudo udevadm trigger
```

#### Using USB2

If you have are using an USB2 cable/USB2 port, have a bad USB3 cable, or are using longer cable (eg. larger than 2.5 meters), you
might get an error like X_LINK_COMMUNICATION_NOT_OPEN or X_LINK_ERROR. In such cases, a workaround is to force USB2 communication.
This will upload a USB2-version of the firmware to the device, and will limit the communication speed to USB2.

To force USB2 mode, you set the maxUsbSpeed=dai.UsbSpeed.HIGH when creating the dai.Device object

```python
import depthai as dai
pipeline = dai.Pipeline()
# Populate you pipeline with nodes
# Force USB2 communication
with dai.Device(maxUsbSpeed=dai.UsbSpeed.HIGH) as device:
    # ...
```

> **Stuck in USB2 mode**
> Only the booted device enumerates as USB3. If it stays at USB2 speed after the app starts, the most common cause is the
host-side USB port or cable. Try a different USB3 port, a shorter cable, or a powered USB3 hub.

### Runtime

After connecting and uploading the pipeline, the connection should be stable, latency should be low (under 0.5 sec), and USB3
downlink should be about 2.5 Gbps.

### Debugging

See the debugging tabs below in case the application stops working or if the communication is slow.

#### Connection drop

If the connection drops after a period of time, there are a few potential issues that could be causing it.

#### Device crash

Device could crash due to a bug in the firmware, or in the user's application.

If there was a firmware bug or (user-defined) pipeline issue, a [Crash
report](https://docs.luxonis.com/software-v3/depthai/examples/crash_report.md) will get generated, which you should send over to
us (support@luxonis.com).

If no crash report was found when running the script linked above, it means that either it's a power issue, or connection dropped
(or, less likely, a hardware issue).

Alternatively, with depthai 2.28.0, automatic crash report generation is enabled by default. This will send the crash report to
Luxonis servers and will include:

 * crash dump itself
 * pipeline configuration
 * depthai version
 * host operating system version (MacOS/Linux/Windows)

It does not include any user data, images or NN models. It does however include the
[Script](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/script.md) node code. If you wish to disable
automatic crash report generation, you can do so by setting the environment variable DEPTHAI_DISABLE_CRASHDUMP_COLLECTION to 1.

#### Power issue

This is quite a common issue, and it's usually due to the USB controller not being able to provide enough power to the device. It
can work for some time and then crash due to a random power spikes.

Please read the section above on how to properly power the device (Initial Connection > Powering OAK cameras).

#### Communication issue

This is also a common issue if the device is moving (eg. on a moving robot), and the USB cable is not properly secured. It can
happen that an USB line gets interrupted (disconnected) for a split second, and the device will drop the connection.

If that is the case, we'd suggest using an USB3 screw locking cable ([Amazon
link](https://www.amazon.com/StarTech-com-Screw-Locking-Cable-10Gbps/dp/B09J99WGSF)).

#### Low speed / High latency

If you noticed low FPS and/or high message latency you should check the following:

 1. [OAK bandwidth test](https://github.com/luxonis/oak-examples/tree/master/random-scripts#oak-bandwidth-test) - Should be around
    2.5/1.0 Gbps with USB3
 2. [OAK Latency test](https://github.com/luxonis/oak-examples/tree/master/random-scripts#oak-latency-test) - Should be below 5ms

If you get lower bandwidth/latency as expected, you should check the [Low Latency
documentation](https://docs.luxonis.com/software/depthai/optimizing.md).

### Next Steps

After successfully deploying the device, you can use the following resources to learn more about the software ecosystem:

 1. [Software docs](https://docs.luxonis.com/software-v3.md)
 2. [DepthAI code examples](https://docs.luxonis.com/software-v3/depthai/examples.md)
