openmv和esp32如何实现串口通信
-
我写的关于串口通信的代码,esp32部分提示uart不存在,openmv部分提示无效语法。
esp32:from machine import Pin,PWM,ADC,SoftI2C #from pid import PID from ssd1306 import SSD1306_I2C from machine import UART import sensor, time, os #引脚定义 #电机引脚 IN1=Pin(8,Pin.OUT) #右侧电机 IN2=Pin(9,Pin.OUT) IN3=Pin(18,Pin.OUT) #左侧电机 IN4=Pin(10,Pin.OUT) #开关引脚 KEY=Pin(0,Pin.IN,Pin.PULL_UP) #构建 KEY 对象 # IN1_pwm=PWM(IN1,duty=298) # IN3_pwm=PWM(IN3,duty=280) state = 0 #药品检测引脚 number_model = 0 # 应检测到的模板数字初始化为0 #初始化 i2c=SoftI2C(sda=Pin(42),scl=Pin(40)) oled=SSD1306_I2C(128,64,i2c,addr=0x3c) uart_tx = UART(43, 9600) uart_tx.init(9600, bits=8, parity=None, stop=1) uart_rx = UART(44, 9600) uart_rx.init(9600, bits=8, parity=None, stop=1) def control(KEY): global state time.sleep_ms(10) #消除抖动 if KEY.value()==0: #确认按键被按下 state=not state #使用 not 语句而非~语句很抱歉B IN1.value(state) IN3.value(state)#LED 状态翻转 # IN1_pwm=PWM(IN1,duty=256) # IN3_pwm=PWM(IN3,duty=256) IN1.frep(600) time.sleep(1) #前进 def Go(): IN1_pwm=PWM(IN1,duty=298) IN3_pwm=PWM(IN3,duty=280) # IN1.value(1) # IN3.value(1) def left():#左转 IN1_pwm=PWM(IN3,duty=0) IN3_pwm=PWM(IN4,duty=0) time.sleep(0.5) IN1_pwm=PWM(IN1,duty=298) IN3_pwm=PWM(IN3,duty=280) def right():#右转 IN1_pwm=PWM(IN1,duty=0) IN3_pwm=PWM(IN2,duty=0) IN1.value(1) IN2.value(1) time.sleep(0.5) IN1_pwm=PWM(IN1,duty=298) IN3_pwm=PWM(IN3,duty=280) def led():#装好药品后红灯亮起,回到药房处绿灯亮起 # if 放药处避障模块给出信号(放上药品): if barrier1() == 1: led1.value(1) if barrier1() == 2: led1.value(0) return #if flag=1: #led2.value(1) #倒车,后退 def Back(): IN1.value(not state) IN3.value(not state) return 1 def stop(): IN1.value(0) IN2.value(0) IN3.value(0) IN4.value(0) # time.sleep(1) def barrier():#返回药盒处的红外避障模块给出的0/1信号 barrier1=ADC(Pin(6)) # return barrier1.read() #print(barrier1.read()) #time.sleep(1) #print(barrier1.value()) if barrier1.read()<1000: #红外避障模块输出为1,已装载药品 # 装载药品后即开始检测数字 sensor.reset() # 复位和初始化传感器。 sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) #sensor.set_framesize(sensor.VGA) sensor.set_windowing((240, 240)) # Set 240x240 window.设置传感器的窗口大小 sensor.skip_frames(time=2000) # Let the camera adjust. # 跳过一定时间内的图像帧 clock = time.clock() while True: clock.tick() img = sensor.snapshot().binary([(0,64)]) for obj in tf.classify("trained.tflite", img, min_scale=1.0, scale_mul=0.9, x_overlap=0.0, y_overlap=0.0): output = obj.output() #print(output) #print(max(output)) global number_model number_model = output.index(max(output)) uart_tx.write(number_model) Go() oled.fill(0) oled.text("The medication",0,20) oled.text(" has ready!",0,30) oled.show() return 1 else: stop() oled.text("Please put in ",0,20) oled.text("the medication!",0,30) oled.show() #可以不加在定时器,根据车行驶状态来给出调用此函数的位置 def main(): KEY.irq(control,Pin.IRQ_FALLING) Barrier=Pin(47,Pin.OUT) #Barrier.irq(AvoidingObstacles,Pin.IRQ_FALLING) while True: #Back() barrier() #flag = uart_rx.read(10) uart_rx.readinto(flag) if flag == 'left': left() elif flag == 'right': right() elif flag == 'Go': Go() elif flag == 'stop': stop() if __name__ == '__main__': main()
openmv:
THRESHOLD = (70, 4, 18, 127, -125, 127) # Grayscale threshold for dark things... # 灰度阈值 from machine import Pin,PWM,ADC,SoftI2C from pid import PID from ssd1306 import SSD1306_I2C import sensor, image, time, os, tfe from pyb import UART uart_rx = UART('P5',9600) uart_rx.init(9600, bits=8, parity=None, stop=1) uart_tx = UART('P4',9600) uart_tx.init(9600, bits=8, parity=None, stop=1) def motor(): rho_pid = PID(p=0.4, i=0) theta_pid = PID(p=0.001, i=0) sensor.reset() sensor.set_vflip(True) # 垂直翻转,从传感器获取的图像或数据在显示或处理时会上下颠倒。 sensor.set_hmirror(True) # 将传感器的图像或数据在水平方向上进行镜像操作(从传感器获取的图像或数据在显示或处理时会左右翻转 sensor.set_pixformat(sensor.RGB565) # 设置传感器像素格式 sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000. # 设置帧尺寸 sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds # 使传感器跳过接下来的2000毫秒内的帧 clock = time.clock() # to process a frame sometimes. # 创建一个定时器对象 while(True): clock.tick() #number_model = uart_rx.read() uart_rx.readinto(number_model) # 获取自上次调用tick()方法以来的毫秒数,常用于实现帧率控制或时间延迟 img = sensor.snapshot().binary([THRESHOLD]) # 获取当前图像帧,然后对图像进行二值化处理 list_line = img.find_lines() num = len(list_line) if num == 2: if number_model =1 : #left() uart_tx.write('left\n') break elif number_model = 2: #right() uart_tx.write('right\n') break line = img.get_regression([(100,100)], 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) print(rho_err,line.magnitude(),rho_err) if line.magnitude()>8: #if -40<b_err<40 and -30<t_err<30: 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(50+output, 50-output) #right() if 50+output>50-output: #左快右慢,向右转 #right() uart_tx.write('right\n') break elif 50+output<50-output: #left() uart_tx.write('left\n') break else: #Go() uart_tx.write('Go\n') break else: #car.run(0,0)# 左右速度为0,停止 #stop() uart_tx.write('stop\n') break else: #car.run(50,-50)#向左转 #left() uart_tx.write('left\n') break pass #print(clock.fps()) # 此函数仅用于确保小车沿着红线走 def numfind(num_model): sensor.reset() # 复位和初始化传感器。 sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) #sensor.set_framesize(sensor.VGA) sensor.set_windowing((240, 240)) # Set 240x240 window.设置传感器的窗口大小 sensor.skip_frames(time=2000) # Let the camera adjust. # 跳过一定时间内的图像帧 clock = time.clock() while(True): clock.tick() img = sensor.snapshot().binary([(0,64)]) for obj in tf.classify("trained.tflite", img, min_scale=1.0, scale_mul=0.9, x_overlap=0.0, y_overlap=0.0): output = obj.output() #print(output) #print(max(output)) number = output.index(max(output)) if number == num_model: if number == 3: #left() uart_tx.write('left\n') break elif number==4: #right() uart_tx.write('right\n') break elif number==5 or number==7: #left() uart_tx.write('left\n') while True: clock.tick() img = sensor.snapshot().binary([(0,64)]) for obj in tf.classify("trained.tflite", img, min_scale=1.0, scale_mul=0.9, x_overlap=0.0, y_overlap=0.0): output = obj.output() number = output.index(max(output)) if number ==5: #right() uart_tx.write('right\n') break elif number==7: #left() uart_tx.write('left\n') break elif number==6 or number==8: #right() uart_tx.write('right\n') while True: clock.tick() img = sensor.snapshot().binary([(0,64)]) for obj in tf.classify("trained.tflite", img, min_scale=1.0, scale_mul=0.9, x_overlap=0.0, y_overlap=0.0): output = obj.output() number = output.index(max(output)) if number ==6: #right() uart_tx.write('right\n') break elif number==8: #left() uart_tx.write('left\n') break while True: motor() num_model = uart_rx.read(10) numfind(num_model)
-
https://singtown.com/learn/50235/
先单独测试发送串口的代码。