Raspberry Pi & OpenCV – UART Communication (Part 2)

In this second post regarding my experiments with OpenCV on the Raspberry pi, I will go over interfacing the OpenCV code with the Pi’s UART, so that I can send image centroids to the auxiliary MCU that will be used for motor control. These posts will be somewhere between a tutorial and a build log – all steps will be mentioned, but not in great detail.

Part 1: http://richarthurs.com/2017/08/20/getting-started-with-opencv-and-raspberry-pi/

 

Configuring UART

First, follow the instructions here: https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/

 

I disabled the UART>console connection using the raspi-config (sudo raspi-config), meaning that I didn’t need to manually remove it from the cmdline.txt file.

 

Testing UART

On my Pi 3, the UART connected to GPIO 14 and 15 is available at /dev/serial0. On the Pi, run the following commands to install the screen utility and get an instance running.

sudo apt-get install screen
screen /dev/serial0 115200

If you short GPIO 14 and 15, you should see the entered characters echoed back on your display. To quit a screen instance on the pi, press escape, then ctrl+a, then \

 

Laptop Test

I wanted to test out communication to my laptop, so I grabbed a package to install screen. After installing, I connected a 3.3V USB-UART converter, being sure to connect Pi Rx to converter TX, and vice-versa. Also connect a GND line between the Pi and the converter, since they aren’t likely connected to the same power source.

To find the location of your serial converter on mac or linux, run ls /dev and look for something along the lines of “tty.usbserial-A50285BI.” This is the equivalent of serial0 on the Pi.  Run the following command to start a screen instance:

screen /dev/_your_adapter_address_here 115200

Assuming you have screen instances running on the Pi and computer simultaneously, you should see characters echoed between the two as you type on either keyboard. I initially forgot to connect a common GND between the two devices, which caused me to only receive on my computer from the Pi, but not from my computer.

 

To quit screen on mac or linux, ctrl + a, then :, then “quit” (no quotes).

 

PySerial

PySerial is an excellent and easy to use serial communication library that I’ve used before. We’ll bring it into this project on the Pi, and it can be installed with pip install pyserial. 

I wrote up the following test script and ran it with python serialtest.py.

import serial 
ser = serial.Serial('/dev/serial0', 115200, timeout = 1) 
print ser.name 

for i in range(0,5,1):
    ser.write(str(i))
ser.close()

After confirming that everything worked as expected, we are now ready to test integration with the rest of the OpenCV code and confirm that the centroid locations are being sent over correctly.

 

OpenCV Integration

Since I was already printing the centroid locations to the terminal, I just needed to replace the print() commands with ser.write() and some internal string formatting to include a return and newline. Running the code and looking at the UART output from the Pi, we can see that it is correctly sending the x,y coordinates of the centroid as I move the camera around.

Screen output showing centroid locations coming from the Pi over UART

 

Next Steps

Now that serial communication is working and it can be easily integrated with the rest of the OpenCV code, I need to determine if I want to use any sort of protocol between the auxiliary MCU and the Pi. Since I will likely need to send data back and forth, it is probably a good idea to keep everything well defined. More on that in the next post!

 

Reviewing the task list in the first post, I’d say I’m about half way through part 3.

  1. Install and compile OpenCV on the pi
  2. Track and find centroid of an object
  3. Send out obstacle centroid over UART
    1. Integrate OpenCV and pySerial
    2. Determine message protocol
  4. Get a simple motor control system on the auxiliary MCU working
  5. Start developing obstacle avoidance functionality

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *