关于openmv和arduino串口通信
-
char str; void setup(){ Serial.begin(9600); } void loop(){ if(Serial.available()>0) { str=Serial.read(); Serial.println(str); } delay(1000); }
import time, pyb from machine import UART led = pyb.LED(3) #from pyb import UART uart = UART(3, 9600) while(True): uart.write("1") time.sleep_ms(5000) led.on()
openmv显示灯亮了应该发送成功了 可是arduino串口没接收到是怎么回事 老师
-
就是arduino的串口监视器一直输出-1不知道咋回事,也用了Serial.readString()也没用
-
注意一下接线:
https://book.openmv.cc/MCU/serial2.html
我推荐Arduino上用softSerial(10, 11)来和OpenMV通信,因为Seria被串口监视器占用了。
-
@kidswong999 我就是引用官方的例程.寻找黄色色块,openmv发现以后arduino的串口还是一个数据都没有
#include <SoftwareSerial.h> SoftwareSerial softSerial(10, 11); // RX, TX typedef struct { int data[50][2] = {{0,0}}; int len = 0; }List; List list; void setup() { // put your setup code here, to run once: softSerial.begin(115200); Serial.begin(115200); } void loop() { if(softSerial.available()) { getList(); for (int i=0; i<list.len; i++) { Serial.print(list.data[i][0]); Serial.print('\t'); Serial.println(list.data[i][1]); } Serial.println("============"); clearList(); } } String detectString() { while(softSerial.read() != '{'); return(softSerial.readStringUntil('}')); } void clearList() { memset(list.data, sizeof(list.data),0); list.len = 0; } void getList() { String s = detectString(); String numStr = ""; for(int i = 0; i<s.length(); i++) { if(s[i]=='('){ numStr = ""; } else if(s[i] == ','){ list.data[list.len][0] = numStr.toInt(); numStr = ""; } else if(s[i]==')'){ list.data[list.len][1] = numStr.toInt(); numStr = ""; list.len++; } else{ numStr += s[i]; } } }
import sensor, image, time import json from machine import UART yellow_threshold = ( 46, 100, -68, 72, 58, 92) sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS. uart = UART(3, 115200) while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. blobs = img.find_blobs([yellow_threshold]) if blobs: #print('sum : %d'% len(blobs)) data=[] for b in blobs: # Draw a rect around the blob. img.draw_rectangle(b.rect()) # rect img.draw_cross(b.cx(), b.cy()) # cx, cy data.append((b.cx(),b.cy())) #{(1,22),(-3,33),(22222,0),(9999,12),(0,0)} data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('you send:',data_out) else: print("not found!")
-
#include <SoftwareSerial.h> SoftwareSerial softSerial(10, 11); // RX, TX void setup() { softSerial.begin(9600); Serial.begin(9600); } void Openmv(String s) { if(s[1]=='0') { Serial.print('0'); Serial.print('\n'); } if(s[1]=='1') { Serial.print('1'); Serial.print('\n'); } if(s[1]=='2') { Serial.print('2'); Serial.print('\n'); } } void loop() { if(softSerial.available()) { String s = detectString(); Openmv(s); } } String detectString() { while(softSerial.read() != '{'); return(softSerial.readStringUntil('}')); }
这个是arduino的
import sensor, image, time import json from pyb import UART blue_threshold = (22, 67, 9, 127, -128, -54) red_threshold = (41, 61, 42, 127, -128, 127) yellow_threshold = ( 60 ,10,-15,20,30,80) sensor.set_hmirror(True) sensor.set_vflip(True) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(10) sensor.set_auto_whitebal(False) clock = time.clock() uart = UART(3, 9600) def find_max(blobs): max_size=0 for blob in blobs: if blob.pixels() > max_size: max_blob=blob max_size = blob.pixels() return max_blob while(True): clock.tick() img = sensor.snapshot() blobs0 = img.find_blobs([blue_threshold]) blobs1 = img.find_blobs([red_threshold]) blobs2 = img.find_blobs([yellow_threshold]) if blobs0: max_blob=find_max(blobs0) img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) data="0" data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('you send:',data_out) elif blobs1: max_blob=find_max(blobs1) img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) data="1" data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('you send:',data_out) elif blobs2: max_blob=find_max(blobs2) img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) data="2" data_out = json.dumps(set(data)) uart.write(data_out +'\n') print('you send:',data_out) else: print('not found')
这个是openmv的
这两个程序更直观点,但是就是没有数据,用程序看openmv应该是发送成功了,但是arduino接收不到,串口监视器一个数据没有
-
OpenMV画面里有找到黄色色块吗?没找到不会发送。还有注意接线,一定要接GND。
-
@kidswong999 两个板子是共地的,摄像头也检测到了