我帮你把缩进对齐了,并且OpenMV u盘中保存了pid.py文件,运行后没有问题了。
就是两个问题,你自己检查校对一下。
1.要把pid.py放进openmv的u盘。
2.缩进要对齐。(主要是你代码里if flag==4: 这里)
import sensor, image, time, math,pyb
from pyb import Servo
from pyb import UART
from pid import PID
from pyb import millis
from pyb import Pin, Timer
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
f_x = (12 / 3.984) * 160
f_y = (12 / 2.952) * 120
c_x = 160 * 0.5
c_y = 120 * 0.5
s1 = Servo(1)
s1.angle(0)
x_pid = PID(p=3, i=0.05, imax=50)
x1_pid = PID(p=3, i=0.02, imax=50)
uart = UART(1, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
flag=3
flag1=1
flag2=0
c1=-38
f=0
f0=0
def tick(timer):
from pyb import Servo
global flag
global s1
print("Timer callback")
s1.angle(0)
flag=3
tim = Timer(5, freq=0.1)
tim.callback(tick)
def tick1(timer):
global flag
global f
global f0
global z0
print("Timer1 callback")
if flag==3 and f==0:
if f0==0:
flag=4
uart.writechar(66)
else:
if z0>-20:
uart.writechar(83)
else:
uart.writechar(67)
flag=4
tim1 = Timer(3, freq=0.3)
tim1.callback(tick1)
while(True):
while(flag!=2):
res=uart.readchar()
if res>=85 and res<=125:
res=res-105
out=x1_pid.get_pid(res,1)+105;
if out>125:
out=125
if out<85:
out=85
uart.writechar(int(out))
if res==67:
flag=3
if res==66:
flag=4
while(flag==3):
img = sensor.snapshot()
for tag in img.find_apriltags(families=image.TAG16H5,fx=f_x, fy=f_y, cx=c_x, cy=c_y):
f=1
if flag1==0 :
uart.writechar(65)
flag1=1
else:
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
print_args = (tag.x_translation(), tag.z_translation())
print("Tx: %f, ,Tz %f" % print_args)
x_output=x_pid.get_pid(tag.x_translation(),1)
x_output=x_output*1.2+105
if tag.z_translation()<-15:
uart.writechar(int(x_output))
print("out!! %f" % x_output)
z0=tag.z_translation()
else:
uart.writechar(83)
flag=2
flag3=0
print("浇水2" )
f0=f
f=0
while (flag==4):
print("执行了一次4" )
uart.writechar(66)
for i in range(90):
print("i= %d" %i )
s1.pulse_width(1500 - i*10)
img = sensor.snapshot()
for tag in img.find_apriltags(families=image.TAG16H5,fx=f_x, fy=f_y, cx=c_x, cy=c_y):
print_args = (tag.x_translation(), tag.z_translation())
if tag.x_translation()<3 and tag.x_translation()>-3 :
c=int(i/10)
c1=-10
out=c+105
uart.writechar(int(out))
flag=3
pyb.delay(300)
s1.angle(0)
if flag==3:
break
if flag==4:
for i in range(180):
print("i= %d" %i )
s1.pulse_width(600 + i*10)
img = sensor.snapshot()
for tag in img.find_apriltags(families=image.TAG16H5,fx=f_x, fy=f_y, cx=c_x, cy=c_y):
print_args = (tag.x_translation(), tag.z_translation())
if tag.x_translation()<3 and tag.x_translation()>-3 :
c=int(-(i-90)/10)
if c>0:
c1=10
else:
c1=-10
out=c+105
uart.writechar(out)
flag=3
pyb.delay(300)
s1.angle(0)
if flag==3:
break
if flag==4:
for i in range(90):
print("i= %d" %i )
s1.pulse_width(2400 - i*10)
img = sensor.snapshot()
for tag in img.find_apriltags(families=image.TAG16H5,fx=f_x, fy=f_y, cx=c_x, cy=c_y):
print_args = (tag.x_translation(), tag.z_translation())
if tag.x_translation()<3 and tag.x_translation()>-3 :
c=int(-(90-i)/10)
out=c+105
uart.writechar(out)
flag=3
c1=10
pyb.delay(300)
s1.angle(0)
if flag==3:
break
if flag==4:
uart.writechar(67)
pyb.delay(2000)