Integrate Arducam AR1820HS in Python program (Raspberry Pi 3)

Hi guys,

I’m trying to replace a Raspberry Pi Camera Module with an Arducam AR1820HS, but I’m struggling with the code - especially when it comes to integrate the library.

Is there anybody willing to help?

Thanks!

 

This is the Python program:

#!/usr/bin/python3

final 2 stepper version 5.2

import RPi.GPIO as GPIO

import time

import picamera

import sys

test python version

version = sys.version.split(’.’)[0]

if version == “2”:

print(“This program must be run with python3”)

print("python used aborting program ")

sys.exit()

setup gpio

GPIO.setmode(GPIO.BOARD)

GPIO.setwarnings(False)

control_pinsX = [7,11,13,15]

control_pinsZ = [19,21,23,29]

for pin in control_pinsX:

GPIO.setup(pin, GPIO.OUT)

GPIO.output(pin, 0)

 

for pin in control_pinsZ:

GPIO.setup(pin, GPIO.OUT)

GPIO.output(pin, 0)

steps lists

Stops X

stopsX = [0,4096,8192,12288,16384,20480,24567,28672,32768]

stop_countX = 0

Stops Z

stopsZ = [0,114,228,342,455,569,683,797,910,1024,1138,1252,1365,1479,1593,1707,1820,1934,2048,2162,2275,2389,2503,2617,2730,2844,2958,3072,3185,3299,3413,3527,3640,3754,3868,3982,4096]

stop_countZ = 0

gpio ouput values list

halfstep_seq = [

[1,0,0,0],

[1,1,0,0],

[0,1,0,0],

[0,1,1,0],

[0,0,1,0],

[0,0,1,1],

[0,0,0,1],

[1,0,0,1]

]

stepper x starting halfstep value do not change

halfstep = 0

stepper Z starting Halfstep value do not change

position = 0

Stepper X cycles starting value

cycle = 1

stop time after moving stepper Z you can reduce this if you want but not below 0.5

zstop = 5

cmaera picture count start value

campic = 1

#step timer value adjust this value higher to slow stepping of motors typical values 0.001 0.001007

steptime = 0.001007

delay for picture taking stop can be reduced once you have your picture taking code added but not below 0.5

picdelay = 5

def take_picture(campic):

print(“Take picture”, campic)

the following line contains the path and file name that will be use to save file

filename = “/home/pi/” + name + str(campic) + “.jpg”

 

with picamera.PiCamera() as camera:

camera.resolution = (1920, 1080)

camera.start_preview()

time.sleep(2)

camera.capture(filename)

time.sleep(0.5)

camera.stop_preview()

def stepperz(Halfstep,stop_countZ):

Stepper Z

start = stopsZ[stop_countZ]

if stop_countZ > 0:

start = start + 1

stop = stopsZ[stop_countZ + 1] + 1

 

for I in range(start, stop): # this now the number of steps for one move

#print(I)

for pin in range(4):

GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])

 

time.sleep(steptime)

Halfstep = Halfstep + 1

if Halfstep == 8:

Halfstep = 0

#stop

print(“Z moved to”, (stopsZ[stop_countZ + 1]))

time.sleep(1)

return Halfstep

 

file name entry

print (“Program Start”)

print (" ")

print (“please input a file name for this program run”)

name = input ("> ")

print(" ")

if name =="":

name = (time.strftime("%d%H%M_"))

main program loop

start = time.time()

for L in range(18): #loop for 18 times clockwise 18 counter clockwise

Clock wise rotation including stops

for i in range(32769): # this now the number of steps for a 240 degrees rotation

#print(i)

for pin in range(4):

GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

 

time.sleep(steptime)

halfstep = halfstep + 1

if halfstep == 8:

halfstep = 0

#stops

if i == (stopsX[stop_countX]):

print(“X Moved to”, (stopsX[stop_countX]))

time.sleep(picdelay)

#call to take picure

take_picture(campic)

campic = campic + 1

stop_countX = stop_countX + 1

 

 

End of Clockwise rotation

 

print ("End of camera clockwise rotation cycle ", cycle )

 

call stepper Z

stop_countZ = cycle - 1

Halfstep = position

position = stepperz(Halfstep,stop_countZ)

 

cycle = cycle + 1

stop_countX = 8 # reset stops

time.sleep(zstop)

 

rotate back to start with stops

for i in range(32768, -1, -1): # this now the number of steps for the required rotation

#print(i)

for pin in range(4):

GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

time.sleep(steptime)

halfstep = halfstep - 1

if halfstep == -1:

halfstep = 7

#stops

if i == (stopsX[stop_countX]):

print(“X moved to”, (stopsX[stop_countX]))

time.sleep(picdelay)

#call to take picure

take_picture(campic)

campic = campic + 1

stop_countX = stop_countX - 1

 

 

#End of counter clockwise rotation

print ("End of counter clockwise camera rotation cycle ", cycle)

 

call stepper Z

stop_countZ = cycle - 1

Halfstep = position

position = stepperz(Halfstep,stop_countZ)

 

cycle = cycle + 1

stop_countX = 0 # reset stops

time.sleep(zstop)

GPIO.cleanup()

end = time.time()

runT = (end - start)

a=str(runT//3600)

b=str((runT%3600)//60)

c=str(round(((runT%3600)%60),3))

print (“End of program run it took {} hours {} mins {} seconds”.format(a, b, c))

Hi,

I don’t have much suggestions for python code, but for using AR1820 on Raspberry Pi, I suggest you try this library: https://github.com/ArduCAM/MIPI_Camera/tree/master/RPI

This is great! I’ll have a look. Thanks so much :slight_smile:

If you have any questions, please feel free to contact us.