Mar-10-2018, 07:17 PM
I am working on a piece of code that uses OpenCV in my python code. I am using python 2.7.4 with the OpenCV 3.4.0.
In my code, i have built a tracking program to track certain colours in my case its red. I also have a servo motor sitting on top of the servo motor. The centre point of the targeted object is retrieved and printed.
I have verified that the servo motor works fine on its own. when inputting angle from the user, but in my code, i have the servo motor changing angles depending on the position of the red object. In my for loop the angle is retrieved and the duty cycle is sent to the motor.
The problem I am having is that the angle is being retrieved from the code and sent to the servo motor and it can't process it because the next angle has already been sent. This process is happening to fast and my servo motor can't keep up.
I have narrowed the problem down to here. In this for loop i have got the contour information and i just plot bounding boxes around my red objected. I also get the distance from an ultrasonic sensor.
In my code, i have built a tracking program to track certain colours in my case its red. I also have a servo motor sitting on top of the servo motor. The centre point of the targeted object is retrieved and printed.
I have verified that the servo motor works fine on its own. when inputting angle from the user, but in my code, i have the servo motor changing angles depending on the position of the red object. In my for loop the angle is retrieved and the duty cycle is sent to the motor.
The problem I am having is that the angle is being retrieved from the code and sent to the servo motor and it can't process it because the next angle has already been sent. This process is happening to fast and my servo motor can't keep up.
I have narrowed the problem down to here. In this for loop i have got the contour information and i just plot bounding boxes around my red objected. I also get the distance from an ultrasonic sensor.
for c in contours: x1, y1, w1, h1 = cv2.boundingRect(c) cv2.rectangle(image, (x1,y1), (x1+w1,y1+h1), (0,255,0), 2) rect = cv2.minAreaRect(c) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(image, [box], 0, (0, 0, 255), 2) # finally, get the min enclosing circle (x, y), radius = cv2.minEnclosingCircle(c) # convert all values to int center = (int(x), int(y)) radius = int(radius) x_cor = int(x) y_cor = int(y) image = cv2.circle(image, center, 1, (255, 0, 0), 2) Ob_Distance = int(distance()) print "Distance = %d cm Centre point x = %d y = %d" %(Ob_Distance, x_cor, y_cor) if x_cor < XAxisCentre and ServoPosition >= 5: ServoPosition = ServoPosition - 2 elif x_cor > XAxisCentre and ServoPosition <= 175: ServoPosition = ServoPosition + 2 #end if else print "servo position = " + str(ServoPosition) UpdateServoPositions(pwmObject, ServoPosition) #time.sleep(0.0001) #end for