Hello,
I'm using python program to do image processing and calculating the degrees that the motors should moves and send these data to the arduino and it works. However the data is not changing , the motors keep moving to the first location. I was using arduino uno and I tried to restart the arduino at the end of the loop by pushing the switch '' I know that I can use the REST pin to restart it but I was just checking'' and it works fine, I got the new data by restarting the arduino
Now I'm using arduino mega and If i push the switch to rest the arduino it stop moving '' it's not getting any data'' until I restart the python program then it received the new data and star working.
why I'm not getting the same results as arduino uno .. I can't restarting python program because it takes some time to run and it should not work like that
any idea to get the new data ?
here is my arduino and python code
import cv2
import numpy as np
from numpy import *
import serial
import struct
import time
ARM_Y_MAX = 350
ARM_X_MAX = 700
RESCALE_FACTOR = 0.5
cap = cv2.VideoCapture(1)
cap.set(3, int(1280*RESCALE_FACTOR))
cap.set(4, int(720*RESCALE_FACTOR))
for i in range(15):
_, frame = cap.read()
#r = cv2.selectROI(frame)
arduino = serial.Serial('COM8', 9600)
time.sleep(2)
while 1:
_, frame = cap.read()
img_crop = frame[int(50):int(50+239), int(78):int(78+478)]
hsv = cv2.cvtColor(img_crop, cv2.COLOR_BGR2HSV)
lower_red = np.array([0, 80, 50])
upper_red = np.array([30, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(img_crop, img_crop, mask = mask)
blur = cv2.GaussianBlur(res, (15, 15), 0)
cv2.imshow('Gaussian Blurring', blur)
_, puck = cv2.threshold(blur, 70, 255, cv2.THRESH_BINARY)
cv2.imshow('Puck', puck)
cv2.imshow('frame', frame)
xs = np.where(puck != 0)[1]
ys = np.where(puck != 0)[0]
xstd = np.std(xs)
ystd = np.std(ys)
x_init_avg = np.mean(xs)
y_init_avg = np.mean(ys)
# possibly if std is too high, we can consider that we have a problem?
xs = [x for x in xs if x <= x_init_avg+xstd or x >= x_init_avg-xstd]
ys = [y for y in ys if y <= y_init_avg+xstd or y >= y_init_avg-xstd]
xavg = np.mean(xs)
yavg = np.mean(ys)
cropped_height, cropped_width, channels = img_crop.shape
puck_x = xavg/cropped_width*100
puck_y = yavg/cropped_height*100
desired_arm_x = ((puck_x/100.0)*(ARM_X_MAX*2))-ARM_X_MAX
desired_arm_y = ARM_Y_MAX*2 - ((puck_y/100.0)*(ARM_Y_MAX*2))
px = desired_arm_x
py = desired_arm_y
z=180
a1= 250-z
a2 = 320
a3 = 400
theta1 = arctan(px/py)
theta1 = rad2deg(theta1)
r1=sqrt(px**2 + py**2)
r2=sqrt(r1**2 + a1**2)
phi1=arctan(r1/a1)
phi2=arccos((a2**2+r2**2-a3**2)/(2*r2*a2))
theta2= pi - (phi1 - phi2)
phi3=arccos((a2**2+a3**2-r2**2)/(2*a2*a3))
theta3= (pi- phi3)
theta2 = rad2deg(theta2)
theta3 = rad2deg(theta3)
if theta1 > 0 :
theta1 = int(theta1)
d=0
if theta1 < 0 :
theta1 = int(abs(theta1))
d=1
if 255 > theta2 > 0 :
arduino.write(struct.pack('>BBBB',d,theta1,int(theta2),int(theta3)))
print('theta_1: ', theta1)
print('theta_2: ', theta2)
print('theta_3: ', theta3)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
Thank you
test.ino (6.17 KB)