1.识别长度
# Untitled - By: YU - 周三 4月 7 2021
import sensor, image, time, ustruct
from pyb import UART #导入串口
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
K=0.16 #实际直径=K*直径像素 故:K=4CM/25PIX
aim_threshold =(24, 80, 47, 72, -44, 106) #L A B
uart = UART(3,115200) #OpenMV上P4,P5对应的串口3 第二个参数是波特率
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
#def sending_data(a):
#global uart;
##frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
##data = bytearray(frame)
#data = ustruct.pack("
#0x2C, #帧头1
#0x12, #帧头2
#int(a), #数据1
#0x5B)
#uart.write(data); #必须要传入一个字节数组
while(True):
clock.tick()
img = sensor.snapshot()
# print(clock.fps())
blobs = img.find_blobs([aim_threshold])
if len(blobs)==1:
b=blobs[0]
img.draw_rectangle(b[0:4])
img.draw_cross(b[5],b[6])
lm=(b[2]+b[3])/2
length=K*lm
print("管长:",length)
length=int(length)
data = bytearray([11,22,length,33])
uart.write(data)
#pack各字母对应类型
#x pad byte no value 1
#c char string of length 1 1
#b signed char integer 1
#B unsigned char integer 1
#? _Bool bool 1
#h short integer 2
#H unsigned short integer 2
#i int integer 4
#I unsigned int integer or long 4
#l long integer 4
#L unsigned long long 4
#q long long long 8
#Q unsilong long long 8
#f float float 4
#d double float 8
#s char[] string 1
#p char[] string 1
#P void * long
2.识别面积
# Untitled - By: YU - 周三 4月 7 2021
import sensor, image, time, ustruct
from pyb import UART #导入串口
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
K=0.16 #实际直径=K*直径像素 故:K=4CM/25PIX
aim_threshold =(24, 80, 47, 72, -44, 106) #L A B
uart = UART(3,115200) #OpenMV上P4,P5对应的串口3 第二个参数是波特率
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
while(True):
clock.tick()
img = sensor.snapshot()
#print(clock.fps())
blobs = img.find_blobs([aim_threshold])
if len(blobs)==1:
b=blobs[0]
img.draw_rectangle(b[0:4])
img.draw_cross(b[5],b[6])
area=b.pixels()
print("面积:",area)
qian=area//100
shi=area%100
data = bytearray([11,22,qian,shi,33])
uart.write(data)
STM32
一定要共地(浪费了一个晚上的教训)
1.识别长度
/* USER CODE BEGIN PTD */
uint8_t re_buf1; //缓冲区
uint8_t openmvdata=0; //长度 整数
uint8_t state=0;
/* USER CODE END PTD */
/* USER CODE BEGIN PM */
void OpmvDataGet(uint8_t data)
{
if(state==0 && data ==11) //帧头1
{
//printf("state=1 data:%d",data);
state =1 ;
}
else if (state==1 && data == 22) //帧头2
{
//printf("state=2 data:%d",data);
state=2;
}
else if(state == 2)
{
//printf("state=3 data:%d",data);
state=3;
openmvdata=data;
}
else if(state ==3)
{
if(data ==33) //帧尾
{
printf("长度:%d",openmvdata);
state=0;
}
else
{
openmvdata=0;
state=0;
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //opmv-receive
{
if(huart == &huart1)
{
HAL_UART_Receive_IT(&huart1,&re_buf1,1);
OpmvDataGet(re_buf1);
}
}
/* USER CODE END PM */
int main(void)
{
/* USER CODE BEGIN 2 */
printf("stm32初始化");
HAL_UART_Receive_IT(&huart1,&re_buf1,1);
/* USER CODE END 2 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
2.识别面积
void OpmvDataGet(uint8_t data)
{
if(state==0 && data ==11) //帧头1
{
//printf("state=1 data:%d",data);
state =1 ;
}
else if (state==1 && data == 22) //帧头2
{
//printf("state=2 data:%d",data);
state=2;
}
else if(state == 2)
{
//printf("state=3 data:%d",data);
state=3;
openmvdata_1=data; //assic转为int
}
else if(state == 3)
{
//printf("state=3 data:%d",data);
state=4;
openmvdata_2=data; //assic转为int
//printf("openmvdata_1:%c",openmvdata_1);
//printf("openmvdata_2:%c",openmvdata_2);
openmvdata=openmvdata_1*100+openmvdata_2;
}
else if(state ==4)
{
if(data ==33) //帧尾
{
printf("面积:%d",openmvdata);
state=0;
}
else
{
openmvdata_1=0;
openmvdata_2=0;
state=0;
}
}
}
串口接收到的字符型数据转化成数字:
k = USART_RXBuff[3]-0x30;1
或者:
k = USART_RXBuff[3]-'0';
1.识别长度
# Untitled - By: YU - 周三 4月 7 2021
import sensor, image, time, ustruct
from pyb import UART #导入串口
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
K=0.16 #实际直径=K*直径像素 故:K=4CM/25PIX
aim_threshold =(24, 80, 47, 72, -44, 106) #L A B
uart = UART(3,115200) #OpenMV上P4,P5对应的串口3 第二个参数是波特率
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
#def sending_data(a):
#global uart;
##frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
##data = bytearray(frame)
#data = ustruct.pack("
#0x2C, #帧头1
#0x12, #帧头2
#int(a), #数据1
#0x5B)
#uart.write(data); #必须要传入一个字节数组
while(True):
clock.tick()
img = sensor.snapshot()
# print(clock.fps())
blobs = img.find_blobs([aim_threshold])
if len(blobs)==1:
b=blobs[0]
img.draw_rectangle(b[0:4])
img.draw_cross(b[5],b[6])
lm=(b[2]+b[3])/2
length=K*lm
print("管长:",length)
length=int(length)
data = bytearray([11,22,length,33])
uart.write(data)
#pack各字母对应类型
#x pad byte no value 1
#c char string of length 1 1
#b signed char integer 1
#B unsigned char integer 1
#? _Bool bool 1
#h short integer 2
#H unsigned short integer 2
#i int integer 4
#I unsigned int integer or long 4
#l long integer 4
#L unsigned long long 4
#q long long long 8
#Q unsilong long long 8
#f float float 4
#d double float 8
#s char[] string 1
#p char[] string 1
#P void * long
2.识别面积
# Untitled - By: YU - 周三 4月 7 2021
import sensor, image, time, ustruct
from pyb import UART #导入串口
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
K=0.16 #实际直径=K*直径像素 故:K=4CM/25PIX
aim_threshold =(24, 80, 47, 72, -44, 106) #L A B
uart = UART(3,115200) #OpenMV上P4,P5对应的串口3 第二个参数是波特率
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
while(True):
clock.tick()
img = sensor.snapshot()
#print(clock.fps())
blobs = img.find_blobs([aim_threshold])
if len(blobs)==1:
b=blobs[0]
img.draw_rectangle(b[0:4])
img.draw_cross(b[5],b[6])
area=b.pixels()
print("面积:",area)
qian=area//100
shi=area%100
data = bytearray([11,22,qian,shi,33])
uart.write(data)
STM32
一定要共地(浪费了一个晚上的教训)
1.识别长度
/* USER CODE BEGIN PTD */
uint8_t re_buf1; //缓冲区
uint8_t openmvdata=0; //长度 整数
uint8_t state=0;
/* USER CODE END PTD */
/* USER CODE BEGIN PM */
void OpmvDataGet(uint8_t data)
{
if(state==0 && data ==11) //帧头1
{
//printf("state=1 data:%d",data);
state =1 ;
}
else if (state==1 && data == 22) //帧头2
{
//printf("state=2 data:%d",data);
state=2;
}
else if(state == 2)
{
//printf("state=3 data:%d",data);
state=3;
openmvdata=data;
}
else if(state ==3)
{
if(data ==33) //帧尾
{
printf("长度:%d",openmvdata);
state=0;
}
else
{
openmvdata=0;
state=0;
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //opmv-receive
{
if(huart == &huart1)
{
HAL_UART_Receive_IT(&huart1,&re_buf1,1);
OpmvDataGet(re_buf1);
}
}
/* USER CODE END PM */
int main(void)
{
/* USER CODE BEGIN 2 */
printf("stm32初始化");
HAL_UART_Receive_IT(&huart1,&re_buf1,1);
/* USER CODE END 2 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
2.识别面积
void OpmvDataGet(uint8_t data)
{
if(state==0 && data ==11) //帧头1
{
//printf("state=1 data:%d",data);
state =1 ;
}
else if (state==1 && data == 22) //帧头2
{
//printf("state=2 data:%d",data);
state=2;
}
else if(state == 2)
{
//printf("state=3 data:%d",data);
state=3;
openmvdata_1=data; //assic转为int
}
else if(state == 3)
{
//printf("state=3 data:%d",data);
state=4;
openmvdata_2=data; //assic转为int
//printf("openmvdata_1:%c",openmvdata_1);
//printf("openmvdata_2:%c",openmvdata_2);
openmvdata=openmvdata_1*100+openmvdata_2;
}
else if(state ==4)
{
if(data ==33) //帧尾
{
printf("面积:%d",openmvdata);
state=0;
}
else
{
openmvdata_1=0;
openmvdata_2=0;
state=0;
}
}
}
串口接收到的字符型数据转化成数字:
k = USART_RXBuff[3]-0x30;1
或者:
k = USART_RXBuff[3]-'0';
举报