Skip to content

Commit

Permalink
Merge pull request #811 from luxonis/uart_example_script
Browse files Browse the repository at this point in the history
Added UART example from Script node
  • Loading branch information
Erol444 authored Apr 23, 2023
2 parents c22d7f8 + 677327e commit 0da2d02
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/source/samples/Script/script_uart.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Script UART communication
=========================

This example uses :ref:`Script` node for `UART communication <https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter>`__. Note that OAK
cameras don't have UART pins easily disposed, and we soldered wires on `OAK-FFC-4P <https://docs.luxonis.com/projects/hardware/en/latest/pages/DD2090.html>`__
to expose UART pins.

.. note::

This should only be run on OAK-FFC-4P, as other OAK cameras might have different GPIO configuration.

Demo
####

.. raw:: html

<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; height: auto;">
<iframe src="https://www.youtube.com/embed/nbS1BczO5sE" frameborder="0" allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe>
</div>


.. figure:: https://user-images.githubusercontent.com/18037362/232458809-a36dc418-6bb5-411f-9172-5130a926191d.jpg

Oscilloscope connected to the OAK-FFC-4P UART pins

Setup
#####

.. include:: /includes/install_from_pypi.rst

Source code
###########

.. tabs::

.. tab:: Python

Also `available on GitHub <https://github.com/luxonis/depthai-python/blob/main/examples/Script/script_uart.py>`__

.. literalinclude:: ../../../../examples/Script/script_uart.py
:language: python
:linenos:

.. tab:: C++

Not yet available


.. include:: /includes/footer-short.rst
1 change: 1 addition & 0 deletions docs/source/tutorials/code_samples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ are presented with code.
- :ref:`Script HTTP server` - Serve still image over HTTP response (only OAK-POE devices)
- :ref:`Script MJPEG server` - Serve MJPEG video stream over HTTP response (only OAK-POE devices)
- :ref:`Script NNData example` - Constructs :ref:`NNData` in Script node and sends it to the host
- :ref:`Script UART communication` - UART communication with Script node

.. rubric:: SpatialDetection

Expand Down
40 changes: 40 additions & 0 deletions examples/Script/script_uart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
'''
NOTE: This should only be run on OAK-FFC-4P, as other OAK cameras might have different GPIO configuration!
'''
import depthai as dai
import time

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

script = pipeline.create(dai.node.Script)
script.setScript("""
import serial
import time
ser = serial.Serial("/dev/ttyS0", baudrate=115200)
i = 0
while True:
i += 1
time.sleep(0.1)
serString = f'TEST_{i}'
ser.write(serString.encode())
""")
# Define script for output
script.setProcessor(dai.ProcessorType.LEON_CSS)


config = dai.Device.Config()
# Get argument first
GPIO = dai.BoardConfig.GPIO
config.board.gpio[15] = GPIO(GPIO.OUTPUT, GPIO.ALT_MODE_2)
config.board.gpio[16] = GPIO(GPIO.INPUT, GPIO.ALT_MODE_2)
config.board.uart[0] = dai.BoardConfig.UART()


with dai.Device(config) as device:
device.startPipeline(pipeline)
print("Pipeline started")
while True:
time.sleep(1)

0 comments on commit 0da2d02

Please sign in to comment.