Arduino代码
#include <Wire.h>
#define BAUD_RATE 9600
#define CHAR_BUF 128
char buff[CHAR_BUF] = {0};
int lineType =0;
int led = 53;
void setup()
{ pinMode(led, OUTPUT);
Serial.begin(BAUD_RATE);
Wire.begin();
delay(500);
}
void getCode(){
String temp1;
String string = String(buff);
int postion = string.indexOf(" ");
temp1 = string.substring(0,postion);
lineType = temp1.toFloat();
}
void get()
{
int32_t temp = 0;
Wire.requestFrom(0x12, 2);
if (Wire.available() == 2)
{
temp = Wire.read() | (Wire.read() << 8);
delay(1);
Wire.requestFrom(0x12, temp);
if (Wire.available() == temp) {
temp = 0;
while (Wire.available()) buff[temp++] = Wire.read();
} else {
while (Wire.available()) Wire.read();
}
} else {
while (Wire.available()) Wire.read();
}
}
void ledrun()
{
if(lineType==1)
{
digitalWrite(led,HIGH);
}
else{
digitalWrite(led,LOW); }
}
void loop()
{
get();
getCode();
ledrun();
Serial.println("L"+String(lineType));
}
OPENMV代码
1.OPENMV代码
import sensor, image, time
import spi
thresholds = [(6, 47, 121, 6, 93, 6),
(0, 63, 18, -74, 57, 20),
(23, 69, 89, -12, -7, -63),
(20, 37, 20, 60, -1, 45),
(24, 36, -1, 20, -55, -25),
(30, 44, -46, -9, 7, 44),
(21, 100, 118, 19, 40, -116)]
ROI=(120,100,40,40)
sensor.reset()
sensor.set_vflip(True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
statistics=img.get_statistics(roi=ROI)
color_l=statistics.l_mode()
color_a=statistics.a_mode()
color_b=statistics.b_mode()
for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
if blob.density()>0.5:
img.draw_rectangle(blob.rect(), color = (255, 0, 0))
if blob.w()>180 or color_l<80:
spi.run(1)
print("识别到黑色",blob.w())
else :
spi.run(0)
img.draw_rectangle(ROI, color = (0, 255, 0))
2.spi代码
import pyb, ustruct
import ujson
from pyb import Pin, Timer
bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12)
bus.deinit()
bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12)
print("Waiting for Arduino...")
def run(lineType):
data = str(lineType)+" "
try:
bus.send(ustruct.pack("<h", len(data)), timeout=10000)
try:
bus.send(data, timeout=10000)
print("Sent Data!")
except OSError as err:
pass
except OSError as err:
pass
注意