Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

SPI output stops while XLink and the rest of the pipeline keep running #20

Open
BeatsByGilb opened this issue Mar 24, 2022 · 2 comments

Comments

@BeatsByGilb
Copy link

I'm running into a really weird issue. I've got a pipeline that sends a lot of data to the host over serial and just spatial detection data to the ESP32 over SPI. After a few minutes (2-10) of running, one of two things will happen:

  1. The whole pipeline crashes with the message "[system] [critical] Fatal error. Please report to the developers. Log: 'PlgWarpHW' '741'. It looks like this issue was addressed in this pull request, which I am just waiting to be merged into main.
  2. The SPI output appears to die while all the XLink outputs still work fine. There are no error messages on the host, and the ESP32 outputs "Timeout: no response from remote device..." and "failed to recv packet" on repeat.

I've tried checking the temperatures using the SystemLogger node, and the temps were around 80 C when the SPI output stopped working the first time, (although this isn't consistent; I've seen it work at 90 C), which is well within the 105 C limit. Any thoughts on what might be causing number 2 and how I could address this issue?

I'm working on getting a minimal example that reproduces the code. I've been able to recreate the issue by adapting the spatial-mobilenet example to use the same pipeline I'm using. Now I'm trying to figure out what I can remove while still making the problem occur.

@BeatsByGilb
Copy link
Author

BeatsByGilb commented Mar 25, 2022

Here's some minimal(ish) reproducible code, meant to be run on the host, with the spatial_image_detections code running on the ESP32. The lines that are commented out are lines that make the pipeline more like the one I'm using in my real application, but I was able to still make the SPI output stop working without these lines.

#!/usr/bin/env python3

from pathlib import Path
import sys
import cv2
import depthai as dai
import numpy as np
import time
from time import sleep

"""
Spatial detection network demo over SPI.
    Performs inference on RGB camera and retrieves spatial location coordinates: x,y,z relative to the center of depth map.
"""

spiOut = True

# MobilenetSSD label texts
labelMap = [
    "background",
    "aeroplane",
    "bicycle",
    "bird",
    "boat",
    "bottle",
    "bus",
    "car",
    "cat",
    "chair",
    "cow",
    "diningtable",
    "dog",
    "horse",
    "motorbike",
    "person",
    "pottedplant",
    "sheep",
    "sofa",
    "train",
    "tvmonitor",
]

syncNN = False

# Get argument first
nnBlobPath = "/home/depthai_testing/submodules/depthai-python/examples/models/mobilenet-ssd_openvino_2021.4_5shave.blob"
# nnBlobPath = str(
#     (Path(/home/depthai_testing/submodules/depthai-python/examples/models/mobilenet-ssd_openvino_2021.4_5shave.blob)
#     .resolve()
#     .absolute()
# )
if len(sys.argv) > 1:
    nnBlobPath = sys.argv[1]

# Start defining a pipeline
pipeline = dai.Pipeline()

# Define a source - color camera
colorCam = pipeline.create(dai.node.ColorCamera)
spatialDetectionNetwork = pipeline.create(dai.node.MobileNetSpatialDetectionNetwork)
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
stereo = pipeline.create(dai.node.StereoDepth)


xoutRgb = pipeline.create(dai.node.XLinkOut)
# xoutNN = pipeline.create(dai.node.XLinkOut)
# xoutBoundingBoxDepthMapping = pipeline.create(dai.node.XLinkOut)
xoutDepth = pipeline.create(dai.node.XLinkOut)
# serial_out_imu = pipeline.create(dai.node.XLinkOut)
serial_out_confidence_map = pipeline.create(dai.node.XLinkOut)
serial_out_left = pipeline.create(dai.node.XLinkOut)
serial_out_right = pipeline.create(dai.node.XLinkOut)
serial_out_sys_info = pipeline.create(dai.node.XLinkOut)

xoutRgb.setStreamName("rgb")
# xoutNN.setStreamName("detections")
# xoutBoundingBoxDepthMapping.setStreamName("boundingBoxDepthMapping")
xoutDepth.setStreamName("depth")
# serial_out_imu.setStreamName("imu")
serial_out_confidence_map.setStreamName("conf_map")
serial_out_left.setStreamName("left")
serial_out_right.setStreamName("right")
serial_out_sys_info.setStreamName("sys_info")

spiOutSpatialNN = pipeline.create(dai.node.SPIOut)
spiOutSpatialNN.input.setBlocking(False)
spiOutSpatialNN.input.setQueueSize(1)
spiOutSpatialNN.setStreamName("spimetaout")
spiOutSpatialNN.setBusId(0)


colorCam.setPreviewSize(300, 300)
colorCam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
colorCam.setInterleaved(False)
colorCam.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
# colorCam.setFps(30.0)
# colorCam.setBoardSocket(dai.CameraBoardSocket.RGB)
# colorCam.setPreviewKeepAspectRatio(False)
# colorCam.initialControl.setManualFocus(120)
# colorCam.setIspScale(1, 3)

monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
# monoLeft.setFps(30.0)
monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
# monoRight.setFps(30.0)
monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)

# setting node configs
stereo.initialConfig.setConfidenceThreshold(255)
stereo.setDepthAlign(dai.CameraBoardSocket.RGB)
stereo.setLeftRightCheck(True)

spatialDetectionNetwork.setBlobPath(nnBlobPath)
spatialDetectionNetwork.setConfidenceThreshold(0.5)
spatialDetectionNetwork.input.setBlocking(False)
spatialDetectionNetwork.setBoundingBoxScaleFactor(0.5)
spatialDetectionNetwork.setDepthLowerThreshold(100)
spatialDetectionNetwork.setDepthUpperThreshold(5000)

# imu_sensors = [
#     dai.IMUSensor.ACCELEROMETER_RAW,
#     dai.IMUSensor.GYROSCOPE_RAW,
#     dai.IMUSensor.MAGNETOMETER_RAW,
#     dai.IMUSensor.ROTATION_VECTOR,
# ]
# imu = pipeline.create(dai.node.IMU)
# imu.enableIMUSensor(imu_sensors, 50)
# imu.setBatchReportThreshold(1)  # Minimum packets per message
# imu.setMaxBatchReports(10)  # Maximum packets per message

sys_info = pipeline.create(dai.node.SystemLogger)


# Create outputs
monoLeft.out.link(stereo.left)
monoRight.out.link(stereo.right)

colorCam.preview.link(spatialDetectionNetwork.input)
stereo.depth.link(spatialDetectionNetwork.inputDepth)

if syncNN:
    spatialDetectionNetwork.passthrough.link(xoutRgb.input)
else:
    colorCam.preview.link(xoutRgb.input)

# spatialDetectionNetwork.out.link(xoutNN.input)
# spatialDetectionNetwork.boundingBoxMapping.link(xoutBoundingBoxDepthMapping.input)

spatialDetectionNetwork.passthroughDepth.link(xoutDepth.input)

# imu.out.link(serial_out_imu.input)
stereo.confidenceMap.link(serial_out_confidence_map.input)
monoLeft.out.link(serial_out_left.input)
monoRight.out.link(serial_out_right.input)
sys_info.out.link(serial_out_sys_info.input)

spatialDetectionNetwork.out.link(spiOutSpatialNN.input)


def printSystemInformation(info):
    if info is None:
        return
    m = 1024 * 1024  # MiB
    print(
        f"Ddr used / total - {info.ddrMemoryUsage.used / m:.2f} / {info.ddrMemoryUsage.total / m:.2f} MiB"
    )
    print(
        f"Cmx used / total - {info.cmxMemoryUsage.used / m:.2f} / {info.cmxMemoryUsage.total / m:.2f} MiB"
    )
    print(
        f"LeonCss heap used / total - {info.leonCssMemoryUsage.used / m:.2f} / {info.leonCssMemoryUsage.total / m:.2f} MiB"
    )
    print(
        f"LeonMss heap used / total - {info.leonMssMemoryUsage.used / m:.2f} / {info.leonMssMemoryUsage.total / m:.2f} MiB"
    )
    t = info.chipTemperature
    print(
        f"Chip temperature - average: {t.average:.2f}, css: {t.css:.2f}, mss: {t.mss:.2f}, upa: {t.upa:.2f}, dss: {t.dss:.2f}"
    )
    print(
        f"Cpu usage - Leon CSS: {info.leonCssCpuUsage.average * 100:.2f}%, Leon MSS: {info.leonMssCpuUsage.average * 100:.2f} %"
    )
    print("----------------------------------------")


# Pipeline defined, now the device is connected to
with dai.Device(pipeline) as device:
    # Start pipeline
    # device.startPipeline()

    # if not spiOut:
    # Output queues will be used to get the rgb frames and nn data from the outputs defined above
    previewQueue = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
    # detectionNNQueue = device.getOutputQueue(
    #     name="detections", maxSize=4, blocking=False
    # )
    # xoutBoundingBoxDepthMapping = device.getOutputQueue(
    #     name="boundingBoxDepthMapping", maxSize=4, blocking=False
    # )
    depthQueue = device.getOutputQueue(name="depth", maxSize=4, blocking=False)
    # imu_queue = device.getOutputQueue(name="imu", maxSize=4, blocking=False)
    conf_map_queue = device.getOutputQueue(name="conf_map", maxSize=4, blocking=False)
    left_queue = device.getOutputQueue(name="left", maxSize=4, blocking=False)
    right_queue = device.getOutputQueue(name="right", maxSize=4, blocking=False)
    sys_info_queue = device.getOutputQueue(name="sys_info", maxSize=4, blocking=False)

    frame = None
    detections = []

    start_time = time.monotonic()
    counter = 0
    fps = 0
    color = (255, 255, 255)

    while True:
        # if spiOut:
        #     sleep(1)
        #     continue

        inPreview = previewQueue.get()
        # inNN = detectionNNQueue.get()
        depth = depthQueue.get()
        conf_map = conf_map_queue.get()
        left = left_queue.get()
        right = right_queue.get()
        # while imu_queue.has():
        #     imu_queue.get()
        printSystemInformation(sys_info_queue.tryGet())

        counter += 1
        current_time = time.monotonic()
        if (current_time - start_time) > 1:
            fps = counter / (current_time - start_time)
            counter = 0
            start_time = current_time

        frame = inPreview.getCvFrame()
        depthFrame = depth.getFrame()
        conf_map_frame = conf_map.getCvFrame()
        left_frame = left.getCvFrame()
        right_frame = right.getCvFrame()

        depthFrameColor = cv2.normalize(
            depthFrame, None, 255, 0, cv2.NORM_INF, cv2.CV_8UC1
        )
        depthFrameColor = cv2.equalizeHist(depthFrameColor)
        depthFrameColor = cv2.applyColorMap(depthFrameColor, cv2.COLORMAP_HOT)
        # detections = inNN.detections
        # if len(detections) != 0:
        #     boundingBoxMapping = xoutBoundingBoxDepthMapping.get()
        #     roiDatas = boundingBoxMapping.getConfigData()

        #     for roiData in roiDatas:
        #         roi = roiData.roi
        #         roi = roi.denormalize(
        #             depthFrameColor.shape[1], depthFrameColor.shape[0]
        #         )
        #         topLeft = roi.topLeft()
        #         bottomRight = roi.bottomRight()
        #         xmin = int(topLeft.x)
        #         ymin = int(topLeft.y)
        #         xmax = int(bottomRight.x)
        #         ymax = int(bottomRight.y)

        #         cv2.rectangle(
        #             depthFrameColor,
        #             (xmin, ymin),
        #             (xmax, ymax),
        #             color,
        #             cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
        #         )

        # # if the frame is available, draw bounding boxes on it and show the frame
        # height = frame.shape[0]
        # width = frame.shape[1]
        # for detection in detections:
        #     # denormalize bounding box
        #     x1 = int(detection.xmin * width)
        #     x2 = int(detection.xmax * width)
        #     y1 = int(detection.ymin * height)
        #     y2 = int(detection.ymax * height)
        #     try:
        #         label = labelMap[detection.label]
        #     except:
        #         label = detection.label
        #     cv2.putText(
        #         frame,
        #         str(label),
        #         (x1 + 10, y1 + 20),
        #         cv2.FONT_HERSHEY_TRIPLEX,
        #         0.5,
        #         color,
        #     )
        #     cv2.putText(
        #         frame,
        #         "{:.2f}".format(detection.confidence * 100),
        #         (x1 + 10, y1 + 35),
        #         cv2.FONT_HERSHEY_TRIPLEX,
        #         0.5,
        #         color,
        #     )
        #     cv2.putText(
        #         frame,
        #         f"X: {int(detection.spatialCoordinates.x)} mm",
        #         (x1 + 10, y1 + 50),
        #         cv2.FONT_HERSHEY_TRIPLEX,
        #         0.5,
        #         color,
        #     )
        #     cv2.putText(
        #         frame,
        #         f"Y: {int(detection.spatialCoordinates.y)} mm",
        #         (x1 + 10, y1 + 65),
        #         cv2.FONT_HERSHEY_TRIPLEX,
        #         0.5,
        #         color,
        #     )
        #     cv2.putText(
        #         frame,
        #         f"Z: {int(detection.spatialCoordinates.z)} mm",
        #         (x1 + 10, y1 + 80),
        #         cv2.FONT_HERSHEY_TRIPLEX,
        #         0.5,
        #         color,
        #     )

        #     cv2.rectangle(frame, (x1, y1), (x2, y2), color, cv2.FONT_HERSHEY_SIMPLEX)

        # cv2.putText(
        #     frame,
        #     "NN fps: {:.2f}".format(fps),
        #     (2, frame.shape[0] - 4),
        #     cv2.FONT_HERSHEY_TRIPLEX,
        #     0.4,
        #     color,
        # )
        cv2.imshow("depth", depthFrameColor)
        cv2.imshow("confidence", conf_map_frame)
        cv2.imshow("left", left_frame)
        cv2.imshow("right", right_frame)
        cv2.imshow("rgb", frame)

        if cv2.waitKey(1) == ord("q"):
            break

@BeatsByGilb
Copy link
Author

After some offline conversation with @themarpe , we managed to find a workaround for this. The issue seems to be caused by the SPI clock speed. I changed this from 16 MHz to 4 MHz in components/depthai-spi-api/common/esp32_spi_impl.c:

 //Configuration for the SPI device on the other side of the bus
    spi_device_interface_config_t devcfg={
        .command_bits=0,
        .address_bits=0,
        .dummy_bits=0,
        .clock_speed_hz=4000000,
  ...

After this change, I was having trouble getting the ESP32 to receive any data from the Myriad X at all. The issue here ended up being that the Myriad X needs to be running before the ESP32 tries to initialize a connection. I was able to achieve this in a hacky way by adding a delay at the beginning of the ESP32 code, but it means that restarting the pipeline requires me to:

  1. Power cycle the entire camera.
  2. Manually start the pipeline during the ESP32 delay.

It's not ideal, but it gets the job done until a more elegant solution can be implemented.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant