Micropython TPYBoard v102 DIY Camera

Camera (CAMERA or WEBCAM), also known as computer camera, computer eye, electronic eye, etc., is a video input device, which is widely used in video

Conference, security system, image acquisition system, environmental monitoring, industrial field process control, etc. This experiment uses TPYBoard v102 to

And PTC06 serial camera module DIY a simple camera.

1. Equipment used:

 TPYBoard v102 1 piece

 1 PTC06 serial camera module

 Several DuPont lines

 1 TF card

 1 download data cable

2. Introduction to TPYBoard v102:

TPYBoard v102 main control chip adopts STM32F405, M4 core, the highest running speed of 168MHZ, 1024KB Flash, 192KB

RAM, built-in SD card slot, maximum support 8G memory memory card. There are four LED lights and an accelerometer onboard. TPYBoard v102 onboard

30 general-purpose GPIO interfaces, 2 SPI interfaces, 2 CAN interfaces, 2 I2C interfaces, 5 USART interfaces, 2

ADC connected

port (precision 12bit), 2 DAC interfaces, 1 SWD interface. Can be connected to most common sensor modules.image description

                                                                                                                       

3. Introduction of PTC06 serial camera module:

The PTC06 serial camera module is an industrial-grade image acquisition and processing module that integrates image acquisition, shooting control, data compression, and serial transmission.

Piece. Its built-in high-performance digital signal processing chip achieves high-ratio compression of the original image. Product image output in standard JPEG format

It can be easily compatible with various image processing software; the 3-wire TTL level UART communication interface can be easily

chip machine or

other microprocessor connections.

The default baud rate is 115200, other optional baud rates are 9600, 19200, 38400 and 57600.

Among them, for the application of serial port control to take pictures and read static pictures, only 4 signal lines of GND, RX, TX and VCC are needed.
image description
The CVBS signal is only required if the application is to output analog video.

Note: TX and RX are 3.3v TTL level signals.
4. Wiring method:

TPYBoard v102 - Camera Module
VIN - 5V
GND - GND
X1(UART 4 ​​TX) - RX
X2(UART 4 ​​RX) - TX

5. Experimental procedure.

Basic principle: TPYboard v102 is connected to the serial camera module (PTC06) through the serial port, and the camera is controlled by the button to take pictures.

Save to TF card.

After power on, wait for the onboard green light of the ToyBoard v102 to light up, indicating that the camera module is initialized. Press the USR button to control the camera to shoot.

The blue light will flash, indicating that the picture data is being transferred, and when the blue light goes out, the transfer is complete.

Press the rst key to let the board reload the TF card, open the TF card and you will see a newly generated jpeg file.

See the video for details. https://v.qq.com/x/page/s0608s2re8g.html

6. Source code:

"""
#TPYBoard v102 驱动串口摄像头PTC06 拍照并保存到TF卡
#------------------------------------------------------
#作者:山东萝卜电子科技有限公司
#时间:2018年03月15日
"""

import pyb
from pyb import UART,Switch

"""
拍照的基本流程
--------------------
1.清空图片缓存
2.发送拍照命令
3.获取图片的长度
4.根据长度读取图片数据

使用到的指令与返回的数据格式
-------------------------------
复位
0x56 0x00 0x26 0x00
返回
0x76 0x00 0x26 0x00 +DSP 版本信息+Init end
------------------------
清空图片缓存 
0x56 0x00 0x36 0x01 0x02
返回
0x76 0x00 0x36 0x00 0x00
-------------------------
拍照命令
0x56 0x00 0x36 0x01 0x00
返回
0x76 0x00 0x36 0x00 0x00
--------------------------
读所拍图片的长度
56 00 34 01 00
返回
0x76 0x00 0x34 0x00 0x04 0x00 0x00 XX YY
(XX 为高位字节,YY 为低位字节)
--------------------------
读取图片数据
0x56 0x00 0x32 0x0C 0x00 0x0A 0x00 0x00 AA BB 0x00 0x00 XX YY 0x00 0xFF
AA BB:起始地址(先高位字节,后低位字节.必须是8的倍数)
XX YY:本次读的数据长度(先高位字节,后低位字节)
返回
76 00 32 00 00 FF D8 ....... FF D9 76 00 32 00 00
(完整的JPEG 图片文件是以 FF D8 开始 FF D9 结尾)
"""
#-----Command---------#
initcmd=b'\x56\x00\x26\x00' #复位指令
clearcmd=b'\x56\x00\x36\x01\x02' #清除缓存
photocmd=b'\x56\x00\x36\x01\x00' #拍照
lengthcmd=b'\x56\x00\x34\x01\x00' #获取图片长度
readcmd=b'\x56\x00\x32\x0C\x00\x0A\x00\x00' #获取图片数据
responseCmd=b'\x76\x00\x32\x00\x00'#返回的图片数据固定头和尾
#---------------------------------#
isFlag=0#标识是否初始化
isPhoto=0#标识是否发送拍照命令
num=1
f_name='/sd/photo%s.jpeg'#保存的文件名称
nBytes=512#每次读取的字节数
#---------------------------------#
uart=UART(4,115200,timeout=100)#串口4 TX-X1 RX-X2

#------将10进制转为16进制字节数组--------#
def convert_Data(num):
    if num>255:
        num_h=hex(num)
        if len(num_h)<6:
            num_h_a=num_h[:3]
            num_h_b='0x'+num_h[3:]
        else:
            num_h_a=num_h[:4]
            num_h_b='0x'+num_h[4:]
        byte_num=bytes([int(num_h_a,16),int(num_h_b,16)])
    else:
        byte_num=b'\x00'+bytes([num])
    return byte_num
#---------------------------------
#函数名:get_photo
#描述:获取图片数据
#参数:起始地址、读取长度
#返回:成功返回正常数据,失败返回0
#---------------------------------/
def get_photo(add,readlen):
    global readcmd,responseCmd

    cmd=readcmd+add+b'\x00\x00'+readlen+b'\x00\xFF'
    uart.write(cmd)
    while uart.any()<=0:
        continue
    data=uart.read()
    #print('data:',data)
    #print('data[0:5]:',data[0:5])
    #print('data[-5:]:',data[-5:])
    if data[0:5]==responseCmd and data[-5:]==responseCmd:
        revdata=data[5:-5]
        print('revdata:',revdata)
    else:
        revdata=0
    return revdata
#---------------------------------
#函数名:test
#描述:USR按键的回调函数。
#按键每按1次拍照1次
#---------------------------------/
def test():
    global num,isPhoto
    pyb.delay(30)
    if(sw()):
        sw.callback(None)#正在获取数据时 禁用回调
        isPhoto=0
        num+=1
        pyb.LED(3).on()
        #清除缓存
        uart.write(clearcmd)
#等待模块上电完毕       
print('wait......')
pyb.delay(2800)
print('init start.......')
uart.write(initcmd)
sw=Switch()
sw.callback(test)
while True:
    if uart.any()>0:
        data=uart.read()
        print('revdata:',data)
        if isFlag==0:
            #说明接收的是复位后的信息
            if data==b'Init end\r\n':
                #复位完毕
                print('init ok.......')
                pyb.delay(2000)
                isFlag=1
                pyb.LED(2).on()
        else:
            if len(data)>=5:
                if data[0]==118:#0x76
                    if data[2]==54:#0x36
                        if isPhoto==0:
                            #清除缓存返回
                            print('-----clear buffer ok----')
                            isPhoto=1
                            uart.write(photocmd)
                        else:
                            #拍照返回
                            print('-----taking pictures ok----')
                            uart.write(lengthcmd)
                    if data[2]==52:#0x34
                        print('photo length:',data[7],'-',data[8])
                        tlen=data[7]*256+data[8]
                        t_c=tlen//nBytes
                        t_y=tlen%nBytes
                        add=0
                        #256=[0x01,0x00] 512=[0x02,0x00]
                        length=convert_Data(nBytes)
                        name=f_name % str(num)
                        print('filename:',name)
                        for i in range(0,t_c):
                            add=convert_Data(i*nBytes)
                            #每512字节写一次
                            revdata=get_photo(add,length)
                            if revdata!=0:
                                f=open(name,'a')
                                f.write(revdata)
                                f.close()
                            pyb.LED(4).toggle()
                            print('-------------',i)
                            pyb.delay(100)
                        add=convert_Data(t_c*nBytes)    
                        revdata=get_photo(add,convert_Data(t_y))
                        if revdata!=0:
                            f=open(name,'a')
                            f.write(revdata)
                            f.close()
                        pyb.LED(3).off()
                        pyb.LED(4).off()
                        pyb.delay(100)
                        print('*========================================*')
                        sw.callback(test)
            else:
                print('-----data length error-----')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518474&siteId=291194637
Recommended