Categories
Hardware Locomotion Vision

Robot prep 2: GPIO and Camera

So I’ve got the RPi camera images sending to my laptop now, after installing OpenCV4, and running the test code from https://github.com/jeffbass/imagezmq

Next, we need to try move the servos with code.

https://learn.adafruit.com/16-channel-pwm-servo-driver/python-circuitpython

ok so i installed these

First, if no pip3,

sudo apt-get update

sudo apt-get install python-pip3

Then,

sudo pip3 install adafruit-circuitpython-pca9685

sudo pip3 install adafruit-circuitpython-servokit

Adafruit make you copy paste line by line from here…

https://github.com/adafruit/Adafruit_CircuitPython_Motor

Ok looking in the example folder of that,..

from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
from adafruit_motor import servo
i2c = busio.I2C(SCL, SDA)

pca = PCA9685(i2c)
pca.frequency = 50

servo2 = servo.Servo(pca.channels[2])

for i in range(90):
servo2.angle = i
for i in range(90):
servo2.angle = 90 - i
pca.deinit()

i changed it to 90 degrees and got rid of the comments. It suggests a min and max for the servo.

I ran it and the servo got angry with me and wouldn’t stop. I had to unplug everything because it was eating up the cables in its madness.

Ok so datasheet of MG996R: https://www.electronicoscaldas.com/datasheet/MG996R_Tower-Pro.pdf

It keeps going if I plug just the power back in. It seems to rotate continuously. So something is f***ed. Rebooting RPi. It’s supposed to be 180 degree rotation. Will need to read up on servo GPIO forums.

I also tried the ‘fraction’ style code: https://github.com/adafruit/Adafruit_CircuitPython_Motor/blob/master/examples/motor_pca9685_servo_sweep.py

and it rotated and rotated.

So, I think it must be a continuous servo. Now that I look at the product https://mantech.co.za/ProductInfo.aspx?Item=15M8959 i see it was a continuous servo. Derp.

Ok so let’s see… continuous servo: https://github.com/adafruit/Adafruit_CircuitPython_Motor/blob/master/examples/motor_pca9685_continuous_servo.py

We need to set some limits, apparently these are the defaults.

servo7 = servo.ContinuousServo(pca.channels[7], min_pulse=750, max_pulse=2250)

and possibly set this using a calibrated servo, using the calibrate.py program

pca = PCA9685(i2c, reference_clock_speed=25630710)

https://github.com/adafruit/Adafruit_CircuitPython_PCA9685/tree/master/examples

ok. cool.


At a later date, testing the MG996R servo,

I needed to initialise the min_pulse value at 550, or it continuously rotated.

servo7 = servo.ContinuousServo(pca.channels[1], min_pulse=550, max_pulse=2250)

Categories
Vision

Installing OpenCV on RPi

I followed these instructions:

Install OpenCV 4 on Raspberry Pi 4 and Raspbian Buster

sudo apt-get -y update && sudo apt-get -y upgrade

sudo apt-get -y install build-essential cmake pkg-config

sudo apt-get -y install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev

sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

sudo apt-get -y install libxvidcore-dev libx264-dev

sudo apt-get -y install libfontconfig1-dev libcairo2-dev

sudo apt-get -y install libgdk-pixbuf2.0-dev libpango1.0-dev

sudo apt-get -y install libgtk2.0-dev libgtk-3-dev

sudo apt-get -y install libatlas-base-dev gfortran

sudo apt-get -y install libhdf5-dev libhdf5-serial-dev libhdf5-103

sudo apt-get -y install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5

sudo apt-get -y install python3-dev

but then needed two more libs installed, which I found here:

https://github.com/EdjeElectronics/TensorFlow-Object-Detection-on-the-Raspberry-Pi/issues/18

sudo apt install libilmbase23
sudo apt install libopenexr-dev

According to the PyImageSearch guy, opencv will be faster if we build from source, for RPi, but that takes an extra 2 hours. So no.

So the remaining parts were:

sudo wget https://bootstrap.pypa.io/get-pip.py  sudo python get-pip.py
sudo python3 get-pip.py
sudo rm -rf ~/.cache/pip
sudo pip install virtualenv virtualenvwrapper 

Add these to ~/.bashrc

export WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3source /usr/local/bin/virtualenvwrapper.sh

Then

source ~/.bashrc
mkvirtualenv cv -p python3
pip install "picamera[array]"
pip install opencv-contrib-python 

This gave this error: Could not find OpenSSL

ok so

sudo apt-get install libssl-dev

Categories
OpenCV Vision

Installing OpenCV on computer

OpenCV will be needed, for the robot to make sense of the camera input. We’ll have the camera on a raspberry pi of some sort.

pip install opencv-contrib-python

Someone made a useful website on OpenCV here https://www.pyimagesearch.com/

root@chrx:/opt/imagezmq/tests# python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute '__version'
>>> cv2.__version__
'4.2.0'

Just gotta make sure it uses python3, not python2.

We’ll run it on the raspberry pi (https://www.pyimagesearch.com/2018/09/19/pip-install-opencv/) eventually, but for now, just testing core features on GalliumOS. It’s also required by imagezmq (https://github.com/jeffbass/imagezmq)

The robot has to use its sensors to find chickens, eggs, and decide whether to walk or turn. Stimuli to trigger actions.

pip install zmq

Might also need ‘pip install imutils’

Copying imagezmq.py to the tests folder cause I don’t know shit about python and how to import it from the other folder. So here’s the server:

python3 test_1_receive_images.py

import sys
import cv2
import imagezmq

image_hub = imagezmq.ImageHub()
while True:  # press Ctrl-C to stop image display program
    image_name, image = image_hub.recv_image()
    cv2.imshow(image_name, image)
    cv2.waitKey(1)  # wait until a key is pressed
    image_hub.send_reply(b'OK')

And the client program:

python3 test_1_send_images.py


import sys
import time
import numpy as np
import cv2
import imagezmq

# Create 2 different test images to send
# A green square on a black background
# A red square on a black background

sender = imagezmq.ImageSender()
i = 0
image_window_name = 'From Sender'
while True:  # press Ctrl-C to stop image sending program
    # Increment a counter and print it's value to console
    i = i + 1
    print('Sending ' + str(i))

    # Create a simple image
    image = np.zeros((400, 400, 3), dtype='uint8')
    green = (0, 255, 0)
    cv2.rectangle(image, (50, 50), (300, 300), green, 5)

    # Add counter value to the image and send it to the queue
    cv2.putText(image, str(i), (100, 150), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 4)
    sender.send_image(image_window_name, image)
    time.sleep(1)

Cool it sent pics to my screen. Heh “Hershey Simplex” sounds more like a virus than a font.