I am using my raspberry pi to try to complete the task, which is taking a picture in, then use the function get_angle to find the angle of reference of the blue dots in the picture. Then, turing the survo motor to aim the angle. However when I run my program I got this error:
/home/ricky/Desktop/moduleç»ä¹ /module6.py:238: RuntimeWarning: This channel is already >in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(PWM_pin, GPIO.OUT) # Create PWM instance for pin w freqency [0:07:40.899633954] [2414] INFO Camera camera_manager.cpp:327 libcamera v0.4.0+50-83cb8101 [0:07:40.926652928] [2435] WARN RPiSdn sdn.cpp:40 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise [0:07:40.929008930] [2435] INFO RPI vc4.cpp:447 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media2 and ISP device /dev/media0 [0:07:40.929134722] [2435] INFO RPI pipeline_base.cpp:1121 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
I modified my code to be this:
from picamera2 import Picamera2
import cv2
import numpy as np
import time
import math
import argparse
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
PWM_pin = 32 #Define pin, frequency and duty cycle
freq = 50
dutyCycle = 7.5 #90 degrees at first#
#Values 0 - 100 (represents 4%, ~27 deg)
GPIO.setup(PWM_pin, GPIO.OUT) # Create PWM instance for pin w freqency
pwm = GPIO.PWM(PWM_pin, freq)
picam2 = Picamera2()
parser=argparse.ArgumentParser(description="lower and upper bound for trying")
parser.add_argument('--tim',default=10,help="length of time to run")
parser.add_argument('--delay',default=0.5,help="time between image captures")
parser.add_argument('--debug',action='store_true',default=False,help="debug mode")
args=parser.parse_args()
def get_duty(direction)
duty=(1/18)*direction +2.5
return duty
def get_angle(img,debug):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (90, 20, 130), (110, 255, 255))
cv2.imwrite('masked.jpg', mask)
M = cv2.moments(mask)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
if M['m00']==0:
angle=360
print('no center of mass found')
else:
img_2=img.copy()
img_2 = cv2.circle(img_2, (cX,cY), 5, (0,0,255), 2) # red circle placed on the center of mass
cv2.imwrite('center_of_mass.jpg', img_2)
angle=180/math.pi*(math.atan((cX-0.5*img.shape[1])/(img.shape[0]-cY))) #calculate angle of reference
print(f'''angle in degree {angle:0.2f}''')
if debug== True:
print(f'center of mass:({cX},{cY})')
cv2.imshow("masked",mask)
cv2.imshow("center_of_mass",img_2)
cv2.waitKey(1000)
cv2.destroyAllWindows()
return angle
start_time=time.time()
now_time=time.time()
while now_time-start_time<args.tim:
img_RAM = picam2.capture_array("main") #take picture into memory
cv2.imshow("picture",img_RAM)
cv2.waitKey(1000)
cv2.destroyAllWindows()
# debug=args.debug
# # img_test = cv2.imread("bluetape.jpg")
# angle=get_angle(img_RAM,debug) #call function
# if -91 <angle< 91:
# real_angle= angle+90 #real angle from horizontal
# dutyCycle=get_duty(real_angle)
# pwm.ChangeDutyCycle(dutyCycle)
# if debug ==True:
# print(f'PWM:{dutyCycle}')
# else:
# pass
now_time=time.time()
time.sleep(args.delay)'''
You can see how I add a "imshow" in the while loop and a "print('no center of mass found')" in the function get_angle. I would expect the picture to show up for a second and the screen printing the message of "no center of mass found" cause I just take a picture of the walls and there are no blue dots in the picture.
Strange enough, it does not print out anything, and no pictures shows up. Also, it does not automatically end for after 10s.
resolved, forget to put picam2.start(). It should be placed in at the start of the while loop.