PDA

View Full Version : ZIC2410 + JPEG Camera , remote image capture


Kham
12-20-2009, 05:07 AM
More info in this thread
http://forums.synapse-wireless.com/showthread.php?p=2794

If you need more comments to under stand the script, start with this thread
http://forums.synapse-wireless.com/showthread.php?t=605

How it should work.
1. A button is pressed.
2. The RFE syncs with the camera module.
3. Sends the initialisation and capture commands
4. Receives each packet, strips the headers, sends to portal.
5. ACKs the data packet to start receiving the next one.
6. The last data packet may be smaller than the rest.
7. A final ACK is sent to terminate the data transfer.


There is room for improvement
.checking data from the camera for errors
.reduce number of file open/close operations. Use arrays?

Post if it was helpful to you :)

polodioxide
02-22-2010, 10:30 AM
did u test this code? because i have the same camera but it is not working. it save a hex file but it is so small.. i am using the same code

Kham
02-22-2010, 02:06 PM
The code is test and working on a ZIC2410 EVB3 board.
I found that 320x240 is the max resolution I can use otherwise the files would get corrupted.

Will need a bit more info before I can help.

What are RFE/Module you using?
How is it connected?
Do you have a serial port sniffer?
Does the camera work when connected directly to the computer? (via the appropriate level converters)

polodioxide
02-23-2010, 02:11 AM
I am using Synapse EK2500 evaluation kit protoboard. I am using bridge board to connect synapse portal. No, i don't have sniffer and yes, my camera is working..

Kham
02-23-2010, 02:50 AM
we can make a sniffer out of what you have :)

I was meaning more what is connected to what pin.

Also post your code, in case something is not connected right.

polodioxide
02-23-2010, 03:08 AM
sorry :P

i used your code, i just change a few things.. like SW1= GPIO_5 (the button on the board)

instead of "portal_print" i used "logEvent"
i added "global takeSnap", "global SW1" to button event..and some logEvents for debugging.

I connect my camera pins to camera Rx to board tx, camera tx board rx.


from synapse.platforms import *

#define constants
DS_NULL = 0
DS_UART0 = 1
DS_UART1 = 2
DS_TRANSPARENT = 3
DS_STDIO = 4
DS_CLI = 5
DS_PACKET_SERIAL = 6

LED1 = 3 # P0.3
LED2 = 2 # P0.2
LED3 = 1 # P0.1
LED4 = 16 # P3.0

SW1 = GPIO_5 # P1.3
SW2 = 12 # P1.4
SW3 = 18 # P3.2
SW4 = 19 # P3.3

#Define global Variables
serial_in_data = ""
takeSnap = 0 #1 sync, 2 ack sent, 3 data, 4 end?

ackCounter = 0
var_data_sent = True #Flag to indicate RPC data sent
var_bytes_total = 0
var_bytes_current = 0
var_bytes_frame = "" #needs to be global so we can use it between functions, even if it's the same function we are re-entering
var_send_wait = 0 #rpc send wait counter
var_ser_wait = 0 #serial wait counter, after command sent, how long to wait for data to show up in buffer

var_z_Xkb_send = False
g_counter_1 = 0

var_data_rts = False #Flag to signal, get next packet from camera
var_eod = False #end of data



@setHook(HOOK_STARTUP)
def startupEvent():
initUart(0,0)

# Initialize LED pins as outputs
setPinDir(LED1, True)
setPinDir(LED2, True)
setPinDir(LED3, True)
setPinDir(LED4, True)
#Turn them all off
writePin(LED1, False)
writePin(LED2, False)
writePin(LED3, False)
writePin(LED4, False)

setPinDir(SW1, False)
setPinPullup(SW1, True)
monitorPin(SW1, True)

setRate(2) #Default is check every 100ms. 2 = 10ms

#initUart(1, 57600)
initUart(1, 1) #115k
#initUart(1, 57600) #9600
stdinMode(1, False) # Char mode, no echo

crossConnect(DS_UART1, DS_STDIO)

setTXPowerLo()
rpc("\x00\x00\x01", 'logEvent', "startupEvent")




@setHook(HOOK_GPIN)
def buttonEvent(pin, isSet):
global takeSnap
global SW1

if pin == SW1:
rpc("\x00\x00\x01", 'logEvent', "buttonEvent")
if not isSet and takeSnap == 0:
#start sending sync packets to camera, check for response during timer hook
rpc("\x00\x00\x01", 'logEvent', "take snapshot")
counter_1 =0
while counter_1 < 32:
print "\xAA\x0D\x00\x00\x00\x00",
counter_1 += 1

takeSnap = 1


@setHook(HOOK_STDIN)
def stdinEvent(serial_in_buffer):
global serial_in_data
serial_in_data += serial_in_buffer



#@setHook(HOOK_1S)
#def timer1sEvent(currentS):
@setHook(HOOK_100MS)
#def timer100msEvent(currentMs):
def timerStartEvent(currentMs):
global serial_in_data, takeSnap, ackCounter, var_bytes_total, var_eod
#take snap increments as it set's up the system for download
#Note order of takeSnap check, otherwise each one will activate the next one without exiting hook, ie no delay

if takeSnap > 1 and takeSnap < 6:
buf_len = len(serial_in_data)
if takeSnap == 5: #check num,ber of packets expected #get total number of packets
if buf_len < 10:
takeSnap = 0
serial_in_data=""
rpc("\x00\x00\x01", 'logEvent', "err")
else:
#a 24 bit (3byte) interger is sent back, we only need the first 2 bytes as a fullsize jpeg won't exceed 35kb
#AA 0A 01 x04 x06 x00 = 0604 = 1540bytes
#AA 0E 04 03 00 00 AA 0A 01*04*06 00
#convert back to 16 bit interger
tbyte_0 = ord(serial_in_data[9])
tbyte_1 = ord(serial_in_data[10])
tbyte_1 = tbyte_1 * 256
var_bytes_total = tbyte_0 + tbyte_1
#Debug, print to portal
rpc("\x00\x00\x01", 'logEvent', "n_bytes total")
rpc("\x00\x00\x01", 'logEvent', var_bytes_total)

serial_in_data= ""
print "\xAA\x0E\x00\x00\x00\x00", #ack to start recieving the image data
ackCounter += 1
takeSnap = 6
var_eod = False

if takeSnap == 4:
serial_in_data= ""
rpc("\x00\x00\x01", 'logEvent', "ts_4" )
print "\xAA\x04\x01\x00\x00\x00", #get pic
takeSnap = 5
rpc("\x00\x00\x01", 'logEvent', "takeSnap = 5")

if takeSnap == 3:
print "\xAA\x05\x00\x00\x00\x00", #Take pic, we have the option to discard frames to allow the camera to spablize
takeSnap = 4
rpc("\x00\x00\x01", 'logEvent', "takeSnap = 4")

if takeSnap == 2:
#print "\xAA\x01\x00\x07\x00\x01", # jpeg 80x64
#print "\xAA\x01\x00\x07\x00\x03", # jpeg 160x
print "\xAA\x01\x00\x07\x00\x05", # jpeg 320x
#print "\xAA\x01\x00\x07\x00\x07", # jpeg 640x
takeSnap = 3
rpc("\x00\x00\x01", 'logEvent', "takeSnap = 3")

if takeSnap == 1:
#check for ack from camera, if not resend sync
#else send ack and rest of commands
buf_len = len(serial_in_data)
if buf_len < 5:
print "\xAA\x0D\x00\x00\x00\x00",
rpc("\x00\x00\x01", 'logEvent', "sync<5" )
else:
serial_in_data= ""
print "\xAA\x0E\x0D\x00\x00\x00", #send ack
takeSnap = 2
rpc("\x00\x00\x01", 'logEvent', "takeSnap = 2")



#@setHook(HOOK_100MS)
#@setHook(HOOK_10MS)
@setHook(HOOK_1MS)
#def timer1msEvent(currentMs):
def timerMsEvent(currentMs):
global ackCounter, var_data_sent, var_bytes_total, var_bytes_current, var_send_wait, takeSnap, serial_in_data, var_ser_wait, var_frame_req_sent, var_frame_resend, var_frame_resend_cnt, var_bytes_frame, var_data_rts, var_eod

if takeSnap == 7: #all data recieved, send end command to camera
print "\xAA\x0E\x00\x00\xF0\xF0",
rpc("\x00\x00\x01", 'portal_file_end' )
rpc("\x00\x00\x01", 'logEvent', "send waits, rpc/ser=")
rpc("\x00\x00\x01", 'logEvent', var_send_wait)
rpc("\x00\x00\x01", 'logEvent', var_ser_wait)
var_ser_wait = 0
var_send_wait = 0
serial_in_data= ""
takeSnap = 0

#seperate serial read and rpc send routine
if takeSnap == 6:
if var_data_sent == True: #previous packet sent?
if var_data_rts == True: #next packet ready?
if var_bytes_frame != "":
rpc("\x00\x00\x01", 'pf', var_bytes_frame ) #send to portal
var_bytes_frame = ""
if var_eod == True: #all data sent?
takeSnap = 7
else:
var_data_sent = False #Flag is clear in RPC sent hook function
var_data_rts = False #Flag is clear when data extracted from serial buffer
else:
if var_data_rts == True:
var_send_wait += 1 #counter for how many times we are waiting for the last rpc data transfer to finish

if var_data_rts == False and var_eod == False:
buf_len = len(serial_in_data)
if buf_len > 5:
var_frame_req_sent = False
#setup slicing parameters
buf_len -= 2 #64-2=62, compensate for 2 checksum characters
var_bytes_frame = "" #init var
#start position 3, , compensate for 4 header characters
var_bytes_frame = serial_in_data[4:buf_len]
var_data_rts = True #flag data put in frame buffer

serial_in_data = "" #clear rcv buffer

buf_len -= 4 #Calc bytes sent
var_bytes_current += buf_len #add to total counter
#check if all data buffered
if var_bytes_current < var_bytes_total:
hi_frameID = 0
lo_frameID = ackCounter % 256
if ackCounter > 256:
hi_frameID = ackCounter / 256

print "\xAA\x0E\x00\x00",
print chr(lo_frameID),chr(hi_frameID),
ackCounter += 1
var_frame_req_sent = True
else: #all image data sent
ackCounter = 0
var_bytes_current = 0
var_eod = True

else:
if var_frame_req_sent == True:
#pulsePin(LED2, 1, True)
var_ser_wait += 1 #request for new data sent, waiting for serial buffer to fill



@setHook(HOOK_RPC_SENT)
def RPC_sent_Event():
#def rc():
global var_data_sent
var_data_sent = True


#Misc Functions
def setTXPowerLo():
txPwr(0)

def setTXPowerHi():
txPwr(17)

def checkTXPower():
#Function can only one value so we remote print values to portal
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'logEvent', memval)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'logEvent', memval)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'logEvent', memval)
rpc("\x00\x00\x01", 'logEvent', "end tx pwr check")

Kham
02-23-2010, 03:30 AM
And a picture of your setup if you can :)

polodioxide
02-23-2010, 03:50 AM
maybe the pin numbers and colors of cable is not clear enough..

(GND) black to pin 1
(VCC) red to pin 9
(Rx) green to pin 10
(Tx) yellow to pin 11