os error 是什么错误该怎么改
请在这里粘贴代码
#main
# Main Controller of UAV
# 2021.Nov
######
from utime import sleep
import trace
import digit
import car
from pyb import Pin
from pyb import LED
stand_speed = 40
def test_left():
while True:
car.run(50,0)
def test_right():
while True:
car.run(0,50)
def test_forword():
while True:
car.run(50,50)
def wait_load_signal():
p = Pin("P4", Pin.IN, Pin.PULL_DOWN)
while True:
if p.value() == 1:
return
sleep(0.1)
def wait_unload_signal():
p = Pin("P4", Pin.IN, Pin.PULL_DOWN)
while True:
if p.value() == 0:
return
sleep(0.1)
def work_mode(): # binary [00] Task1 ; [01] Task2; [10] Task3
p5 = Pin("P5", Pin.IN, Pin.PULL_DOWN)
p6 = Pin("P6", Pin.IN, Pin.PULL_DOWN)
if (p5.value() == 0) and (p6.value() == 0):
return 1
elif (p5.value() == 1) and (p6.value() == 0):
return 2
elif (p5.value() == 0) and (p6.value() == 1):
return 3
else:
return 0
def cross(move_type: int): # 0 for straight, 1 for left , 2 for right, 3 for 180 rotation
print("Debug: cross")
if move_type == 0:
car.run(100,100)
sleep(1)
elif move_type == 2: # 右
car.run(50,50)
sleep(1)
car.run(40,-40)
sleep(1)
car.run(50,50)
sleep(1)
elif move_type == 1: # 左
car.run(50,50)
sleep(1)
car.run(-44,44)
sleep(1)
car.run(50,50)
sleep(1)
elif move_type == 3: # 掉头
car.run(-44,44)
sleep(1)
car.run(0,0)
sleep(1)
car.run(-44,44)
sleep(1)
if (trace.foward_backward_state == 0) and (trace.cross_cnt < 2):
trace.cross_cnt = trace.cross_cnt + 1
if move_type == 0:
trace.traj = trace.traj + "C"
elif move_type == 1:
trace.traj = trace.traj + "L"
elif move_type == 2:
trace.traj = trace.traj + "R"
elif (trace.foward_backward_state == 0):
trace.cross_cnt = trace.cross_cnt + 1
if move_type == 0:
raise
elif move_type == 1:
trace.traj = trace.traj + "l"
elif move_type == 2:
trace.traj = trace.traj + "r"
return 0
def stop():
print("Debug: stop")
car.run(0,0)
######
# Task 1
##
def task01(room_id:int):
print("Debug: task01")
trace.trace_line(1000,stand_speed)
if room_id == 1:
cross(1)
elif room_id == 2:
cross(2)
stop()
trace.trace_line(200,stand_speed)
stop()
trace.foward_backward_state = 1
print(trace.traj)
wait_unload_signal()
cross(3)
stop()
go_back()
######
# Task 2
##
def task02(room_id:int):
print("Debug: task02")
trace.trace_line(1000,stand_speed)
cross(0)
stop()
trace.trace_line(1000,stand_speed)
stop()
if digit.search_digit(room_id, 2) == 1:
cross(1)
elif digit.search_digit(room_id, 2) == 2:
cross(2)
stop()
trace.trace_line(150,stand_speed)
stop()
trace.foward_backward_state = 1
print(trace.traj)
wait_unload_signal()
cross(3)
stop()
sleep(1)
go_back()
######
# Task 3
##
def task03(room_id:int):
print("Debug: task03")
trace.trace_line(1000,stand_speed)
cross(0)
stop()
trace.trace_line(1000,stand_speed)
cross(0)
stop()
trace.trace_line(1000,stand_speed)
stop()
if digit.search_digit(room_id,3) == 1:
cross(1)
elif digit.search_digit(room_id,3) == 2:
cross(2)
stop()
trace.trace_line(1000,stand_speed)
stop()
if digit.search_digit(room_id,3) == 1:
cross(1)
elif digit.search_digit(room_id,3) == 2:
cross(2)
stop()
trace.trace_line(200,stand_speed)
trace.foward_backward_state = 1
print(trace.traj)
wait_unload_signal()
cross(3)
go_back()
######
# GoBack
##
def go_back():
idx=len(trace.traj)-1
while(idx>=0):
s = trace.traj[idx]
print(s)
if s == "C":
trace.trace_line(1000,stand_speed)
cross(0)
stop()
elif s == "l":
trace.trace_line(1000,stand_speed,True)
cross(2)
stop()
elif s == "r":
trace.trace_line(1000,stand_speed,True)
cross(1)
stop()
elif s == "L":
trace.trace_line(1000,stand_speed)
cross(2)
stop()
elif s == "R":
trace.trace_line(1000,stand_speed)
cross(1)
stop()
elif s == "I":
trace.trace_line(150,stand_speed)
cross(0)
stop()
idx -= 1
##
# Main
##
def main():
print("Debug main")
task_id = work_mode()
print("Choose Task :", task_id)
# recognize target digit
if task_id == 1 :
room_id = digit.wait_target_number()
print("Choose room :", room_id)
task01(room_id)
elif task_id == 2 :
room_id = digit.wait_target_number()
print("Choose room :", room_id)
task02(room_id)
elif task_id == 3 :
room_id = digit.wait_target_number()
print("Choose room :", room_id)
task03(room_id)
print("Done.")
if __name__ == '__main__':
LED(1).on()
LED(2).on()
LED(3).on()
#while True:
#print(work_mode())
#sleep(0.1)
#traj = traj + "C"
#print(traj)
#print(digit.digit_position)
#wait_load_signal()
#wait_unload_signal()
# go_back()
main()
#car.run(30,30)
#sleep(50)
#trace.trace_line(1000,stand_speed)
#stop()
#cross(3)
#test_left()
#test_right()
#test_forword()
#trace.test_trace_line()
#trace.trace_line(1000,100)
#cross(0)
#stop()
#trace.trace_line(1000,100)
#stop()
#if digit.search_digit(3) == 1:
#cross(1)
#elif digit.search_digit(3) == 2:
#cross(2)
#stop()
#trace.trace_line(200,50)
#digit
digit_position = [0, 0, 0, 0, 0, 0]
def search_digit(match_num: int, level: int):
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
templates = ["b3_1.pgm"]
clock = time.clock()
ret = 0
score_str = ""
clock.tick()
img = sensor.snapshot()
statistics=img.get_statistics()
thr = (statistics.max()-statistics.min())/2
img2 = img.copy()
img2.binary([(int(thr), 255)], invert=False)
img.binary([(int(thr), 255)], invert=False)
for t in templates:
template = image.Image(t)
r = img2.find_template(template, 0.60, step=1, search=SEARCH_EX)
if r:
img.draw_rectangle(r, color =(128,128,128))
n = ord(t[1]) - 48
print(n)
print(match_num)
if n == match_num and r[0] < 60:
ret = 1
elif n == match_num and r[0] > 60:
ret = 2
if level == 2 and r[0] < 60:
digit_position[0] = n
elif level == 2 and r[0] > 60:
digit_position[1] = n
elif level == 3 and r[0] < 60:
if digit_position[2] <= 0:
digit_position[2] = n
else :
digit_position[3] = n
elif level == 3 and r[0] > 60:
if digit_position[4] <= 0:
digit_position[4] = n
else :
digit_position[5] = n
return ret
def wait_target_number():
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
templates = ["b3_1.pgm"]
clock = time.clock()
while (True):
score_str = ""
clock.tick()
img = sensor.snapshot()
statistics=img.get_statistics()
thr = (statistics.max()-statistics.min())/2
img2 = img.copy()
img2.binary([(int(thr), 255)], invert=False)
img.binary([(int(thr), 255)], invert=False)
for t in templates:
print(t)
template = image.Image(t)
r = img2.find_template(template, 0.60, step=1, search=SEARCH_EX)
if r:
img.draw_rectangle(r, color =(128,128,128))
score_str = score_str +" " + t
n = ord(t[1]) - 48
return n
img.draw_string(0, 0, score_str)
#car
from pyb import Pin, Timer
inverse_left=False #change it to True to inverse left wheel
inverse_right=False #change it to True to inverse right wheel
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, right_speed):
if inverse_left==True:
left_speed=(-left_speed)
if inverse_right==True:
right_speed=(-right_speed)
if left_speed < 0:
ain1.low()
ain2.high()
else:
ain1.high()
ain2.low()
ch1.pulse_width_percent(int(abs(left_speed)))
if right_speed < 0:
bin1.low()
bin2.high()
else:
bin1.high()
bin2.low()
ch2.pulse_width_percent(int(abs(right_speed)))
#pid
from pyb import millis
from math import pi, isnan
class PID:
_kp = _ki = _kd = _integrator = _imax = 0
_last_error = _last_derivative = _last_t = 0
_RC = 1/(2 * pi * 20)
def __init__(self, p=0, i=0, d=0, imax=0):
self._kp = float(p)
self._ki = float(i)
self._kd = float(d)
self._imax = abs(imax)
self._last_derivative = float('nan')
def get_pid(self, error, scaler):
tnow = millis()
dt = tnow - self._last_t
output = 0
if self._last_t == 0 or dt > 1000:
dt = 0
self.reset_I()
self._last_t = tnow
delta_time = float(dt) / float(1000)
output += error * self._kp
if abs(self._kd) > 0 and dt > 0:
if isnan(self._last_derivative):
derivative = 0
self._last_derivative = 0
else:
derivative = (error - self._last_error) / delta_time
derivative = self._last_derivative + \
((delta_time / (self._RC + delta_time)) * \
(derivative - self._last_derivative))
self._last_error = error
self._last_derivative = derivative
output += self._kd * derivative
output *= scaler
if abs(self._ki) > 0 and dt > 0:
self._integrator += (error * self._ki) * scaler * delta_time
if self._integrator < -self._imax: self._integrator = -self._imax
elif self._integrator > self._imax: self._integrator = self._imax
output += self._integrator
return output
def reset_I(self):
self._integrator = 0
self._last_derivative = float('nan')
#trace
cross_cnt = 0
foward_backward_state = 0
traj = "I"
def trace_line(delay_time = 1000, std_speed = 50, long_end = False):
THRESHOLD = (10, 60, 20, 70, 10, 50)
import sensor, image, time
from pyb import LED
import car
from pid import PID
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
roi1a=(0,0,40,3)
roi1b=(0,3,40,3)
roi1c=(0,6,40,3)
roi2a=(40,0,40,3)
roi2b=(40,3,40,3)
roi2c=(40,6,40,3)
roi_trace = (23,10,39,50)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
while(True):
if delay_time <= 0:
car.run(0,0)
return
delay_time = delay_time -1
clock.tick()
img = sensor.snapshot().binary([THRESHOLD], invert = True)
line = img.get_regression([(0,1)],roi = roi_trace, robust = True)
if (line):
rho_err = abs(line.rho())-img.width()/2
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)
if line.magnitude()>8:
rho_output = rho_pid.get_pid(rho_err,1)
theta_output = theta_pid.get_pid(theta_err,1)
output = rho_output+theta_output
car.run(std_speed+output, std_speed-output)
else:
car.run(std_speed,std_speed)
else:
car.run(50,-50)
pass
statistics1=img.get_statistics(roi=roi1a)
color_l1a=statistics1.mode()
statistics1=img.get_statistics(roi=roi1b)
color_l1b=statistics1.mode()
statistics1=img.get_statistics(roi=roi1c)
color_l1c=statistics1.mode()
statistics2=img.get_statistics(roi=roi2a)
color_l2a=statistics2.mode()
statistics2=img.get_statistics(roi=roi2b)
color_l2b=statistics2.mode()
statistics2=img.get_statistics(roi=roi2c)
color_l2c=statistics2.mode()
print("color_l1:",(color_l1a==0 or color_l1b==0 or color_l1c==0))
print("color_l2:",(color_l2a==0 or color_l2b==0 or color_l2c==0))
print(delay_time)
if long_end == False:
if (color_l1a==0 or color_l1b==0 or color_l1c==0) and (color_l2a==0 or color_l2b==0 or color_l2c==0):
return 0
else:
if (color_l1a==0 or color_l1b==0 or color_l1c==0):
return 1
if (color_l2a==0 or color_l2b==0 or color_l2c==0):
return 2
def test_trace_line():
THRESHOLD = (5, 70, -23, 15, -57, 0)
import sensor, image, time
from pyb import LED
import car
from pid import PID
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
roi1=(0,0,40,16)
roi2=(40,0,40,16)
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
statistics=img.get_statistics()
thr = (statistics.max()-statistics.min())/1.3
img.binary([(int(thr), 255)], invert = False)
line = img.get_regression([(0,1)],roi = (23,0,39,43), robust = True)
if (line):
rho_err = abs(line.rho())-img.width()/2
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)
if line.magnitude()>8:
rho_output = rho_pid.get_pid(rho_err,1)
theta_output = theta_pid.get_pid(theta_err,1)
output = rho_output+theta_output
car.run(40+output, 40-output)
print(output)
else:
car.run(50,50)
else:
car.run(50,-50)