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: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.
I modified my code to be this:/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'
Code:
from picamera2 import Picamera2 import cv2 import numpy as np import timeimport mathimport argparseimport RPi.GPIO as GPIOGPIO.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.5return dutydef 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=360print('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 masscv2.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 referenceprint(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 anglestart_time=time.time()now_time=time.time()while now_time-start_time<args.tim:img_RAM = picam2.capture_array("main") #take picture into memorycv2.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:# passnow_time=time.time()time.sleep(args.delay)
Strange enough, it does not print out anything, and no pictures shows up. Also, it does not automatically end for after 10s.
Statistics: Posted by ricky2777 — Thu Feb 13, 2025 3:58 am