logo

插上数据线和拔下数据线后的程序运行结果不一样?

2022-10-08 15:02
J
asfq
image

问题描述:连上数据线时程序运行结果正确,当视野中不存在Apriltag时电机按照程序正常停止转动。但烧录程序并拔出数据线,重新上电后,openmv识别出Apriltag后电机转动,当视野中没有Apriltag时电机却不会停止转动。

补充:换过电机控制模块和电池,不是这两个元器件的问题。之前拔下数据线程序是能正常跑的,某一次开始突然出bug了。由于插上数据线时程序正常运行,所以应该也不是程序问题,实在不知道是哪里出错了。线路是按照追小球的小车那个教程接的。

元器件:openmv4H7PLUS、TB6612电机驱动、3.7V锂电池、615空心杯电机、IDE版本2.9.0

附程序:

mecar模块:

from pyb import Pin, Timer
inverse_left=False
inverse_front=False
ain1 =  Pin('P0', Pin.OUT_PP)
ain2 =  Pin('P1', Pin.OUT_PP)
bin1 =  Pin('P2', Pin.OUT_PP)
bin2 =  Pin('P3', Pin.OUT_PP)
ain1.low()
ain2.low()
bin1.low()
bin2.low()
pwma = Pin('P7')
pwmb = Pin('P8')
tim = Timer(4, freq=1000)
ch1 = tim.channel(1, Timer.PWM, pin=pwma)
ch2 = tim.channel(2, Timer.PWM, pin=pwmb)
ch1.pulse_width_percent(0)
ch2.pulse_width_percent(0)
def run(left_speed, front_speed):
    if inverse_left==True:
        left_speed=(-left_speed)
    if inverse_front==True:
        front_speed=(-front_speed)
    if left_speed < 0:
        ain1.low()
        ain2.high()
    elif left_speed > 0:
        ain1.high()
        ain2.low()
    else:
        ain1.low()
        ain2.low()
    ch1.pulse_width_percent(20)
    if front_speed < 0:
        bin1.low()
        bin2.high()
    elif front_speed > 0:
        bin1.high()
        bin2.low()
    else:
        bin1.low()
        bin2.low()
    ch2.pulse_width_percent(20)

main模块:

import sensor, image, time
import mecar
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
size_threshold = 2000
def find_max(blobs):
	max_size=0
	for blob in blobs:
		if blob[2]*blob[3] > max_size:
			max_blob=blob
			max_size = blob[2]*blob[3]
	return max_blob
while(True):
	clock.tick()
	img = sensor.snapshot()
	blobs = img.find_apriltags()
	if blobs:
		max_blob = find_max(blobs)
		x_error = max_blob[6]-80
		y_error = max_blob[7]-60
		print("x error: ", x_error)
		print("y error: ", y_error)
		img.draw_rectangle(max_blob[0:4])
		img.draw_cross(int(max_blob[6]), int(max_blob[7]))
		mecar.run(x_error,y_error)
		time.sleep_ms(500)
	else:
		mecar.run(0,0)
2022-10-08 17:08
I
网红小智智 星瞳科技

可能是你保存的步骤不对。先脱机运行闪灯测试:https://book.openmv.cc/example/02-Board-Control/led-control.html

然后写一个程序,当找到apriltag就亮灯,找不到就灭灯的程序,脱机测试。