Hi,
I’m trying to enable the openmv to send information to the STM32 board by using UART. When the openmv is connected to the computer and using IDE, it works fine. However, if the openmv works without computer but powered by STM32, it crashes within seconds (the red LED blinks twice). I’ve comfirmed it by setting the green LED turned on when openmv is functioning.
If I comment the UART code, the openmv works fine. So, there must be something wrong with UART.
How can I fix it?
This is my code:
import sensor, image, time, math
from pid import PID
from pyb import UART, LED
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
rho_weight = 1
theta_weight = 1
count = 0
w = 80
h = 60
mid_x = w/2
mid_y = h/2
gain = 5
uart = UART(3, 115200, timeout_char = 1000)
green_led = LED(2)
def pooling_filter(img):
img.midpoint_pool(2, 2,bias=0.5)
img.binary([(120,130)])
img.midpoint_pool(2, 2,bias=0.5)
img.binary([(120,130)])
def find_middle_line(img):
try:
pooling_filter(img)
line = img.get_regression([(240,255)], robust = True)
img.draw_line(line.line(), color = 139,thickness=2)
rho_error, theta_error = line_to_theta_and_rho(line.rho(),line.theta())
return rho_error, theta_error
except:
return 0,0
def line_to_theta_and_rho(rho,theta):
if rho < 0:
trans_rho = -rho
trans_theta = theta + 180
else:
trans_rho = rho
trans_theta = theta
cos_trans_theta = math.cos(math.radians(trans_theta))
sin_trans_theta = math.sin(math.radians(trans_theta))
distance = abs(mid_x*cos_trans_theta + mid_y*sin_trans_theta - trans_rho)
if trans_theta == 90 or trans_theta == 270:
distance_error = theta_error = 0
elif (trans_rho-mid_y*sin_trans_theta)/cos_trans_theta > mid_x:
distance_error = distance
if theta > 90:
theta_error = theta - 180
else:
theta_error = theta
else:
distance_error = -distance
if theta > 90:
theta_error = theta - 180
else:
theta_error = theta
return distance_error, theta_error
while(True):
clock.tick()
img = sensor.snapshot()
img.find_edges(image.EDGE_CANNY, threshold=(150,190))
rho_error,theta_error = find_middle_line(img)
if count != 10:
if count == 0:
rho_error_sum = theta_error_sum = 0
rho_error_sum += rho_error
theta_error_sum += theta_error
count += 1
continue
else:
rho_error_sum /= 10
theta_error_sum /= 10
count = 0
rho_pid = PID(p=0.40, i=0.1, d=0, imax=10)
theta_pid = PID(p=0.25, i=0.1, d=0, imax=10)
rho_output = rho_pid.get_pid(rho_error_sum,1)
theta_output = theta_pid.get_pid(theta_error_sum,1)
output = gain*int(rho_weight*rho_output+theta_weight*theta_output)
if output > 0:
output = 'A'
else:
output = 'B'
uart.write(str(output)+'\r\n')
green_led.on()
print('output=',output)