Arduino的代码里只写了收到信息执行电机转动的功能,没有写回传的功能,但是
用19200时openmv的串行终端只打印识别到的物体,用115200时openmv的串行终端既打印识别的内容,也打印Arduino传回来的内容,两次的代码只改变了波特率
我测试过,能收到信息
就是用官方示例代码直接做能收到信息
这个说明OpenMV的串口功能没有问题,Arduino那边是怎么写的?提供一下OpenMV的代码和Arduino的代码
然后我上传的这个发送识别物体名字
的代码也能收到信息
你可以连接Arduino的板子,然后在那边打印一下信息(不看电机是否旋转,直接打印他串口的接收),看有没有收到OpenMV给他发的信息
带电机的代码:openmv:Edge Impulse - OpenMV FOMO Object Detection Example
import sensor, image, time, ml, math, uos, gc from pyb import UART
sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((240, 240)) sensor.skip_frames(time=2000)
uart = UART(3, 115200, timeout_char=200)
net = None labels = None min_confidence = 0.5
try: net = ml.Model("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024))) except Exception as e: raise Exception('Failed to load "trained.tflite" ... (' + str(e) + ')')
try: labels = [line.rstrip('\n') for line in open("labels.txt")] except Exception as e: raise Exception('Failed to load "labels.txt" ... (' + str(e) + ')')
name_map = { "triangular": "A", "cube": "B", "cylinder": "C", "K-sanjiao": "D", "K-fang": "E", "K-yuan": "F", "G-fang": "G", } labels = [name_map.get(label, label) for label in labels]
colors = [ (255, 0, 0), ( 0, 255, 0), (255, 255, 0), ( 0, 0, 255), (255, 0, 255), ( 0, 255, 255), (255, 255, 255), (128, 0, 128) ] threshold_list = [(math.ceil(min_confidence * 255), 255)]
def fomo_post_process(model, inputs, outputs): ob, oh, ow, oc = model.output_shape[0] x_scale = inputs[0].roi[2] / ow y_scale = inputs[0].roi[3] / oh scale = min(x_scale, y_scale) x_offset = ((inputs[0].roi[2] - (ow scale)) / 2) + inputs[0].roi[0] y_offset = ((inputs[0].roi[3] - (ow scale)) / 2) + inputs[0].roi[1]
l = [[] for i in range(oc)]
for i in range(oc):
img = image.Image(outputs[0][0, :, :, i] * 255)
blobs = img.find_blobs(
threshold_list, x_stride=1, y_stride=1, area_threshold=1, pixels_threshold=1
)
for b in blobs:
rect = b.rect()
x, y, w, h = rect
score = (
img.get_statistics(thresholds=threshold_list, roi=rect).l_mean() / 255.0
)
x = int((x * scale) + x_offset)
y = int((y * scale) + y_offset)
w = int(w * scale)
h = int(h * scale)
l[i].append((x, y, w, h, score))
return lclock = time.clock() while(True): clock.tick() img = sensor.snapshot()
for i, detection_list in enumerate(net.predict([img], callback=fomo_post_process)):
if i == 0: continue
if len(detection_list) == 0: continue
# 发送识别到的物体名给Arduino
uart.write(labels[i] + "\n")
# 同时在终端打印(可选,方便调试)
print(labels[i])
# 检查是否有来自Arduino的数据
if uart.any():
incoming = uart.read()
# 可在此处理接收数据,目前仅打印
print("Received:", incoming)Arduino代码:void setup() { Serial.begin(19200); pinMode(9, OUTPUT); pinMode(10, OUTPUT); }
void loop() { if (Serial.available()) { String label = Serial.readStringUntil('\n'); // 读取到换行符为止 label.trim(); // 去掉可能的\r等空白字符
// 根据收到的标签做处理
if (label == "A") {
analogWrite(9,180);
analogWrite(10,0);
// 执行A对应的动作
} } }
回复“你可以连接Arduino的板子,然后在那边打印一下信息(不看电机是否旋转,直接打印他串口的接收),看有没有收到OpenMV给他发的信息”:是Arduino的串口监视器上看有没有信息吗,这个我试过没有成功,deepseek说这是硬件串口会和串口监视器干扰,然后我只能在openmv的串行终端看传回来的信息
然后我改成软件串口的话在Arduino的串口监视器上能看到信息,但是用软件串口后面又把Arduino控制的舵机卡住了
我用的Arduino是basra主控板bigfish扩展板
意思是Arduino的串口监视器上可以看到信息,但是会卡住舵机?
对,软件串口和舵机都可以单独实现,加起来就卡住了
之前用I2C通信的,舵机能动但是把
灰度扰乱了,我们就改成uart通信了
Arduino那边的问题,改用使用中断的方式进行串口查询(串口中断),不要直接在while中轮询,会占用CPU。
是用软件定时器millis()计时,每隔一定时间进去一次?
之前说错了,是analogwrite和analogread在我的识别通讯过程中用不了
不是计时,是中断,没有数据就不会进去读,有数据才会读
能不能给个示例代码
这里没有arduino代码,无法提供