Using the pan tilt output pins

Great lesion 58 went really smooth. When I tried to turn on some LEDs using the THRU pins on the PAN/TILD. to indicate which direction it was moving.
The camera locks up. Error says try setting BOARD or BCM. I have set it up both ways and used different pins. But still get the error.
Seems I may be interfering with the servo signals. But I am not using 12 or 13 outputs to light my LEDs.
Any helpful suggestions would be appreciated . In terminal mode LEDS work fine . But when running you tracking example using Thonny it
hangs the camera up and I get the ( try to set mode BCM or BOARD. And I have tried both. Error pops up when i turn on output. Up till then
it all works fine.

Can you provide us with the code you wrote so we can test it?

Thank you for your response. See attached .
If I put # on line 109 the camera runs great.
If I remove # and try to turn on output. Program locks up.

Also Line 9 I do not know if camera pan/tilt feed thru are BCM or BOARD I think it should be BOARD.
Tried both but program still hangs up due to line 109.

I am trying to turn on 4 LED’s to simulate output in all 4 directions PAN +/- and Tilt +/-

Any help is greatly appreciated
Mike

(Attachment openCV-10.py is missing)

import cv2
from picamera2 import Picamera2
import time
import numpy as np
from servo import Servo
#picam2 = Picamera2()
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(17,GPIO.OUT)

pan=Servo(pin=13)
tilt=Servo(pin=12)

panAngle=0
tiltAngle=0

pan.set_angle(panAngle)
tilt.set_angle(tiltAngle)

picam2 = Picamera2()
dispW=1280
dispH=720
picam2.preview_configuration.main.size = (dispW,dispH)
picam2.preview_configuration.main.format = “RGB888”
picam2.preview_configuration.controls.FrameRate=30
picam2.preview_configuration.align()
picam2.configure(“preview”)
picam2.start()
fps=0
pos=(30,60)
font=cv2.FONT_HERSHEY_SIMPLEX
height=1.5
weight=3
myColor=(0,0,255)
track=0

def onTrack1(val):
global hueLow
hueLow=val
print(‘Hue Low’,hueLow)
def onTrack2(val):
global hueHigh
hueHigh=val
print(‘Hue High’,hueHigh)
def onTrack3(val):
global satLow
satLow=val
print(‘Sat Low’,satLow)
def onTrack4(val):
global satHigh
satHigh=val
print(‘Sat High’,satHigh)
def onTrack5(val):
global valLow
valLow=val
print(‘Val Low’,valLow)
def onTrack6(val):
global valHigh
valHigh=val
print(‘Val High’,valHigh)
def onTrack7(val):
global track
track=val
print(‘Track Value’ ,track)

cv2.namedWindow(‘myTracker’)

cv2.createTrackbar(‘Hue Low’,‘myTracker’,10,179,onTrack1)
cv2.createTrackbar(‘Hue High’,‘myTracker’,20,179,onTrack2)
cv2.createTrackbar(‘Sat Low’,‘myTracker’,100,255,onTrack3)
cv2.createTrackbar(‘Sat High’,‘myTracker’,2555,255,onTrack4)
cv2.createTrackbar(‘Val Low’,‘myTracker’,100,255,onTrack5)
cv2.createTrackbar(‘Val High’,‘myTracker’,255,255,onTrack6)
cv2.createTrackbar(‘Train-0 Track-1’,‘myTracker’ ,0,1,onTrack7)

while True:
tStart=time.time()
frame= picam2.capture_array()
frame=cv2.flip(frame,-1)
frameHSV=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
cv2.putText(frame,str(int(fps))+’ FPS’,pos,font,height,myColor,weight)
lowerBound=np.array([hueLow,satLow,valLow])
upperBound=np.array([hueHigh,satHigh,valHigh])
myMask=cv2.inRange(frameHSV,lowerBound,upperBound)
myMaskSmall=cv2.resize(myMask,(int(dispW/2),int(dispH/2)))
myObject=cv2.bitwise_and(frame,frame, mask=myMask)
myObjectSmall=cv2.resize(myObject,(int(dispW/2),int(dispH/2)))

contours,junk=cv2.findContours(myMask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
contours=sorted(contours,key=lambda x:cv2.contourArea(x),reverse=True)
#cv2.drawContours(frame,contours,-1,(255,0,0),3)
contour=contours[0]
x,y,w,h=cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
if track==1:
error=(x+w/2)-dispW/2
if error>35:
panAngle=panAngle-1
GPIO.output(17,1)
if panAngle<-90:
panAngle=-90
pan.set_angle(panAngle)

if error<-35:
panAngle=panAngle+1
if panAngle>90:
panAngle=90
pan.set_angle(panAngle)
tiltError=(y+h/2)-dispH/2
if tiltError>35:
tiltAngle=tiltAngle+1
if tiltAngle>40:
tiltAngle=40
tilt.set_angle(tiltAngle)
if tiltError<-35:
tiltAngle=tiltAngle-1
if tiltAngle<-90:
tiltAngle=-90
tilt.set_angle(tiltAngle)

cv2.imshow(“Camera”, frame)
cv2.imshow(‘Mask’,myMaskSmall)
cv2.imshow(‘My Object’,myObjectSmall)
if cv2.waitKey(1)==ord(‘q’):
break
GPIO.cleanup()
tEnd=time.time()
loopTime=tEnd-tStart
fps=.9fps + .1(1/loopTime)
cv2.destroyAllWindows()

It rejected attach py file so I sent text . Hope this helps

I am trying to turn on a LED line 109 , If PAN is told to go - . In the end I want to turn on 4 led PAN +/- and Tilt +/-
Just trying one now.

If I put # in front of line 109. Camera runs great . But of course not output.

Also line 9 I do not know if the feed thru pins on the camera board are BCM or BOARD. I think BOARD. Tried both.
still did not work and camera program still hangs up.
I tried to send attachment with py file. Your system rejected it twice. So I am sending it as text below.

Ant help is appreciated
Mike

import cv2
from picamera2 import Picamera2
import time
import numpy as np
from servo import Servo
#picam2 = Picamera2()
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(17,GPIO.OUT)

pan=Servo(pin=13)
tilt=Servo(pin=12)

panAngle=0
tiltAngle=0

pan.set_angle(panAngle)
tilt.set_angle(tiltAngle)

picam2 = Picamera2()
dispW=1280
dispH=720
picam2.preview_configuration.main.size = (dispW,dispH)
picam2.preview_configuration.main.format = “RGB888”
picam2.preview_configuration.controls.FrameRate=30
picam2.preview_configuration.align()
picam2.configure(“preview”)
picam2.start()
fps=0
pos=(30,60)
font=cv2.FONT_HERSHEY_SIMPLEX
height=1.5
weight=3
myColor=(0,0,255)
track=0

def onTrack1(val):
global hueLow
hueLow=val
print(‘Hue Low’,hueLow)
def onTrack2(val):
global hueHigh
hueHigh=val
print(‘Hue High’,hueHigh)
def onTrack3(val):
global satLow
satLow=val
print(‘Sat Low’,satLow)
def onTrack4(val):
global satHigh
satHigh=val
print(‘Sat High’,satHigh)
def onTrack5(val):
global valLow
valLow=val
print(‘Val Low’,valLow)
def onTrack6(val):
global valHigh
valHigh=val
print(‘Val High’,valHigh)
def onTrack7(val):
global track
track=val
print(‘Track Value’ ,track)

cv2.namedWindow(‘myTracker’)

cv2.createTrackbar(‘Hue Low’,‘myTracker’,10,179,onTrack1)
cv2.createTrackbar(‘Hue High’,‘myTracker’,20,179,onTrack2)
cv2.createTrackbar(‘Sat Low’,‘myTracker’,100,255,onTrack3)
cv2.createTrackbar(‘Sat High’,‘myTracker’,2555,255,onTrack4)
cv2.createTrackbar(‘Val Low’,‘myTracker’,100,255,onTrack5)
cv2.createTrackbar(‘Val High’,‘myTracker’,255,255,onTrack6)
cv2.createTrackbar(‘Train-0 Track-1’,‘myTracker’ ,0,1,onTrack7)

while True:
tStart=time.time()
frame= picam2.capture_array()
frame=cv2.flip(frame,-1)
frameHSV=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
cv2.putText(frame,str(int(fps))+’ FPS’,pos,font,height,myColor,weight)
lowerBound=np.array([hueLow,satLow,valLow])
upperBound=np.array([hueHigh,satHigh,valHigh])
myMask=cv2.inRange(frameHSV,lowerBound,upperBound)
myMaskSmall=cv2.resize(myMask,(int(dispW/2),int(dispH/2)))
myObject=cv2.bitwise_and(frame,frame, mask=myMask)
myObjectSmall=cv2.resize(myObject,(int(dispW/2),int(dispH/2)))

contours,junk=cv2.findContours(myMask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
contours=sorted(contours,key=lambda x:cv2.contourArea(x),reverse=True)
#cv2.drawContours(frame,contours,-1,(255,0,0),3)
contour=contours[0]
x,y,w,h=cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
if track==1:
error=(x+w/2)-dispW/2
if error>35:
panAngle=panAngle-1
GPIO.output(17,1)
if panAngle<-90:
panAngle=-90
pan.set_angle(panAngle)

if error<-35:
panAngle=panAngle+1
if panAngle>90:
panAngle=90
pan.set_angle(panAngle)
tiltError=(y+h/2)-dispH/2
if tiltError>35:
tiltAngle=tiltAngle+1
if tiltAngle>40:
tiltAngle=40
tilt.set_angle(tiltAngle)
if tiltError<-35:
tiltAngle=tiltAngle-1
if tiltAngle<-90:
tiltAngle=-90
tilt.set_angle(tiltAngle)

cv2.imshow(“Camera”, frame)
cv2.imshow(‘Mask’,myMaskSmall)
cv2.imshow(‘My Object’,myObjectSmall)
if cv2.waitKey(1)==ord(‘q’):
break
GPIO.cleanup()
tEnd=time.time()
loopTime=tEnd-tStart
fps=.9fps + .1(1/loopTime)
cv2.destroyAllWindows()

I tested your code and it basically works fine, of course I’m not quite sure if the indentation of your code is correct, and if the mentioned line #109 refers to GPIO.output(17, 1), here are some of my suggestions:

  1. GPIO.setmode() should use the (GPIO.BCM) parameter, for 17, the BCM serial number is valid, and the BOARD serial number, 17 corresponds to 3.3V

  2. You can use wringpi tool to check the io status of pi by gpio readall command during installation.
    wiringPi updated to 2.52 for the Raspberry Pi 4B | Wiring Pi

  3. the warning “This channel is already in use” when running in thonny is due to not executing GPIO.cleanup() correctly when exiting, but it doesn’t affect the use.

  4. you need to define and pre-assign variables to the hsv values, otherwise the values will only correspond to the values shown in the window if you drag each slider.
    101901

  5. It is recommended that the loop in the code use try, finally, to ensure that the end of the correct execution of some processing.
    101902

  6. If sufficient, it is recommended that the camera image resolution be set to 800*600 to ensure a clear image and to increase the speed of operation.

It’s working great now.
Thank you for the great support.
Mike

1 Like