-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservo.py
More file actions
107 lines (86 loc) · 2.53 KB
/
Copy pathservo.py
File metadata and controls
107 lines (86 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
#-- coding: utf-8 --
import RPi.GPIO as GPIO
import time
from picamera import PiCamera
import numpy as np
import argparse
import datetime
parser = argparse.ArgumentParser()
parser.add_argument("-x", "--xpos", default=0, type=int, help="x position")
parser.add_argument("-y", "--ypos", default=0, type=int, help="y position")
parser.add_argument("-c", "--center", default=90, type=int, help="center position")
parser.add_argument("-s", "--span", default=20, type=int, help="span of the scan")
parser.add_argument("-n", "--steps", default=3, type=int, help="span of the scan")
parser.add_argument("-p", "--path", default="/home/pi/servo_images/", help="span of the scan")
parser.add_argument("-d", "--date", default=False, type=bool, help="add date to the string filename")
args = parser.parse_args()
cli_x=args.xpos
cli_y=args.ypos
center=args.center
span=args.span
steps=args.steps
path=args.path
date=args.date
if date:
dt_date = datetime.datetime.now()
#Set function to calculate percent from angle
def angle_to_percent (angle) :
if angle > 180 or angle < 0 :
return False
start = 4
end = 12.5
ratio = (end - start)/180 #Calcul ratio from angle to percent
angle_as_percent = angle * ratio
return start + angle_as_percent
GPIO.setmode(GPIO.BOARD) #Use Board numerotation mode
GPIO.setwarnings(False) #Disable warnings
#Use pin 12 for PWM signal
pwm_gpio_s = [12,33]
frequence = 50
if ((cli_x==0) and (cli_y==0)):
cli_position=False
else:
cli_position=True
if cli_position:
x_s=[cli_x]
y_s=[cli_y]
else:
# now scan the x,y positions
if span==0:
x_s=[0,90,180]
y_s=[90,120,150]
else:
x_s=np.linspace(center-span,center+span,num=steps,endpoint=True)
y_s=np.linspace(center-span,center+span,num=steps,endpoint=True)
GPIO.setup(pwm_gpio_s[0], GPIO.OUT)
GPIO.setup(pwm_gpio_s[1], GPIO.OUT)
camera = PiCamera()
camera.rotation = 180
pwm_x = GPIO.PWM(pwm_gpio_s[0], frequence)
pwm_y = GPIO.PWM(pwm_gpio_s[1], frequence)
pwm_x.start(angle_to_percent(90))
pwm_y.start(angle_to_percent(90))
try:
for x in x_s:
for y in y_s:
print(x,y)
time.sleep(1)
pwm_x.ChangeDutyCycle(angle_to_percent(x))
pwm_y.ChangeDutyCycle(angle_to_percent(y))
camera.start_preview()
time.sleep(1)
if date:
datestr=dt_date.strftime("%Y%m%d-%H%M")
else:
datestr=''
filename=path+'image_'+str(x)+'_'+str(y)+datestr+'.jpg'
print(filename)
camera.capture(filename)
camera.stop_preview()
except KeyboardInterrupt:
pass
pwm_x.stop()
pwm_y.stop()
#Close GPIO & cleanup
GPIO.cleanup()